Bug 1946226 - Don't parse TOML multiple times. r=janerik,tthibaud

We currently parse `Cargo.lock` for every single Gradle project. This is unnecessary: once is enough. Moreover, this can be a parallelizable and build-cache friendly task. This patch places the task in the :geckoview project, since that's where the version of native Glean is most relevant

Differential Revision: https://phabricator.services.mozilla.com/D234384
This commit is contained in:
Aaditya Dhingra
2025-02-10 19:32:19 +00:00
parent a5bc212f36
commit 98b4b7e69d
2 changed files with 40 additions and 18 deletions

View File

@@ -55,15 +55,36 @@ def tryInt = { string ->
return string
}
abstract class VerifyGleanVersionTask extends DefaultTask {
@InputFile
final RegularFileProperty source = project.objects.fileProperty().convention(project.layout.projectDirectory.file("Cargo.lock"))
@Input
String expectedVersion = Versions.mozilla_glean
@OutputFiles
FileCollection outputFiles = project.objects.fileCollection()
@TaskAction
void verifyGleanVersion() {
def foundVersion = getRustVersionFor(source.get().asFile, "glean")
if (expectedVersion != foundVersion) {
throw new GradleException("Mismatched Glean version, expected: '${expectedVersion}'," +
" found '${foundVersion}'")
} else {
logger.lifecycle("verifyGleanVersion> expected version matches found version '${foundVersion}'")
}
}
// Parses the Cargo.lock and returns the version for the given package name.
def getRustVersionFor(packageName) {
static String getRustVersionFor(file, packageName) {
String version = null;
TomlParseResult result = Toml.parse(file("Cargo.lock").getText());
TomlParseResult result = Toml.parse(file.getText());
for (object in result.getArray("package").toList()) {
def table = (TomlTable) object
if (table.getString("name") == packageName) {
if (version != null) {
throw new StopExecutionException("Multiple versions for '${packageName}' found." +
throw new GradleException("Multiple versions for '${packageName}' found." +
" Ensure '${packageName}' is only included once.")
}
version = table.getString("version")
@@ -71,6 +92,9 @@ def getRustVersionFor(packageName) {
}
return version
}
}
tasks.register("verifyGleanVersion", VerifyGleanVersionTask)
allprojects {
// Expose the per-object-directory configuration to all projects.
@@ -79,11 +103,7 @@ allprojects {
topsrcdir = gradle.mozconfig.topsrcdir
topobjdir = gradle.mozconfig.topobjdir
gleanVersion = Versions.mozilla_glean
if (gleanVersion != getRustVersionFor("glean")) {
throw new StopExecutionException("Mismatched Glean version, expected: ${gleanVersion}," +
" found ${getRustVersionFor("glean")}")
}
gleanVersion = Versions.mozilla_glean // Verification done in verifyGleanVersion task
artifactSuffix = getArtifactSuffix()
versionName = getVersionName()

View File

@@ -510,6 +510,8 @@ task("generateSDKBindings", type: JavaExec) {
dependsOn project(':annotations').jar
}
preBuild.dependsOn(":verifyGleanVersion")
apply plugin: 'org.mozilla.apilint'
apiLint {