Created
November 30, 2015 23:25
-
-
Save fearthecowboy/9c8c19b542547c94d0e8 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
## warning: this is not a generic script, it's tailored to what I'm doing | |
## but I thought I'd post it for fits and giggles. | |
## | |
## This script loads the project.json file and then looks in the nuget feeds | |
## to find updated versions of the packages that for each dependency. | |
## | |
## It tries to keep the packages to ones before RC2 (there are breaking changes | |
## in my code base that I can't move to RC2 yet) | |
## | |
## If there isn't an RC1-final, it looks for the highest RC1 build, then -beta8 | |
## -beta7 and finally -beta bits. | |
## and doesn't go lower than we already have. | |
[CmdletBinding()] | |
param( | |
[string]$project= (get-item (resolve-path ".\project.json")).Name | |
) | |
function Is-FixedVersion { | |
param( [string]$version ) | |
$t = $version.indexOf( "-"); | |
if( $t -gt -1 ) { | |
return $false | |
} | |
return $true | |
} | |
function Get-BaseVersion { | |
param( [string]$version ) | |
if( (Is-FixedVersion $version ) ) { | |
return $version | |
} | |
return $version.substring( 0, $version.indexOf( "-") ) | |
} | |
function Get-Group { | |
param( [string]$version ) | |
if( (Is-FixedVersion $version ) ) { | |
return $null | |
} | |
$t = $version.indexOf( "-") | |
$tt = $version.indexOf( "-", $t+1) | |
if( $tt -eq -1 ) { | |
$result = $version.substring( $t+1 ) | |
} else { | |
$result = $version.substring( $t+1, $tt - $t-1 ) | |
} | |
if( -not (Is-Int $result) ) { | |
return $result | |
} | |
return $null | |
} | |
function Get-GroupNumber { | |
param( [string]$version ) | |
$g = get-group $version | |
if( $g ) { | |
$v = $g -replace "[^\d*]","" | |
if( Is-Int $v ) { | |
return $v | |
} | |
} | |
return $null | |
} | |
function Is-Int { | |
param( [string]$val ) | |
$rtn = "" | |
return [int]::TryParse($val,[ref]$rtn) | |
} | |
function Get-Build { | |
param( [string]$version ) | |
if( (Is-FixedVersion $version ) ) { | |
return 0 | |
} | |
$result = $version.substring( $version.LastIndexOf( "-" )+1 ) | |
if( Is-Int $result ) { | |
return $result | |
} | |
if( $result -eq "final") { | |
return $result | |
} | |
return 0 | |
} | |
function Get-BuildNumber { | |
param( [string]$version ) | |
if( (Is-FixedVersion $version ) ) { | |
return 0 | |
} | |
$result = $version.substring( $version.LastIndexOf( "-" )+1 ) | |
if( Is-Int $result ) { | |
return [int]$result | |
} | |
if( $result -eq "final") { | |
return 65535 | |
} | |
return 0 | |
} | |
function Parse-Package { | |
param( $package) | |
$version = $package.Version | |
$v = $package | select *, Base, Group, GroupNumber, Build, IsFixedVersion, SysVer | |
$v.Base = Get-BaseVersion $version | |
$v.Group = Get-Group $version | |
$v.GroupNumber = Get-GroupNumber $version | |
$v.Build = Get-Build $version | |
$v.IsFixedVersion = Is-FixedVersion $version | |
$v.SysVer = [System.Version]$v.Base | |
if( $v.SysVer.REvision -eq -1 ) { | |
$v.SysVer = [System.Version]($v.Base + "." + (Get-BuildNumber $version)) | |
} | |
return $v | |
} | |
# | |
function Try-Match { | |
param ($pkgs) | |
# first, check for RC1-FINAL | |
$found = $pkgs |? { | |
if( ($_.Group -eq "rc1") -and ($_.Build -eq "final" ) ) { | |
return $true | |
} | |
return $false | |
} | |
# nope, just get rc1 packages, and grab the highest | |
if( -not $found ) { | |
$found = $pkgs |? { $_.Group -eq "rc1" } | sort -property build -unique | select -first 1 | |
} | |
# beta8? | |
if( -not $found ) { | |
$found = $pkgs |? { $_.Group -eq "beta8" } | sort -property build -unique | select -first 1 | |
} | |
# beta7? | |
if( -not $found ) { | |
$found = $pkgs |? { $_.Group -eq "beta7" } | sort -property build -unique | select -first 1 | |
} | |
# beta? | |
if( -not $found ) { | |
$found = $pkgs |? { $_.Group -eq "beta" } | sort -property build -unique | select -first 1 | |
} | |
return $found | |
} | |
function Update-Dependency { | |
param ($deps) | |
$success = $true | |
if( -not $deps ) { | |
return $true | |
} | |
$keys = ($deps| get-member -MemberType NoteProperty).Name | |
$vals = ($deps| get-member -MemberType NoteProperty).Value | |
$ignored = @( | |
"System.Management.Automation", | |
"Microsoft.PowerShell.Security", | |
"Microsoft.PowerShell.Commands.Utility", | |
"Microsoft.PowerShell.Commands.Management", | |
"Microsoft.Management.Infrastructure", | |
"Microsoft.Management.Infrastructure.Native" | |
) | |
foreach( $package in $keys) { | |
# the version number listed for that package. | |
$version = $deps.($package) | |
if( (-not $version) -or ($ignored -contains $package) ) { | |
continue | |
} | |
$orig = "" | select Name,Version | |
$orig.Name = $package | |
$orig.Version = $version | |
# (not using) ... https://www.myget.org/F/aspnetcidev/, | |
if( -not ( Is-FixedVersion $version ) ) { | |
# only handle versions that are not fixed. | |
$orig = Parse-Package $orig | |
write-host -fore green "Current Package: $($orig.Name) [$($orig.base)] [$($orig.group)] [$($orig.build)] " | |
$allPkgs = (find-package -provider nuget -source http://nuget.org/api/v2,https://www.myget.org/F/aspnetrelease/ -allversions $orig.Name )|% { return Parse-Package $_ } | |
$pkgs = $allPkgs |? { | |
# we don't want a fixed package version at this point. | |
if( $_.IsFixedVersion ) { | |
return $false | |
} | |
# drop anything with a lowever version than what we already know | |
if( $_.SysVer -lt $orig.SysVer) { | |
return $false | |
} | |
# filter out RC2 packages | |
if( $_.Group -eq "rc2" ) { | |
return $false | |
} | |
# filter out packages with the same group name, but a lower build than we already have listed. | |
if( ($_.Group -eq $orig.Group ) -and ($_.Build -lt $orig.Build) ) { | |
return $false | |
} | |
# otherwise, it's a candidate for acceptance. | |
return $true | |
} | |
$selected = $null | |
#====== try to select the right package ===== | |
# try with the same base version that we already have | |
$found = try-match ($pkgs |? { $_.Base -eq $orig.Base } ) | |
if( -not $found ) { | |
$found = try-match $pkgs | |
if( $found ) { | |
write-host -fore Magenta "Warning: had to upgrade package version." | |
} | |
} | |
# we found a successful match. | |
if ($found ) { | |
$selected = $found | select -first 1 | |
write-host -fore white " Package: $package [$($selected.Version)] " | |
$deps.($package) = $selected.Version; | |
} else { | |
$success = $false | |
$all |% { | |
write-host -fore yellow " Package: $package [$($_.Base)] [$($_.Group)] [$($_.Build)] " | |
} | |
} | |
} | |
#write-host $package -> $version | |
} | |
return $success | |
} | |
if( -not (test-path $project )) { | |
write-host -fore red "Can't find $project (are you in the wrong directory?)" | |
return | |
} | |
$projectData = (convertfrom-json (get-content -raw $project )) | |
if( (Update-Dependency $projectData.frameworks.dnxcore50.dependencies) -and (Update-Dependency $projectData.dependencies ) ) { | |
#back up the original project.json file. | |
$files = (dir "$project.bak.*").Fullname | |
if( $files ) { | |
$c = [int]($files |% { $_.substring( $_.LastIndexOf(".")+1 ) } | sort -Descending | select -first 1)+1 | |
} else { | |
$c =1 | |
} | |
write-host -fore yellow "Backing up original file to $project.bak.$c" | |
copy $project "$project.bak.$c" | |
write-host -fore White "Updating file: $project" | |
$text = convertto-json $projectData -Depth 20 | |
$text = $text -replace "\t"," " | |
$text | out-file -filepath $project -encoding utf8 | |
#if you have node installed, we'll use it to pretty up the json | |
if( Get-Command node.exe ) { | |
node -e "var txt = require('fs').readFileSync('$project', 'utf8'); require('fs').writeFile('$project', JSON.stringify(JSON.parse(txt.substring(txt.indexOf('{'))), null, 4), function(err) { if(err) { console.log(err); } }); " | |
} | |
return $true | |
} else { | |
write-host "Failed." | |
return $false | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment