🔨 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 #!/bin/bash
# Navigate to the root workspace directory
cd "$(dirname "$0")" cd "$(dirname "$0")"
cd .. cd ..
# Check if there are unstaged changes in the Git repository
if [ -n "$(git status --porcelain)" ]; then if [ -n "$(git status --porcelain)" ]; then
echo "You have local changes!" echo "You have local changes!"
exit 1 exit 1
fi 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() { function publish_crate() {
echo -e "\nPublishing ${crate}" echo -e "\nPUBLISHING $CRATE"
cp ../LICENSE-MIT "$crate" # Get the last published version from crates.io
cp ../LICENSE-APACHE "$crate" PUBLISHED_VERSION=$(cargo search "$CRATE " | grep "^$CRATE = " | sed -E 's/^.*"([^"]+)".*$/\1/')
pushd "$crate" # Read the current version from Cargo.toml
git add LICENSE-MIT LICENSE-APACHE CURRENT_VERSION=$(grep '^version = ' Cargo.toml | head -n 1 | sed -E 's/^version = "([^"]+)".*$/\1/')
# cargo publish --no-verify --allow-dirty # Compare the versions
cargo publish --allow-dirty if [ "$PUBLISHED_VERSION" = "$CURRENT_VERSION" ]; then
popd echo "Skipping version $CURRENT_VERSION as it already exists on crates.io"
sleep 20 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 # If package A depends on package B, B must come before A in this list
helpers=( HELPERS=(
pagetop-macros pagetop-macros
pagetop-build pagetop-build
) )
packages=( PACKAGES=(
pagetop-user pagetop-user
pagetop-admin pagetop-admin
pagetop-node pagetop-node
@ -33,17 +63,27 @@ packages=(
pagetop-bulmix pagetop-bulmix
) )
pushd helpers # Publish all helper crates
for crate in "${helpers[@]}"; do publish_crate; done pushd helpers > /dev/null 2>&1
popd 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" # Publish the root crate
cargo publish --allow-dirty CRATE=pagetop; publish_crate
sleep 20
pushd packages # Publish all main packages
for crate in "${packages[@]}"; do publish_crate; done pushd packages > /dev/null 2>&1
popd 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 git reset HEAD --hard