fix(gradle-plugin): publish dependencies to maven local before running tests

This commit is contained in:
Akash Yadav
2023-09-02 23:29:05 +05:30
parent e75b149d76
commit 78dc4df1d3
6 changed files with 102 additions and 60 deletions
+54 -1
View File
@@ -132,7 +132,9 @@ subprojects {
}
project.afterEvaluate {
if (project.plugins.hasPlugin("com.vanniktech.maven.publish.base") && project.description.isNullOrBlank()) {
if (project.plugins.hasPlugin(
"com.vanniktech.maven.publish.base") && project.description.isNullOrBlank()
) {
throw GradleException("Project ${project.path} must have a description")
}
}
@@ -140,6 +142,8 @@ subprojects {
plugins.withId("com.vanniktech.maven.publish.base") {
configure<MavenPublishBaseExtension> {
project.configureMavenLocal()
pom {
name.set(project.name)
description.set(project.description)
@@ -191,6 +195,55 @@ subprojects {
tasks.withType<KotlinCompile>().configureEach {
kotlinOptions.jvmTarget = BuildConfig.javaVersion.toString()
}
gradle.projectsEvaluated {
rootProject.subprojects {
if (project.path in projectsRequiringMavenLocalForTests) {
tasks.withType<Test> {
for ((project, _) in mavenLocalRepos) {
dependsOn(project(project).tasks.getByName("publishAllPublicationsToBuildMavenLocalRepository"))
}
}
}
}
}
}
val projectsRequiringMavenLocalForTests = arrayOf(":gradle-plugin")
val mavenLocalRepos = hashMapOf<String, String>()
fun Project.configureMavenLocal() {
val mavenLocalPath = "${buildDir}/maven-local"
mavenLocalRepos[project.path] = mavenLocalPath
extensions.findByType(PublishingExtension::class.java)?.run {
repositories {
maven {
name = "buildMavenLocal"
url = uri(mavenLocalPath)
}
}
}
tasks.create<Delete>("deleteBuildMavenLocal") {
delete(mavenLocalPath)
}
if (project.path in projectsRequiringMavenLocalForTests) {
tasks.withType<Test> {
dependsOn(tasks.getByName("publishAllPublicationsToBuildMavenLocalRepository"))
doFirst {
val file = File(mavenLocalPath, "repos.txt")
file.writeText(mavenLocalRepos.values.joinToString(separator = File.pathSeparator))
}
}
}
afterEvaluate {
tasks.getByName("publishAllPublicationsToBuildMavenLocalRepository") {
dependsOn(tasks.getByName("deleteBuildMavenLocal"))
}
}
}
tasks.register<Delete>("clean") { delete(rootProject.buildDir) }
-23
View File
@@ -27,29 +27,6 @@ tasks.named<Test>("test") {
useJUnitPlatform()
}
tasks.create<Delete>("deleteBuildMavenLocal") {
delete("$buildDir/maven-local")
}
afterEvaluate {
tasks.getByName("jar") {
dependsOn("deleteBuildMavenLocal")
}
}
tasks.withType<Test> {
dependsOn("publishAllPublicationsToBuildMavenLocalRepository")
}
extensions.findByType(PublishingExtension::class)?.run {
repositories {
maven {
name = "buildMavenLocal"
url = uri("$buildDir/maven-local")
}
}
}
configurations {
val androidBuildTool = create("androidBuildTool")
@@ -52,19 +52,19 @@ class AndroidIDEInitScriptPlugin : Plugin<Gradle> {
*
* **This is an internal property and should not be manually set by users.**
*/
internal const val PROPERTY_MAVEN_LOCAL_REPOSITORY = "androidide.plugins.internal.mavenLocalRepository"
internal const val PROPERTY_MAVEN_LOCAL_REPOSITORY = "androidide.plugins.internal.mavenLocalRepositories"
}
override fun apply(target: Gradle) {
target.settingsEvaluated { settings ->
val (isTestEnv, mavenLocalRepo) = settings.startParameter.run {
val (isTestEnv, mavenLocalRepos) = settings.startParameter.run {
val isTestEnv = projectProperties.containsKey(PROPERTY_IS_TEST_ENV)
&& projectProperties[PROPERTY_IS_TEST_ENV].toString().toBoolean()
val mavenLocalRepo = projectProperties.getOrDefault(PROPERTY_MAVEN_LOCAL_REPOSITORY, "")
isTestEnv to mavenLocalRepo
val mavenLocalRepos = projectProperties.getOrDefault(PROPERTY_MAVEN_LOCAL_REPOSITORY, "")
isTestEnv to mavenLocalRepos
}
settings.addDependencyRepositories(isTestEnv, mavenLocalRepo)
settings.addDependencyRepositories(isTestEnv, mavenLocalRepos)
}
target.projectsLoaded { gradle ->
@@ -95,7 +95,7 @@ class AndroidIDEInitScriptPlugin : Plugin<Gradle> {
private fun RepositoryHandler.configureRepositories(
isMavenLocalEnabled: Boolean,
mavenLocalRepo: String
mavenLocalRepos: String
) {
if (!isMavenLocalEnabled) {
@@ -109,19 +109,20 @@ class AndroidIDEInitScriptPlugin : Plugin<Gradle> {
} else {
logger.info("Using local maven repository for classpath resolution...")
if (mavenLocalRepo.isBlank()) {
mavenLocal(::includeGroupId)
} else {
logger.info("Local repository path: $mavenLocalRepo")
for (mavenLocalRepo in mavenLocalRepos.split(':')) {
if (mavenLocalRepo.isBlank()) {
mavenLocal()
} else {
logger.info("Local repository path: $mavenLocalRepo")
val repo = File(mavenLocalRepo)
if (!repo.exists() || !repo.isDirectory) {
throw FileNotFoundException("Maven local repository '$mavenLocalRepo' not found")
}
val repo = File(mavenLocalRepo)
if (!repo.exists() || !repo.isDirectory) {
throw FileNotFoundException("Maven local repository '$mavenLocalRepo' not found")
}
maven { repository ->
repository.url = repo.toURI()
includeGroupId(repository)
maven { repository ->
repository.url = repo.toURI()
}
}
}
}
@@ -131,11 +132,4 @@ class AndroidIDEInitScriptPlugin : Plugin<Gradle> {
mavenCentral()
}
@Suppress("UnstableApiUsage")
private fun includeGroupId(repository: ArtifactRepository) {
repository.content { contentDescriptor ->
contentDescriptor.includeGroupAndSubgroups(BuildInfo.MVN_GROUP_ID)
}
}
}
@@ -34,16 +34,18 @@ internal val Project.isTestEnv: Boolean
get() = hasProperty(PROPERTY_IS_TEST_ENV) && property(
PROPERTY_IS_TEST_ENV).toString().toBoolean()
internal fun depVersion(testEnv: Boolean) : String {
return if (testEnv && !System.getenv("CI").toBoolean()) {
BuildInfo.VERSION_NAME_SIMPLE
} else {
BuildInfo.VERSION_NAME_DOWNLOAD
}
}
fun Project.ideDependency(artifact: String): Dependency {
return dependencies.ideDependency(artifact, isTestEnv)
}
fun DependencyHandler.ideDependency(artifact: String, testEnv: Boolean): Dependency {
val version = if (testEnv) {
BuildInfo.VERSION_NAME_SIMPLE
} else {
BuildInfo.VERSION_NAME_DOWNLOAD
}
return create("${BuildInfo.MVN_GROUP_ID}:${artifact}:${version}")
return create("${BuildInfo.MVN_GROUP_ID}:${artifact}:${depVersion(testEnv)}")
}
@@ -24,6 +24,8 @@ import org.gradle.testkit.runner.BuildResult
import org.gradle.testkit.runner.GradleRunner
import org.gradle.testkit.runner.internal.PluginUnderTestMetadataReading
import org.junit.jupiter.api.Test
import java.io.File
import java.io.FileNotFoundException
import kotlin.io.path.pathString
/**
@@ -39,25 +41,40 @@ class AndroidIDEInitScriptPluginTest {
@Test
fun `test behavior on minimum supported version`() {
val result = buildProject(agpVersion = BuildInfo.AGP_VERSION_MININUM)
val result = buildProject(agpVersion = BuildInfo.AGP_VERSION_MININUM, gradleVersion = "7.5.1")
assertBasics(result)
}
private fun buildProject(
agpVersion: String = BuildInfo.AGP_VERSION_LATEST,
gradleVersion: String = "8.2",
vararg plugins: String
): BuildResult {
val projectRoot = openProject(agpVersion, *plugins)
val initScript = FileProvider.testHomeDir().resolve(".androidide/init/androidide.init.gradle")
val mavenLocal = FileProvider.projectRoot().resolve("gradle-plugin/build/maven-local")
val mavenLocal = FileProvider.projectRoot().resolve("gradle-plugin/build/maven-local/repos.txt").toFile()
if (!(mavenLocal.exists() && mavenLocal.isFile)) {
throw FileNotFoundException("repos.txt file not found")
}
val repositories = mavenLocal.readText()
for (repo in repositories.split(':')) {
val file = File(repo)
if (!(file.exists() && file.isDirectory)) {
throw FileNotFoundException("Maven local repository does not exist : $repo")
}
}
val runner = GradleRunner.create()
.withProjectDir(projectRoot.toFile())
.withGradleVersion(gradleVersion)
.withArguments(
":app:tasks", // run any task, as long as it applies the plugins
"--init-script", initScript.pathString,
"-Pandroidide.plugins.internal.isTestEnv=true", // plugins should be published to maven local first
"-Pandroidide.plugins.internal.mavenLocalRepository=${mavenLocal.pathString}",
"-Pandroidide.plugins.internal.mavenLocalRepositories=$repositories",
"--stacktrace"
)
@@ -82,7 +99,7 @@ class AndroidIDEInitScriptPluginTest {
for ((project, variants) in mapOf(":app" to arrayOf("demoDebug", "fullDebug"))) {
for (variant in variants) {
assertThat(result.output).contains(
"Adding LogSender dependency (version '${BuildInfo.VERSION_NAME_SIMPLE}') to variant '${variant}' of project '${project}'"
"Adding LogSender dependency (version '${depVersion(true)}') to variant '${variant}' of project '${project}'"
)
}
}
@@ -2,7 +2,6 @@
plugins {
id("com.android.application") version "@@AGP_VERSION@@" apply false
id("com.android.library") version "@@AGP_VERSION@@" apply false
}
tasks.register<Delete>("clean") {