Skip to content

Commit

Permalink
Merge pull request #8 from theohbrothers/refactor/move-functions-to-.…
Browse files Browse the repository at this point in the history
…-generate-functions

Refactor: Move functions to `./generate/functions`
  • Loading branch information
leojonathanoh authored Dec 1, 2023
2 parents 3f805a3 + 6435361 commit b654d9e
Show file tree
Hide file tree
Showing 3 changed files with 165 additions and 166 deletions.
2 changes: 1 addition & 1 deletion generate/definitions/VARIANTS.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ $VARIANTS = @(
$variant['package_version']
$subVariant['components'] | ? { $_ }
) -join '-'
tag_as_latest = if ($variant['base_image_tag'] -eq $local:BASE_IMAGE_TAG_LATEST_STABLE -and $subVariant['components'].Count -eq 0) { $true } else { $false }
tag_as_latest = if ($variant['package_version'] -eq $local:VARIANTS_MATRIX[0]['package_version'] -and $subVariant['components'].Count -eq 0) { $true } else { $false }
}
}
}
Expand Down
164 changes: 164 additions & 0 deletions generate/functions/Generate-DownloadBinary.ps1
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; \
"@
}
}

@"
:
"@
}
165 changes: 0 additions & 165 deletions generate/templates/Dockerfile.ps1
Original file line number Diff line number Diff line change
@@ -1,168 +1,3 @@
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; \
"@
}
}

@"
:
"@
}

@"
FROM alpine:3.17
Expand Down

0 comments on commit b654d9e

Please sign in to comment.