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>) = private fun updateVersion(dep: Dependency, mapped: Map<String, String>) =
if ( dep.version.startsWith("\${")) { if ( dep.version.startsWith("\${")) {
val property = dep.version.substring(2, dep.version.length - 1) 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 { } else {
dep dep
} }

View file

@ -13,6 +13,7 @@ open public class Project(
@Directive open var buildDirectory: String? = KFiles.KOBALT_BUILD_DIR, @Directive open var buildDirectory: String? = KFiles.KOBALT_BUILD_DIR,
@Directive open var group: String? = null, @Directive open var group: String? = null,
@Directive open var artifactId: String? = null, @Directive open var artifactId: String? = null,
@Directive open var packaging: String? = null,
@Directive open var dependencies: Dependencies? = null, @Directive open var dependencies: Dependencies? = null,
@Directive open var sourceSuffix : String = "", @Directive open var sourceSuffix : String = "",
@Directive open var compilerInfo : ICompilerInfo, @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)) { if (id.startsWith(IClasspathDependency.PREFIX_FILE)) {
return FileDependency(id.substring(IClasspathDependency.PREFIX_FILE.length)) return FileDependency(id.substring(IClasspathDependency.PREFIX_FILE.length))
} else { } else {
val c = id.split(":") val mavenId = MavenId(id)
var version = mavenId.version
var packaging = mavenId.packaging
var repoResult: RepoFinder.RepoResult? var repoResult: RepoFinder.RepoResult?
var version: String? = null
if (! MavenDependency.hasVersion(id)) { if (! mavenId.hasVersion) {
if (localFirst) version = localRepo.findLocalVersion(c[0], c[1]) if (localFirst) version = localRepo.findLocalVersion(mavenId.groupId, mavenId.artifactId,
mavenId.packaging)
if (! localFirst || version == null) { if (! localFirst || version == null) {
repoResult = repoFinder.findCorrectRepo(id) repoResult = repoFinder.findCorrectRepo(id)
if (!repoResult.found) { if (!repoResult.found) {
throw KobaltException("Couldn't resolve ${id}") throw KobaltException("Couldn't resolve $id")
} else { } else {
version = repoResult.version version = repoResult.version
} }
} }
} else {
version = c[2]
} }
return MavenDependency(c[0], c[1], version, executor, localRepo, repoFinder, return MavenDependency(mavenId.groupId, mavenId.artifactId, packaging, version!!,
pomFactory, downloadManager) executor, localRepo, repoFinder, pomFactory, downloadManager)
} }
} }
} }

View file

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

View file

@ -7,8 +7,8 @@ import java.util.concurrent.Future
import kotlin.properties.Delegates import kotlin.properties.Delegates
open public class LocalDep(override val groupId: String, override val artifactId: String, open public class LocalDep(override val groupId: String, override val artifactId: String,
override val version: String, override val packaging: String?, override val version: String,
open val localRepo: LocalRepo) : SimpleDep(groupId, artifactId, version) { open val localRepo: LocalRepo) : SimpleDep(groupId, artifactId, packaging, version) {
fun toAbsoluteJarFilePath(v: String) = localRepo.toFullPath(toJarFile(v)) 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 * 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 // No version: look at all the directories under group/artifactId, pick the latest and see
// if it contains a maven and jar file // if it contains a maven and jar file
val dir = toFullPath(KFiles.joinDir(groupId.replace(".", File.separator), artifactId)) 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 v2.compareTo(v1) // we want the most recent at position 0
}) })
val result = directories.get(0).name 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)) { if (existsPom(newDep, result) && existsJar(newDep, result)) {
return result return result
} }

View file

@ -12,13 +12,14 @@ import kotlin.properties.Delegates
public class MavenDependency @Inject constructor(override @Assisted("groupId") val groupId : String, public class MavenDependency @Inject constructor(override @Assisted("groupId") val groupId : String,
override @Assisted("artifactId") val artifactId : String, override @Assisted("artifactId") val artifactId : String,
override @Assisted("packaging") val packaging: String?,
override @Assisted("version") val version : String, override @Assisted("version") val version : String,
val executor: ExecutorService, val executor: ExecutorService,
override val localRepo: LocalRepo, override val localRepo: LocalRepo,
val repoFinder: RepoFinder, val repoFinder: RepoFinder,
val pomFactory: Pom.IFactory, val pomFactory: Pom.IFactory,
val downloadManager: DownloadManager) val downloadManager: DownloadManager)
: LocalDep(groupId, artifactId, version, localRepo), IClasspathDependency, : LocalDep(groupId, artifactId, packaging, version, localRepo), IClasspathDependency,
Comparable<MavenDependency> { Comparable<MavenDependency> {
override var jarFile: Future<File> by Delegates.notNull() override var jarFile: Future<File> by Delegates.notNull()
var pomFile: 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) jarFile = CompletedFuture(jar)
pomFile = CompletedFuture(pom) pomFile = CompletedFuture(pom)
} else { } else {
val repoResult = repoFinder.findCorrectRepo(toId(groupId, artifactId, version)) val repoResult = repoFinder.findCorrectRepo(toId(groupId, artifactId, packaging, version))
if (repoResult.found) { if (repoResult.found) {
jarFile = jarFile =
if (repoResult.hasJar) { 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, pomFile = downloadManager.download(repoResult.repoUrl + toPomFile(repoResult), pom.absolutePath,
executor) executor)
} else { } 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) return depFactory.create(id, ex)
} }
fun hasVersion(id: String) : Boolean { fun toId(g: String, a: String, packaging: String?, v: String) =
val c = id.split(":") if (packaging.isNullOrBlank()) "$g:$a:$v"
return c.size == 3 && !Strings.isEmpty(c[2]) else "$g:$a:$packaging:$v"
}
fun toId(g: String, a: String, v: String) = "$g:$a:$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 { override fun toMavenDependencies(): org.apache.maven.model.Dependency {
with(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 { pomFactory.create(id, pomFile.get()).dependencies.filter {
it.mustDownload && it.isValid it.mustDownload && it.isValid
}.forEach { }.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 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() val XPATH = XPATH_FACTORY.newXPath()
var groupId: String? = null var groupId: String? = null
var artifactId: String? = null var artifactId: String? = null
var packaging: String? = null
var version: String? = null var version: String? = null
var name: String? = null var name: String? = null
var properties = sortedMapOf<String, String>() 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 fun create(@Assisted id: String, @Assisted documentFile: java.io.File): Pom
} }
data public class Dependency(val groupId: String, val artifactId: String, val version: String, data public class Dependency(val groupId: String, val artifactId: String, val packaging: String?,
val optional: Boolean = false, val scope: String? = null) { val version: String, val optional: Boolean = false, val scope: String? = null) {
/** When a variable is used in a maven file, e.g. ${version} */ /** When a variable is used in a maven file, e.g. ${version} */
private val VAR = "$" + "{" 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 val d = deps.item(i) as NodeList
var groupId: String? = null var groupId: String? = null
var artifactId: String? = null var artifactId: String? = null
var packaging: String? = null
var version: String = "" var version: String = ""
var optional: Boolean? = false var optional: Boolean? = false
var scope: String? = null var scope: String? = null
@ -88,6 +90,7 @@ public class Pom @javax.inject.Inject constructor(@Assisted val id: String,
when (e.tagName) { when (e.tagName) {
"groupId" -> groupId = e.textContent "groupId" -> groupId = e.textContent
"artifactId" -> artifactId = e.textContent "artifactId" -> artifactId = e.textContent
"packaging" -> packaging = e.textContent
"version" -> version = e.textContent "version" -> version = e.textContent
"optional" -> optional = "true".equals(e.textContent, true) "optional" -> optional = "true".equals(e.textContent, true)
"scope" -> scope = e.textContent "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") 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) 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 buildDir = com.beust.kobalt.misc.KFiles.makeDir(project.directory, project.buildDirectory!!)
val outputDir = com.beust.kobalt.misc.KFiles.makeDir(buildDir.path, "libs") 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) val outputFile = File(outputDir, pomFile)
outputFile.writeText(s.toString(), Charset.defaultCharset()) outputFile.writeText(s.toString(), Charset.defaultCharset())
log(1, " Wrote $outputFile") 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.CacheLoader
import com.google.common.cache.LoadingCache import com.google.common.cache.LoadingCache
import java.io.File 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.Callable
import java.util.concurrent.ExecutorCompletionService import java.util.concurrent.ExecutorCompletionService
import java.util.concurrent.TimeUnit 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. * Execute a single HTTP request to one repo.
*/ */
inner class RepoFinderCallable(val id: String, val repoUrl: String) : Callable<RepoResult> { inner class RepoFinderCallable(val id: String, val repoUrl: String) : Callable<RepoResult> {
override fun call(): RepoResult { override fun call(): RepoResult {
log(2, "Checking $repoUrl for $id") log(2, "Checking $repoUrl for $id")
val c = id.split(":") val mavenId = MavenId(id)
if (! MavenDependency.hasVersion(id)) { val groupId = mavenId.groupId
val ud = UnversionedDep(c[0], c[1]) 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) val foundVersion = findCorrectVersionRelease(ud.toMetadataXmlPath(false), repoUrl)
if (foundVersion != null) { if (foundVersion != null) {
return RepoResult(repoUrl, true, foundVersion) return RepoResult(repoUrl, true, foundVersion)
@ -81,19 +101,25 @@ public class RepoFinder @Inject constructor(val http: Http, val executors: Kobal
return RepoResult(repoUrl, false, "") return RepoResult(repoUrl, false, "")
} }
} else { } else {
if (c[2].contains("SNAPSHOT")) { if (version!!.contains("SNAPSHOT")) {
val dep = SimpleDep(c[0], c[1], c[2]) val dep = SimpleDep(groupId, artifactId, packaging, version)
val snapshotVersion = findSnapshotVersion(dep.toMetadataXmlPath(false), repoUrl) val snapshotVersion = findSnapshotVersion(dep.toMetadataXmlPath(false), repoUrl)
if (snapshotVersion != null) { 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 { } else {
return RepoResult(repoUrl, false, "") return RepoResult(repoUrl, false, "")
} }
} else { } else {
val dep = SimpleDep(c[0], c[1], c[2]) val dep = SimpleDep(groupId, artifactId, packaging, version)
// Try to find the jar file // Try to find the jar file
val urlJar = repoUrl + dep.toJarFile(dep.version) 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 = val found =
if (! hasJar) { if (! hasJar) {
@ -129,7 +155,7 @@ public class RepoFinder @Inject constructor(val http: Http, val executors: Kobal
} }
}) })
} catch(ex: Exception) { } catch(ex: Exception) {
log(2, "Couldn't find metadata at $url") log(2, "Couldn't find metadata at $url: ${ex.message}")
} }
return null return null
} }

View file

@ -1,14 +1,16 @@
package com.beust.kobalt.maven package com.beust.kobalt.maven
import com.beust.kobalt.misc.Strings 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 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 { 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 { 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 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 @Directive
override var artifactId: String? = null, override var artifactId: String? = null,
@Directive @Directive
override var dependencies: Dependencies? = null) override var dependencies: Dependencies? = null,
: Project(name, version, directory, buildDirectory, group, artifactId, dependencies, ".java", JavaCompilerInfo()) { @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 override public fun toString() = ToString("JavaProject", "name", name!!).s
} }

View file

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