Skip to content

Commit 71bec22

Browse files
authored
feat: Set up ktfmt to format code (#247)
* feat: Set up ktfmt to format code * Actually format code
1 parent bd71bc5 commit 71bec22

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+4430
-2773
lines changed

BUILD

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,8 @@ alias(
22
name = "bazel-diff",
33
actual = "//cli:bazel-diff",
44
)
5+
6+
alias(
7+
name = "format",
8+
actual = "//cli:format",
9+
)

MODULE.bazel

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ module(
44
compatibility_level = 0,
55
)
66

7+
bazel_dep(name = "aspect_rules_lint", version = "1.0.2")
78
bazel_dep(name = "bazel_skylib", version = "1.6.1")
89
bazel_dep(name = "rules_proto", version = "6.0.0-rc2")
910
bazel_dep(name = "rules_java", version = "7.6.5")
@@ -31,3 +32,6 @@ use_repo(
3132
maven,
3233
bazel_diff_maven = "bazel_diff_maven",
3334
)
35+
36+
non_module_repositories = use_extension("//:extensions.bzl", "non_module_repositories")
37+
use_repo(non_module_repositories, "ktfmt")

MODULE.bazel.lock

Lines changed: 1223 additions & 35 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

WORKSPACE

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,17 @@ maven_install(
6464
load("@bazel_diff_maven//:defs.bzl", "pinned_maven_install")
6565

6666
pinned_maven_install()
67+
68+
load("@aspect_bazel_lib//lib:repositories.bzl", "aspect_bazel_lib_dependencies")
69+
70+
aspect_bazel_lib_dependencies()
71+
72+
load(
73+
"@aspect_rules_lint//format:repositories.bzl",
74+
"fetch_ktfmt",
75+
"rules_lint_dependencies",
76+
)
77+
78+
rules_lint_dependencies()
79+
80+
fetch_ktfmt()

cli/BUILD

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
load("@aspect_rules_lint//format:defs.bzl", "format_multirun")
12
load("@rules_java//java:defs.bzl", "java_binary")
23
load("@rules_kotlin//kotlin:jvm.bzl", "kt_jvm_library", "kt_jvm_test")
34

@@ -58,7 +59,6 @@ kt_jvm_test(
5859
runtime_deps = [":cli-test-lib"],
5960
)
6061

61-
6262
kt_jvm_test(
6363
name = "SourceFileHasherTest",
6464
data = [
@@ -106,10 +106,10 @@ kt_jvm_test(
106106

107107
kt_jvm_test(
108108
name = "E2ETest",
109+
data = [":workspaces"],
109110
jvm_flags = ["-Djava.security.manager=allow"],
110111
test_class = "com.bazel_diff.e2e.E2ETest",
111112
runtime_deps = [":cli-test-lib"],
112-
data = [":workspaces"],
113113
)
114114

115115
kt_jvm_test(
@@ -146,3 +146,15 @@ filegroup(
146146
"src/test/resources/workspaces",
147147
],
148148
)
149+
150+
java_binary(
151+
name = "ktfmt",
152+
main_class = "com.facebook.ktfmt.cli.Main",
153+
runtime_deps = ["@ktfmt//jar"],
154+
)
155+
156+
format_multirun(
157+
name = "format",
158+
kotlin = ":ktfmt",
159+
visibility = ["//visibility:public"],
160+
)
Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
package com.bazel_diff
22

33
import com.bazel_diff.cli.BazelDiff
4-
import picocli.CommandLine
54
import kotlin.system.exitProcess
5+
import picocli.CommandLine
66

77
class Main {
8-
companion object {
9-
@JvmStatic
10-
fun main(args: Array<String>) {
11-
val exitCode = CommandLine(BazelDiff()).execute(*args)
12-
exitProcess(exitCode)
13-
}
8+
companion object {
9+
@JvmStatic
10+
fun main(args: Array<String>) {
11+
val exitCode = CommandLine(BazelDiff()).execute(*args)
12+
exitProcess(exitCode)
1413
}
14+
}
1515
}

cli/src/main/kotlin/com/bazel_diff/bazel/BazelClient.kt

Lines changed: 50 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -2,59 +2,65 @@ package com.bazel_diff.bazel
22

33
import com.bazel_diff.log.Logger
44
import com.google.devtools.build.lib.query2.proto.proto2api.Build
5+
import java.util.Calendar
56
import org.koin.core.component.KoinComponent
67
import org.koin.core.component.inject
7-
import java.util.Calendar
88

99
class BazelClient(
10-
private val useCquery: Boolean,
11-
private val fineGrainedHashExternalRepos: Set<String>,
12-
private val excludeExternalTargets: Boolean
10+
private val useCquery: Boolean,
11+
private val fineGrainedHashExternalRepos: Set<String>,
12+
private val excludeExternalTargets: Boolean
1313
) : KoinComponent {
14-
private val logger: Logger by inject()
15-
private val queryService: BazelQueryService by inject()
14+
private val logger: Logger by inject()
15+
private val queryService: BazelQueryService by inject()
1616

17-
suspend fun queryAllTargets(): List<BazelTarget> {
18-
val queryEpoch = Calendar.getInstance().getTimeInMillis()
17+
suspend fun queryAllTargets(): List<BazelTarget> {
18+
val queryEpoch = Calendar.getInstance().getTimeInMillis()
1919

20-
val repoTargetsQuery = if (excludeExternalTargets) emptyList() else listOf("//external:all-targets")
21-
val targets = if (useCquery) {
22-
// Explicitly listing external repos here sometimes causes issues mentioned at
23-
// https://bazel.build/query/cquery#recursive-target-patterns. Hence, we query all dependencies with `deps`
24-
// instead. However, we still need to append all "//external:*" targets because fine-grained hash
25-
// computation depends on hashing of source files in external repos as well, which is limited to repos
26-
// explicitly mentioned in `fineGrainedHashExternalRepos` flag. Therefore, for any repos not mentioned there
27-
// we are still relying on the repo-generation target under `//external` to compute the hash.
28-
//
29-
// In addition, we must include all source dependencies in this query in order for them to show up in
30-
// `configuredRuleInput`. Hence, one must not filter them out with `kind(rule, deps(..))`. However, these
31-
// source targets are omitted inside BazelQueryService with the custom starlark function used to print
32-
// labels.
33-
(queryService.query("deps(//...:all-targets)", useCquery = true) +
34-
queryService.query(repoTargetsQuery.joinToString(" + ") { "'$it'" }))
35-
.distinctBy { it.rule.name }
20+
val repoTargetsQuery =
21+
if (excludeExternalTargets) emptyList() else listOf("//external:all-targets")
22+
val targets =
23+
if (useCquery) {
24+
// Explicitly listing external repos here sometimes causes issues mentioned at
25+
// https://bazel.build/query/cquery#recursive-target-patterns. Hence, we query all
26+
// dependencies with `deps`
27+
// instead. However, we still need to append all "//external:*" targets because
28+
// fine-grained hash
29+
// computation depends on hashing of source files in external repos as well, which is
30+
// limited to repos
31+
// explicitly mentioned in `fineGrainedHashExternalRepos` flag. Therefore, for any repos
32+
// not mentioned there
33+
// we are still relying on the repo-generation target under `//external` to compute the
34+
// hash.
35+
//
36+
// In addition, we must include all source dependencies in this query in order for them to
37+
// show up in
38+
// `configuredRuleInput`. Hence, one must not filter them out with `kind(rule, deps(..))`.
39+
// However, these
40+
// source targets are omitted inside BazelQueryService with the custom starlark function
41+
// used to print
42+
// labels.
43+
(queryService.query("deps(//...:all-targets)", useCquery = true) +
44+
queryService.query(repoTargetsQuery.joinToString(" + ") { "'$it'" }))
45+
.distinctBy { it.rule.name }
3646
} else {
37-
val buildTargetsQuery = listOf("//...:all-targets") + fineGrainedHashExternalRepos.map { "@$it//...:all-targets" }
38-
queryService.query((repoTargetsQuery + buildTargetsQuery).joinToString(" + ") { "'$it'" })
47+
val buildTargetsQuery =
48+
listOf("//...:all-targets") +
49+
fineGrainedHashExternalRepos.map { "@$it//...:all-targets" }
50+
queryService.query((repoTargetsQuery + buildTargetsQuery).joinToString(" + ") { "'$it'" })
3951
}
40-
val queryDuration = Calendar.getInstance().getTimeInMillis() - queryEpoch
41-
logger.i { "All targets queried in $queryDuration" }
42-
return targets.mapNotNull { target: Build.Target ->
43-
when (target.type) {
44-
Build.Target.Discriminator.RULE -> BazelTarget.Rule(target)
45-
Build.Target.Discriminator.SOURCE_FILE -> BazelTarget.SourceFile(
46-
target
47-
)
48-
49-
Build.Target.Discriminator.GENERATED_FILE -> BazelTarget.GeneratedFile(
50-
target
51-
)
52-
53-
else -> {
54-
logger.w { "Unsupported target type in the build graph: ${target.type.name}" }
55-
null
56-
}
57-
}
52+
val queryDuration = Calendar.getInstance().getTimeInMillis() - queryEpoch
53+
logger.i { "All targets queried in $queryDuration" }
54+
return targets.mapNotNull { target: Build.Target ->
55+
when (target.type) {
56+
Build.Target.Discriminator.RULE -> BazelTarget.Rule(target)
57+
Build.Target.Discriminator.SOURCE_FILE -> BazelTarget.SourceFile(target)
58+
Build.Target.Discriminator.GENERATED_FILE -> BazelTarget.GeneratedFile(target)
59+
else -> {
60+
logger.w { "Unsupported target type in the build graph: ${target.type.name}" }
61+
null
5862
}
63+
}
5964
}
65+
}
6066
}

0 commit comments

Comments
 (0)