Skip to content

Instantly share code, notes, and snippets.

@msewell
Created March 31, 2021 07:54
Show Gist options
  • Save msewell/9a5219197c6500a4db4a2d59505bc81e to your computer and use it in GitHub Desktop.
Save msewell/9a5219197c6500a4db4a2d59505bc81e to your computer and use it in GitHub Desktop.
Running SwiftLint as an Xcode behavior
#!/bin/bash -l
exec > /tmp/lintbehaviorlog-$(date "+%s").txt
exec 2>&1
set -x # Call this command at the top of your script and every following command will be echoed. You can turn it off with set +x
set -e # abort the script when any command exits with non-zero status
OIFS="$IFS"
IFS=$'\n'
PROJECT_OR_WORKSPACE="${XcodeProjectPath:-$XcodeWorkspacePath}"
cd $(dirname $PROJECT_OR_WORKSPACE)
count=0
for file in $(git diff --diff-filter=d --line-prefix=`git rev-parse --show-toplevel`/ --name-only | rg -i '.swift'); do
export SCRIPT_INPUT_FILE_$count="${file}"
count=$((count + 1))
done
export SCRIPT_INPUT_FILE_COUNT=$count
if [ "$count" -ne 0 ]; then
swiftlint autocorrect --use-script-input-files --config ~/Developer/.swiftlint.yml > $(dirname $PROJECT_OR_WORKSPACE)/lint_autocorrections.txt
# FILE=$(dirname $PROJECT_OR_WORKSPACE)/lint_autocorrections.txt
# if [ -s ${FILE} ]
# then
# open ${FILE}
# fi
# swiftlint lint --use-script-input-files --config ~/Developer/.swiftlint.yml | fold -w180 > $(dirname $PROJECT_OR_WORKSPACE)/lint_behavior_violations.txt
swiftlint lint --use-script-input-files --config ~/Developer/.swiftlint.yml > $(dirname $PROJECT_OR_WORKSPACE)/lint_behavior_violations.txt
# FILE=$(dirname $PROJECT_OR_WORKSPACE)/lint_behavior_violations.txt
# if [ -s ${FILE} ]
# then
# open ${FILE}
# fi
fi
IFS="$OIFS"
set +x
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment