From 1aa84883e86d7598317e73439f843c70347f4af2 Mon Sep 17 00:00:00 2001 From: Derk-Jan Karrenbeld Date: Wed, 16 Dec 2020 22:22:25 +0100 Subject: [PATCH 1/3] Add configlet fetch script --- .gitignore | 19 +++++++++++++++ bin/fetch-configlet | 52 +++++++++++++++++++++++++++++++++++++++++ bin/fetch-configlet.ps1 | 24 +++++++++++++++++++ 3 files changed, 95 insertions(+) create mode 100644 bin/fetch-configlet create mode 100644 bin/fetch-configlet.ps1 diff --git a/.gitignore b/.gitignore index 3c3629e647..b91eaeb78c 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,20 @@ +# OS specific auto-generated folders and files +*.swp +.DS_Store + +# Platform specific tools (use fetch-configlet) +bin/configlet +bin/configlet.exe + +# IDE folders and files +.idea + +# package.json generated content node_modules + +# Ignore npm error/log +npm-debug.log + +# Ignore yarn lockfile and error, in case someone accidentally runs yarn +yarn.lock +yarn-error.log diff --git a/bin/fetch-configlet b/bin/fetch-configlet new file mode 100644 index 0000000000..867d1e5f2c --- /dev/null +++ b/bin/fetch-configlet @@ -0,0 +1,52 @@ +#!/usr/bin/env bash + +set -eo pipefail + +readonly LATEST='https://api.github.com/repos/exercism/configlet/releases/latest' + +case "$(uname)" in + (Darwin*) OS='mac' ;; + (Linux*) OS='linux' ;; + (Windows*) OS='windows' ;; + (MINGW*) OS='windows' ;; + (MSYS_NT-*) OS='windows' ;; + (*) OS='linux' ;; +esac + +case "$OS" in + (windows*) EXT='zip' ;; + (*) EXT='tgz' ;; +esac + +case "$(uname -m)" in + (*64*) ARCH='64bit' ;; + (*686*) ARCH='32bit' ;; + (*386*) ARCH='32bit' ;; + (*) ARCH='64bit' ;; +esac + +if [ -z "${GITHUB_TOKEN}" ] +then + HEADER='' +else + HEADER="authorization: Bearer ${GITHUB_TOKEN}" +fi + +FILENAME="configlet-${OS}-${ARCH}.${EXT}" + +get_url () { + curl --header "$HEADER" -s "$LATEST" | + awk -v filename=$FILENAME '$1 ~ /browser_download_url/ && $2 ~ filename { print $2 }' | + tr -d '"' +} + +URL=$(get_url) + +case "$EXT" in + (*zip) + curl --header "$HEADER" -s --location "$URL" -o bin/latest-configlet.zip + unzip bin/latest-configlet.zip -d bin/ + rm bin/latest-configlet.zip + ;; + (*) curl --header "$HEADER" -s --location "$URL" | tar xz -C bin/ ;; +esac \ No newline at end of file diff --git a/bin/fetch-configlet.ps1 b/bin/fetch-configlet.ps1 new file mode 100644 index 0000000000..8960c3d971 --- /dev/null +++ b/bin/fetch-configlet.ps1 @@ -0,0 +1,24 @@ +Function DownloadUrl ([string] $FileName, $Headers) { + $latestUrl = "https://api.github.com/repos/exercism/configlet/releases/latest" + $json = Invoke-RestMethod -Headers $Headers -Uri $latestUrl + $json.assets | Where-Object { $_.browser_download_url -match $FileName } | Select-Object -ExpandProperty browser_download_url +} + +Function Headers { + If ($GITHUB_TOKEN) { @{ Authorization = "Bearer ${GITHUB_TOKEN}" } } Else { @{ } } +} + +Function Arch { + If ([Environment]::Is64BitOperatingSystem) { "64bit" } Else { "32bit" } +} + +$arch = Arch +$headers = Headers +$fileName = "configlet-windows-$arch.zip" +$outputDirectory = "bin" +$outputFile = Join-Path -Path $outputDirectory -ChildPath $fileName +$zipUrl = DownloadUrl -FileName $fileName -Headers $headers + +Invoke-WebRequest -Headers $headers -Uri $zipUrl -OutFile $outputFile +Expand-Archive $outputFile -DestinationPath $outputDirectory -Force +Remove-Item -Path $outputFile \ No newline at end of file From 3eaf92bd1539e2d139f21f5b5b832936bb10fef2 Mon Sep 17 00:00:00 2001 From: Derk-Jan Karrenbeld Date: Wed, 16 Dec 2020 22:23:26 +0100 Subject: [PATCH 2/3] Make executable --- bin/fetch-configlet | 0 bin/fetch-configlet.ps1 | 0 2 files changed, 0 insertions(+), 0 deletions(-) mode change 100644 => 100755 bin/fetch-configlet mode change 100644 => 100755 bin/fetch-configlet.ps1 diff --git a/bin/fetch-configlet b/bin/fetch-configlet old mode 100644 new mode 100755 diff --git a/bin/fetch-configlet.ps1 b/bin/fetch-configlet.ps1 old mode 100644 new mode 100755 From c1588d7239c28724739066939ecd1b73bc096ab0 Mon Sep 17 00:00:00 2001 From: Derk-Jan Karrenbeld Date: Wed, 16 Dec 2020 22:41:51 +0100 Subject: [PATCH 3/3] Apply suggestions from code review Co-authored-by: Sascha Mann --- bin/fetch-configlet | 2 +- bin/fetch-configlet.ps1 | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/bin/fetch-configlet b/bin/fetch-configlet index 867d1e5f2c..1db608054c 100755 --- a/bin/fetch-configlet +++ b/bin/fetch-configlet @@ -49,4 +49,4 @@ case "$EXT" in rm bin/latest-configlet.zip ;; (*) curl --header "$HEADER" -s --location "$URL" | tar xz -C bin/ ;; -esac \ No newline at end of file +esac diff --git a/bin/fetch-configlet.ps1 b/bin/fetch-configlet.ps1 index 8960c3d971..f89ed4ecb6 100755 --- a/bin/fetch-configlet.ps1 +++ b/bin/fetch-configlet.ps1 @@ -21,4 +21,4 @@ $zipUrl = DownloadUrl -FileName $fileName -Headers $headers Invoke-WebRequest -Headers $headers -Uri $zipUrl -OutFile $outputFile Expand-Archive $outputFile -DestinationPath $outputDirectory -Force -Remove-Item -Path $outputFile \ No newline at end of file +Remove-Item -Path $outputFile