1
0
Fork 0
mirror of https://github.com/ethauvin/kobalt.git synced 2025-04-26 08:27:12 -07:00

aar work.

This commit is contained in:
Cedric Beust 2015-10-28 04:18:12 -07:00
parent 169760cca2
commit b61388f04b
14 changed files with 116 additions and 53 deletions

View file

@ -105,7 +105,8 @@ public class ProjectGenerator {
private fun updateVersion(dep: Dependency, mapped: Map<String, String>) =
if ( dep.version.startsWith("\${")) {
val property = dep.version.substring(2, dep.version.length - 1)
Dependency(dep.groupId, dep.artifactId, "\${${mapped.get(property)}}", dep.optional, dep.scope)
Dependency(dep.groupId, dep.artifactId, dep.packaging, "\${${mapped.get(property)}}", dep.optional,
dep.scope)
} else {
dep
}

View file

@ -13,6 +13,7 @@ open public class Project(
@Directive open var buildDirectory: String? = KFiles.KOBALT_BUILD_DIR,
@Directive open var group: String? = null,
@Directive open var artifactId: String? = null,
@Directive open var packaging: String? = null,
@Directive open var dependencies: Dependencies? = null,
@Directive open var sourceSuffix : String = "",
@Directive open var compilerInfo : ICompilerInfo,

View file

@ -18,26 +18,26 @@ public class DepFactory @Inject constructor(val localRepo: LocalRepo,
if (id.startsWith(IClasspathDependency.PREFIX_FILE)) {
return FileDependency(id.substring(IClasspathDependency.PREFIX_FILE.length))
} else {
val c = id.split(":")
val mavenId = MavenId(id)
var version = mavenId.version
var packaging = mavenId.packaging
var repoResult: RepoFinder.RepoResult?
var version: String? = null
if (! MavenDependency.hasVersion(id)) {
if (localFirst) version = localRepo.findLocalVersion(c[0], c[1])
if (! mavenId.hasVersion) {
if (localFirst) version = localRepo.findLocalVersion(mavenId.groupId, mavenId.artifactId,
mavenId.packaging)
if (! localFirst || version == null) {
repoResult = repoFinder.findCorrectRepo(id)
if (!repoResult.found) {
throw KobaltException("Couldn't resolve ${id}")
throw KobaltException("Couldn't resolve $id")
} else {
version = repoResult.version
}
}
} else {
version = c[2]
}
return MavenDependency(c[0], c[1], version, executor, localRepo, repoFinder,
pomFactory, downloadManager)
return MavenDependency(mavenId.groupId, mavenId.artifactId, packaging, version!!,
executor, localRepo, repoFinder, pomFactory, downloadManager)
}
}
}

View file

@ -8,7 +8,7 @@ import java.util.concurrent.Future
interface IClasspathDependency {
companion object {
val PREFIX_FILE: String = "file:"
val PREFIX_FILE: String = "file:/"
}
/** Identifier for this dependency */

View file

@ -7,8 +7,8 @@ import java.util.concurrent.Future
import kotlin.properties.Delegates
open public class LocalDep(override val groupId: String, override val artifactId: String,
override val version: String,
open val localRepo: LocalRepo) : SimpleDep(groupId, artifactId, version) {
override val packaging: String?, override val version: String,
open val localRepo: LocalRepo) : SimpleDep(groupId, artifactId, packaging, version) {
fun toAbsoluteJarFilePath(v: String) = localRepo.toFullPath(toJarFile(v))

View file

@ -26,7 +26,7 @@ open public class LocalRepo(open val localRepo: String = KFiles.localRepo) {
/**
* If the dependency is local, return the correct version for it
*/
fun findLocalVersion(groupId: String, artifactId: String) : String? {
fun findLocalVersion(groupId: String, artifactId: String, packaging: String? = null) : String? {
// No version: look at all the directories under group/artifactId, pick the latest and see
// if it contains a maven and jar file
val dir = toFullPath(KFiles.joinDir(groupId.replace(".", File.separator), artifactId))
@ -41,7 +41,7 @@ open public class LocalRepo(open val localRepo: String = KFiles.localRepo) {
v2.compareTo(v1) // we want the most recent at position 0
})
val result = directories.get(0).name
val newDep = LocalDep(groupId, artifactId, result, this)
val newDep = LocalDep(groupId, artifactId, packaging, result, this)
if (existsPom(newDep, result) && existsJar(newDep, result)) {
return result
}

View file

@ -12,13 +12,14 @@ import kotlin.properties.Delegates
public class MavenDependency @Inject constructor(override @Assisted("groupId") val groupId : String,
override @Assisted("artifactId") val artifactId : String,
override @Assisted("packaging") val packaging: String?,
override @Assisted("version") val version : String,
val executor: ExecutorService,
override val localRepo: LocalRepo,
val repoFinder: RepoFinder,
val pomFactory: Pom.IFactory,
val downloadManager: DownloadManager)
: LocalDep(groupId, artifactId, version, localRepo), IClasspathDependency,
: LocalDep(groupId, artifactId, packaging, version, localRepo), IClasspathDependency,
Comparable<MavenDependency> {
override var jarFile: Future<File> by Delegates.notNull()
var pomFile: Future<File> by Delegates.notNull()
@ -30,7 +31,7 @@ public class MavenDependency @Inject constructor(override @Assisted("groupId") v
jarFile = CompletedFuture(jar)
pomFile = CompletedFuture(pom)
} else {
val repoResult = repoFinder.findCorrectRepo(toId(groupId, artifactId, version))
val repoResult = repoFinder.findCorrectRepo(toId(groupId, artifactId, packaging, version))
if (repoResult.found) {
jarFile =
if (repoResult.hasJar) {
@ -41,7 +42,7 @@ public class MavenDependency @Inject constructor(override @Assisted("groupId") v
pomFile = downloadManager.download(repoResult.repoUrl + toPomFile(repoResult), pom.absolutePath,
executor)
} else {
throw KobaltException("Couldn't resolve ${toId(groupId, artifactId, version)}")
throw KobaltException("Couldn't resolve ${toId(groupId, artifactId, packaging, version)}")
}
}
}
@ -61,18 +62,15 @@ public class MavenDependency @Inject constructor(override @Assisted("groupId") v
return depFactory.create(id, ex)
}
fun hasVersion(id: String) : Boolean {
val c = id.split(":")
return c.size == 3 && !Strings.isEmpty(c[2])
}
fun toId(g: String, a: String, v: String) = "$g:$a:$v"
fun toId(g: String, a: String, packaging: String?, v: String) =
if (packaging.isNullOrBlank()) "$g:$a:$v"
else "$g:$a:$packaging:$v"
}
public override fun toString() = toId(groupId, artifactId, version)
public override fun toString() = toId(groupId, artifactId, packaging, version)
override val id = toId(groupId, artifactId, version)
override val id = toId(groupId, artifactId, packaging, version)
override fun toMavenDependencies(): org.apache.maven.model.Dependency {
with(org.apache.maven.model.Dependency()) {
@ -94,7 +92,7 @@ public class MavenDependency @Inject constructor(override @Assisted("groupId") v
pomFactory.create(id, pomFile.get()).dependencies.filter {
it.mustDownload && it.isValid
}.forEach {
result.add(create(toId(it.groupId, it.artifactId, it.version)))
result.add(create(toId(it.groupId, it.artifactId, it.packaging, it.version)))
}
return result
}

View file

@ -0,0 +1,21 @@
package com.beust.kobalt.maven
public class MavenId(val id: String) {
lateinit var groupId: String
lateinit var artifactId: String
var packaging: String? = null
var version: String? = null
init {
val c = id.split(":")
if (c.size != 3 && c.size != 4) {
throw IllegalArgumentException("Illegal id: $id")
}
groupId = c[0]
artifactId = c[1]
packaging = if (c.size == 4) c[2] else null
version = if (c.size == 4) c[3] else c[2]
}
val hasVersion = version != null
}

View file

@ -16,6 +16,7 @@ public class Pom @javax.inject.Inject constructor(@Assisted val id: String,
val XPATH = XPATH_FACTORY.newXPath()
var groupId: String? = null
var artifactId: String? = null
var packaging: String? = null
var version: String? = null
var name: String? = null
var properties = sortedMapOf<String, String>()
@ -25,8 +26,8 @@ public class Pom @javax.inject.Inject constructor(@Assisted val id: String,
fun create(@Assisted id: String, @Assisted documentFile: java.io.File): Pom
}
data public class Dependency(val groupId: String, val artifactId: String, val version: String,
val optional: Boolean = false, val scope: String? = null) {
data public class Dependency(val groupId: String, val artifactId: String, val packaging: String?,
val version: String, val optional: Boolean = false, val scope: String? = null) {
/** When a variable is used in a maven file, e.g. ${version} */
private val VAR = "$" + "{"
@ -79,6 +80,7 @@ public class Pom @javax.inject.Inject constructor(@Assisted val id: String,
val d = deps.item(i) as NodeList
var groupId: String? = null
var artifactId: String? = null
var packaging: String? = null
var version: String = ""
var optional: Boolean? = false
var scope: String? = null
@ -88,6 +90,7 @@ public class Pom @javax.inject.Inject constructor(@Assisted val id: String,
when (e.tagName) {
"groupId" -> groupId = e.textContent
"artifactId" -> artifactId = e.textContent
"packaging" -> packaging = e.textContent
"version" -> version = e.textContent
"optional" -> optional = "true".equals(e.textContent, true)
"scope" -> scope = e.textContent
@ -95,7 +98,7 @@ public class Pom @javax.inject.Inject constructor(@Assisted val id: String,
}
}
log(3, "Done parsing: $groupId $artifactId $version")
val tmpDependency = Dependency(groupId!!, artifactId!!, version, optional!!, scope)
val tmpDependency = Dependency(groupId!!, artifactId!!, packaging, version, optional!!, scope)
dependencies.add(tmpDependency)
}
}

View file

@ -54,7 +54,8 @@ public class PomGenerator @Inject constructor(@Assisted val project: Project) {
val buildDir = com.beust.kobalt.misc.KFiles.makeDir(project.directory, project.buildDirectory!!)
val outputDir = com.beust.kobalt.misc.KFiles.makeDir(buildDir.path, "libs")
val pomFile = SimpleDep(project.group!!, project.artifactId!!, project.version!!).toPomFileName()
val pomFile = SimpleDep(project.group!!, project.artifactId!!, project.packaging, project.version!!)
.toPomFileName()
val outputFile = File(outputDir, pomFile)
outputFile.writeText(s.toString(), Charset.defaultCharset())
log(1, " Wrote $outputFile")

View file

@ -9,6 +9,9 @@ import com.google.common.cache.CacheBuilder
import com.google.common.cache.CacheLoader
import com.google.common.cache.LoadingCache
import java.io.File
import java.net.HttpURLConnection
import java.net.URI
import java.net.URL
import java.util.concurrent.Callable
import java.util.concurrent.ExecutorCompletionService
import java.util.concurrent.TimeUnit
@ -63,17 +66,34 @@ public class RepoFinder @Inject constructor(val http: Http, val executors: Kobal
}
}
private fun urlExists(url: String) : Boolean {
val connection = URL(url).openConnection()
val result =
if (connection is HttpURLConnection) {
connection.responseCode == 200
} else if (url.startsWith(IClasspathDependency.PREFIX_FILE)) {
val fileName = url.substring(IClasspathDependency.PREFIX_FILE.length)
File(fileName).exists()
} else {
false
}
return result
}
/**
* Execute a single HTTP request to one repo.
*/
inner class RepoFinderCallable(val id: String, val repoUrl: String) : Callable<RepoResult> {
override fun call(): RepoResult {
log(2, "Checking $repoUrl for $id")
val c = id.split(":")
if (! MavenDependency.hasVersion(id)) {
val ud = UnversionedDep(c[0], c[1])
val mavenId = MavenId(id)
val groupId = mavenId.groupId
val artifactId = mavenId.artifactId
var packaging = mavenId.packaging
val version = mavenId.version
if (! mavenId.hasVersion) {
val ud = UnversionedDep(groupId, artifactId)
val foundVersion = findCorrectVersionRelease(ud.toMetadataXmlPath(false), repoUrl)
if (foundVersion != null) {
return RepoResult(repoUrl, true, foundVersion)
@ -81,19 +101,25 @@ public class RepoFinder @Inject constructor(val http: Http, val executors: Kobal
return RepoResult(repoUrl, false, "")
}
} else {
if (c[2].contains("SNAPSHOT")) {
val dep = SimpleDep(c[0], c[1], c[2])
if (version!!.contains("SNAPSHOT")) {
val dep = SimpleDep(groupId, artifactId, packaging, version)
val snapshotVersion = findSnapshotVersion(dep.toMetadataXmlPath(false), repoUrl)
if (snapshotVersion != null) {
return RepoResult(repoUrl, true, c[2], true /* hasJar, potential bug here */, snapshotVersion)
return RepoResult(repoUrl, true, version, true /* hasJar, potential bug here */,
snapshotVersion)
} else {
return RepoResult(repoUrl, false, "")
}
} else {
val dep = SimpleDep(c[0], c[1], c[2])
val dep = SimpleDep(groupId, artifactId, packaging, version)
// Try to find the jar file
val urlJar = repoUrl + dep.toJarFile(dep.version)
val hasJar = http.get(urlJar).code == 200
if (repoUrl.contains("beust")) {
println("DONOTCOMMIT")
}
val hasJar = urlExists(urlJar)
val found =
if (! hasJar) {
@ -129,7 +155,7 @@ public class RepoFinder @Inject constructor(val http: Http, val executors: Kobal
}
})
} catch(ex: Exception) {
log(2, "Couldn't find metadata at $url")
log(2, "Couldn't find metadata at $url: ${ex.message}")
}
return null
}

View file

@ -1,14 +1,16 @@
package com.beust.kobalt.maven
import com.beust.kobalt.misc.Strings
import com.google.common.base.CharMatcher
import java.io.File
import kotlin.properties.Delegates
open public class SimpleDep(override val groupId: String, override val artifactId: String,
open val version: String) : UnversionedDep(groupId, artifactId) {
open val packaging: String?, open val version: String) : UnversionedDep(groupId, artifactId) {
companion object {
fun create(id: String) = id.split(":").let { SimpleDep(it[0], it[1], it[2])}
fun create(id: String) = MavenId(id).let {
if (id.contains("android")) {
println("DONOTCOMMIT")
}
SimpleDep(it.groupId, it.artifactId, it.packaging, it.version!!)
}
}
override public fun toMetadataXmlPath(fileSystem: Boolean): String {
@ -25,9 +27,14 @@ open public class SimpleDep(override val groupId: String, override val artifactI
fun toPomFile(r: RepoFinder.RepoResult) = toFile(r.version, r.snapshotVersion, ".pom")
fun toJarFile(v: String = version) = toFile(v, "", ".jar")
fun toJarFile(v: String = version) = toFile(v, "", suffix)
fun toJarFile(r: RepoFinder.RepoResult) = toFile(r.version, r.snapshotVersion, ".jar")
fun toJarFile(r: RepoFinder.RepoResult) = toFile(r.version, r.snapshotVersion, suffix)
fun toPomFileName() = "${artifactId}-${version}.pom"
fun toPomFileName() = "$artifactId-$version.pom"
val suffix : String
get() {
return if (packaging != null && ! packaging.isNullOrBlank()) ".${packaging!!}" else ".jar"
}
}

View file

@ -23,8 +23,11 @@ public class JavaProject(
@Directive
override var artifactId: String? = null,
@Directive
override var dependencies: Dependencies? = null)
: Project(name, version, directory, buildDirectory, group, artifactId, dependencies, ".java", JavaCompilerInfo()) {
override var dependencies: Dependencies? = null,
@Directive
override var packaging: String? = null)
: Project(name, version, directory, buildDirectory, group, artifactId, packaging, dependencies,
".java", JavaCompilerInfo()) {
override public fun toString() = ToString("JavaProject", "name", name!!).s
}

View file

@ -24,8 +24,10 @@ public class KotlinProject(
@Directive
override var artifactId: String? = name,
@Directive
override var dependencies: Dependencies? = null)
: Project(name, version, directory, buildDirectory, group, artifactId, dependencies, ".kt",
override var dependencies: Dependencies? = null,
@Directive
override var packaging: String? = null)
: Project(name, version, directory, buildDirectory, group, artifactId, packaging, dependencies, ".kt",
KotlinCompilerInfo()) {
override public fun toString() = ToString("KotlinProject", "name", name!!).s