Skip to content

Instantly share code, notes, and snippets.

@drrost
Created November 3, 2019 15:48
Show Gist options
  • Save drrost/f01bce9ccf644c6f9669082097a76421 to your computer and use it in GitHub Desktop.
Save drrost/f01bce9ccf644c6f9669082097a76421 to your computer and use it in GitHub Desktop.
Runs Swiftlint only for changed files - Xcode run script
# Created by: Rostyslav Druzhchenko
#
# Get current time
#
START_DATE=$(date +"%s")
# Save Swiftlint full path to a local variable
#
SWIFT_LINT=/usr/local/bin/swiftlint
# Function
#
# Runs SwiftLint for given filename
run_swiftlint() {
local FILE_NAME="${1}"
if [[ "${FILE_NAME##*.}" == "swift" ]]; then
${SWIFT_LINT} autocorrect --path "${FILE_NAME}" # Run autocorrect
${SWIFT_LINT} lint --path "${FILE_NAME}" # Run lint
fi
}
# Check if Swiftlint is insatlled on the systesm where the script runs
# (it can be a local machine and it can also be remote build host)
#
if [[ -e "${SWIFT_LINT}" ]]; then
echo "SwiftLint version: $(${SWIFT_LINT} version)"
# Obtain full path to the file
FULL_PATH=$"$(git rev-parse --show-toplevel)"
# Run for both staged and unstaged files
git diff --name-only | while read FILE_NAME; do run_swiftlint "${FULL_PATH}/${FILE_NAME}"; done
git diff --cached --name-only | while read FILE_NAME; do run_swiftlint "${FULL_PATH}/${FILE_NAME}"; done
else
echo "${SWIFT_LINT} is not installed."
exit 0
fi
# Calculate time spent by Swiflint
#
END_DATE=$(date +"%s")
DIFF=$(($END_DATE - $START_DATE))
echo "SwiftLint took $(($DIFF / 60)) minutes and $(($DIFF % 60)) seconds to complete."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment