-
Notifications
You must be signed in to change notification settings - Fork 3
/
_common.ps1
130 lines (111 loc) · 3.92 KB
/
_common.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
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
function Invoke-GithubApi {
param(
[Parameter(
ValueFromRemainingArguments=$true
)][string[]]
$listArgs
)
$headers = $null
if ($env:GITHUB_TOKEN) {
$headers = @{ Authorization = "Bearer "+ $env:GITHUB_TOKEN }
} elseif ($env:GITHUB_USER -and $env:GITHUB_PASS) {
$headers = @{ Authorization = "Basic "+ [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes("$($env:GITHUB_USER):$($env:GITHUB_PASS)")) }
}
Invoke-WebRequest @listArgs -Headers $headers
}
function Get-GithubReleases {
param (
[Parameter(Position = 0, Mandatory = $true)]$Repo
)
$releases = "https://api.github.com/repos/$repo/releases"
return (Invoke-GithubApi $releases | ConvertFrom-Json)
}
function Get-GithubAssets {
param (
[Parameter(Position = 0, Mandatory = $true)]$Repo,
[Parameter(Position = 1, Mandatory = $true)]$ReleaseId
)
$assets = "https://api.github.com/repos/$Repo/releases/$ReleaseId/assets"
return (Invoke-GithubApi $assets | ConvertFrom-Json)
}
function Get-Property ($Object, $PropertyName, [object[]]$ArgumentList) {
return $Object.GetType().InvokeMember($PropertyName, 'Public, Instance, GetProperty', $null, $Object, $ArgumentList)
}
function Get-MsiInfo {
param (
[Parameter(Position = 0, Mandatory = $true)]$Url
)
$windowsInstaller = New-Object -ComObject WindowsInstaller.Installer
$urlHashStream = [IO.MemoryStream]::new([byte[]][char[]]$Url)
$urlHash = (Get-FileHash -InputStream $urlHashStream -Algorithm MD5).Hash
$FileName = $urlHash + "-" + $Url.Split('/')[-1]
$FilePath = ".tmp\$FileName"
New-Item -ItemType Directory -Path ".tmp" -ErrorAction SilentlyContinue
if(-not (Test-Path -Path $FilePath)) {
Start-BitsTransfer -Source $Url -Destination $FilePath
}
$MSI = $windowsInstaller.OpenDatabase($FilePath, 0)
$View = $MSI.OpenView("SELECT * FROM Property")
$View.Execute()
$hash = @{}
while ($Record = $View.Fetch()) {
$name = Get-Property $Record StringData 1
$value = Get-Property $Record StringData 2
$hash.Add($name,$value)
}
$View.Close()
$checksum = (Get-FileHash -Path $FilePath -Algorithm SHA256).Hash
return [pscustomobject]@{
Version = $hash.ProductVersion
ProductCode = $hash.ProductCode
UpgradeCode = $hash.UpgradeCode
Hashsum = $checksum
}
}
function Get-ProductReleases {
param (
[Parameter(Position = 0, Mandatory = $true)]$Repo,
[Switch]$WithPrerelease
)
@(
Get-GithubReleases -Repo $Repo |
Where-Object { (-not $_.prerelease) -or ($_.prerelease -eq $WithPrerelease) } |
Where-Object { $_.name -inotmatch "deprecated" } |
ForEach-Object {
[pscustomobject]@{
Release = $_
Assets = (Get-GithubAssets -Repo $Repo -ReleaseId $_.id |
Where-Object { $_.name -like "*.msi" } |
ForEach-Object {
$msi = (Get-MsiInfo -Url $_.browser_download_url)
[pscustomobject]@{
PackageVersionObject = [System.Version]$msi.Version
PackageVersion = $msi.Version
InstallerUrl = $_.browser_download_url
InstallerSha256 = $msi.Hashsum
ProductCode = $msi.ProductCode
UpgradeCode = $msi.UpgradeCode
}
})
}
})
}
function Check-LastCommand {
param (
$Msg = "ERROR!"
)
if ($lastexitcode -ne 0)
{
throw $Msg
}
}
function Invoke-Git {
param(
[Parameter(
ValueFromRemainingArguments=$true
)][string[]]
$listArgs
)
& "git" @listArgs
Check-LastCommand "git call failed! $listArgs"
}