Last active
February 1, 2021 17:22
-
-
Save jahmai-ca/6868dbaeb9032eabb1cd0c6b6a2088e8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This script can update packages.config/project.son and <ProjectReference /> | |
param ( | |
[Parameter(Mandatory=$true)][String]$Solution | |
) | |
$ErrorActionPreference = 'Stop' | |
$solutionPath = (Resolve-Path $Solution -ErrorAction SilentlyContinue) | |
if (!$solutionPath) | |
{ | |
Write-Error "Could not find solution file" | |
} | |
$solutionPath = $solutionPath.Path | |
$solutionDirectory = [System.IO.Path]::GetDirectoryName($solutionPath) | |
$packagesDir = [System.IO.Path]::Combine($solutionDirectory, "packages") | |
$NuGetPath = [System.IO.Path]::Combine($solutionDirectory, ".nuget\nuget.exe") | |
if (!(Test-Path $NuGetPath -ErrorAction SilentlyContinue)) | |
{ | |
$NuGetCommand = get-command nuget.exe -ErrorAction SilentlyContinue | |
if (!$NuGetCommand) | |
{ | |
Write-Error "Could not find nuget.exe" | |
} | |
$NuGetPath = $NuGetcommand.Source | |
} | |
$projects = gc $Solution | | |
sls 'Project\(' | | |
sls "{2150E333-8FDC-42A3-9474-1A3956D46DE8}" -NotMatch | | |
foreach { | |
$projectParts = $_ -Split '[,=]' | foreach { $_.Trim('[ "{}]') }; | |
New-Object PSObject -Property @{ | |
Name = $projectParts[1]; | |
FilePath = [System.IO.Path]::Combine($solutionDirectory, $projectParts[2]); | |
FileName = [System.IO.Path]::GetFileNameWithoutExtension($projectParts[2]); | |
} | |
} | |
$packages = @() | |
foreach ($project in $projects) | |
{ | |
[xml]$projectContent = gc $project.FilePath | |
if ($projectContent.Project.Sdk -ne "Microsoft.NET.Sdk") | |
{ | |
continue | |
} | |
$projectContent.Project.ItemGroup | | |
foreach { $_.PackageReference } | | |
foreach { $packages += (New-Object PSObject -Property @{ Id = $_.Include; Version = $_.Version }) } | |
} | |
# use the package sources from %AppData%\NuGet\NuGet.config | |
$packageMetadataUris = @() | |
[xml]$sourcesXml = gc $env:AppData\NuGet\NuGet.config | |
$sourceUris = $sourcesXml.configuration.packageSources.add | select -ExpandProperty value | |
$selectedSourceUris = @() | |
foreach ($sourceUri in $sourceUris) | |
{ | |
try | |
{ | |
$source = iwr $sourceUri | ConvertFrom-Json | |
$packageMetadataUri = $source.resources | where { $_.'@type' -like "PackageDisplayMetadataUriTemplate*" } | select -ExpandProperty '@id' -First 1 | |
$selectedSourceUris += $sourceUri | |
$packageMetadataUris += $packageMetadataUri | |
} | |
catch | |
{ | |
} | |
} | |
Write-Output "Using NuGet Source: $selectedSourceUris" | |
$updatePackages = @() | |
$packageIds = $packages | select -ExpandProper Id -Unique | |
$packageInfos = @() | |
foreach ($packageId in $packageIds) | |
{ | |
foreach ($packageMetadataUri in $packageMetadataUris) | |
{ | |
try | |
{ | |
$metadataUri = $packageMetadataUri -replace "{id-lower}",$packageId.ToLowerInvariant() | |
Write-Output "Getting metadata for $packageId from $metadataUri" | |
$packageMetadata = iwr $metadataUri | ConvertFrom-Json | |
if (!$latestPackageMetadata -or [System.Version]::Parse($packageMetadata.items.upper) -gt [System.Version]::Parse($latestPackageMetadata.items.upper)) | |
{ | |
$latestPackageMetadata = $packageMetadata | |
} | |
} | |
catch | |
{ | |
continue | |
} | |
} | |
if ($latestPackageMetadata) | |
{ | |
$packageInfos += (New-Object PSObject -Property @{ Id = $packageId; Metadata = $packageMetadata }) | |
} | |
} | |
foreach ($project in $projects) | |
{ | |
$changed = $false | |
[xml]$xml = gc $project.FilePath | |
if ($xml.Project.Sdk -eq "Microsoft.NET.Sdk") | |
{ | |
# manipulating and saving the xml can sometimes lead to the serializer writing things out different to the way they were read (e.g. -> into ->), so we use text manipulation instead | |
$text = [System.IO.File]::ReadAllText($project.FilePath) | |
foreach ($packageInfo in $packageInfos) | |
{ | |
$latestVersion = $packageInfo.Metadata.items | foreach { $_.items } | where { $_.catalogEntry.listed } | foreach { $_.catalogEntry.version } | where { $_ -ne $null -and !$_.Contains('-') } | select -Last 1 | |
if ($latestVersion) | |
{ | |
# this won't match anything with version constraints, they can be manually updated | |
$match = "PackageReference Include=""$($packageInfo.Id)"" Version=""([0-9.]+)""" | |
if ($text -match $match -and [System.Version]::Parse($Matches[1]) -lt [System.Version]::Parse($latestVersion)) | |
{ | |
Write-Output "$($project.Name): Updating $packageId version to $latestVersion" | |
$replace = "PackageReference Include=""$($packageInfo.Id)"" Version=""$latestVersion""" | |
$text = $text -replace $match,$replace | |
$changed = $true | |
} | |
} | |
} | |
if ($changed) | |
{ | |
# .NET Core project files have a byte order marker and are UTF8 | |
$stream = [System.IO.File]::Create("$($project.FilePath)") | |
$streamWriter = New-Object System.IO.StreamWriter($stream, [System.Text.Encoding]::UTF8) | |
$streamWriter.Write($text) | |
$streamWriter.Close() | |
$stream.Close() | |
} | |
} | |
else | |
{ | |
$packagesConfigPath = ([System.IO.Path]::Combine([System.IO.Path]::GetDirectoryName($project.FilePath), "packages.config")) | |
$projectPackagesConfigPath = ([System.IO.Path]::Combine([System.IO.Path]::GetDirectoryName($project.FilePath), "packages." + $project.FileName + ".config")) | |
$jsonPath = ([System.IO.Path]::Combine([System.IO.Path]::GetDirectoryName($project.FilePath), "project.json")) | |
$projectJsonPath = ([System.IO.Path]::Combine([System.IO.Path]::GetDirectoryName($project.FilePath), $project.FileName + "project.json")) | |
try | |
{ | |
if (Test-Path $packagesConfigPath) | |
{ | |
. $NuGetPath update $project.FilePath -RepositoryPath $packagesDir | |
} | |
elseif (Test-Path $projectPackagesConfigPath) | |
{ | |
cp $projectPackagesConfigPath $packagesConfigPath | |
. $NuGetPath update $project.FilePath -RepositoryPath $packagesDir | |
cp $packagesConfigPath $projectPackagesConfigPath | |
rm $packagesConfigPath | |
} | |
elseif (Test-Path $jsonPath) | |
{ | |
. $NuGetPath update $project.FilePath -RepositoryPath $packagesDir | |
} | |
elseif (Test-Path $projectJsonPath) | |
{ | |
cp $projectJsonPath $jsonPath | |
. $NuGetPath update $project.FilePath -RepositoryPath $packagesDir | |
cp $jsonPath $projectJsonPath | |
rm $jsonPath | |
} | |
} | |
catch | |
{ | |
} | |
} | |
} | |
. $NuGetPath restore $solutionPath | |
$dotnetCommand = get-command dotnet.exe | |
if (!$dotnetCommand) | |
{ | |
Write-Warning "Could not find dotnet.exe, you should run dotnet restore yourself" | |
} | |
else | |
{ | |
. $dotnetCommand.Source restore $solutionPath | |
} |
What about scanning imported props for PackageReference
s to update too?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi, just wanted to say thanks for this. Much appreciated.
Quick suggestion: you might want to write a header comment stating that this will not in fact resolve PackageReference elements the same way the MSBuild engine would. If any have conditions or are added through custom MSBuild targets, this may not end up working as expected.
Again, thanks!