fix(init-script): use latest version from snapshots repository instead of 'latest.integration'

This commit is contained in:
Akash Yadav
2023-09-03 20:33:21 +05:30
parent 46582e1c02
commit 8d6e531f59
6 changed files with 79 additions and 10 deletions
+8 -5
View File
@@ -219,21 +219,24 @@ tasks.create("generateInitScript") {
doLast {
out.bufferedWriter().use {
it.write(
"""
initscript {
repositories {
// Always specify the snapshots repository first
maven {
mavenCentral()
google()
// Add snapshots repository for AndroidIDE CI builds
url "https://s01.oss.sonatype.org/content/repositories/snapshots/"
url "${VersionUtils.SNAPSHOTS_REPO}"
}
mavenCentral()
google()
}
dependencies {
classpath '${BuildConfig.packageName}:gradle-plugin:${downloadVersion}'
classpath '${BuildConfig.packageName}:gradle-plugin:$downloadVersion'
}
}
+3 -1
View File
@@ -54,7 +54,9 @@ tasks.create("generateBuildInfo") {
"PROJECT_SITE" to ProjectConfig.PROJECT_SITE,
"AGP_VERSION_MININUM" to AGP_VERSION_MINIMUM,
"AGP_VERSION_LATEST" to libs.versions.agp.tooling.get()
"AGP_VERSION_LATEST" to libs.versions.agp.tooling.get(),
"SNAPSHOTS_REPOSITORY" to VersionUtils.SNAPSHOTS_REPO,
)
)
}
@@ -52,4 +52,5 @@ public class BuildInfo {
// ------- Misc ------------------------
public static final String PROJECT_SITE = "@@PROJECT_SITE@@";
public static final String SNAPSHOTS_REPOSITORY = "@@SNAPSHOTS_REPOSITORY@@";
}
@@ -101,6 +101,8 @@ val Project.downloadVersion: String
return if (CI.isCiBuild) {
publishingVersion
} else {
"latest.integration"
// sometimes, when working locally, Gradle fails to download the latest snapshot version
// this may cause issues while initializing the project in AndroidIDE
VersionUtils.getLatestSnapshotVersion("gradle-plugin")
}
}
@@ -0,0 +1,63 @@
import org.gradle.api.GradleException
import java.io.BufferedInputStream
import java.net.URI
import javax.xml.parsers.DocumentBuilderFactory
import javax.xml.xpath.XPathFactory
/*
* This file is part of AndroidIDE.
*
* AndroidIDE is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* AndroidIDE is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with AndroidIDE. If not, see <https://www.gnu.org/licenses/>.
*/
/**
* @author Akash Yadav
*/
object VersionUtils {
/**
* The Sonatype snapshots repository.
*/
const val SNAPSHOTS_REPO = "https://s01.oss.sonatype.org/content/repositories/snapshots/"
/**
* The latest integration version name.
*/
const val LATEST_INTEGRATION = "latest.integration"
/**
* Gets the latest snapshot version of the given artifact from the Sonatype snapshots repository.
*/
@JvmStatic
fun getLatestSnapshotVersion(artifact: String): String {
val groupId = BuildConfig.packageName.replace('.', '/')
val moduleMetadata = "${SNAPSHOTS_REPO}/$groupId/${artifact}/maven-metadata.xml"
return try {
BufferedInputStream(URI.create(moduleMetadata).toURL().openStream()).use { inputStream ->
val builderFactory = DocumentBuilderFactory.newInstance()
val builder = builderFactory.newDocumentBuilder()
val document = builder.parse(inputStream)
val xPathFactory = XPathFactory.newInstance()
val xPath = xPathFactory.newXPath()
val latestVersion = xPath.evaluate("/metadata/versioning/latest", document)
println("Found latest version of artifact '$artifact' : '$latestVersion'")
return@use latestVersion
}
} catch (err: Throwable) {
throw GradleException("Failed to download $moduleMetadata", err)
}
}
}
@@ -102,9 +102,7 @@ class AndroidIDEInitScriptPlugin : Plugin<Gradle> {
// For AndroidIDE CI builds
maven { repository ->
repository.url = URI.create(
"https://s01.oss.sonatype.org/content/repositories/snapshots/"
)
repository.url = URI.create(BuildInfo.SNAPSHOTS_REPOSITORY)
}
} else {
logger.info("Using local maven repository for classpath resolution...")