🔨 Improve crate publishing with version check

This commit is contained in:
Manuel Cillero 2024-03-21 20:17:37 +01:00
parent 49d2d398b8
commit 366a3c1c74

View file

@ -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