Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 4 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import com.diffplug.spotless.extra.wtp.EclipseWtpFormatterStep
import com.itsaky.androidide.build.config.BuildConfig
import com.itsaky.androidide.build.config.FDroidConfig
import com.itsaky.androidide.build.config.publishingVersion
import com.itsaky.androidide.build.maven.renameFilesToModuleUrls
import com.itsaky.androidide.plugins.AndroidIDEPlugin
import com.itsaky.androidide.plugins.conf.configureAndroidModule
import com.itsaky.androidide.plugins.conf.configureJavaModule
Expand Down Expand Up @@ -664,6 +665,9 @@ fun convertCacheToLocalMavenRepo(
Files.copy(filePath, targetFile, StandardCopyOption.REPLACE_EXISTING)
logger.lifecycle("Copied ${filePath.relativeTo(normalizedSource)} -> ${targetFile.relativeTo(normalizedDestination)}")
}

// The cache keeps a file under its .module "name"; builds ask for its "url" (ADFA-7220).
renameFilesToModuleUrls(normalizedDestination).forEach(logger::lifecycle)
}

fun zeroCompressMavenRepo(
Expand Down
13 changes: 8 additions & 5 deletions composite-builds/build-logic/common/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,14 @@
*/

plugins {
//noinspection JavaPluginLanguageLevel
id("java-library")
alias(libs.plugins.kotlin.jvm)
//noinspection JavaPluginLanguageLevel
id("java-library")
alias(libs.plugins.kotlin.jvm)
}

dependencies {
api(gradleApi())
}
api(gradleApi())

testImplementation(libs.tests.junit)
testImplementation(libs.tests.google.truth)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.itsaky.androidide.build.maven

import groovy.json.JsonSlurper
import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.StandardCopyOption
import kotlin.io.path.isRegularFile
import kotlin.io.path.name

/**
* Stores each file of a local Maven repository under the name a repository serves it at.
*
* Gradle's module cache keeps a file under the `name` its `.module` metadata gives it, but a build
* asks the repository for the `url`. For androidx.lifecycle:lifecycle-viewmodel-android:2.9.4 the two
* differ (lifecycle-viewmodel-release.aar against lifecycle-viewmodel-android-2.9.4.aar), and an
* offline build fails until the file carries its url name (ADFA-7220). dev-assets'
* scripts/cache-to-local-maven-repo.py does the same; keep the two in step.
*
* A url with a slash points outside the module's directory and is left alone, as is a file that is
* missing or whose url name already exists. The name copy is removed unless another variant serves
* the file under that name.
*
* @return one line per file copied or removed, for the log
*/
fun renameFilesToModuleUrls(repo: Path): List<String> {
val log = mutableListOf<String>()
val modules =
Files.walk(repo).use { paths ->
paths.filter { it.isRegularFile() && it.name.endsWith(".module") }.sorted().toList()
}
for (module in modules) {
val metadata =
try {
JsonSlurper().parse(module.toFile())
} catch (e: Exception) {
throw IllegalStateException("Cannot read $module: ${e.message}", e)
}
val variants = ((metadata as? Map<*, *>)?.get("variants") as? List<*>).orEmpty().filterIsInstance<Map<*, *>>()
val files = variants.flatMap { (it["files"] as? List<*>).orEmpty().filterIsInstance<Map<*, *>>() }
val urls = files.mapNotNull { it["url"] as? String }.toSet()
val copiedNames = sortedSetOf<String>()
for (file in files) {
val name = file["name"] as? String ?: continue
val url = file["url"] as? String ?: continue
if (name == url || '/' in name || '/' in url) continue
val src = module.resolveSibling(name)
val dst = module.resolveSibling(url)
if (!src.isRegularFile() || Files.exists(dst)) continue
Files.copy(src, dst, StandardCopyOption.COPY_ATTRIBUTES)
copiedNames += name
log += "Copied ${repo.relativize(src)} -> $url"
}
for (name in copiedNames - urls) {
val src = module.resolveSibling(name)
Files.delete(src)
log += "Removed ${repo.relativize(src)}"
}
}
return log
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
package com.itsaky.androidide.build.maven

import com.google.common.truth.Truth.assertThat
import org.junit.Assert.assertThrows
import org.junit.Rule
import org.junit.Test
import org.junit.rules.TemporaryFolder
import java.io.File

/** The cases of dev-assets' scripts/tests/test-cache-to-local-maven-repo.py (ADFA-7220). */
class ModuleUrlNamesTest {
@get:Rule
val tmp = TemporaryFolder()

private fun module(vararg variants: List<Pair<String, String>>): String =
variants.withIndex().joinToString(",", "{\"formatVersion\": \"1.1\", \"variants\": [", "]}") { (i, files) ->
files.joinToString(",", "{\"name\": \"v$i\", \"files\": [", "]}") { (name, url) ->
"{\"name\": \"$name\", \"url\": \"$url\"}"
}
}

private fun dir(path: String): File = File(tmp.root, path).apply { mkdirs() }

private fun File.put(
name: String,
text: String = "x",
): File = File(this, name).apply { writeText(text) }

private fun File.listing(): List<String> = list()!!.sorted()

@Test
fun `a file takes its module url name`() {
val d = dir("androidx/lifecycle/lifecycle-viewmodel-android/2.9.4")
val pair = "lifecycle-viewmodel-release.aar" to "lifecycle-viewmodel-android-2.9.4.aar"
d.put("lifecycle-viewmodel-android-2.9.4.module", module(listOf(pair), listOf(pair)))
d.put("lifecycle-viewmodel-android-2.9.4.pom", "<project/>")
d.put("lifecycle-viewmodel-release.aar", "aar bytes")

renameFilesToModuleUrls(tmp.root.toPath())

assertThat(d.listing())
.containsExactly(
"lifecycle-viewmodel-android-2.9.4.aar",
"lifecycle-viewmodel-android-2.9.4.module",
"lifecycle-viewmodel-android-2.9.4.pom",
).inOrder()
assertThat(File(d, "lifecycle-viewmodel-android-2.9.4.aar").readText()).isEqualTo("aar bytes")
}

@Test
fun `matching names are untouched`() {
val d = dir("androidx/core/core/1.13.1")
d.put("core-1.13.1.module", module(listOf("core-1.13.1.aar" to "core-1.13.1.aar")))
d.put("core-1.13.1.aar")

assertThat(renameFilesToModuleUrls(tmp.root.toPath())).isEmpty()
assertThat(d.listing()).containsExactly("core-1.13.1.aar", "core-1.13.1.module").inOrder()
}

@Test
fun `a name another variant serves is kept`() {
val d = dir("g/x/lib/1")
d.put("lib-1.module", module(listOf("lib.jar" to "lib-1.jar"), listOf("lib.jar" to "lib.jar")))
d.put("lib.jar", "jar")

renameFilesToModuleUrls(tmp.root.toPath())

assertThat(d.listing()).containsExactly("lib-1.jar", "lib-1.module", "lib.jar").inOrder()
}

@Test
fun `one name serves two urls`() {
val d = dir("g/x/lib/4")
d.put("lib-4.module", module(listOf("lib.aar" to "lib-4.aar"), listOf("lib.aar" to "lib-4-debug.aar")))
d.put("lib.aar", "aar")

renameFilesToModuleUrls(tmp.root.toPath())

assertThat(d.listing()).containsExactly("lib-4-debug.aar", "lib-4.aar", "lib-4.module").inOrder()
}

@Test
fun `a missing file and an existing url are left alone`() {
val d = dir("g/x/lib/2")
d.put("lib-2.module", module(listOf("absent.aar" to "lib-2.aar"), listOf("lib-old.jar" to "lib-2.jar")))
d.put("lib-old.jar", "old")
d.put("lib-2.jar", "new")

renameFilesToModuleUrls(tmp.root.toPath())

assertThat(d.listing()).containsExactly("lib-2.jar", "lib-2.module", "lib-old.jar").inOrder()
assertThat(File(d, "lib-2.jar").readText()).isEqualTo("new")
}

@Test
fun `a url outside the directory is ignored`() {
val d = dir("g/x/kmp/3")
d.put("kmp-3.module", module(listOf("kmp.jar" to "../../kmp-jvm/3/kmp-jvm-3.jar")))
d.put("kmp.jar")

renameFilesToModuleUrls(tmp.root.toPath())

assertThat(d.listing()).containsExactly("kmp-3.module", "kmp.jar").inOrder()
}

@Test
fun `a module that is not JSON stops the conversion`() {
val bad = dir("g/x/bad/1").put("bad-1.module", "not json")

val e = assertThrows(IllegalStateException::class.java) { renameFilesToModuleUrls(tmp.root.toPath()) }
assertThat(e).hasMessageThat().contains(bad.path)
}

@Test
fun `odd module shapes are skipped`() {
dir("g/x/odd/0").put("odd-0.module", "[]")
dir("g/x/odd/1").put("odd-1.module", "{\"variants\": null}")
val d = dir("g/x/odd/2")
d.put("odd-2.module", "{\"variants\": [{\"files\": null}, {\"files\": [\"x\"]}, 3]}")

assertThat(renameFilesToModuleUrls(tmp.root.toPath())).isEmpty()
assertThat(d.listing()).containsExactly("odd-2.module")
}
}
Loading