From 14268a89a6dcdbba65c2ea8ba39dcc11ecb9820a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrzej=20G=C5=82uszak?= Date: Thu, 12 Oct 2023 18:11:45 +0200 Subject: [PATCH] Add support for ktfmt --- README.md | 2 ++ examples/hello.kt | 3 +++ format/BUILD.bazel | 11 +++++++++++ format/format.sh | 13 +++++++++++++ format/langs.bzl | 2 +- format/repositories.bzl | 5 +++++ 6 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 examples/hello.kt diff --git a/README.md b/README.md index fdb1df5..03406d1 100644 --- a/README.md +++ b/README.md @@ -16,6 +16,7 @@ Supported languages: | --------- | ------------------------- | -------------------------------------------------------------- | | ✓ | Python | [Black](https://pypi.org/project/black/) | | ✓ | Java | [google-java-format] | +| ✓ | Kotlin | [ktfmt](https://github.com/facebook/ktfmt) | | ✓ | JavaScript/TypeScript/TSX | [Prettier] | | ✓ | CSS/HTML | [Prettier] | | ✓ | JSON | [Prettier] | @@ -107,6 +108,7 @@ Add some of these lines to `.bazelrc`: run --@aspect_rules_format//format:go_enabled run --@aspect_rules_format//format:java_enabled run --@aspect_rules_format//format:jsonnet_enabled +run --@aspect_rules_format//format:kotlin_enabled run --@aspect_rules_format//format:proto_enabled run --@aspect_rules_format//format:python_enabled run --@aspect_rules_format//format:swift_enabled diff --git a/examples/hello.kt b/examples/hello.kt new file mode 100644 index 0000000..55a1551 --- /dev/null +++ b/examples/hello.kt @@ -0,0 +1,3 @@ +fun main() { + println("Hello, world!") +} diff --git a/format/BUILD.bazel b/format/BUILD.bazel index ab44ae1..01cac63 100644 --- a/format/BUILD.bazel +++ b/format/BUILD.bazel @@ -34,6 +34,12 @@ java_binary( runtime_deps = ["@google-java-format//jar"], ) +java_binary( + name = "ktfmt", + main_class = "com.facebook.ktfmt.cli.Main", + runtime_deps = ["@ktfmt//jar"], +) + alias( name = "black", # Don't use the requirements.bzl#entry_point helper as that makes an eager load @@ -116,6 +122,11 @@ sh_binary( ":format_java": [":java-format"], "//conditions:default": [], }, + ) + select( + { + ":format_kotlin": [":ktfmt"], + "//conditions:default": [], + }, ) + select( { ":format_swift": [":swiftformat"], diff --git a/format/format.sh b/format/format.sh index c8a64fa..dba8453 100755 --- a/format/format.sh +++ b/format/format.sh @@ -57,6 +57,7 @@ case "$mode" in prettiermode="--check" blackmode="--check" javamode="--set-exit-if-changed --dry-run" + ktmode="--set-exit-if-changed --dry-run" gofmtmode="-l" bufmode="format -d --exit-code" tfmode="-check -diff" @@ -67,6 +68,7 @@ case "$mode" in prettiermode="--write" blackmode="" javamode="--replace" + ktmode="" gofmtmode="-w" bufmode="format -w" tfmode="" @@ -162,6 +164,17 @@ if [ -n "$files" ] && [ -n "$bin" ]; then echo "$files" | tr \\n \\0 | JAVA_RUNFILES="${RUNFILES_MANIFEST_FILE%_manifest}" xargs -0 $bin $javamode fi +if [ "$#" -eq 0 ]; then + files=$(git ls-files '*.kt') +else + files=$(find "$@" -name '*.kt') +fi +bin=$(rlocation aspect_rules_format/format/ktfmt) +if [ -n "$files" ] && [ -n "$bin" ]; then + echo "Running ktfmt..." + echo "$files" | tr \\n \\0 | xargs -0 $bin $ktmode +fi + if [ "$#" -eq 0 ]; then files=$(git ls-files '*.go') else diff --git a/format/langs.bzl b/format/langs.bzl index a342ab9..2156846 100644 --- a/format/langs.bzl +++ b/format/langs.bzl @@ -3,4 +3,4 @@ # These are the ones users can enable. # We always do Prettier since it does so many languages. # keep sorted -LANGS = ["go", "java", "jsonnet", "proto", "python", "swift", "terraform"] +LANGS = ["go", "java", "jsonnet", "kotlin", "proto", "python", "swift", "terraform"] diff --git a/format/repositories.bzl b/format/repositories.bzl index 066e8ef..7670a2e 100644 --- a/format/repositories.bzl +++ b/format/repositories.bzl @@ -96,6 +96,11 @@ def rules_format_dependencies(): url = "https://github.com/google/google-java-format/releases/download/v1.17.0/google-java-format-1.17.0-all-deps.jar", ) + http_jar( + name = "ktfmt", + url = "https://repo1.maven.org/maven2/com/facebook/ktfmt/0.46/ktfmt-0.46-jar-with-dependencies.jar", + ) + http_archive( name = "io_bazel_rules_go", sha256 = "099a9fb96a376ccbbb7d291ed4ecbdfd42f6bc822ab77ae6f1b5cb9e914e94fa",