Skip to content

Instantly share code, notes, and snippets.

@PH9
Last active April 14, 2021 14:16
Show Gist options
  • Save PH9/52e2e43b93dc2d860b569ccf31f49c6e to your computer and use it in GitHub Desktop.
Save PH9/52e2e43b93dc2d860b569ccf31f49c6e to your computer and use it in GitHub Desktop.
SwiftLint - That run only on modified (new, changes, cached, staged) files
#!/bin/bash
#
# Run SwiftLint
START_DATE=$(date +"%s")
SWIFT_LINT=/usr/local/bin/swiftlint
SWIFT_FORMAT=/usr/local/bin/swiftformat
if [[ -e "${SWIFT_LINT}" ]]; then
echo "[I] Found SwiftLint at ${SWIFT_LINT}"
else
echo "[!] Local SwiftLint not found at ${SWIFT_LINT}"
export SWIFT_LINT=${PWD}/Pods/SwiftLint/swiftlint
echo "[I] Try to using SwiftLint from cocoapods at ${SWIFT_LINT}"
fi
if [[ -e "${SWIFT_FORMAT}" ]]; then
echo "[I] Found SwiftFormat at ${SWIFT_FORMAT}"
else
echo "[!] Local SwiftFormat not found at ${SWIFT_FORMAT}"
export SWIFT_FORMAT=${PWD}/Pods/SwiftFormat/CommandLineTool/swiftformat
echo "[I] Try to using SwiftFormat from cocoapods at ${SWIFT_FORMAT}"
fi
if [[ ! -e "${SWIFT_LINT}" ]]; then
echo "[!] SwifLint is not installed."
echo "[!] Expected location is '${SWIFT_LINT}'"
echo "[!] Please install it. eg. 'brew install swiftlint'"
echo "[!] Or using via Cocoapods by 'pod 'SwiftLint'' in your Podfile"
exit 1
fi
echo "[I] SwiftLint version: $(${SWIFT_LINT} version)"
if [[ ! -e "${SWIFT_FORMAT}" ]]; then
echo "[!] SwiftFormat is not installed."
echo "[!] Expected location is '${SWIFT_LINT}'"
echo "[!] Please install it. eg. 'brew install swiftformat'"
echo "[!] Or using via Cocoapods by 'pod 'SwiftFormat/CLI'' in your Podfile"
exit 1
fi
echo "[I] SwiftFormat version: $(${SWIFT_FORMAT} --version)"
# Run for unstaged, staged, new files, excluded for deleted files
git diff --diff-filter=d --name-only -- "*.swift" | while read filename; do
$SWIFT_FORMAT "$filename"
$SWIFT_LINT lint --path "$filename";
done
git diff --cached --diff-filter=d --name-only -- "*.swift" | while read filename; do
$SWIFT_FORMAT "$filename"
$SWIFT_LINT lint --path "$filename";
done
git ls-files --others --exclude-standard -- "*.swift" | while read filename; do
$SWIFT_FORMAT "$filename"
$SWIFT_LINT lint --path "$filename";
done
END_DATE=$(date +"%s")
DIFF=$(($END_DATE - $START_DATE))
echo "SwiftLint took $(($DIFF / 60)) minutes and $(($DIFF % 60)) seconds to complete."
@PH9
Copy link
Author

PH9 commented Jul 22, 2020

SwiftFormat is better is many ways.

@PH9
Copy link
Author

PH9 commented Jul 23, 2020

Create .swiftformat file in your root with config below

--swiftversion 5.0

--header strip

--indent 2

--maxwidth 120

--wraparguments before-first
--wrapcollections before-first

--exclude bin,Carthage,Frameworks,Generated,Pods

@PH9
Copy link
Author

PH9 commented Aug 28, 2020

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment