How do you add SwiftLint into an Xcode project? #2963
-
Hi, How can add SwiftLint into a xcodeproj? In a "regular" XCode project, I would add a Run Script Phase with
I'm not able to do the same with a xcodeproj bazel rule. In my BUILD.bazel file I have :
It doesn't work, I get the following error : /private/var/tmp/_bazel_nicolasbush/63bf14c1f1d6f26e2f612fc11c487faa/rules_xcodeproj.noindex/build_output_base/external/rules_swift~/tools/worker/BUILD:113:10: Compiling tools/worker/worker_main.cc [for tool] failed: absolute path inclusion(s) found in rule '@@rules_swift~//tools/worker:worker': So here my questions :
Is there a way to add a Run Script Phase? I can't find any corresponding parameter in the xcodeproj method.
Thanks for your answers. |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
This is a question about SwiftLint, but these instructions are applicable for running any other script or CLI tool on pre-build Xcode actions. We use SwiftLint as part of a pre-build phase for our First, we have a shell script wrapper that can download and run a prebuilt SwiftLint binary pinned to a specific version, so that devs don't all have to install it via Homebrew where the versions can easily get out of sync. It looks like this: #!/bin/bash
set -euo pipefail
readonly swiftlint_version="0.53.0"
readonly swiftlint_version_sha="8a949e9e8113ba0f60d69275e444a3ec78adb9598f2b93579bcaf2bb568950e5"
readonly swiftlint_version_url="https://github.com/realm/swiftlint/releases/download/$swiftlint_version/portable_swiftlint.zip"
script_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
readonly swiftlint_root="$script_root/tmp/swiftlint/versions"
readonly swiftlint="$swiftlint_root/swiftlint-$swiftlint_version"
if [[ ! -x "$swiftlint" ]]; then
echo "Installing SwiftLint..." >&2
mkdir -p $swiftlint_root
download_swiftlint() {
readonly swiftlint_zip="$swiftlint_root/portable_swiftlint.zip"
curl --fail -L --retry 5 --retry-connrefused --silent --progress-bar \
--output "$swiftlint_zip" "$swiftlint_version_url"
unzip -p "$swiftlint_zip" swiftlint >"$swiftlint"
rm -f "$swiftlint.zip"
}
download_swiftlint || download_swiftlint
if echo "$swiftlint_version_sha $swiftlint" | shasum --check --status; then
chmod +x "$swiftlint"
else
echo "swiftlint sha mismatch" >&2
shasum -a 256 "$swiftlint"
rm -f "$swiftlint"
exit 1
fi
fi
exec "$swiftlint" "$@" So we can run it as Then in our xcodeproj(
name = "xcodeproj",
pre_build = """set -euo pipefail
if [[ "$ACTION" == "build" ]]; then
cd "$SRCROOT"
./scripts/swiftlint --quiet --lenient
fi""",
project_name = "Ramp",
tags = ["manual"],
# etc...
) This works great for us, and SwiftLint violations render correctly and quickly in the Xcode source editor and issue navigator. This is just one way to do it. You could also run SwiftLint through Bazel using skylib's |
Beta Was this translation helpful? Give feedback.
-
The above answer by @jpsim is pretty similar to what we do. One difference given our project is very big is that we use the git information to only lint the changed files, but that's totally optional and based on your needs. So you could modify your
|
Beta Was this translation helpful? Give feedback.
-
Thanks a lot for your replies, it's very helpful. It's that kind of advice that helps new users to start using Bazel. You rocks! |
Beta Was this translation helpful? Give feedback.
This is a question about SwiftLint, but these instructions are applicable for running any other script or CLI tool on pre-build Xcode actions.
We use SwiftLint as part of a pre-build phase for our
rules_xcodeproj
project and set it up this way:First, we have a shell script wrapper that can download and run a prebuilt SwiftLint binary pinned to a specific version, so that devs don't all have to install it via Homebrew where the versions can easily get out of sync.
It looks like this: