Skip to content

Commit

Permalink
fixes #1 - identify pom,jar,ivy artifacts locally
Browse files Browse the repository at this point in the history
  • Loading branch information
cessationoftime committed Oct 19, 2016
1 parent 4c7ca92 commit 9b97b2c
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 20 deletions.
2 changes: 1 addition & 1 deletion plugin/project/plugins.sbt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
libraryDependencies += "org.scala-sbt" % "scripted-plugin" % sbtVersion.value

addSbtPlugin("io.get-coursier" % "sbt-coursier" % "1.0.0-M13")
addSbtPlugin("io.get-coursier" % "sbt-coursier" % "1.0.0-M14")
Original file line number Diff line number Diff line change
Expand Up @@ -28,30 +28,72 @@ class CoursierArtifactFetcher(scalaVersion: String,
resolution.dependencyArtifacts.map { case ((dependency, artifact)) =>
val downloadedArtifact = Cache.file(artifact).run.run
val tpe = artifact.attributes.`type`

val moduleId = ToSbt.moduleId(dependency)
NixRepoModule(
module = ToSbt.moduleId(dependency),
artifacts = Seq(
buildArtifact(
tpe,
new URL(artifact.url),
downloadedArtifact.toOption,
artifact.authentication
),
// TODO: Cleaner way to get POM?
buildArtifact(
"pom",
new URL(artifact.url.replaceAll(s"\\.$tpe$$", ".pom")),
None,
artifact.authentication
)
)
module = moduleId,
artifacts =
downloadedArtifact.toOption.map(d=>findArtifacts(new URL(artifact.url),d, artifact.authentication)).toSeq.flatten
)
}
)
}

def findArtifacts(url: URL, localFile: File, auth: Option[Authentication]): Seq[NixRepoArtifact] = {

val authedUri = auth match {
case Some(a) => new URI(url.getProtocol, s"${a.user}:${a.password}", url.getHost, url.getPort, url.getPath, url.getQuery, url.getRef)
case None => url.toURI
}

val isIvy = localFile.getParentFile().getName() == "jars"

val localSearchLocation = if (isIvy) {
localFile.getParentFile().getParentFile()
} else {
localFile.getParentFile()
}

def parentURI(uri:URI) = if (uri.getPath().endsWith("/")) uri.resolve("..") else uri.resolve(".")

val calculatedParentURI = if (isIvy) {
parentURI(parentURI(authedUri))
} else {
parentURI(authedUri)
}

def calculateURI(f:File) = if (isIvy) {
calculatedParentURI.resolve(f.getParentFile().getName() + "/" + f.getName())
} else {
calculatedParentURI.resolve(f.getName())
}

def recursiveListFiles(f: File): Array[File] = {
val these = f.listFiles
these ++ these.filter(_.isDirectory).flatMap(recursiveListFiles)
}

val allArtifacts = recursiveListFiles(localSearchLocation)
val targetArtifacts = allArtifacts.filter(f => """.*(\.jar|\.pom|ivy.xml)$""".r.findFirstIn(f.getName).isDefined)

def getFileType(file:File) : String = {
val name = file.getName()
if (name == "ivy.xml") return "ivy"

name.substring(name.lastIndexOf(".") + 1);
}

targetArtifacts.map{ artifactLocalFile =>

val calcUrl = calculateURI(artifactLocalFile).toURL

NixRepoArtifact(
getFileType(artifactLocalFile),
calcUrl,
fetchChecksum(artifactLocalFile.toURI.toURL)
)}.toSeq
}

def buildArtifact(`type`: String, url: URL, localFile: Option[File], auth: Option[Authentication]): NixRepoArtifact = {
def buildPrimaryArtifact(`type`: String, url: URL, localFile: Option[File], auth: Option[Authentication]): NixRepoArtifact = {
val authedUrl = auth match {
case Some(a) => new URI(url.getProtocol, s"${a.user}:${a.password}", url.getHost, url.getPort, url.getPath, url.getQuery, url.getRef).toURL
case None => url
Expand Down
6 changes: 5 additions & 1 deletion plugin/src/sbt-test/sbtix/simple/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,8 @@ lazy val one = project.settings(
lazy val two = project
lazy val three = project.dependsOn(one).enablePlugins(JavaAppPackaging)

lazy val root = project.in(file(".")).aggregate(one, two, three)
lazy val root = project.in(file(".")).aggregate(one, two, three)

//resolvers += Resolver.typesafeIvyRepo("releases")

//resolvers += Resolver.sbtPluginRepo("releases")
4 changes: 4 additions & 0 deletions plugin/src/sbt-test/sbtix/simple/project/plugins.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,8 @@ if (sys.props.contains("plugin.version")) {
Seq()
}

resolvers += Resolver.typesafeIvyRepo("releases")

resolvers += Resolver.sbtPluginRepo("releases")

addSbtPlugin("com.typesafe.sbt" % "sbt-native-packager" % "1.1.4")

0 comments on commit 9b97b2c

Please sign in to comment.