forked from PowerShell/PowerShell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUpdateDotnetRuntime.ps1
More file actions
175 lines (134 loc) · 5.96 KB
/
Copy pathUpdateDotnetRuntime.ps1
File metadata and controls
175 lines (134 loc) · 5.96 KB
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
[CmdletBinding()]
param (
[Parameter()]
[string]$SDKVersionOverride
)
<#
.DESCRIPTION Update the global.json with the new SDK version to be used.
#>
function Update-GlobalJson([string] $Version) {
$psGlobalJsonPath = Resolve-Path "$PSScriptRoot/../global.json"
$psGlobalJson = Get-Content -Path $psGlobalJsonPath -Raw | ConvertFrom-Json
if ($psGlobalJson.sdk.version -eq $Version) {
throw '.NET SDK version is not updated'
}
$psGlobalJson.sdk.version = $Version
$psGlobalJson | ConvertTo-Json | Out-File -FilePath $psGlobalJsonPath -Force
}
<#
.DESCRIPTION Iterate through all the csproj to find all the packages that need to be updated
#>
function Update-PackageVersion {
class PkgVer {
[string] $Name
[string] $Version
[string] $NewVersion
[string] $Path
PkgVer($n, $v, $nv, $p) {
$this.Name = $n
$this.Version = $v
$this.NewVersion = $nv
$this.Path = $p
}
}
$skipModules = @(
"NJsonSchema"
"Markdig.Signed"
"PowerShellHelpFiles"
"Newtonsoft.Json"
"Microsoft.ApplicationInsights"
"Microsoft.Management.Infrastructure"
"Microsoft.PowerShell.Native"
"Microsoft.NETCore.Windows.ApiSets"
)
$packages = [System.Collections.Generic.Dictionary[[string], [PkgVer[]] ]]::new()
$paths = @(
"$PSScriptRoot/packaging/projects/reference/Microsoft.PowerShell.Commands.Utility/Microsoft.PowerShell.Commands.Utility.csproj"
"$PSScriptRoot/packaging/projects/reference/System.Management.Automation/System.Management.Automation.csproj"
"$PSScriptRoot/../src/"
"$PSScriptRoot/../test/tools/"
)
Get-ChildItem -Path $paths -Recurse -Filter "*.csproj" -Exclude 'PSGalleryModules.csproj','PSGalleryTestModules.csproj' | ForEach-Object {
Write-Verbose -Message "Reading - $($_.FullName)" -Verbose
$prj = [xml] (Get-Content $_.FullName -Raw)
$pkgRef = $prj.Project.ItemGroup.PackageReference
foreach ($p in $pkgRef) {
if ($null -ne $p -and -not $skipModules.Contains($p.Include)) {
if (-not $packages.ContainsKey($p.Include)) {
$packages.Add($p.Include, @([PkgVer]::new($p.Include, $p.Version, $null, $_.FullName)))
}
else {
$packages[$p.Include] += [PkgVer]::new($p.Include, $p.Version, $null, $_.FullName)
}
}
}
}
$versionPattern = (Get-Content "$PSScriptRoot/../DotnetRuntimeMetadata.json" | ConvertFrom-Json).sdk.packageVersionPattern
$packages.GetEnumerator() | ForEach-Object {
$pkgs = Find-Package -Name $_.Key -AllVersions -AllowPrereleaseVersions -Source 'dotnet5'
foreach ($v in $_.Value) {
$version = $v.Version
foreach ($p in $pkgs) {
if ($p.Version -like "$versionPattern*") {
if ([System.Management.Automation.SemanticVersion] ($version) -lt [System.Management.Automation.SemanticVersion] ($p.Version)) {
$v.NewVersion = $p.Version
break
}
}
}
}
}
# we need a ForEach-Object below to unravel each of the items in 'Values' which is an array of PkgVer
$pkgsByPath = $packages.Values | ForEach-Object { $_ } | Group-Object -Property Path
$pkgsByPath | ForEach-Object {
Update-CsprojFile -Path $_.Name -Values $_.Group
}
}
<#
.DESCRIPTION Update package versions to the latest as per the pattern mentioned in DotnetRuntimeMetadata.json
#>
function Update-CsprojFile([string] $path, $values) {
$fileContent = Get-Content $path -Raw
$updated = $false
foreach ($v in $values) {
if ($v.NewVersion) {
$stringToReplace = "<PackageReference Include=`"$($v.Name)`" Version=`"$($v.Version)`" />"
$newString = "<PackageReference Include=`"$($v.Name)`" Version=`"$($v.NewVersion)`" />"
$fileContent = $fileContent -replace $stringToReplace, $newString
$updated = $true
}
}
if ($updated) {
($fileContent).TrimEnd() | Out-File -FilePath $path -Force
}
}
$dotnetMetadataPath = "$PSScriptRoot/../DotnetRuntimeMetadata.json"
$dotnetMetadataJson = Get-Content $dotnetMetadataPath -Raw | ConvertFrom-Json
# Channel is like: $Channel = "5.0.1xx-preview2"
$Channel = $dotnetMetadataJson.sdk.channel
Import-Module "$PSScriptRoot/../build.psm1" -Force
Find-Dotnet
if(-not (Get-PackageSource -Name 'dotnet5' -ErrorAction SilentlyContinue))
{
$nugetFeed = ([xml](Get-Content .\nuget.config -Raw)).Configuration.packagesources.add | Where-Object { $_.Key -eq 'dotnet5' } | Select-Object -ExpandProperty Value
Register-PackageSource -Name 'dotnet5' -Location $nugetFeed -ProviderName NuGet
Write-Verbose -Message "Register new package source 'dotnet5'" -Verbose
}
## Install latest version from the channel
$sdkVersion = if ($SDKVersionOverride) { $SDKVersionOverride } else { "latest" }
Install-Dotnet -Channel "$Channel" -Version $sdkVersion
Write-Verbose -Message "Installing .NET SDK completed." -Verbose
$isWindowsEnv = [System.Environment]::OSVersion.Platform -eq [System.PlatformID]::Win32NT
$dotnetPath = if ($IsWindowsEnv) { "$env:LocalAppData\Microsoft\dotnet" } else { "$env:HOME/.dotnet" }
$pathSep = [System.IO.Path]::PathSeparator
if (-not (($ENV:PATH -split $pathSep) -contains "$dotnetPath")) {
$env:PATH = "$dotnetPath" + $pathSep + "$ENV:PATH"
}
$latestSdkVersion = (dotnet --list-sdks | Select-Object -Last 1 ).Split() | Select-Object -First 1
Write-Verbose -Message "Installing .NET SDK completed, version - $latestSdkVersion" -Verbose
Update-GlobalJson -Version $latestSdkVersion
Write-Verbose -Message "Updating global.json completed." -Verbose
Update-PackageVersion
Write-Verbose -Message "Updating project files completed." -Verbose