-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from theohbrothers/refactor/move-functions-to-.…
…-generate-functions Refactor: Move functions to `./generate/functions`
- Loading branch information
Showing
3 changed files
with
165 additions
and
166 deletions.
There are no files selected for viewing
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 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,164 @@ | ||
function Generate-DownloadBinary ($o) { | ||
Set-StrictMode -Version Latest | ||
|
||
if ($o['checksumsUrl']) { | ||
Set-Checksums "$( $o['binary'] )-$( $o['version'] )" $o['checksumsUrl'] | ||
}else { | ||
$release = Invoke-RestMethod "https://api.github.com/repos/$( $o['repository'] )/releases/tags/$( $o['version'] )" | ||
$releaseAssetsFiles = $release.assets | ? { $_.name -match [regex]::Escape($o['binary']) -and $_.name -notmatch '\.sha\d+$' } | ||
$files = [ordered]@{} | ||
foreach ($f in $releaseAssetsFiles ) { | ||
$sha = & { | ||
$shaF = $release.assets | ? { $_.name -eq "$( $f.name ).sha256" -or $_ -eq "$( $f.name ).sha512" } | ||
$r = Invoke-WebRequest $shaF.browser_download_url | ||
$c = if ($r.headers['Content-Type'] -eq 'text/plain') { $r.Content } else { [System.Text.Encoding]::UTF8.GetString($r.Content) } | ||
$c = $c.Trim() -replace '^([a-fA-F0-9]+) .+', '$1' # The checksum is the first column | ||
$c | ||
} | ||
$files[$f.name] = $sha | ||
} | ||
} | ||
$shellVariable = "$( $o['binary'].ToUpper() -replace '[^A-Za-z0-9_]', '_' )_VERSION" | ||
@" | ||
# Install $( $o['binary'] ) | ||
RUN set -eux; \ | ||
$shellVariable=$( $o['version'] ); \ | ||
case "`$( uname -m )" in \ | ||
"@ | ||
|
||
$o['architectures'] = if ($o.Contains('architectures')) { $o['architectures'] } else { 'linux/386,linux/amd64,linux/arm/v6,linux/arm/v7,linux/arm64,linux/ppc64le,linux/riscv64,linux/s390x' } | ||
foreach ($a in ($o['architectures'] -split ',') ) { | ||
$split = $a -split '/' | ||
$os = $split[0] # E.g. 'linux' | ||
$arch = $split[1] # E.g. 'amd64' | ||
$archv = if ($split.Count -gt 2) { $split[2] } else { '' } # E.g. 'v6' or '' | ||
switch ($a) { | ||
"$os/386" { | ||
$hardware = 'x86' | ||
$regex = "$os[-_](i?$arch|x86(_64)?)[-_]?$archv$( [regex]::Escape($o['archiveformat']) )$|(i?$arch|x86(_64)?)[-_]?$archv.*?[-_]$os.*?$( [regex]::Escape($o['archiveformat']) )$" | ||
} | ||
"$os/amd64" { | ||
$hardware = 'x86_64' | ||
$regex = "$os[-_]($arch|x86(_64)?)[-_]?$archv$( [regex]::Escape($o['archiveformat']) )$|($arch|x86(_64)?)[-_]?$archv.*?[-_]$os.*?$( [regex]::Escape($o['archiveformat']) )$" | ||
} | ||
"$os/arm/v6" { | ||
$hardware = 'armhf' | ||
$regex = "$os[-_]($arch|arm)[-_]?($archv)?$( [regex]::Escape($o['archiveformat']) )$|($arch|arm)[-_]?($archv)?.*?[-_]$os.*?$( [regex]::Escape($o['archiveformat']) )$" | ||
} | ||
"$os/arm/v7" { | ||
$hardware = 'armv7l' | ||
$regex = "$os[-_]($arch|arm)[-_]?($archv)?$( [regex]::Escape($o['archiveformat']) )$|($arch|arm)[-_]?($archv)?.*?[-_]$os.*?$( [regex]::Escape($o['archiveformat']) )$" | ||
} | ||
"$os/arm64" { | ||
$hardware = 'aarch64' | ||
$regex = "$os[-_]($arch|aarch64)[-_]?$archv$( [regex]::Escape($o['archiveformat']) )$|($arch|aarch64)[-_]?$archv.*?[-_]$os.*?$( [regex]::Escape($o['archiveformat']) )$" | ||
} | ||
"$os/ppc64le" { | ||
$hardware = 'ppc64le' | ||
$regex = "$os[-_]$arch[-_]?$archv$( [regex]::Escape($o['archiveformat']) )$|$arch[-_]?$archv.*?[-_]$os.*?$( [regex]::Escape($o['archiveformat']) )$" | ||
} | ||
"$os/riscv64" { | ||
$hardware = 'riscv64' | ||
$regex = "$os[-_]$arch[-_]?$archv$( [regex]::Escape($o['archiveformat']) )$|$arch[-_]?$archv.*?[-_]$os.*?$( [regex]::Escape($o['archiveformat']) )$" | ||
} | ||
"$os/s390x" { | ||
$hardware = 's390x' | ||
$regex = "$os[-_]$arch[-_]?$archv$( [regex]::Escape($o['archiveformat']) )$|$arch[-_]?$archv.*?[-_]$os.*?$( [regex]::Escape($o['archiveformat']) )$" | ||
} | ||
default { | ||
throw "Unsupported architecture: $a" | ||
} | ||
} | ||
|
||
$file = $files.Keys | ? { $_ -match $regex } | Select-Object -First 1 | ||
if ($file) { | ||
$url = if ($o['checksumsUrl']) { | ||
Split-Path $o['checksumsUrl'] -Parent | ||
}else { | ||
"https://github.com/$( $o['repository'] )/releases/download/$( $o['version'] )" | ||
} | ||
$sha = $files[$file] | ||
@" | ||
'$hardware') \ | ||
URL="$url/$file"; \ | ||
SHA256=$sha; \ | ||
;; \ | ||
"@ | ||
} | ||
} | ||
|
||
@" | ||
*) \ | ||
echo "Architecture not supported"; \ | ||
exit 1; \ | ||
;; \ | ||
esac; \ | ||
"@ | ||
|
||
@" | ||
FILE=$( $o['binary'] )$( $o['archiveformat'] ); \ | ||
wget -q "`$URL" -O "`$FILE"; \ | ||
echo "`$SHA256 `$FILE" | sha256sum -c -; \ | ||
"@ | ||
|
||
if ($o['archiveformat'] -match '\.tar\.gz|\.tgz') { | ||
if ($o['archivefiles'].Count -gt 0) { | ||
@" | ||
tar -xvf "`$FILE" --no-same-owner --no-same-permissions -- $( $o['archivefiles'] -join ' ' ); \ | ||
rm -f "`$FILE"; \ | ||
"@ | ||
}else { | ||
@" | ||
tar -xvf "`$FILE" --no-same-owner --no-same-permissions; \ | ||
rm -f "`$FILE"; \ | ||
"@ | ||
} | ||
}elseif ($o['archiveformat'] -match '\.bz2') { | ||
@" | ||
bzip2 -d "`$FILE"; \ | ||
"@ | ||
}elseif ($o['archiveformat'] -match '\.gz') { | ||
@" | ||
gzip -d "`$FILE"; \ | ||
"@ | ||
}elseif ($o['archiveformat'] -match '\.zip') { | ||
@" | ||
unzip "`$FILE" $( $o['binary'] ); \ | ||
"@ | ||
} | ||
|
||
$destination = if ($o.Contains('destination')) { $o['destination'] } else { "/usr/local/bin/$( $o['binary'] )" } | ||
$destinationDir = Split-Path $destination -Parent | ||
@" | ||
mkdir -pv $destinationDir; \ | ||
mv -v $( $o['binary'] ) $destination; \ | ||
chmod +x $destination; \ | ||
$( $o['testCommand'] ); \ | ||
"@ | ||
|
||
if ($o.Contains('archivefiles')) { | ||
if ($license = $o['archivefiles'] | ? { $_ -match 'LICENSE' }) { | ||
@" | ||
mkdir -p /licenses; \ | ||
mv -v $license /licenses/$license; \ | ||
"@ | ||
} | ||
} | ||
|
||
@" | ||
: | ||
"@ | ||
} |
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