Skip to content
This repository has been archived by the owner on Feb 20, 2024. It is now read-only.

Commit

Permalink
Add support for ktfmt
Browse files Browse the repository at this point in the history
  • Loading branch information
agluszak authored and alexeagle committed Oct 12, 2023
1 parent e218fb0 commit 14268a8
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 1 deletion.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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] |
Expand Down Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions examples/hello.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fun main() {
println("Hello, world!")
}
11 changes: 11 additions & 0 deletions format/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -116,6 +122,11 @@ sh_binary(
":format_java": [":java-format"],
"//conditions:default": [],
},
) + select(
{
":format_kotlin": [":ktfmt"],
"//conditions:default": [],
},
) + select(
{
":format_swift": [":swiftformat"],
Expand Down
13 changes: 13 additions & 0 deletions format/format.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -67,6 +68,7 @@ case "$mode" in
prettiermode="--write"
blackmode=""
javamode="--replace"
ktmode=""
gofmtmode="-w"
bufmode="format -w"
tfmode=""
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion format/langs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
5 changes: 5 additions & 0 deletions format/repositories.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 14268a8

Please sign in to comment.