Skip to content

Commit fb15352

Browse files
authored
Pin bazel_common_maven artifacts and add related tooling to update the generated lock file. (#69)
* Pin `bazel_common_maven` artifacts and add related tooling to update the generated lock file. * Update `README.md`
1 parent 3359f45 commit fb15352

17 files changed

+3246
-29
lines changed

.bazelrc

+2
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,6 @@ query --experimental_google_legacy_api
1919
test --test_output=errors # Print test logs for failed tests
2020
test --build_tests_only
2121

22+
run --ui_event_filters=-info,-stdout --noshow_progress
23+
2224
try-import local.bazelrc

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ bazel_common_initialize(
3030
buildifier_version = "5.1.0",
3131
)
3232

33+
load("@grab_bazel_common//android:maven.bzl", "pin_bazel_common_artifacts")
34+
35+
pin_bazel_common_artifacts()
36+
3337
load("@grab_bazel_common//:workspace_defs.bzl", "GRAB_BAZEL_COMMON_ARTIFACTS")
3438

3539
# Bazel common uses few artifacts under @maven repository

WORKSPACE

+5
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,13 @@ load("@grab_bazel_common//android:initialize.bzl", "bazel_common_initialize")
99

1010
bazel_common_initialize(
1111
buildifier_version = "5.1.0",
12+
pinned_maven_install = True,
1213
)
1314

15+
load("@grab_bazel_common//android:maven.bzl", "pin_bazel_common_artifacts")
16+
17+
pin_bazel_common_artifacts()
18+
1419
load("@io_bazel_rules_kotlin//kotlin:repositories.bzl", "kotlin_repositories")
1520

1621
kotlin_repositories()

android/initialize.bzl

+8-4
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,9 @@ load("@grab_bazel_common//tools/buildifier:defs.bzl", "BUILDIFIER_DEFAULT_VERSIO
88
load("@grab_bazel_common//android/tools:defs.bzl", "android_tools")
99
load("@bazel_common_dagger//:workspace_defs.bzl", "DAGGER_ARTIFACTS", "DAGGER_REPOSITORIES")
1010
load("@rules_jvm_external//:defs.bzl", "maven_install")
11-
1211
# load("@rules_proto//proto:repositories.bzl", "rules_proto_dependencies", "rules_proto_toolchains")
1312

14-
# Setup android databinding compilation and optionally used patched android tools jar
13+
# Setup android databinding compilation and optionally use patched android tools jar
1514
def _android(patched_android_tools):
1615
native.bind(
1716
name = "databinding_annotation_processor",
@@ -22,7 +21,8 @@ def _android(patched_android_tools):
2221

2322
def bazel_common_initialize(
2423
patched_android_tools = True,
25-
buildifier_version = BUILDIFIER_DEFAULT_VERSION):
24+
buildifier_version = BUILDIFIER_DEFAULT_VERSION,
25+
pinned_maven_install = True):
2626
#rules_proto_dependencies()
2727
#rules_proto_toolchains()
2828

@@ -32,8 +32,11 @@ def bazel_common_initialize(
3232
),
3333
)
3434

35+
repo_name = "bazel_common_maven"
36+
maven_install_json = "@grab_bazel_common//:%s_install.json" % repo_name if pinned_maven_install else None
37+
3538
maven_install(
36-
name = "bazel_common_maven",
39+
name = repo_name,
3740
artifacts = DAGGER_ARTIFACTS + [
3841
"com.google.guava:guava:29.0-jre",
3942
"com.google.auto:auto-common:0.10",
@@ -60,6 +63,7 @@ def bazel_common_initialize(
6063
"https://maven.google.com",
6164
],
6265
strict_visibility = True,
66+
maven_install_json = maven_install_json,
6367
)
6468

6569
_android(patched_android_tools)

android/maven.bzl

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
load("@bazel_common_maven//:defs.bzl", _pinned_maven_install = "pinned_maven_install")
2+
3+
def pin_bazel_common_artifacts():
4+
_pinned_maven_install()

bazel_common_maven_install.json

+2,968
Large diffs are not rendered by default.

docs/Dependencies.md

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
## Updating dependencies
2+
3+
Whenever any maven dependencies are updated, the pinned artifacts information stored in
4+
`bazel_common_maven_install.json` need to be refreshed. This can be done by running
5+
`bazelisk run -- //tools/maven:maven_pinner --maven_repo=bazel_common_maven` or by running
6+
`Pin Artifacts` IDE configuration that is imported after syncing the project in IntelliJ.

main.bazelproject

-25
This file was deleted.

project_views/Pin Artifacts.run.xml

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<component name="ProjectRunConfigurationManager">
2+
<configuration default="false" name="Pin Artifacts" type="BlazeCommandRunConfigurationType" factoryName="Bazel Command">
3+
<blaze-settings handler-id="BlazeJavaRunConfigurationHandlerProvider" blaze-command="run" kind="java_binary">
4+
<blaze-target>//tools/maven:maven_pinner</blaze-target>
5+
<blaze-user-exe-flag>--maven_repo=bazel_common_maven</blaze-user-exe-flag>
6+
</blaze-settings>
7+
<method v="2">
8+
<option name="Blaze.BeforeRunTask" enabled="true" />
9+
</method>
10+
</configuration>
11+
</component>

project_views/Run all tests.run.xml

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<component name="ProjectRunConfigurationManager">
2+
<configuration default="false" name="Run all tests" type="BlazeCommandRunConfigurationType" factoryName="Bazel Command">
3+
<blaze-settings handler-id="BlazeCommandGenericRunConfigurationHandlerProvider" blaze-command="test">
4+
<blaze-target>//...</blaze-target>
5+
</blaze-settings>
6+
<method v="2">
7+
<option name="Blaze.BeforeRunTask" enabled="true" />
8+
</method>
9+
</configuration>
10+
</component>

project_views/default.bazelproject

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
directories:
2+
.
3+
-bazel-cache
4+
5+
# Automatically includes all relevant targets under the 'directories' above
6+
derive_targets_from_directories: true
7+
8+
workspace_type: android
9+
10+
java_language_level: 11
11+
12+
additional_languages:
13+
android
14+
java
15+
kotlin
16+
17+
android_sdk_platform: android-30
18+
19+
import_run_configurations:
20+
project_views/Pin Artifacts.run.xml
21+
project_views/Run all tests.run.xml

tools/bazel_lib/BUILD.bazel

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
load("@io_bazel_rules_kotlin//kotlin:jvm.bzl", "kt_jvm_library")
2+
3+
kt_jvm_library(
4+
name = "bazel_lib",
5+
srcs = glob([
6+
"src/main/kotlin/**/*.kt",
7+
"src/main/kotlin/**/*.java",
8+
]),
9+
visibility = [
10+
"//visibility:public",
11+
],
12+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package com.grab.bazel
2+
3+
import java.io.File
4+
5+
interface BazelInvoker {
6+
val workingDir: String
7+
val bazelBinary: String
8+
9+
fun build(targets: List<String>, vararg options: String): BazelResult
10+
11+
fun run(targets: List<String>, vararg options: String): BazelResult
12+
13+
operator fun invoke(command: () -> List<String>): BazelResult
14+
}
15+
16+
data class BazelResult(
17+
val exitCode: Int,
18+
val logs: String
19+
)
20+
21+
val BazelResult.isError get() = exitCode == 1
22+
23+
const val BAZELISK = "bazelisk"
24+
25+
object BazelInvokerFactory {
26+
fun create(
27+
workingDir: String,
28+
bazelBinary: String = BAZELISK
29+
): BazelInvoker = DefaultBazelInvoker(workingDir, bazelBinary)
30+
}
31+
32+
class DefaultBazelInvoker(
33+
override val workingDir: String,
34+
override val bazelBinary: String
35+
) : BazelInvoker {
36+
37+
private fun bazelCommand(command: List<String>): BazelResult {
38+
val process = ProcessBuilder()
39+
.directory(File(workingDir))
40+
.command(listOf(bazelBinary) + command)
41+
.redirectErrorStream(true)
42+
.start()
43+
val output = buildString {
44+
process.inputStream
45+
.reader()
46+
.forEachLine { line ->
47+
appendLine(line)
48+
println(line)
49+
}
50+
}
51+
process.waitFor()
52+
return BazelResult(
53+
exitCode = process.exitValue(),
54+
logs = output
55+
)
56+
}
57+
58+
override fun build(
59+
targets: List<String>,
60+
vararg options: String
61+
) = bazelCommand(listOf("build") + targets + options)
62+
63+
override fun run(
64+
targets: List<String>,
65+
vararg options: String
66+
) = bazelCommand(listOf("run") + targets + options)
67+
68+
override fun invoke(
69+
command: () -> List<String>
70+
) = bazelCommand(command())
71+
}

tools/maven/BUILD.bazel

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
load("@io_bazel_rules_kotlin//kotlin:jvm.bzl", "kt_jvm_library")
2+
3+
kt_jvm_library(
4+
name = "maven_lib",
5+
srcs = glob([
6+
"src/main/kotlin/**/*.kt",
7+
]),
8+
visibility = [
9+
"//visibility:public",
10+
],
11+
deps = [
12+
"//tools/bazel_lib",
13+
"@bazel_common_maven//:com_github_ajalt_clikt",
14+
],
15+
)
16+
17+
java_binary(
18+
name = "maven_pinner",
19+
main_class = "com.grab.maven.MavenInstallPinKt",
20+
visibility = [
21+
"//visibility:public",
22+
],
23+
runtime_deps = [
24+
":maven_lib",
25+
],
26+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.grab.maven
2+
3+
import com.github.ajalt.clikt.core.CliktCommand
4+
import com.github.ajalt.clikt.parameters.options.option
5+
import com.github.ajalt.clikt.parameters.options.required
6+
import com.grab.bazel.BazelInvokerFactory
7+
import com.grab.bazel.isError
8+
import com.grab.maven.PinningTarget.Pinned
9+
import java.io.File
10+
11+
class MavenInstallPin : CliktCommand() {
12+
13+
private val mavenRepoName by option(
14+
"-M",
15+
"--maven_repo",
16+
help = "Name of the maven_install repo to run pinning"
17+
).required()
18+
19+
private val workingDir = System.getenv("BUILD_WORKSPACE_DIRECTORY")
20+
?: error("Missing BUILD_WORKSPACE_DIRECTORY environment")
21+
22+
private val bazelInvoker = BazelInvokerFactory.create(workingDir = workingDir)
23+
24+
25+
private fun file(name: String) = File(workingDir, name)
26+
27+
override fun run() {
28+
val workspaceUpdater = WorkspaceUpdater(file("WORKSPACE"))
29+
println("Unpinning @$mavenRepoName, output:")
30+
workspaceUpdater.unpin()
31+
val result = bazelInvoker.run(listOf(Pinned(mavenRepoName).toString()))
32+
workspaceUpdater.pin()
33+
if (!result.isError) {
34+
println("Pinned artifacts successfully!")
35+
} else {
36+
println("Error occurred while pinning artifacts")
37+
}
38+
}
39+
}
40+
41+
fun main(args: Array<String>) {
42+
MavenInstallPin().main(args)
43+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.grab.maven
2+
3+
sealed class PinningTarget(open val mavenRepoName: String) {
4+
5+
data class Unpinned(
6+
override val mavenRepoName: String
7+
) : PinningTarget(mavenRepoName) {
8+
override fun toString() = "@unpinned_${mavenRepoName}//:pin"
9+
}
10+
11+
data class Pinned(
12+
override val mavenRepoName: String
13+
) : PinningTarget(mavenRepoName) {
14+
override fun toString() = "@${mavenRepoName}//:pin"
15+
}
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.grab.maven
2+
3+
import java.io.File
4+
5+
class WorkspaceUpdater(
6+
private val workspace: File
7+
) {
8+
9+
private val pinnedMavenInstallAttr = "pinned_maven_install ="
10+
private val pinBazelCommonArtifacts = "pin_bazel_common_artifacts()"
11+
12+
fun pin() {
13+
with(workspace) {
14+
writeText(
15+
readText().replace(
16+
"$pinnedMavenInstallAttr False",
17+
"$pinnedMavenInstallAttr True"
18+
).replace(
19+
"#+pin_bazel_common_artifacts\\(\\)".toRegex(),
20+
pinBazelCommonArtifacts
21+
)
22+
)
23+
}
24+
}
25+
26+
fun unpin() {
27+
with(workspace) {
28+
writeText(
29+
readText().replace(
30+
"$pinnedMavenInstallAttr True",
31+
"$pinnedMavenInstallAttr False"
32+
).replace(
33+
"$pinBazelCommonArtifacts",
34+
"#$pinBazelCommonArtifacts"
35+
)
36+
)
37+
}
38+
}
39+
}

0 commit comments

Comments
 (0)