From 4f357329f9b7b75a25eb52879a4a62203636c619 Mon Sep 17 00:00:00 2001 From: Doron Behar Date: Sun, 28 Jul 2024 01:32:12 +0300 Subject: [PATCH] Convert bump-version to a set of Makefile targets Easier to run parts that fail once more manually. --- Makefile | 101 ++++++++++++++++++++++++++++++++++++++++++++++-- bump-version.sh | 90 ------------------------------------------ flake.nix | 9 +++-- 3 files changed, 104 insertions(+), 96 deletions(-) delete mode 100755 bump-version.sh diff --git a/Makefile b/Makefile index 37d34ec..473f591 100644 --- a/Makefile +++ b/Makefile @@ -22,8 +22,101 @@ build: ifeq (, $(shell which jq)$(shell which nix)) $(warning "No jq and/or nix executables in PATH, cannot get info from flake.nix") else -release: - ./bump-version.sh +THIS_MAKEFILE_PATH:=$(word $(words $(MAKEFILE_LIST)),$(MAKEFILE_LIST)) +THIS_DIR:=$(shell cd $(dir $(THIS_MAKEFILE_PATH));pwd) +# https://stackoverflow.com/a/76119094/4935114 +COLOUR_GREEN='\033[0;32m' +COLOUR_RED='\033[0;31m' +COLOUR_BLUE='\033[0;34m' +COLOUR_YELLOW='\033[1;33m' +COLOUR_CYAN='\033[1;36m' +COLOUR_PURPLE='\033[1;35m' +COLOUR_WHITE='\033[1;37m' +COLOUR_RESET='\033[0m' + +# Interestingly, builtins.currentSystem is undefined for `nix repl` and a few +# other nix commands. This example is from +# https://nix.dev/manual/nix/stable/language/builtin-constants#builtins-currentSystem +NIX_CURRENT_SYSTEM=$(shell nix-instantiate \ + --eval \ + --expr builtins.currentSystem \ + --json |\ + jq --raw-output . \ +) +NIX_ATTRIBUTES=$(shell nix search \ + $(THIS_DIR) \ + pistol-static-linux \ + --json |\ + jq --raw-output 'keys | .[]' |\ + sed 's/^packages.$(NIX_CURRENT_SYSTEM).//g' \ +) +NIX_TARGETS=$(foreach attr, $(NIX_ATTRIBUTES), releaseAssets/$(attr)) + +V_MAJOR=$(shell cut -d. -f1 VERSION) +V_MINOR=$(shell cut -d. -f2 VERSION) +V_PATCH=$(shell cut -d. -f3 VERSION) +VERSION:=$(V_MAJOR).$(V_MINOR).$(shell echo $$(($(V_PATCH)+1))) +version_ok=$(strip $(shell \ + for version_part_idx in 1 2 3; do \ + version_part=$$(echo $(VERSION) | cut -d. -f$$version_part_idx); \ + if test "$$version_part" -eq "$$version_part" 2> /dev/null; then \ + continue; \ + else \ + echo error: semver part $$version_part_idx of \ + version $(VERSION) is \'$$version_part\' which is not \ + an integer; \ + break; \ + fi; \ + done \ +)) +ifneq (, $(version_ok)) +$(error $(version_ok)) +endif + +check-git-clean: + @git diff-index --quiet HEAD || ( \ + echo -e $(COLOUR_RED)Git directory is dirty, Cannot commit a new \ + VERSION file and use Nix to compile from a clean checkout.\ + $(COLOUR_RESET); exit 2) + +# This below 2 wildcard checkes essentially mean: No matter how old the +# new{VersionFile,Tag} files, consider these targets as updated if the files +# exist. Useful when debugging these phases. +ifeq (,$(wildcard newVersionFile)) +newVersionFile: VERSION check-git-clean + @echo -e $(COLOUR_CYAN)❯ Updating version: \ + $(COLOUR_WHITE)$(V_MAJOR).$(V_MINOR).$(V_PATCH)$(COLOUR_RESET) \ + "->" \ + $(COLOUR_WHITE)$(VERSION)$(COLOUR_RESET) + @echo $(VERSION) > VERSION + git add VERSION + git commit -m "Bump version to $(VERSION)" + @touch newVersionFile +endif +ifeq (,$(wildcard newTag)) +newTag: newVersionFile + git tag -a -m v$(VERSION) v$(VERSION) + git push + git push origin --tags v$(VERSION) + @touch newTag +endif + +$(NIX_TARGETS): + @mkdir -p releaseAssets + ln -sf $$(nix build \ + --print-build-logs \ + --no-link \ + --print-out-paths \ + .\#$(@F) \ + )/bin/pistol "$@" + ldd "$@" 2>&1 | grep -q 'not a dynamic executable' + +release: pistol.1 newTag $(NIX_TARGETS) + gh release create v$(VERSION) --generate-notes pistol.1 $(NIX_TARGETS) + $(MAKE) cleanReleaseTemps + +cleanReleaseTemps: + rm -f newTag newVersionFile $(NIX_TARGETS) endif # Manpage @@ -92,4 +185,6 @@ test: pistol @echo ------------------- @./pistol --config tests/config tests/multi-extra A B -.PHONY: build install changelog +# Nix is smarter then gnumake in deciding whether a target is already available +# in the /nix/store cache or not +.PHONY: build install changelog $(NIX_TARGETS) diff --git a/bump-version.sh b/bump-version.sh deleted file mode 100755 index 7cddb0d..0000000 --- a/bump-version.sh +++ /dev/null @@ -1,90 +0,0 @@ -#!/usr/bin/env bash - -# Thanks goes to @pete-otaqui for the initial gist: -# https://gist.github.com/pete-otaqui/4188238 -# -# Original version modified by Marek Suscak -# -# works with a file called VERSION in the current directory, -# the contents of which should be a semantic version number -# such as "1.2.3" or even "1.2.3-beta+001.ab" - -# this script will display the current version, automatically -# suggest a "minor" version update, and ask for input to use -# the suggestion, or a newly entered value. - -# once the new version number is determined, the script will -# pull a list of changes from git history, prepend this to -# a file called CHANGELOG.md (under the title of the new version -# number), give user a chance to review and update the changelist -# manually if needed and create a GIT tag. - -set -e - -NOW="$(date +'%B %d, %Y')" -RED="\033[1;31m" -GREEN="\033[0;32m" -YELLOW="\033[1;33m" -BLUE="\033[1;34m" -PURPLE="\033[1;35m" -CYAN="\033[1;36m" -WHITE="\033[1;37m" -RESET="\033[0m" - -QUESTION_FLAG="${GREEN}?" -WARNING_FLAG="${YELLOW}!" -NOTICE_FLAG="${CYAN}❯" - -ADJUSTMENTS_MSG="${QUESTION_FLAG} ${CYAN}Now you can make adjustments to ${WHITE}CHANGELOG.md${CYAN}. Then press enter to continue." -PUSHING_MSG="${NOTICE_FLAG} Pushing new version to the ${WHITE}origin${CYAN}..." - -if [ -f VERSION ]; then - BASE_STRING=`cat VERSION` - BASE_LIST=(`echo $BASE_STRING | tr '.' ' '`) - V_MAJOR=${BASE_LIST[0]} - V_MINOR=${BASE_LIST[1]} - V_PATCH="${BASE_LIST[2]}" - if [[ -z "$V_PATCH" ]]; then - V_PATCH=0 - fi - echo -e "${NOTICE_FLAG} Current version: ${WHITE}$BASE_STRING" - SUGGESTED_VERSION="$V_MAJOR.$V_MINOR.$((V_PATCH + 1))" - echo -ne "${QUESTION_FLAG} ${CYAN}Enter a version number [${WHITE}$SUGGESTED_VERSION${CYAN}]: " - read INPUT_STRING - if [ "$INPUT_STRING" = "" ]; then - INPUT_STRING=$SUGGESTED_VERSION - fi - echo -e "${NOTICE_FLAG} Will set new version to be ${WHITE}$INPUT_STRING" - echo $INPUT_STRING > VERSION - git add VERSION - git commit -m "Bump version to ${INPUT_STRING}." - if ! git diff-index --quiet HEAD; then - echo -e "${WARNING_FLAG} Git directory is dirty, refusing to compile pistol-static." >&2 - exit 2 - fi - rm -f result - nix search . pistol-static --json | jq --raw-output 'keys | .[]' | cut -d'.' -f3 | grep -v native | while read target; do - rm -f result-"$target" - nix build --print-build-logs --print-out-paths ".#$target" --out-link result-"$target" - echo -e "${NOTICE_FLAG} Checking that the produced executable is not a dynamically linked" - ldd ./result-"$target"/bin/pistol 2>&1 | grep 'not a dynamic executable' - echo -e "${NOTICE_FLAG} Checking that the produced executable has the version string compiled into it" - done - # Test the only executable that we can run that it has a good --version output - ./result-pistol-static-linux-x86_64/bin/pistol --version | grep $INPUT_STRING - gh release create v$INPUT_STRING --generate-notes \ - ./result-pistol-static-linux-x86_64/share/man/man1/pistol.1.gz - git tag -a -m "v$INPUT_STRING" "v$INPUT_STRING" - git push - git push origin --tags "v$INPUT_STRING" - nix search . pistol-static --json | jq --raw-output 'keys | .[]' | cut -d'.' -f3 | grep -v native | while read target; do - ln -s result-"$target"/bin/pistol "$target" - gh release upload v$INPUT_STRING "$target" - rm "$target" - done -else - echo -e "${WARNING_FLAG} Could not find a VERSION file." >&2 - exit 1 -fi - -echo -e "${NOTICE_FLAG} Finished." diff --git a/flake.nix b/flake.nix index f056fa2..1a2a8b9 100644 --- a/flake.nix +++ b/flake.nix @@ -52,7 +52,10 @@ .direnv ### Makefile related files ./Makefile - "bump-version.sh" + ./newVersionFile + ./newTag + ./releaseAssets + ./releaseAssetsUploaded # built by go build or simply with `make` ./pistol ./pistol.1 @@ -71,7 +74,7 @@ inherit version src; }; pistol = pkgs.callPackage ./pkg.nix pkgArgs; - pistol-static-linux-native = pkgs.pkgsStatic.callPackage ./pkg.nix pkgArgs; + pistol-static-native = pkgs.pkgsStatic.callPackage ./pkg.nix pkgArgs; pistol-static-linux-x86_64 = pkgs.pkgsCross.gnu64.pkgsStatic.callPackage ./pkg.nix pkgArgs; pistol-static-linux-aarch64 = pkgs.pkgsCross.aarch64-multiplatform-musl.pkgsStatic.callPackage ./pkg.nix pkgArgs; pistol-static-linux-armv7l = pkgs.pkgsCross.armv7l-hf-multiplatform.pkgsStatic.callPackage ./pkg.nix pkgArgs; @@ -91,7 +94,7 @@ packages = { inherit pistol - pistol-static-linux-native + pistol-static-native pistol-static-linux-x86_64 pistol-static-linux-aarch64 pistol-static-linux-armv7l