Skip to content

Commit

Permalink
Merge pull request #133 from xerial/nested-proj2
Browse files Browse the repository at this point in the history
#132: Handle the context project appropriately
  • Loading branch information
xerial authored Jan 12, 2018
2 parents c6877ed + 197c98c commit 7320fe7
Show file tree
Hide file tree
Showing 11 changed files with 106 additions and 32 deletions.
60 changes: 34 additions & 26 deletions src/main/scala/xerial/sbt/pack/PackPlugin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -124,15 +124,15 @@ object PackPlugin extends AutoPlugin with PackArchive {
packGenerateWindowsBatFile := true,
packGenerateMakefile := true,
packMainDiscovered := Def.taskDyn {
val mainClasses = getFromSelectedProjects(discoveredMainClasses in Compile, state.value, packExclude.value)
val mainClasses = getFromSelectedProjects(thisProjectRef.value, discoveredMainClasses in Compile, state.value, packExclude.value)
Def.task { mainClasses.value.flatMap(_._1.map(mainClass => hyphenize(mainClass.split('.').last) -> mainClass).toMap).toMap }
}.value,
packAllUnmanagedJars := Def.taskDyn {
val allUnmanagedJars = getFromSelectedProjects(unmanagedJars in Runtime, state.value, packExclude.value)
val allUnmanagedJars = getFromSelectedProjects(thisProjectRef.value, unmanagedJars in Runtime, state.value, packExclude.value)
Def.task { allUnmanagedJars.value }
}.value,
packLibJars := Def.taskDyn {
val libJars = getFromSelectedProjects(packageBin in Runtime, state.value, packExcludeLibJars.value)
val libJars = getFromSelectedProjects(thisProjectRef.value, packageBin in Runtime, state.value, packExcludeLibJars.value)
Def.task { libJars.value }
}.value,
(mappings in pack) := Seq.empty,
Expand Down Expand Up @@ -208,9 +208,11 @@ object PackPlugin extends AutoPlugin with PackArchive {
},
pack := {
val out = streams.value
val logPrefix = "[" + name.value + "] "
val base: File = new File(".") // Using the working directory as base for readability

val distDir: File = packTargetDir.value / packDir.value
out.log.info("Creating a distributable package in " + rpath(baseDirectory.value, distDir))
out.log.info(logPrefix + "Creating a distributable package in " + rpath(base, distDir))
IO.delete(distDir)
distDir.mkdirs()

Expand All @@ -219,70 +221,74 @@ object PackPlugin extends AutoPlugin with PackArchive {
libDir.mkdirs()

// Copy project jars
val base: File = new File(".") // Using the working directory as base for readability
out.log.info("Copying libraries to " + rpath(base, libDir))
out.log.info(logPrefix + "Copying libraries to " + rpath(base, libDir))
val libs: Seq[File] = packLibJars.value.map(_._1)
out.log.info("project jars:\n" + libs.map(path => rpath(base, path)).mkString("\n"))
out.log.info(logPrefix + "project jars:\n" + libs.map(path => rpath(base, path)).mkString("\n"))
libs.foreach(l => IO.copyFile(l, libDir / l.getName))

// Copy dependent jars

val distinctDpJars = packModuleEntries.value
out.log.info("project dependencies:\n" + distinctDpJars.mkString("\n"))
out.log.info(logPrefix + "project dependencies:\n" + distinctDpJars.mkString("\n"))
val jarNameConvention = packJarNameConvention.value
for (m <- distinctDpJars) {
val targetFileName = resolveJarName(m, jarNameConvention)
IO.copyFile(m.file, libDir / targetFileName, true)
}

// Copy unmanaged jars in ${baseDir}/lib folder
out.log.info("unmanaged dependencies:")
out.log.info(logPrefix + "unmanaged dependencies:")
for ((m, projectRef) <- packAllUnmanagedJars.value; um <- m; f = um.data) {
out.log.info(f.getPath)
IO.copyFile(f, libDir / f.getName, true)
}

// Copy explicitly added dependencies
val mapped: Seq[(File, String)] = (mappings in pack).value
out.log.info("explicit dependencies:")
out.log.info(logPrefix + "explicit dependencies:")
for ((file, path) <- mapped) {
out.log.info(file.getPath)
IO.copyFile(file, distDir / path, true)
}

// Create target/pack/bin folder
val binDir = distDir / "bin"
out.log.info("Create a bin folder: " + rpath(base, binDir))
out.log.info(logPrefix + "Create a bin folder: " + rpath(base, binDir))
binDir.mkdirs()

def write(path: String, content: String) {
val p = distDir / path
out.log.info("Generating %s".format(rpath(base, p)))
out.log.info(logPrefix + "Generating %s".format(rpath(base, p)))
IO.write(p, content)
}

// Create launch scripts
out.log.info("Generating launch scripts")
out.log.info(logPrefix + "Generating launch scripts")
val mainTable: Map[String, String] = packMain.value
if (mainTable.isEmpty) {
out.log.warn("No mapping (program name) -> MainClass is defined. Please set packMain variable (Map[String, String]) in your sbt project settings.")
out.log.warn(logPrefix + "No mapping (program name) -> MainClass is defined. Please set packMain variable (Map[String, String]) in your sbt project settings.")
}

val progVersion = version.value
val macIconFile = packMacIconFile.value

// Check the current Git revision
val gitRevision: String = Try {
out.log.info("Checking the git revison of the current project")
sys.process.Process("git rev-parse HEAD").!!
if((base / ".git").exists()) {
out.log.info(logPrefix + "Checking the git revision of the current project")
sys.process.Process("git rev-parse HEAD").!!
}
else {
"unknown"
}
}.getOrElse("unknown").trim

val pathSeparator = "${PSEP}"
val expandedCp = packExpandedClasspath.value

// Render script via Scalate template
for ((name, mainClass) <- mainTable) {
out.log.info("main class for %s: %s".format(name, mainClass))
out.log.info(logPrefix + "main class for %s: %s".format(name, mainClass))
def extraClasspath(sep: String): String = packExtraClasspath.value.get(name).map(_.mkString("", sep, sep)).getOrElse("")
def expandedClasspath(sep: String): String = {
val projJars = libs.map(l => "${PROG_HOME}/lib/" + l.getName)
Expand Down Expand Up @@ -344,7 +350,7 @@ object PackPlugin extends AutoPlugin with PackArchive {
// Copy resources
val otherResourceDirs = packResourceDir.value
val binScriptsDir = otherResourceDirs.map(_._1 / "bin").filter(_.exists)
out.log.info(s"packed resource directories = ${otherResourceDirs.map(_._1).mkString(",")}")
out.log.info(logPrefix + s"packed resource directories = ${otherResourceDirs.map(_._1).mkString(",")}")

def linkToScript(name: String) =
"\t" + """ln -sf "../$(PROG)/current/bin/%s" "$(PREFIX)/bin/%s"""".format(name, name)
Expand Down Expand Up @@ -382,7 +388,7 @@ object PackPlugin extends AutoPlugin with PackArchive {
// chmod +x the scripts in bin directory
binDir.listFiles.foreach(_.setExecutable(true, false))

out.log.info("done.")
out.log.info(logPrefix + "done.")
distDir
},
packInstall := {
Expand All @@ -398,23 +404,25 @@ object PackPlugin extends AutoPlugin with PackArchive {
}
)

//private def getFromAllProjects[T](targetTask: taskKey[T])(currentProject: ProjectRef, structure: BuildStructure): Task[Seq[(T, ProjectRef)]] =
private def getFromAllProjects[T](targetTask: TaskKey[T], state: State): Task[Seq[(T, ProjectRef)]] =
getFromSelectedProjects(targetTask, state, Seq.empty)
private def getFromAllProjects[T](contextProject:ProjectRef, targetTask: TaskKey[T], state: State): Task[Seq[(T, ProjectRef)]] =
getFromSelectedProjects(contextProject, targetTask, state, Seq.empty)

//private def getFromSelectedProjects[T](targetTask: TaskKey[T])(currentProject: ProjectRef, structure: BuildStructure, exclude: Seq[String]): Task[Seq[(T, ProjectRef)]] = {
private def getFromSelectedProjects[T](targetTask: TaskKey[T], state: State, exclude: Seq[String]): Task[Seq[(T, ProjectRef)]] = {
private def getFromSelectedProjects[T](contextProject:ProjectRef, targetTask: TaskKey[T], state: State, exclude: Seq[String]): Task[Seq[(T, ProjectRef)]] = {
val extracted = Project.extract(state)
val structure = extracted.structure

def transitiveDependencies(currentProject: ProjectRef): Seq[ProjectRef] = {
def isExcluded(p: ProjectRef) = exclude.contains(p.project)

// Traverse all dependent projects
val children = Project.getProject(currentProject, structure).toSeq.flatMap(_.dependencies.map(_.project))
val children = Project
.getProject(currentProject, structure)
.toSeq
.flatMap{ _.dependencies.map(_.project) }

(currentProject +: (children flatMap (transitiveDependencies(_)))) filterNot (isExcluded)
}
val projects: Seq[ProjectRef] = transitiveDependencies(extracted.currentRef).distinct
val projects: Seq[ProjectRef] = transitiveDependencies(contextProject).distinct
projects.map(p => (Def.task { ((targetTask in p).value, p) }) evaluate structure.data).join
}

Expand Down
11 changes: 8 additions & 3 deletions src/sbt-test/sbt-pack/archive-modules/test
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,20 @@
$ exists target/archive-modules-0.1.tar.gz
$ exists target/archive-modules-0.1.zip
$ exists target/pack/bin/module1
$ exec sh ./target/pack/bin/module1
$ absent target/pack/bin/module2
$ exists target/pack/lib/xerial-core-3.3.6.jar
$ absent target/pack/lib/snappy-java-1.1.1.6.jar

$ exec sh ./target/pack/bin/module1
$ exists module2/target/module2-0.1.tar.gz
$ exists module2/target/module2-0.1.zip
$ absent module2/target/pack/bin/module2
$ absent target/pack/lib/snappy-java-1.1.1.6.jar
$ exists module2/target/pack/bin/module2
$ exists module2/target/pack/lib/snappy-java-1.1.1.6.jar

$ exists module1/target/module1-0.1.tar.gz
$ exists module1/target/module1-0.1.zip
$ exists module1/target/pack/bin/module1
$ exists module1/target/pack/lib/xerial-core-3.3.6.jar
$ exec sh ./module1/target/pack/bin/module1
> 'set packArchiveName := "my-archive"'
> packArchiveTgz
Expand Down
2 changes: 1 addition & 1 deletion src/sbt-test/sbt-pack/duplicate-jars/build.sbt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
scalaVersion in ThisBuild := "2.12.4"
val commonSettings = Defaults.coreDefaultSettings ++ Seq(
scalaVersion := "2.11.8",
version := "0.1",
crossPaths := false
)
Expand Down
4 changes: 2 additions & 2 deletions src/sbt-test/sbt-pack/duplicate-jars/test
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
> 'set version := "0.1"'
> pack
$ exists target/pack/lib/scala-library-2.11.8.jar
$ exists target/pack/lib/scala-library-2.12.4.jar
$ absent target/pack/lib/slf4j-api-1.7.2.jar
$ exists target/pack/lib/slf4j-api-1.7.6.jar
$ exists target/pack/lib/slf4j-api-1.7.6.jar
16 changes: 16 additions & 0 deletions src/sbt-test/sbt-pack/nested-project/build.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
name := "nested"

version in ThisBuild := "0.1"

scalaVersion in ThisBuild := "2.12.4"

enablePlugins(PackPlugin)

lazy val root = (project in file("."))
.dependsOn(module1, module2)

lazy val module1 = (project in file("modules/module1")).dependsOn(lib1)
lazy val module2 = (project in file("modules/module2")).dependsOn(lib2)

lazy val lib1 = project in file("libs/lib1")
lazy val lib2 = project in file("libs/lib2")
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package lib1

object Lib1 {
val value = 1
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package lib2

object Lib2 {
val value = 2
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package service

import lib1.Lib1

object Main {
def main(args: Array[String]): Unit = {
println(Lib1.value)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package service

import lib2.Lib2

object Main {
def main(args: Array[String]): Unit = {
println(Lib2.value)
}
}
1 change: 1 addition & 0 deletions src/sbt-test/sbt-pack/nested-project/project/plugins.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
addSbtPlugin("org.xerial.sbt" % "sbt-pack" % "0.10.1-SNAPSHOT")
16 changes: 16 additions & 0 deletions src/sbt-test/sbt-pack/nested-project/test
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
> 'set version := "0.1"'
> pack
$ exists target/pack/lib/module1_2.12-0.1.jar
$ exists target/pack/lib/module2_2.12-0.1.jar
$ exists target/pack/lib/lib1_2.12-0.1.jar
$ exists target/pack/lib/lib2_2.12-0.1.jar
$ absent modules/module1/target/pack/lib/module1_2.12-0.1.jar
$ absent modules/module1/target/pack/lib/module2_2.12-0.1.jar
$ absent modules/module1/target/pack/lib/lib1_2.12-0.1.jar
$ absent modules/module1/target/pack/lib/lib2_2.12-0.1.jar

> module1/pack
$ exists modules/module1/target/pack/lib/module1_2.12-0.1.jar
$ absent modules/module1/target/pack/lib/module2_2.12-0.1.jar
$ exists modules/module1/target/pack/lib/lib1_2.12-0.1.jar
$ absent modules/module1/target/pack/lib/lib2_2.12-0.1.jar

0 comments on commit 7320fe7

Please sign in to comment.