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 100755 index 0000000000..1db608054c --- /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 diff --git a/bin/fetch-configlet.ps1 b/bin/fetch-configlet.ps1 new file mode 100755 index 0000000000..f89ed4ecb6 --- /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