Skip to content

Instantly share code, notes, and snippets.

@iamsonal
Created March 21, 2022 15:58
Show Gist options
  • Save iamsonal/b5369104fa3a511c9f4f3284e873c547 to your computer and use it in GitHub Desktop.
Save iamsonal/b5369104fa3a511c9f4f3284e873c547 to your computer and use it in GitHub Desktop.
#############################################################################
## SETUP DEMO ##
#############################################################################
cd /Users/first.last/Documents/workspace/cli-demo-repo
CONSUMER_KEY="xxxx"
USERNAME="xxxx"
INSTANCE_URL="https://login.salesforce.com/"
KEY="../cli-demo/server.key"
rm -rf "/Users/first.last/Documents/workspace/cli-demo-repo/output"
mkdir "/Users/first.last/Documents/workspace/cli-demo-repo/output"
#############################################################################
## Install SFDX & Other Libraries ##
## - Store files in source control instead of downloading them each time##
## - Have these included in your docker/aws instance image ##
#############################################################################
## Install SFDX
INSTALL_DIR=/location_to_folder
## Dowload from web
wget https://developer.salesforce.com/media/salesforce-cli/sfdx-linux-amd64.tar.xz
## Unpack
tar xJf sfdx-linux-amd64.tar.xz -C "$INSTALL_DIR" --strip-components 1
## Install SFDX
sudo "$INSTALL_DIR/install"
## Install YQ (Used for reading JSON)
wget -O yq https://github.com/mikefarah/yq/releases/download/2.4.0/yq_linux_amd64
chmod +x yq
## Install Python
wget -O Python https://www.python.org/ftp/python/3.9.1/Python-3.9.1.tar.xz
tar -xf Python
chmod +x "$INSTALL_DIR/Python-3.9.1"
cd "$INSTALL_DIR/Python-3.9.1"
./configure --with-ensurepip=install > /dev/null 2>&1
sudo make install > /dev/null 2>&1
python3 --version
#############################################################################
## Login to SFDX ##
#############################################################################
echo "Setup: Login to SFDX"
{
sfdx auth:logout --noprompt --targetusername "$USERNAME";
} || { # catch
echo "no default username found"
}
sfdx auth:jwt:grant -i $CONSUMER_KEY -r $INSTANCE_URL -u $USERNAME -f "$KEY"
#############################################################################
## Create Package ##
#############################################################################
# Install sfdx-git-delta
# https://www.npmjs.com/package/sfdx-git-delta
# npm install sfdx-cli --global --force <-- fix node version issue
echo "y" | sfdx plugins:install sfdx-git-delta
# Create Package by DIFF from Master
sfdx sgd:source:delta --to HEAD --from master --generate-delta
#############################################################################
## Deploy ##
#############################################################################
sfdx force:source:deploy -x output/package/package.xml -u $USERNAME
#############################################################################
## Deploy with specified tests ##
#############################################################################
# Generate tests
PY_PATH="./lib/python/get-test-class-list.py"
chmod 755 "${PY_PATH}"
CLASS_PATH="./output/force-app/main/default/classes"
TRIGGER_PATH="./output/force-app/main/default/triggers"
TEST_FILES="$("${PY_PATH}" "$CLASS_PATH" "$TRIGGER_PATH")";
echo $TEST_FILES
#Deploy
sfdx force:source:deploy \
-x output/package/package.xml \
--targetusername "${USERNAME}" \
--testlevel RunSpecifiedTests \
--runtests "${TEST_FILES}" \
-w 10000;
#############################################################################
## Destory components using destructiveChanges.xml generated from gitdelta plugin ##
#############################################################################
#Define your deployment folder
DEPLOY_DESTRUCTIVE_DIR="output/destructiveChanges"
# Copy a blank package.xml into the destructiive folder
cp "../package-blank.xml" "${DEPLOY_DESTRUCTIVE_DIR}/package.xml";
sfdx force:mdapi:deploy \
--deploydir "${DEPLOY_DESTRUCTIVE_DIR}" \
--targetusername "${USERNAME}" \
--testlevel RunSpecifiedTests \
--runtests "MyGenericTest" \
--ignorewarnings \
--wait -1;
#############################################################################
## Code Scanning ##
#############################################################################
echo "Install SFDX Scanner Plugin"
# https://forcedotcom.github.io/sfdx-scanner/en/getting-started/install/
sfdx plugins:install @salesforce/sfdx-scanner
sfdx plugins
echo "Code Scanning: Available Rules"
sfdx scanner:rule:list
echo "Code Scanning: Run Scanner"
CLASS_PATH="./output/force-app/main/default/classes"
TRIGGER_PATH="./output/force-app/main/default/triggers"
SCAN_CATEGORIES="Best Practices,Code Style,Design,Documentation,Error,Error Prone,Stylistic Issues,Performance,Possible Errors,ECMAScript 6,Security,Variables"
sfdx scanner:run --target "$CLASS_PATH/*.cls" --category "$SCAN_CATEGORIES"
echo "Code Scanning: Run Scanner and save output as junit"
sfdx scanner:run --target "$CLASS_PATH/*.cls" --category "$SCAN_CATEGORIES" --format junit --outfile "./output/junit-apex-classes.xml"
sfdx scanner:run --target "$TRIGGER_PATH/*.cls" --format junit --outfile "./output/junit-apex-triggers.xml" --category "$SCAN_CATEGORIES"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment