-
Notifications
You must be signed in to change notification settings - Fork 0
/
choco-outdated.ps1
88 lines (71 loc) · 2.82 KB
/
choco-outdated.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# compare packages in local repo to community repo, download new versions
# needs Chocolatey installed
# Thanks to bog for this function
function Get-NUPKG-Version {
Param(
[Parameter(Mandatory = $true, Position = 0)]
[string] $fullpath
)
Add-Type -assembly "system.io.compression.filesystem"
$zip = [io.compression.zipfile]::OpenRead($fullpath)
$file = $zip.Entries | where-object { $_.Name -Like "*.nuspec" }
$stream = $file.Open()
$reader = New-Object IO.StreamReader($stream)
$text = $reader.ReadToEnd()
$reader.Close()
$stream.Close()
$zip.Dispose()
$xml = [xml]$text
$version = $xml.package.metadata.version
return $version
}
function Get-NUPKG-Name {
Param(
[Parameter(Mandatory = $true, Position = 0)]
[string] $fullpath
)
Add-Type -assembly "system.io.compression.filesystem"
$zip = [io.compression.zipfile]::OpenRead($fullpath)
$file = $zip.Entries | where-object { $_.Name -Like "*.nuspec" }
$stream = $file.Open()
$reader = New-Object IO.StreamReader($stream)
$text = $reader.ReadToEnd()
$reader.Close()
$stream.Close()
$zip.Dispose()
$xml = [xml]$text
$packageName = $xml.package.metadata.id
return $packageName
}
# Use system proxy, user creds
$Wcl = new-object System.Net.WebClient
$Wcl.Headers.Add("user-agent", "PowerShell Script")
$Wcl.Proxy.Credentials = [System.Net.CredentialCache]::DefaultNetworkCredentials
Write-output "Getting list of packages"
$outputUnsorted = Get-ChildItem -path \\server\share$\choco\packages -Recurse -Attributes !Directory -Filter *.nupkg
# sort by package name, then package version, using dotnet's [version] data type
$output = ($outputUnsorted | Sort-Object -property @{ Expression = { (Get-NUPKG-Name($_.FullName)) } }, @{ Expression = { [version] (Get-NUPKG-Version($_.FullName)) } } )
# checks package with next package on list, if different then latest version in local repo, compare to community repo, download new versions
for ($i = 0; $i -lt $output.count; $i++) {
$package = $output[$i]
Write-Output "Checking $package"
$packageName = Get-NUPKG-Name($package.Fullname)
if ($i -lt $output.count - 1) {
$nextPackageName = Get-NUPKG-Name($output[$i + 1].FullName)
}
else {
$nextPackageName = "lastpackage"
}
if ($nextPackageName -ne $packageName ) {
Write-Output "latest local version $package"
$latestPackage = choco info $packageName --limit-output
Write-Output "Comparing $packageName : new version $latestPackage to old version $package"
$a = $latestPackage.split("|")
$newFileName = $latestPackage.Replace("|", ".") + ".nupkg"
if ($newFilename -ne $package) {
$b = "https://chocolatey.org/api/v2/package/$packageName/$($a[1])"
Write-Output "Downloading $b"
Invoke-WebRequest -uri $b -OutFile "$($a[0]).$($a[1]).nupkg"
}
}
}