只对新文件启用SwiftLint



我有一个非常大的iOS(遗留)项目,我想在其中添加SwiftLint。所有我希望执行的规则,我只会在新创建的文件上执行。这样,我就不必回去解决所有可能出现的问题,因为过去没有遵循规则(因为没有使用SwiftLint)。我不确定如何实现这一点?

你可以运行这样的脚本

# Run SwiftLint
START_DATE=$(date +"%s")

SWIFT_LINT=/usr/local/bin/swiftlint

# Run SwiftLint for given filename
run_swiftlint() {
local filename="${1}"
if [[ "${filename##*.}" == "swift" ]]; then
#${SWIFT_LINT} autocorrect --path "${filename}"
${SWIFT_LINT} lint --path "${filename}"
fi
}
if [[ -e "${SWIFT_LINT}" ]]; then
echo "SwiftLint version: $(${SWIFT_LINT} version)"
# Run for both staged and unstaged files
git diff --name-only | while read filename; do run_swiftlint                 "${filename}"; done
git diff --cached --name-only | while read filename; do                 run_swiftlint "${filename}"; done
else
echo "${SWIFT_LINT} is not installed."
exit 0
fi
END_DATE=$(date +"%s")
DIFF=$(($END_DATE - $START_DATE))
echo "SwiftLint took $(($DIFF / 60)) minutes and $(($DIFF % 60)) seconds to complete."

只需在Xcode中添加一个构建阶段运行脚本,并使用以下命令

"${SRCROOT}/Scripts/swiftlint.sh"

它来自这里https://github.com/realm/SwiftLint/issues/413#issuecomment-184077062

它不能在M1芯片上工作,因为你需要这样说

if test -d "/opt/homebrew/bin/"; then
PATH="/opt/homebrew/bin/:${PATH}"
fi
export PATH
if which swiftlint >/dev/null; then
swiftlint
else
echo "warning: SwiftLint not installed, download from https://github.com/realm/SwiftLint"
fine

基于此博客https://www.anotheriosdevblog.com/installing-swiftlint-on-a-m1/

如果你正在使用特性分支,你可以这样运行:

swiftlint --config PATH/TO/CONFIG/.swiftlint.yml $(git diff develop --name-only | grep .swift)

最新更新