Skip to content

Instantly share code, notes, and snippets.

@gbutt
Created January 24, 2023 01:09
Show Gist options
  • Save gbutt/8ced61b167df1c79e37f849cfcbfe889 to your computer and use it in GitHub Desktop.
Save gbutt/8ced61b167df1c79e37f849cfcbfe889 to your computer and use it in GitHub Desktop.
Create Diff Package - Salesforce
#!/bin/bash
# this function will only rsync files and folders once
rsync_unique () {
local syncpath=$1
if [[ -d ${syncpath} ]]; then
if [[ " ${RSYNED_DIRS[@]} " =~ " ${syncpath} " ]]; then
echo "already rsyned ${syncpath}" &> /dev/null
else
RSYNED_DIRS+=("${syncpath}")
echo " - adding folder ${syncpath}"
rsync -rR ${syncpath} $TEMP_DIR
fi
elif [[ -f ${syncpath} ]]; then
if [[ " ${RSYNED_FILES[@]} " =~ " ${syncpath} " ]]; then
echo "already rsyned ${syncpath}" &> /dev/null
else
RSYNED_FILES+=("${syncpath}")
echo " - adding file ${syncpath}"
rsync -R ${syncpath} $TEMP_DIR
fi
fi
}
CURRENT_BRANCH=`git rev-parse --abbrev-ref HEAD`
if [ $# -ne 0 ]
then
CURRENT_BRANCH=$1
fi
TEMP_DIR=.temp
mkdir $TEMP_DIR &> /dev/null
rm -rf $TEMP_DIR/src &> /dev/null
rm -rf $TEMP_DIR/deploy &> /dev/null
DIFF_CMD="git diff -z --name-only --no-renames --diff-filter=ACM $CURRENT_BRANCH src"
echo "detecting changes using ${DIFF_CMD}"
while IFS= read -r -d '' file; do
if [[ ${file} =~ -meta.xml$ ]]; then # metadata files
META_FILE=${file}
if [[ ${file} =~ \/staticresources\/ ]]; then # static resources are complicated because they could be files or directories
# strip extension from filename
PATH_WO_EXTENSION=`expr "$file" : '\(.*\/staticresources\/[^\.]*\)'`
if [[ -d $PATH_WO_EXTENSION ]]; then # resource is a folder
rsync_unique "${PATH_WO_EXTENSION}"
rsync_unique "${META_FILE}"
else # static resource is a file
RESOURCE_DIR=`expr "${PATH_WO_EXTENSION}" : '\(.*\/staticresources\)\/*'`
FILENAME_WO_EXTENSION=`expr "${PATH_WO_EXTENSION}" : '.*\/staticresources\/\(.*\)'`
# find actual filename and extension
SOURCE_FILE=`find $RESOURCE_DIR -name "${FILENAME_WO_EXTENSION}.*" -maxdepth 1 | grep -v resource-meta.xml$`
rsync_unique "${SOURCE_FILE}"
rsync_unique "${META_FILE}"
fi
elif [[ ${file} =~ \/aura\/ ]]; then # aura bundle
BUNDLE_DIR=`expr "$file" : '\(.*\/aura\/[^\/]*\)'`
rsync_unique "${BUNDLE_DIR}"
elif [[ ${file} =~ \/lwc\/ ]]; then # lwc bundle
BUNDLE_DIR=`expr "$file" : '\(.*\/lwc\/[^\/]*\)'`
rsync_unique "${BUNDLE_DIR}"
else # all other files
SOURCE_FILE=`expr "${file}" : '\(.*\)-meta.xml$'` # strip -meta.xml from file to get filename
rsync_unique "${SOURCE_FILE}"
rsync_unique "${META_FILE}"
fi
elif [[ ${file} =~ \/aura\/ ]]; then # aura bundles
BUNDLE_DIR=`expr "${file}" : '\(.*\/aura\/[^\/]*\)'`
rsync_unique "${BUNDLE_DIR}"
elif [[ ${file} =~ \/lwc\/ ]]; then # lwc bundles
BUNDLE_DIR=`expr "${file}" : '\(.*\/lwc\/[^\/]*\)'`
rsync_unique "${BUNDLE_DIR}"
elif [[ ${file} =~ \/staticresources\/ ]]; then # static resources
RESOURCE_ROOT_PATH=`expr "${file}" : '\(.*\/staticresources\/[^\/]*\)'`
if [[ -d ${RESOURCE_ROOT_PATH} ]]; then # resource is a folder
rsync_unique "${RESOURCE_ROOT_PATH}"
rsync_unique "${RESOURCE_ROOT_PATH}.resource-meta.xml"
else # resource is a file
PATH_WO_EXTENSION=`expr "${file}" : '\(.*\/staticresources\/[^\.]*\)'`
rsync_unique "${file}"
rsync_unique "${PATH_WO_EXTENSION}.resource-meta.xml"
fi
else # all other files
rsync_unique "${file}"
rsync_unique "${file}-meta.xml"
fi
done < <(sh -c "${DIFF_CMD}")
if [[ ! -d $TEMP_DIR/src ]]; then
echo "no changed files"
echo ""
exit 0
fi
echo "convert sfdx format to mdapi package"
cp sfdx-project.json $TEMP_DIR
cp .forceignore $TEMP_DIR
pushd $TEMP_DIR &> /dev/null
sfdx force:source:convert -r src -d deploy
popd &> /dev/null
echo ""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment