Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: CI
name: Test and lint

on:
pull_request:
Expand All @@ -23,7 +23,7 @@ jobs:
with:
path: |
~/.cache/go-build
~/go/pkg/mod
${{ env.GOPATH }}/pkg/mod
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
restore-keys: |
${{ runner.os }}-go-
Expand Down
8 changes: 4 additions & 4 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Build and Publish Release Binaries
name: Build and publish release binaries

on:
release:
Expand Down Expand Up @@ -28,13 +28,13 @@ jobs:
output=remake_${{ matrix.goos }}_${{ matrix.goarch }}
if [ "${{ matrix.goos }}" = "windows" ]; then output=$output.exe; fi
GOOS=${{ matrix.goos }} GOARCH=${{ matrix.goarch }} \
go build -ldflags "-s -w" -o dist/$output .
go build -ldflags "-s -w -X github.com/TrianaLab/remake/config.buildVersion=${{ github.event.release.tag_name }}" \
-o dist/$output .

- name: Publish to GitHub Release
uses: softprops/action-gh-release@v2
if: startsWith(github.event.release.tag_name, 'v')
with:
# Subir sólo el binario correspondiente a esta matriz
files: dist/remake_${{ matrix.goos }}_${{ matrix.goarch }}${{ matrix.goos == 'windows' && '.exe' || '' }}
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
5 changes: 4 additions & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ type Config struct {
// userHomeDir allows us to override os.UserHomeDir in tests.
var userHomeDir = os.UserHomeDir

// buildVersion is populated via -ldflags at build time.
var buildVersion = "dev"

// InitConfig initializes directory structure and loads configuration from
// ~/.remake/config.yaml, applying defaults for all settings.
func InitConfig() (*Config, error) {
Expand All @@ -96,7 +99,7 @@ func InitConfig() (*Config, error) {
viper.SetDefault("cacheDir", filepath.Join(baseDir, "cache"))
viper.SetDefault("defaultMakefile", "makefile")
viper.SetDefault("defaultRegistry", "ghcr.io")
viper.SetDefault("version", "dev")
viper.SetDefault("version", buildVersion)
viper.SetDefault("noCache", false)

// Create default config file if it does not exist
Expand Down
5 changes: 4 additions & 1 deletion makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@ ifeq ($(GOBIN),)
GOBIN := $(shell go env GOPATH)/bin
endif

VERSION ?= $(shell git rev-parse HEAD)
LDFLAGS = -X 'github.com/TrianaLab/remake/config.buildVersion=$(VERSION)'

.PHONY: all build install test coverage lint clean

all: build

build:
go build -o bin/remake .
go build -ldflags "$(LDFLAGS)" -o bin/remake .

install: build
@echo "Installing remake to $(GOBIN)"
Expand Down
196 changes: 136 additions & 60 deletions scripts/get-remake.sh
Original file line number Diff line number Diff line change
@@ -1,84 +1,160 @@
#!/usr/bin/env bash
set -e

REPO="TrianaLab/remake"
API_URL="https://api.github.com/repos/$REPO/releases/latest"
: ${BINARY_NAME:="remake"}
: ${USE_SUDO:="true"}
: ${DEBUG:="false"}
: ${REMAKE_INSTALL_DIR:="/usr/local/bin"}
: ${REPO:="TrianaLab/remake"}
: ${API_URL:="https://api.github.com/repos/$REPO/releases"}

detect_os_arch() {
OS="$(uname | tr '[:upper:]' '[:lower:]')"
case "$OS" in
linux|darwin|mingw*|msys*|cygwin*)
if [[ "$OS" == mingw* || "$OS" == msys* || "$OS" == cygwin* ]]; then
OS="windows"
fi
;;
*)
echo "Unsupported OS: $OS" >&2
exit 1
;;
HAS_CURL="$(type curl >/dev/null 2>&1 && echo true || echo false)"
HAS_WGET="$(type wget >/dev/null 2>&1 && echo true || echo false)"

initArch() {
ARCH=$(uname -m)
case $ARCH in
x86_64|amd64) ARCH="amd64";;
aarch64|arm64) ARCH="arm64";;
*) echo "Unsupported architecture: $ARCH" >&2; exit 1;;
esac
}

ARCH="$(uname -m)"
case "$ARCH" in
x86_64|amd64) ARCH="amd64" ;;
aarch64|arm64) ARCH="arm64" ;;
*)
echo "Unsupported architecture: $ARCH" >&2
exit 1
;;
initOS() {
OS=$(uname | tr '[:upper:]' '[:lower:]')
case "$OS" in
linux|darwin) ;;
mingw*|msys*|cygwin*) OS="windows";;
*) echo "Unsupported OS: $OS" >&2; exit 1;;
esac
}

EXT=""
if [ "$OS" = "windows" ]; then
EXT=".exe"
runAsRoot() {
if [ "$USE_SUDO" = "true" ] && [ "$(id -u)" -ne 0 ]; then
sudo "$@"
else
"$@"
fi

echo "${OS}" "${ARCH}" "${EXT}"
}

fetch_latest_tag() {
curl -sSL "$API_URL" \
| grep -E '"tag_name":' \
| sed -E 's/.*"([^"]+)".*/\1/'
verifySupported() {
supported="linux-amd64 linux-arm64 darwin-amd64 darwin-arm64 windows-amd64 windows-arm64"
if ! echo "$supported" | grep -qw "$OS-$ARCH"; then
echo "No prebuilt binary for $OS-$ARCH" >&2
exit 1
fi
if [ "$HAS_CURL" != "true" ] && [ "$HAS_WGET" != "true" ]; then
echo "curl or wget is required" >&2
exit 1
fi
}

find_install_dir() {
IFS=':' read -r -a paths <<< "$PATH"
for dir in "${paths[@]}"; do
if [ -d "$dir" ] && [ -w "$dir" ]; then
echo "$dir"
return
checkDesiredVersion() {
if [ -z "$DESIRED_VERSION" ]; then
if [ "$HAS_CURL" = "true" ]; then
TAG=$(curl -sSL "$API_URL/latest" | grep -E '"tag_name":' | sed -E 's/.*"([^"]+)".*/\1/')
else
TAG=$(wget -qO- "$API_URL/latest" | grep -E '"tag_name":' | sed -E 's/.*"([^"]+)".*/\1/')
fi
if [ -z "$TAG" ]; then
echo "Failed to fetch latest version" >&2
exit 1
fi
else
TAG="$DESIRED_VERSION"
status_code=0
if [ "$HAS_CURL" = "true" ]; then
status_code=$(curl -sSL -o /dev/null -w "%{http_code}" "$API_URL/tags/$TAG")
else
status_code=$(wget --server-response --spider -q "https://api.github.com/repos/$REPO/releases/tags/$TAG" 2>&1 | awk '/HTTP\//{print $2}')
fi
if [ "$status_code" != "200" ]; then
echo "Version $TAG not found in $REPO releases" >&2
exit 1
fi
done
echo ""
fi
}

install_binary() {
os="$1"; arch="$2"; ext="$3"; tag="$4"
filename="remake_${os}_${arch}${ext}"
url="https://github.com/$REPO/releases/download/$tag/$filename"

tmpdir="$(mktemp -d)"
target="$tmpdir/$filename"
checkInstalledVersion() {
if [ -f "$REMAKE_INSTALL_DIR/$BINARY_NAME$EXT" ]; then
INSTALLED=$("$REMAKE_INSTALL_DIR/$BINARY_NAME$EXT" version 2>/dev/null || true)
if [ "$INSTALLED" = "$TAG" ]; then
echo "$BINARY_NAME $TAG is already installed"
exit 0
fi
fi
}

curl -sSL "$url" -o "$target"
downloadFile() {
filename="${BINARY_NAME}_${OS}_${ARCH}${EXT}"
url="https://github.com/$REPO/releases/download/$TAG/$filename"
tmp="$(mktemp -d)"
target="$tmp/$filename"
if [ "$HAS_CURL" = "true" ]; then
curl -sSL "$url" -o "$target"
else
wget -qO "$target" "$url"
fi
chmod +x "$target"
mv "$target" "$tmp/$BINARY_NAME$EXT"
DOWNLOAD_DIR="$tmp"
}

install_dir="$(find_install_dir)"
if [ -z "$install_dir" ]; then
install_dir="$HOME/.local/bin"
mkdir -p "$install_dir"
echo "Warning: no writable dir in PATH, installing into $install_dir" >&2
fi
installFile() {
runAsRoot mv "$DOWNLOAD_DIR/$BINARY_NAME$EXT" "$REMAKE_INSTALL_DIR/"
echo "$BINARY_NAME installed to $REMAKE_INSTALL_DIR/$BINARY_NAME$EXT"
}

mv "$target" "$install_dir/remake${ext}"
echo "Installed remake to $install_dir/remake${ext}"
help() {
echo "Usage: install.sh [--version <version>] [--no-sudo] [--help]"
echo " --version, -v specify version (e.g. v1.2.3)"
echo " --no-sudo disable sudo for installation"
echo " --help, -h show help"
}

main() {
read -r os arch ext <<<"$(detect_os_arch)"
tag="$(fetch_latest_tag)"
install_binary "$os" "$arch" "$ext" "$tag"
cleanup() {
[ -n "$DOWNLOAD_DIR" ] && rm -rf "$DOWNLOAD_DIR"
}

main
trap cleanup EXIT

while [ $# -gt 0 ]; do
case $1 in
--version|-v)
shift
if [ -n "$1" ]; then
DESIRED_VERSION="$1"
else
echo "Expected version after $1" >&2
exit 1
fi
;;
--no-sudo)
USE_SUDO="false"
;;
--help|-h)
help
exit 0
;;
*)
echo "Unknown option: $1" >&2
help
exit 1
;;
esac
shift
done

initArch
initOS
verifySupported
checkDesiredVersion

EXT=""
if [ "$OS" = "windows" ]; then
EXT=".exe"
fi

checkInstalledVersion
downloadFile
installFile