-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate-targetframework.ps1
71 lines (59 loc) · 1.85 KB
/
update-targetframework.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
using namespace System.Collections.Generic # for List<T>
$dir = "." # points to "repository root"
$targetVersion = "net6.0"
$projFiles = Get-ChildItem $dir -Recurse -Filter *.csproj
$projsWithVersion = [List[object]]::new()
foreach($file in $projFiles)
{
$content = [xml](Get-Content $file.FullName)
$versionNodes = $content.GetElementsByTagName("TargetFramework");
switch($versionNodes.Count)
{
0 {
Write-Host "The project has no framework version: $file.FullName"
break;
}
1 {
$version = $versionNodes[0].InnerText;
$projsWithVersion.Add([PsCustomObject]@{
File = $file;
XmlContent = $content;
VersionNode = $versionNodes[0];
VersionRaw = $version;
Version = $version;
})
break;
}
default {
Write-Host "The project has multiple elements of TargetFramework: $file.FullName"
break;
}
}
}
function Print-Version-Statistics([List[object]] $projsWithVersion)
{
$numberOfProjectsByVersion = @{}
foreach($proj in $projsWithVersion)
{
if($numberOfProjectsByVersion.ContainsKey($proj.Version))
{
$numberOfProjectsByVersion[$proj.Version] = $numberOfProjectsByVersion[$proj.Version] + 1
}
else
{
$numberOfProjectsByVersion[$proj.Version] = 1
}
}
Write-Host "`nCurrent version distribution:"
$numberOfProjectsByVersion
}
Print-Version-Statistics($projsWithVersion)
foreach($proj in $projsWithVersion)
{
if($targetVersion -ne $proj.Version)
{
$proj.VersionNode.set_InnerXML("$targetVersion")
$proj.XmlContent.Save($proj.File.FullName);
Write-Host "Updated $($proj.File.FullName) to $targetVersion"
}
}