Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add validation task infrastructure + java/kotlin version matcher #1247

Merged
merged 6 commits into from
Mar 10, 2025
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -779,6 +779,10 @@ internal constructor(
KotlinVersion.fromVersion(it)
}

/** Defines .java_version . */
public val javaVersionFilePath: String?
get() = optionalStringProperty("foundry.jvm.java-version-file-path", blankIsNull = true)

/** Defines a required vendor for JDK toolchains. */
public val jvmVendor: Provider<String>
get() =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,13 @@ import foundry.gradle.properties.sneakyNull
import foundry.gradle.stats.ModuleStatsTasks
import foundry.gradle.tasks.AndroidTestApksTask
import foundry.gradle.tasks.CoreBootstrapTask
import foundry.gradle.tasks.FoundryValidationTask
import foundry.gradle.tasks.GjfDownloadTask
import foundry.gradle.tasks.InstallCommitHooksTask
import foundry.gradle.tasks.KtLintDownloadTask
import foundry.gradle.tasks.KtfmtDownloadTask
import foundry.gradle.tasks.SortDependenciesDownloadTask
import foundry.gradle.tasks.ValidateJavaVersionMatches
import foundry.gradle.tasks.robolectric.UpdateRobolectricJarsTask
import foundry.gradle.testing.EmulatorWtfTests
import foundry.gradle.testing.RoborazziTests
Expand All @@ -49,6 +51,7 @@ import foundry.gradle.util.Thermals
import foundry.gradle.util.ThermalsData
import java.util.Locale
import javax.inject.Inject
import kotlin.jvm.optionals.getOrNull
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.configuration.BuildFeatures
Expand Down Expand Up @@ -164,6 +167,20 @@ internal class FoundryRootPlugin @Inject constructor(private val buildFeatures:
AndroidSourcesConfigurer.patchSdkSources(compileSdk, project, latestCompileSdkWithSources)
}
}

FoundryValidationTask.registerLifecycleTask(project)

foundryProperties.javaVersionFilePath?.let { javaVersionFilePath ->
foundryProperties.versions.jdk.getOrNull()?.let { catalogJdk ->
ValidateJavaVersionMatches.register(
project,
javaVersionFilePath,
catalogJdk,
foundryProperties.versions,
)
}
}

project.configureFoundryRootBuildscript(
foundryProperties.versions.jdk.asProvider(project.providers),
foundryProperties.jvmVendor.map(JvmVendorSpec::matching).orNull,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ internal class FoundryVersions(
private val libResolver: (String) -> Optional<String>,
bundleResolver: (String) -> Optional<Provider<ExternalModuleDependencyBundle>>,
val boms: Set<Provider<MinimalExternalModuleDependency>>,
val catalogName: String,
) {

/**
Expand All @@ -48,6 +49,7 @@ internal class FoundryVersions(
it.endsWith(".bom")
}
.mapTo(LinkedHashSet()) { catalog.findLibrary(it).get() },
catalogName = catalog.name,
)

// Have to use Optional because ConcurrentHashMap doesn't allow nulls for absence
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright (C) 2025 Slack Technologies, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package foundry.gradle.tasks

import org.gradle.api.Project
import org.gradle.api.Task
import org.gradle.api.tasks.TaskProvider

/** Marker interface for Foundry validation tasks that can be depended on by type. */
public interface FoundryValidationTask : Task {
public companion object {
internal fun registerLifecycleTask(project: Project): TaskProvider<out Task> {
return LifecycleTask.register(project, "validateFoundryProject") {
dependsOn(project.tasks.withType(FoundryValidationTask::class.java))
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright (C) 2025 Slack Technologies, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package foundry.gradle.tasks

import foundry.gradle.register
import org.gradle.api.Action
import org.gradle.api.DefaultTask
import org.gradle.api.Project
import org.gradle.api.tasks.TaskProvider
import org.gradle.api.tasks.UntrackedTask

@UntrackedTask(because = "Just a lifecycle task")
internal abstract class LifecycleTask : DefaultTask() {
companion object {
internal fun register(
project: Project,
name: String,
group: String = "foundry",
action: Action<LifecycleTask> = Action {},
): TaskProvider<LifecycleTask> {
return project.tasks.register<LifecycleTask>(name) {
this.group = group
action.execute(this)
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
* Copyright (C) 2025 Slack Technologies, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package foundry.gradle.tasks

import foundry.gradle.FoundryVersions
import foundry.gradle.register
import org.gradle.api.DefaultTask
import org.gradle.api.Project
import org.gradle.api.file.RegularFileProperty
import org.gradle.api.provider.Property
import org.gradle.api.tasks.CacheableTask
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.InputFile
import org.gradle.api.tasks.OutputFile
import org.gradle.api.tasks.PathSensitive
import org.gradle.api.tasks.PathSensitivity
import org.gradle.api.tasks.TaskAction

/**
* A Gradle task that validates whether the Java version specified in a `.java_version` matches the
* expected version defined in a version catalog. If the versions match, an output file is generated
* with the status "valid". If they do not match, the task fails with a descriptive error message.
*
* This is useful for projects that define the JDK in both places, as some tools like Renovate and
* github actions work well with a `.java_version` file.
*/
@CacheableTask
public abstract class ValidateJavaVersionMatches : DefaultTask(), FoundryValidationTask {
@get:InputFile
@get:PathSensitive(PathSensitivity.NONE)
public abstract val javaVersionFile: RegularFileProperty

@get:Input public abstract val javaVersionFileRelativePath: Property<String>
@get:Input public abstract val catalogName: Property<String>
@get:Input public abstract val catalogJdkVersion: Property<Int>

@get:OutputFile public abstract val outputFile: RegularFileProperty

init {
group = "foundry"
}

@TaskAction
internal fun validate() {
val javaVersion = javaVersionFile.asFile.get().readText().trim().toInt()
val catalogVersion = catalogJdkVersion.get()

check(javaVersion == catalogVersion) {
"Java version ($javaVersion) in file '${javaVersionFileRelativePath.get()}' does not match the JDK version in ${catalogName.get()}.versions.toml ($catalogVersion). Please ensure these are aligned"
}

outputFile.asFile.get().writeText("valid")
}

internal companion object {
fun register(
project: Project,
javaVersionFilePath: String,
catalogJdk: Int,
foundryVersions: FoundryVersions,
) {
project.tasks.register<ValidateJavaVersionMatches>("validateJavaVersions") {
javaVersionFile.set(project.layout.projectDirectory.file(javaVersionFilePath))
javaVersionFileRelativePath.set(javaVersionFilePath)
catalogJdkVersion.set(catalogJdk)
catalogName.set(foundryVersions.catalogName)
outputFile.set(
project.layout.buildDirectory.file("foundry/validate_java_version/output.txt")
)
}
}
}
}
Loading