diff --git a/.gitignore b/.gitignore index 886ffbd..d6bfde3 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ dist/ +bin/ coverage.out ghsetup diff --git a/gh-setup b/gh-setup index 031f75c..19b98ac 100755 --- a/gh-setup +++ b/gh-setup @@ -1,34 +1,49 @@ -#!/bin/bash +#!/usr/bin/env bash set -e -repo="github.com/k1LoW/gh-setup" +# Get extension repository path +extPath="$(dirname "$0")" -extensionPath="$(dirname "$0")" -cd "${extensionPath}" > /dev/null -tag="$(git tag | tail -1)" +# Get latest version +cd "${extPath}" > /dev/null +ver="$(git tag | grep ^v | sort --version-sort | tail -1)" +if [ "${ver}" = "" ]; then + git fetch --tags > /dev/null 2>&1 + ver="$(git tag | grep ^v | sort --version-sort | tail -1)" +fi cd - > /dev/null +# Get arch arch="$(uname -m)" -exe="" - +# Get binary file name +exe="gh-setup" # default if uname -a | grep Msys > /dev/null; then - if [ $arch = "x86_64" ]; then - exe="gh-setup_${tag}_windows_amd64.exe" + if [ "${arch}" = "x86_64" ]; then + exe="gh-setup_${ver}_windows_amd64.exe" fi elif uname -a | grep Darwin > /dev/null; then - if [ $arch = "x86_64" ]; then - exe="gh-setup_${tag}_darwin_amd64" - elif [ $arch = "arm64" ]; then - exe="gh-setup_${tag}_darwin_arm64" + if [ "${arch}" = "x86_64" ]; then + exe="gh-setup_${ver}_darwin_amd64" + elif [ "${arch}" = "arm64" ]; then + exe="gh-setup_${ver}_darwin_arm64" fi elif uname -a | grep Linux > /dev/null; then - if [ $arch = "x86_64" ]; then - exe="gh-setup_${tag}_linux_amd64" + if [ "${arch}" = "x86_64" ]; then + exe="gh-setup_${ver}_linux_amd64" + elif [ "${arch}" = "arm64" ] || [ "${arch}" = "aarch64" ]; then + exe="gh-setup_${ver}_linux_arm64" fi fi -if [ "${exe}" == "" ]; then +# Cleanup bin/ dir +rm -f "${extPath}/bin/*" +mkdir -p "${extPath}/bin" + +binPath="${extPath}/bin/${exe}" + +if [ "${exe}" == "gh-setup" ]; then + # Build binary if [ "$(which go)" = "" ]; then echo "go must be installed to use this gh extension on this platform" exit 1 @@ -36,18 +51,19 @@ if [ "${exe}" == "" ]; then exe="cmd.out" - cd "${extensionPath}" > /dev/null - go build -o "${exe}" cmd/gh-setup/main.go + cd "${extPath}" > /dev/null + go build -o "${binPath}" cd - > /dev/null - mv "${exe}" "${extensionPath}/bin/${exe}" - chmod +x "${extensionPath}/bin/${exe}" else - if [[ ! -x "${extensionPath}/bin/${exe}" ]]; then - mkdir -p "${extensionPath}/bin" - rm -f ${extensionPath}/bin/gh-setup* - gh release -R "${repo}" download "${tag}" -p "${exe}" --dir="${extensionPath}/bin" - chmod +x "${extensionPath}/bin/${exe}" + # Download release binary + if [[ ! -x "${binPath}" ]]; then + if [ "$(which curl)" = "" ]; then + echo "curl must be installed to use this gh extension on this platform" + exit 1 + fi + curl -sL -o "${binPath}" "https://github.com/k1LoW/gh-setup/releases/download/${ver}/${exe}" fi fi +chmod +x "${binPath}" -exec "${extensionPath}/bin/${exe}" "$@" +exec "${binPath}" "$@"