Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Integrate linting #529

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
72 changes: 67 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,13 @@ http_archive(
sha256 = rules_kotlin_sha,
)

load("@io_bazel_rules_kotlin//kotlin:kotlin.bzl", "kotlin_repositories", "kt_register_toolchains")
load("@io_bazel_rules_kotlin//kotlin:repositories.bzl", "kotlin_repositories")
kotlin_repositories() # if you want the default. Otherwise see custom kotlinc distribution below

load("@io_bazel_rules_kotlin//kotlin:setup.bzl", "kotlin_setup")
kotlin_setup()

load("@io_bazel_rules_kotlin//kotlin:kotlin.bzl", "kt_register_toolchains")
kt_register_toolchains() # to use the default toolchain, otherwise see toolchains below
```

Expand All @@ -109,8 +114,13 @@ http_archive(
sha256 = rules_kotlin_sha,
)

load("@io_bazel_rules_kotlin//kotlin:kotlin.bzl", "kotlin_repositories", "kt_register_toolchains")
load("@io_bazel_rules_kotlin//kotlin:repositories.bzl", "kotlin_repositories")
kotlin_repositories() # if you want the default. Otherwise see custom kotlinc distribution below

load("@io_bazel_rules_kotlin//kotlin:setup.bzl", "kotlin_setup")
kotlin_setup()

load("@io_bazel_rules_kotlin//kotlin:kotlin.bzl", "kt_register_toolchains")
kt_register_toolchains() # to use the default toolchain, otherwise see toolchains below
```

Expand Down Expand Up @@ -158,7 +168,7 @@ To choose a different `kotlinc` distribution (1.3 and 1.4 variants supported), d
in your `WORKSPACE` file (or import from a `.bzl` file:

```python
load("@io_bazel_rules_kotlin//kotlin:kotlin.bzl", "kotlin_repositories")
load("@io_bazel_rules_kotlin//kotlin:repositories.bzl", "kotlin_repositories")

KOTLIN_VERSION = "1.3.31"
KOTLINC_RELEASE_SHA = "107325d56315af4f59ff28db6837d03c2660088e3efeb7d4e41f3e01bb848d6a"
Expand All @@ -173,6 +183,52 @@ KOTLINC_RELEASE = {
kotlin_repositories(compiler_release = KOTLINC_RELEASE)
```

## Automatic Linting

`rules_kotlin` provides a facility for automatically generating lint tests. This functionality is opt-in, and can be enabled like so:

Early in the project's `WORKSPACE`:
```python
http_archive(
name = "apple_rules_lint",
sha256 = "8feab4b08a958b10cb2abb7f516652cd770b582b36af6477884b3bba1f2f0726",
strip_prefix = "apple_rules_lint-0.1.1",
url = "https://github.com/apple/apple_rules_lint/archive/0.1.1.zip",
)

load("@apple_rules_lint//lint:setup.bzl", "lint_setup")

lint_setup({
# This is a mapping of a "well known name" (kt-ktlint) to the
# configuration used by the linter (`//:ktlint-config`)
"kt-ktlint": "//:ktlint-config",
})
```

Now, in the `BUILD.bazel` file that you've chosen to use to house the linter configuration (in this example, the top-level `BUILD.bazel`, but it could be anywhere):
```python
load("@io_bazel_rules_kotlin//kotlin:rules.bzl", "ktlint_config")

# The kt-ktlint rules need their configuration to be a `ktlint_config`
ktlint_config(
name = "ktlint-config",
# editorconfig = ".editorconfig",
visibility = [
"//visibility:public",
]
)
```

`ktlint` can be configured using an [.editorconfig](https://github.com/pinterest/ktlint#editorconfig) file.

Once enabled, lint tests will be generated for rules that accept Kotlin sources _if there are Kotlin sources added_. They are tagged with `lint` and `kt-lint`. You can run them using:

`bazel test --test_tag_filters=lint //...`

The lint tests are simply regular bazel tests, so they'll also run when you execute a regular `bazel test //...`.

To disable linting for a specific target, add the `no-lint` or `no-kt-ktlint` tag.

## Third party dependencies
_(e.g. Maven artifacts)_

Expand All @@ -192,8 +248,14 @@ local_repository(

load("@io_bazel_rules_kotlin//kotlin:dependencies.bzl", "kt_download_local_dev_dependencies")
kt_download_local_dev_dependencies()
load("@io_bazel_rules_kotlin//kotlin:kotlin.bzl", "kotlin_repositories", "kt_register_toolchains")
kotlin_repositories() # if you want the default. Otherwise see custom kotlinc distribution below

load("@io_bazel_rules_kotlin//kotlin:repositories.bzl", "kotlin_repositories")
kotlin_repositories()

load("@io_bazel_rules_kotlin//kotlin:setup.bzl", "kotlin_setup")
kotlin_setup()

load("@io_bazel_rules_kotlin//kotlin:kotlin.bzl", "kt_register_toolchains")
kt_register_toolchains() # to use the default toolchain, otherwise see toolchains below
```

Expand Down
6 changes: 5 additions & 1 deletion WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,14 @@ load("//kotlin:dependencies.bzl", "kt_download_local_dev_dependencies")

kt_download_local_dev_dependencies()

load("//kotlin:kotlin.bzl", "kotlin_repositories", "kt_register_toolchains")
load("//kotlin:repositories.bzl", "kotlin_repositories")

kotlin_repositories()

load("//kotlin:setup.bzl", "kotlin_setup")

kotlin_setup()

register_toolchains("@dev_io_bazel_rules_kotlin//kotlin/internal:default_toolchain")

# Creates toolchain configuration for remote execution with BuildKite CI
Expand Down