Skip to content

Commit

Permalink
adding sha checksum logic to speed up subsequent script/bootstrap runs
Browse files Browse the repository at this point in the history
  • Loading branch information
GrantBirki committed Sep 5, 2024
1 parent 223cde5 commit d3527be
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 3 deletions.
66 changes: 63 additions & 3 deletions script/unzipper
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,72 @@ trap cleanup EXIT
mkdir -p "$SHARDS_INSTALL_PATH"
mkdir -p "$SHARDS_CACHE_PATH/github.com"

# iterate over all the cached shards in the vendor/shards/cache directory
for shard in $(find "$SHARDS_CACHED" -type f -maxdepth 1); do
# check if the .shards.info file exists, if it doesn't this is a fresh bootstrapping
if [ ! -f "$VENDOR_DIR/shards/install/.shards.info" ]; then
# if no .shards.info file found, this must be a fresh bootstrapping

# iterate over all the cached shards in the vendor/shards/cache directory
for shard in $(find "$SHARDS_CACHED" -type f -maxdepth 1); do

# unzip the file into the TRASHDIR
unzip -q -o "$shard" -d "$TRASHDIR"

# get the only name of the dir in the TRASHDIR
shard_name=$(ls "$TRASHDIR/shard/")

# clear up the shard in the install dir if it exists
rm -rf "$SHARDS_INSTALL_PATH/$shard_name"

# move the shard and cache directories to the correct location
mv -f "$TRASHDIR/shard/"* "$SHARDS_INSTALL_PATH/" 2>/dev/null || true
mv -f "$TRASHDIR/cache/"* "$SHARDS_CACHE_PATH/github.com/" 2>/dev/null || true
done

# cleanup the TRASHDIR
rm -rf "$TRASHDIR/shard"
rm -rf "$TRASHDIR/cache"
done

else
# if found .shards.info file, this must be a bootstrap re-run - we will check if the shards have changed by comparing the sha256 of the cached shard and the sha256 of the current shard

file="vendor/shards/install/.shards.info"

# Use yq to parse the file and extract shard names and versions
shards=$(yq eval '.shards | to_entries | .[] | "\(.key)|\(.value.git)|\(.value.version)"' $file)

# Loop over each shard
echo "$shards" | while IFS= read -r shard; do
# Extract name and version
name=$(echo $shard | cut -d'|' -f1)
version=$(echo $shard | cut -d'|' -f3)

shard_cache_sha=$(shasum -a 256 "$SHARDS_CACHED/$name-$version.shard" | cut -d' ' -f1)
shard_current_sha=""
if [ -f "$SHARDS_INSTALL_PATH/$name/.shard.vendor.cache.sha256" ]; then
shard_current_sha=$(cat "$SHARDS_INSTALL_PATH/$name/.shard.vendor.cache.sha256")
fi

if [ "$shard_cache_sha" != "$shard_current_sha" ]; then
echo "shard $name $version has changed, updating"

# unzip the file into the TRASHDIR
unzip -q -o "$SHARDS_CACHED/$name-$version.shard" -d "$TRASHDIR"

# clear up the shard in the install dir if it exists
rm -rf "$SHARDS_INSTALL_PATH/$name"

# move the shard and cache directories to the correct location
mv -f "$TRASHDIR/shard/"* "$SHARDS_INSTALL_PATH/" 2>/dev/null || true
mv -f "$TRASHDIR/cache/"* "$SHARDS_CACHE_PATH/github.com/" 2>/dev/null || true

# write the new sha to the .shard.vendor.cache.sha256 file
cat > "$SHARDS_INSTALL_PATH/$name/.shard.vendor.cache.sha256" <<< "$shard_cache_sha"

# cleanup the TRASHDIR
rm -rf "$TRASHDIR/shard"
rm -rf "$TRASHDIR/cache"
else
echo "shard $name $version has not changed, skipping"
fi
done
fi
Binary file modified vendor/shards/cache/ameba-1.6.1.shard
Binary file not shown.
Binary file modified vendor/shards/cache/halite-0.12.0.shard
Binary file not shown.
Binary file modified vendor/shards/cache/json_mapping-0.1.1.shard
Binary file not shown.
Binary file modified vendor/shards/cache/octokit-0.3.0.shard
Binary file not shown.

0 comments on commit d3527be

Please sign in to comment.