From 366a3c1c748798938f69418e0db7cccb3fdd30f0 Mon Sep 17 00:00:00 2001 From: Manuel Cillero Date: Thu, 21 Mar 2024 20:17:37 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=A8=20Improve=20crate=20publishing=20w?= =?UTF-8?q?ith=20version=20check?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tools/publish.sh | 82 +++++++++++++++++++++++++++++++++++------------- 1 file changed, 61 insertions(+), 21 deletions(-) diff --git a/tools/publish.sh b/tools/publish.sh index 61b01f79..2f117d2f 100755 --- a/tools/publish.sh +++ b/tools/publish.sh @@ -1,31 +1,61 @@ #!/bin/bash +# Navigate to the root workspace directory cd "$(dirname "$0")" cd .. +# Check if there are unstaged changes in the Git repository if [ -n "$(git status --porcelain)" ]; then echo "You have local changes!" exit 1 fi +# Updates the 'latest' branch with changes from 'main' +read -p "Do you want to update the 'latest' branch? (y/n) " -n 1 -r +echo +if [[ $REPLY =~ ^[Yy]$ ]] +then + echo "UPDATING 'latest' branch" + git checkout latest + git merge main + echo "PUSHING updated 'latest' branch to remote repository" + git push origin latest + git checkout main +else + echo "Omitting update of 'latest' branch" +fi + + +# Define a function to publish a crate to crates.io function publish_crate() { - echo -e "\nPublishing ${crate}" - cp ../LICENSE-MIT "$crate" - cp ../LICENSE-APACHE "$crate" - pushd "$crate" - git add LICENSE-MIT LICENSE-APACHE -# cargo publish --no-verify --allow-dirty - cargo publish --allow-dirty - popd - sleep 20 + echo -e "\nPUBLISHING $CRATE" + # Get the last published version from crates.io + PUBLISHED_VERSION=$(cargo search "$CRATE " | grep "^$CRATE = " | sed -E 's/^.*"([^"]+)".*$/\1/') + # Read the current version from Cargo.toml + CURRENT_VERSION=$(grep '^version = ' Cargo.toml | head -n 1 | sed -E 's/^version = "([^"]+)".*$/\1/') + # Compare the versions + if [ "$PUBLISHED_VERSION" = "$CURRENT_VERSION" ]; then + echo "Skipping version $CURRENT_VERSION as it already exists on crates.io" + else + echo "Publishing version $CURRENT_VERSION..." + if [ "$CRATE" = "pagetop" ]; then + cargo publish + else + cp ../../LICENSE-MIT . + cp ../../LICENSE-APACHE . + git add LICENSE-MIT LICENSE-APACHE + cargo publish --allow-dirty + fi + sleep 20 + fi } # If package A depends on package B, B must come before A in this list -helpers=( +HELPERS=( pagetop-macros pagetop-build ) -packages=( +PACKAGES=( pagetop-user pagetop-admin pagetop-node @@ -33,17 +63,27 @@ packages=( pagetop-bulmix ) -pushd helpers -for crate in "${helpers[@]}"; do publish_crate; done -popd +# Publish all helper crates +pushd helpers > /dev/null 2>&1 +for CRATE in "${HELPERS[@]}"; do + pushd "$CRATE" > /dev/null 2>&1 + publish_crate + popd > /dev/null 2>&1 +done +popd > /dev/null 2>&1 -echo -e "\nPublishing root crate" -cargo publish --allow-dirty -sleep 20 +# Publish the root crate +CRATE=pagetop; publish_crate -pushd packages -for crate in "${packages[@]}"; do publish_crate; done -popd +# Publish all main packages +pushd packages > /dev/null 2>&1 +for CRATE in "${PACKAGES[@]}"; do + pushd "$CRATE" > /dev/null 2>&1 + publish_crate + popd > /dev/null 2>&1 +done +popd > /dev/null 2>&1 -echo "Cleaning local state" +# Reset local Git repository to clean licenses after publishing +echo -e "\nCleaning local state" git reset HEAD --hard