From 85abdc5b1530f9f455ce1b381f1a33f1d61d0fdc Mon Sep 17 00:00:00 2001 From: edu-diaz Date: Sat, 24 May 2025 14:24:55 +0200 Subject: [PATCH 1/2] Update workflow names and improve installation script --- .github/workflows/ci.yml | 2 +- .github/workflows/release.yml | 8 +- config/config.go | 5 +- makefile | 5 +- scripts/get-remake.sh | 196 +++++++++++++++++++++++----------- 5 files changed, 149 insertions(+), 67 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 7d6d3c3..ae8d83e 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -1,4 +1,4 @@ -name: CI +name: Test and lint on: pull_request: diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index d509cfc..2c31e63 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -1,4 +1,4 @@ -name: Build and Publish Release Binaries +name: Build and publish release binaries on: release: @@ -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 }} \ No newline at end of file + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/config/config.go b/config/config.go index 494e0ca..5d195ee 100644 --- a/config/config.go +++ b/config/config.go @@ -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) { @@ -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 diff --git a/makefile b/makefile index e29bb5e..9cd1f58 100644 --- a/makefile +++ b/makefile @@ -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)" diff --git a/scripts/get-remake.sh b/scripts/get-remake.sh index 3e82c9f..25797e6 100755 --- a/scripts/get-remake.sh +++ b/scripts/get-remake.sh @@ -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 ] [--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 From d40ec3d34b834c33bbdebfc156c4ae9b6dcf9334 Mon Sep 17 00:00:00 2001 From: edu-diaz Date: Sat, 24 May 2025 14:28:06 +0200 Subject: [PATCH 2/2] Fix cache directory --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index ae8d83e..6bab1fa 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -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-