diff --git a/.bazelignore b/.bazelignore new file mode 100644 index 000000000..24e5b0a1a --- /dev/null +++ b/.bazelignore @@ -0,0 +1 @@ +.build diff --git a/.bazelrc b/.bazelrc index 86cd2c477..8f5aeeecd 100644 --- a/.bazelrc +++ b/.bazelrc @@ -1,3 +1,5 @@ +common --enable_bzlmod + try-import %workspace%/ci.bazelrc try-import %workspace%/user.bazelrc diff --git a/MODULE.bazel b/MODULE.bazel new file mode 100644 index 000000000..d5cd6a80b --- /dev/null +++ b/MODULE.bazel @@ -0,0 +1,26 @@ +module( + name = "swiftlint", + version = "0.50.3", + compatibility_level = 1, + repo_name = "SwiftLint", +) + +bazel_dep(name = "platforms", version = "0.0.5") +bazel_dep(name = "rules_apple", version = "2.0.0", repo_name = "build_bazel_rules_apple") +bazel_dep(name = "rules_swift", version = "1.5.1", repo_name = "build_bazel_rules_swift") +bazel_dep(name = "rules_xcodeproj", version = "0.12.2", repo_name = "com_github_buildbuddy_io_rules_xcodeproj") +bazel_dep(name = "sourcekitten", version = "0.34.0", repo_name = "com_github_jpsim_sourcekitten") +bazel_dep(name = "swift_argument_parser", version = "1.2.1", repo_name = "sourcekitten_com_github_apple_swift_argument_parser") +bazel_dep(name = "yams", version = "5.0.3", repo_name = "sourcekitten_com_github_jpsim_yams") + +swiftlint_repos = use_extension("//bazel:repos.bzl", "swiftlint_repos_bzlmod") +use_repo( + swiftlint_repos, + "com_github_apple_swift_syntax", + "com_github_johnsundell_collectionconcurrencykit", + "com_github_krzyzanowskim_cryptoswift", + "swiftlint_com_github_scottrhoyt_swifty_text_table", +) + +extra_rules = use_extension("//bazel:extensions.bzl", "extra_rules") +use_repo(extra_rules, "swiftlint_extra_rules") diff --git a/Makefile b/Makefile index 042ac9d5a..5bb2ddf7d 100644 --- a/Makefile +++ b/Makefile @@ -166,6 +166,7 @@ endif $(eval NEW_VERSION := $(shell echo $(NEW_VERSION_AND_NAME) | sed 's/:.*//' )) @sed -i '' 's/## Main/## $(NEW_VERSION_AND_NAME)/g' CHANGELOG.md @sed 's/__VERSION__/$(NEW_VERSION)/g' tools/Version.swift.template > Source/SwiftLintFramework/Models/Version.swift + @sed -e '3s/.*/ version = "$(NEW_VERSION)",/' -i '' MODULE.bazel make package make bazel_release make portable_zip diff --git a/README.md b/README.md index f5cb113ad..30f86a317 100644 --- a/README.md +++ b/README.md @@ -64,13 +64,19 @@ You can also build and install from source by cloning this project and running ### Using Bazel -Put this in your `WORKSPACE`: +Put this in your `MODULE.bazel`: + +```bzl +bazel_dep(name = "swiftlint", version = "0.50.4", repo_name = "SwiftLint") +``` + +Or put this in your `WORKSPACE`:
WORKSPACE -```python +```bzl load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") http_archive( diff --git a/WORKSPACE b/WORKSPACE index 59186162f..ccbc0f255 100644 --- a/WORKSPACE +++ b/WORKSPACE @@ -1,58 +1,5 @@ workspace(name = "SwiftLint") -load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") - -http_archive( - name = "build_bazel_rules_apple", - sha256 = "43737f28a578d8d8d7ab7df2fb80225a6b23b9af9655fcdc66ae38eb2abcf2ed", - url = "https://github.com/bazelbuild/rules_apple/releases/download/2.0.0/rules_apple.2.0.0.tar.gz", -) - -load( - "@build_bazel_rules_apple//apple:repositories.bzl", - "apple_rules_dependencies", -) - -apple_rules_dependencies() - -http_archive( - name = "rules_swift", - sha256 = "84e2cc1c9e3593ae2c0aa4c773bceeb63c2d04c02a74a6e30c1961684d235593", - url = "https://github.com/bazelbuild/rules_swift/releases/download/1.5.1/rules_swift.1.5.1.tar.gz", -) - -load( - "@rules_swift//swift:repositories.bzl", - "swift_rules_dependencies", -) - -swift_rules_dependencies() - -load( - "@rules_swift//swift:extras.bzl", - "swift_rules_extra_dependencies", -) - -swift_rules_extra_dependencies() - -load("//bazel:repos.bzl", "swiftlint_repos") - -swiftlint_repos() - -load("//bazel:deps.bzl", "swiftlint_deps") - -swiftlint_deps() - -http_archive( - name = "com_github_buildbuddy_io_rules_xcodeproj", - sha256 = "9c86784491854f205b075e5c4d8a838612d433d9454a226d270ad1a17ad8d634", - url = "https://github.com/buildbuddy-io/rules_xcodeproj/releases/download/0.12.2/release.tar.gz", -) - -load("@com_github_buildbuddy_io_rules_xcodeproj//xcodeproj:repositories.bzl", "xcodeproj_rules_dependencies") - -xcodeproj_rules_dependencies() - load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository") git_repository( @@ -70,10 +17,6 @@ load( rules_ruby_dependencies() -load("@bazel_skylib//:workspace.bzl", "bazel_skylib_workspace") - -bazel_skylib_workspace() - rules_ruby_select_sdk(version = "host") load( diff --git a/bazel/deps.bzl b/bazel/deps.bzl index c1c3de88d..4ee3b6ce7 100644 --- a/bazel/deps.bzl +++ b/bazel/deps.bzl @@ -1,18 +1,29 @@ load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") load("@com_github_jpsim_sourcekitten//bazel:repos.bzl", "sourcekitten_repos") -def _default_extra_swift_sources_impl(ctx): +def _extra_swift_sources_impl(ctx): ctx.file("WORKSPACE", "") ctx.file("empty.swift", "func extraRules() -> [Rule.Type] { [] }") + label = ":empty" + if ctx.attr.srcs: + label = ctx.attr.srcs ctx.file("BUILD.bazel", """ filegroup( - name = "extra_rules", + name = "empty", srcs = ["empty.swift"], - visibility = ["//visibility:public"], -)""") +) -_default_extra_swift_sources = repository_rule( - implementation = _default_extra_swift_sources_impl, +alias( + name = "extra_rules", + actual = "{}", + visibility = ["//visibility:public"], +)""".format(label)) + +extra_swift_sources = repository_rule( + implementation = _extra_swift_sources_impl, + attrs = { + "srcs": attr.label(allow_files = [".swift"]), + }, ) def swiftlint_deps(): @@ -24,6 +35,6 @@ def swiftlint_deps(): fail("error: this depends on rules_swift but that wasn't setup") if not native.existing_rule("swiftlint_extra_rules"): - _default_extra_swift_sources(name = "swiftlint_extra_rules") + extra_swift_sources(name = "swiftlint_extra_rules") sourcekitten_repos() diff --git a/bazel/extensions.bzl b/bazel/extensions.bzl new file mode 100644 index 000000000..faa9e8884 --- /dev/null +++ b/bazel/extensions.bzl @@ -0,0 +1,31 @@ +"""bzlmod module_extension for providing extra rule sources that should be loaded by swiftlint""" + +load(":deps.bzl", "extra_swift_sources") + +def _extra_rules(module_ctx): + root_modules = [m for m in module_ctx.modules if m.is_root] + if len(root_modules) > 1: + fail("Expected at most one root module, found {}".format(", ".join([x.name for x in root_modules]))) + + if root_modules: + root_module = root_modules[0] + else: + root_module = module_ctx.modules[0] + + kwargs = {} + if root_module.tags.setup: + kwargs["srcs"] = root_module.tags.setup[0].srcs + + extra_swift_sources( + name = "swiftlint_extra_rules", + **kwargs + ) + +extra_rules = module_extension( + implementation = _extra_rules, + tag_classes = { + "setup": tag_class(attrs = { + "srcs": attr.label(allow_files = [".swift"]), + }), + }, +) diff --git a/bazel/repos.bzl b/bazel/repos.bzl index bb5ee4d71..a8d92a850 100644 --- a/bazel/repos.bzl +++ b/bazel/repos.bzl @@ -1,13 +1,14 @@ load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") -def swiftlint_repos(): +def swiftlint_repos(bzlmod = False): """Fetches SwiftLint repositories""" - http_archive( - name = "com_github_jpsim_sourcekitten", - sha256 = "7a38debb93ef11504f6b6a776be598095591fae7e128263754d041c5ada8e856", - strip_prefix = "SourceKitten-0.34.0", - url = "https://github.com/jpsim/SourceKitten/archive/refs/tags/0.34.0.tar.gz", - ) + if not bzlmod: + http_archive( + name = "com_github_jpsim_sourcekitten", + sha256 = "7a38debb93ef11504f6b6a776be598095591fae7e128263754d041c5ada8e856", + strip_prefix = "SourceKitten-0.34.0", + url = "https://github.com/jpsim/SourceKitten/archive/refs/tags/0.34.0.tar.gz", + ) http_archive( name = "swiftlint_com_github_scottrhoyt_swifty_text_table", @@ -41,3 +42,8 @@ def swiftlint_repos(): strip_prefix = "CryptoSwift-ea07950124f7e44e88bd783bf29e6691a7866228", url = "https://github.com/krzyzanowskim/CryptoSwift/archive/ea07950124f7e44e88bd783bf29e6691a7866228.tar.gz", ) + +def _swiftlint_repos_bzlmod(_): + swiftlint_repos(bzlmod = True) + +swiftlint_repos_bzlmod = module_extension(implementation = _swiftlint_repos_bzlmod) diff --git a/tools/test-analyze.sh b/tools/test-analyze.sh index 27135950f..169e6a5c2 100755 --- a/tools/test-analyze.sh +++ b/tools/test-analyze.sh @@ -2,8 +2,12 @@ set -euo pipefail -readonly swiftlint="$RUNFILES_DIR/SwiftLint/swiftlint" -readonly swiftpm_yaml="$RUNFILES_DIR/SwiftLint/swiftpm.yaml" +readonly swiftlint="$RUNFILES_DIR/_main/swiftlint" +if [[ ! -x "$swiftlint" ]]; then + echo "SwiftLint not found at $swiftlint" + exit 1 +fi + # Change to workspace directory cd "$(dirname "$(readlink Package.swift)")" swift build --build-tests