From 9baf2c5c41413c015ba4bf96303eb113d0b532cb Mon Sep 17 00:00:00 2001 From: Ruben Nijveld Date: Fri, 24 Nov 2023 15:36:16 +0100 Subject: [PATCH] Add release tools (#331) --- docs/man/statime.toml.5.md | 2 +- docs/precompiled/man/statime.toml.5 | 2 +- utils/prepare-artifacts.sh | 132 ++++++++++++++++++++++++++++ utils/release.sh | 4 + utils/update-version.sh | 42 +++++++++ 5 files changed, 180 insertions(+), 2 deletions(-) create mode 100755 utils/prepare-artifacts.sh create mode 100755 utils/release.sh create mode 100755 utils/update-version.sh diff --git a/docs/man/statime.toml.5.md b/docs/man/statime.toml.5.md index e239ea84a..9b980f778 100644 --- a/docs/man/statime.toml.5.md +++ b/docs/man/statime.toml.5.md @@ -1,5 +1,5 @@ # NAME diff --git a/docs/precompiled/man/statime.toml.5 b/docs/precompiled/man/statime.toml.5 index f4d01f2bd..7b3ec6462 100644 --- a/docs/precompiled/man/statime.toml.5 +++ b/docs/precompiled/man/statime.toml.5 @@ -14,7 +14,7 @@ . ftr VB CB . ftr VBI CBI .\} -.TH "STATIME.TOML" "5" "" "statime 1.0.0" "statime" +.TH "STATIME.TOML" "5" "" "statime 0.1.0" "statime" .hy .SH NAME .PP diff --git a/utils/prepare-artifacts.sh b/utils/prepare-artifacts.sh new file mode 100755 index 000000000..bb825a0f0 --- /dev/null +++ b/utils/prepare-artifacts.sh @@ -0,0 +1,132 @@ +#!/usr/bin/env bash + +set -eo pipefail + +print_help() { + echo "Usage: utils/prepare-artifacts.sh OPTIONS" + echo "" + echo "Options:" + echo " --[no-]reset Clear the pkg directory or not (default: no)" + echo " --[no-]delete-zips Delete the downloaded zip files or not (default: no)" + echo " --[no-]download-artifacts Download the artifacts or not (default: no)" + echo " --api-token TOKEN The Github API token for downloading artifacts" + echo " --run-id ID The id of the Workflow run for which to download artifacts" +} + +GITHUB_API_TOKEN= +RUN_ID= +TARGET_DIR="target/pkg" +DO_RESET=false +DELETE_ZIPS=false +DOWNLOAD_ARTIFACTS=false +ZIP_DOWNLOADS_FOLDER="$TARGET_DIR/zips" + + +while [[ $# -gt 0 ]]; do + case $1 in + --reset) + DO_RESET=true + ;; + --no-reset) + DO_RESET=false + ;; + --delete-zips) + DELETE_ZIPS=true + ;; + --no-delete-zips) + DELETE_ZIPS=false + ;; + --download-artifacts) + DOWNLOAD_ARTIFACTS=true + ;; + --no-download-artifacts) + DOWNLOAD_ARTIFACTS=false + ;; + --api-token) + GITHUB_API_TOKEN="$2" + shift + ;; + --run-id) + RUN_ID="$2" + shift + ;; + --help) + print_help + exit 0 + ;; + esac + shift +done + +if [ "$DOWNLOAD_ARTIFACTS" = true ] && ([ -z "$GITHUB_API_TOKEN" ] || [ -z "$RUN_ID" ]); then + if [ -z "$GITHUB_API_TOKEN" ]; then + echo "ERROR: Missing Github API token" + fi + + if [ -z "$RUN_ID" ]; then + echo "ERROR: Missing github workflow run id" + fi + print_help + exit 1 +fi + +if [ "$DO_RESET" = true ]; then + echo "Resetting target dir" + rm -rf "$TARGET_DIR" +fi + +mkdir -p "$ZIP_DOWNLOADS_FOLDER" + +if [ "$DOWNLOAD_ARTIFACTS" = true ]; then + echo "Retrieving artifacts from run" + artifacts_json=$(curl -L \ + -H "Accept: application/vnd.github+json" \ + -H "X-GitHub-Api-Version: 2022-11-28" \ + -H "Authorization: Bearer $GITHUB_API_TOKEN" \ + https://api.github.com/repos/pendulum-project/statime/actions/runs/$RUN_ID/artifacts) + + echo "Extracting artifact urls" + artifact_urls=$(echo "$artifacts_json" | jq -r '.artifacts[] | (.name + ";" + .archive_download_url)') + + echo "Download artifacts" + while read artifact; do + IFS=';' read -r name url <<< "$artifact" + echo "Downloading artifact '$name' from '$url'" + zipfile="$ZIP_DOWNLOADS_FOLDER/$name.zip" + curl -L \ + -H "Accept: application/vnd.github+json" \ + -H "X-GitHub-Api-Version: 2022-11-28" \ + -H "Authorization: Bearer $GITHUB_API_TOKEN" \ + -o "$zipfile" "$url" + done < <(echo "$artifact_urls") + echo "Downloads complete" +fi + +for f in $(find "$ZIP_DOWNLOADS_FOLDER" -type f); do + echo "Extracting downloaded zip file '$f'" + unzip -d "$TARGET_DIR" "$f" +done + +if [ "$DELETE_ZIPS" = true ]; then + echo "Delete the downloaded zips" + rm -rf "$ZIP_DOWNLOADS_FOLDER" +else + echo "Not deleting any downloaded zips" +fi + +echo "Flatten file structure" +find "$TARGET_DIR" -mindepth 2 -type f -not -path "$ZIP_DOWNLOADS_FOLDER/*" -exec mv -t "$TARGET_DIR" -i '{}' + + +echo "Remove any leftover directories" +find "$TARGET_DIR" -mindepth 1 -type d -not -path "$ZIP_DOWNLOADS_FOLDER" -exec rm -Rf '{}' + + +echo "Replace tildes by dashes for github" +for f in $(find "$TARGET_DIR" -type f); do + newf=$(echo "$f" | sed 's/~/-/g') + if [ "$f" != "$newf" ]; then + mv "$f" "$newf" + fi +done + +echo "Create SHA256SUMS" +(cd "$TARGET_DIR" && rm -rf "SHA256SUMS" && find * -type f -not -name "SHA256SUMS" -exec sha256sum -b {} + | tee "SHA256SUMS") diff --git a/utils/release.sh b/utils/release.sh new file mode 100755 index 000000000..cda6bba10 --- /dev/null +++ b/utils/release.sh @@ -0,0 +1,4 @@ +#!/usr/bin/env bash + +cargo publish -p statime +cargo publish -p statime-linux diff --git a/utils/update-version.sh b/utils/update-version.sh new file mode 100755 index 000000000..3ce07ab64 --- /dev/null +++ b/utils/update-version.sh @@ -0,0 +1,42 @@ +#!/usr/bin/env bash + +if [ "$#" -lt 1 ]; then + echo "Missing new version specifier" + exit 1 +fi + +SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd) +PROJECT_DIR=$(dirname "$SCRIPT_DIR") +NEW_VERSION="$1" +NOTICE_LINE="# NOTE: keep this part at the bottom of the file, do not change this line" + +echo "Updating version in Cargo.toml" +sed -i 's/^version\s*=\s*".*"/version = "'"$NEW_VERSION"'"/' "$PROJECT_DIR/Cargo.toml" + +echo "Updating workspace crate versions in Cargo.toml" +tmp_file="$PROJECT_DIR/Cargo.toml.tmp" +replace_flag=0 +while IFS= read -r line; do + if [ $replace_flag -eq 1 ]; then + line=$(echo "$line" | sed 's/version\s*=\s*"[^"]*"/version = "'"$NEW_VERSION"'"/g') + fi + + if [ "$line" = "$NOTICE_LINE" ]; then + replace_flag=1 + fi + + echo "$line" >> "$tmp_file" +done < "$PROJECT_DIR/Cargo.toml" +mv "$tmp_file" "$PROJECT_DIR/Cargo.toml" + +echo "Updating version in man pages" +sed -i 's/^title: STATIME(8) statime .*/title: STATIME(8) statime '"$NEW_VERSION"' | statime/' "$PROJECT_DIR"/docs/man/statime.8.md +sed -i 's/^title: STATIME.TOML(5) statime .*/title: STATIME.TOML(5) statime '"$NEW_VERSION"' | statime/' "$PROJECT_DIR"/docs/man/statime.toml.5.md + +echo "Rebuilding precompiled man pages" +utils/generate-man.sh + +echo "Rebuilding project" +(cd $PROJECT_DIR && cargo clean && cargo build --release) + +echo "!!! Version changes complete, make sure that the changelog is synchronized"