Skip to content

Commit

Permalink
Refactor into yq for determining package removals
Browse files Browse the repository at this point in the history
  • Loading branch information
redstar504 committed Jan 4, 2023
1 parent 3553ba2 commit e9194f3
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 91 deletions.
126 changes: 36 additions & 90 deletions .github/scripts/verifyPodfile.sh
Original file line number Diff line number Diff line change
@@ -1,115 +1,61 @@
#!/bin/bash

# This script requires passing the branch to compare against for removals.
# ie. `./verifyPodfile.sh main`

START_DIR=$(pwd)
ROOT_DIR=$(dirname "$(dirname "$(dirname "$(realpath "${BASH_SOURCE[0]}")")")")
ROOT_DIR=$(dirname "$(dirname "$(dirname "${BASH_SOURCE[0]}")")")
cd "$ROOT_DIR" || exit 1

source scripts/shellUtils.sh

function resetAndExit() {
cd "$START_DIR" || exit 1
exit "$1"
}
title "Verifying that the Podfile.lock is synchronized with the project"

declare EXIT_CODE=0

podfileSha=$(openssl sha1 ios/Podfile | awk '{print $2}')
podfileLockSha=$(awk '/PODFILE CHECKSUM: /{print $3}' ios/Podfile.lock)
PODFILE_SHA=$(openssl sha1 ios/Podfile | awk '{print $2}')
PODFILE_LOCK_SHA=$(awk '/PODFILE CHECKSUM: /{print $3}' ios/Podfile.lock)

echo "Podfile: $podfileSha"
echo "Podfile.lock: $podfileLockSha"
echo "Podfile: $PODFILE_SHA"
echo "Podfile.lock: $PODFILE_LOCK_SHA"

if [ "$podfileSha" == "$podfileLockSha" ]; then
if [ "$PODFILE_SHA" == "$PODFILE_LOCK_SHA" ]; then
success "Podfile checksum verified!"
else
error "Podfile.lock checksum mismatch. Did you forget to run \`npx pod-install\`?"
resetAndExit 1
fi

# Make sure package.json and package-lock.json are committed to avoid testing issues
if [ -n "$(git diff --name-only HEAD -- package.json package-lock.json)" ]; then
error "Uncommitted changes to package.json or package-lock.json. Commit these before continuing!"
resetAndExit 1
fi

# Make sure valid branch ref was passed for removal check
if ! git rev-parse --quiet --verify "$1"; then
error 'Error: Must provide valid branch name as argument for verifyPodfile script.'
resetAndExit 1
fi

# If npm packages were not modified in the feature branch we can skip the remaining checks
if [ -z "$(git diff --name-only .."$1" package-lock.json package.json)" ]; then
success "No changes to npm packages were detected."
resetAndExit 0;
EXIT_CODE=1
fi

info "Verifying that pods are synced..."

# Extracts an array of podspec paths from the config command
function listPodspecPaths() {
npx react-native config | jq '[.dependencies[].platforms.ios.podspecPath | select( . != null )]'
}

# Maps an array of podspec paths to a raw list of formatted `Pod (version)`s
function formatPodList() {
jq --raw-output --slurp 'map((.name + " (" + .version + ")")) | .[]' <<< "$( \
jq -n '$specs | .[]' --argjson specs "$1" | \
xargs -L 1 pod ipc spec --silent
)"
}

# Store an array of the feature branch's podspecs
feature_branch_specs="$(listPodspecPaths)"
info "Verifying Podfile.lock is up to date..."

echo -e "Feature branch specs:\n$feature_branch_specs"
CONFIGSPECS=$( \
jq --raw-output --slurp 'map((.name + " (" + .version + ")")) | .[]' <<< "$( \
npx react-native config | \
jq '.dependencies[].platforms.ios.podspecPath | select( . != null )' | \
xargs -L 1 pod ipc spec --silent
)"
)

# Grab the podspecs from the main branch by checking out the diffed npm packages
git checkout --quiet "$1" -- {package,package-lock}.json
npm i --loglevel silent
main_branch_specs="$(listPodspecPaths)"
LOCKSPECS=$(cat ios/Podfile.lock | yq '.["EXTERNAL SOURCES"].[].":path" | select( . == "*node_modules*")')

echo -e "Main branch specs:\n$main_branch_specs"

# Perform an array subtraction to determine which pods were removed (main_branch - feature_branch)
removed_specs=$(jq -n --jsonargs '$ARGS.positional | first - last | .' -- "$main_branch_specs" "$feature_branch_specs")

# Store the formatted list here as we won't have the chance after switching back to feature branch
formatted_removals="$(formatPodList "$removed_specs")"

# Verbose output
[ -n "$formatted_removals" ] && \
echo -e "Removals:\n$formatted_removals" || \
echo "No package removals"

# Revert back to feature branch npm state
git reset --quiet HEAD {package,package-lock}.json
git checkout --quiet -- {package,package-lock}.json
npm i --loglevel silent

# Initialize failed variable as it may be already set by the environment
failed=0

info "Comparing pods. This may take a moment."
# Check for uncommitted package removals
while read -r SPEC; do
if [ ! -d "${SPEC#../}" ]; then
error "${SPEC#../node_modules/} not found in node_modules. Did you forget to run \`npx pod-install\` after removing the package?"
EXIT_CODE=1
fi
done <<< "$LOCKSPECS"

# Validate additions and updates to Podfile.lock
# Check for uncommitted package additions/updates
while read -r SPEC; do
if ! grep -q "$SPEC" ./ios/Podfile.lock; then
error "Podspec $SPEC not found in Podfile.lock. Did you forget to run \`npx pod-install\`?"
failed=1
EXIT_CODE=1
fi
done <<< "$(formatPodList "$feature_branch_specs")"
done <<< "$CONFIGSPECS"

# Validate deletions from Podfile.lock
while read -r SPEC; do
if [ -n "$SPEC" ] && grep -q "$SPEC" ./ios/Podfile.lock; then
error "Podspec $SPEC was found in Podfile.lock but not in node modules. \
Did you forget to run \`npx pod-install\` after removing the package?" | xargs
failed=1
fi
done <<< "$formatted_removals"
if [[ "$EXIT_CODE" == 0 ]]; then
success "Podfile.lock is synced with project."
fi

[ "$failed" -eq 1 ] && resetAndExit 1
# Cleanup
cd "$START_DIR" || exit 1

success "Podfile.lock is synced with npm packages."
resetAndExit 0
exit $EXIT_CODE
2 changes: 1 addition & 1 deletion .github/workflows/verifyPodfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ jobs:

- uses: Expensify/App/.github/actions/composite/setupNode@main

- run: ./.github/scripts/verifyPodfile.sh origin/main
- run: ./.github/scripts/verifyPodfile.sh

0 comments on commit e9194f3

Please sign in to comment.