Skip to content

Commit

Permalink
Bloop: Fix output path of submodules
Browse files Browse the repository at this point in the history
Currently, the output path of a submodule includes the relative path
to its build file, although it should only be relative to the root
project's build path.

Another limitation that these changes address is that the output
folder was not created by Seed such that Bloop might fail to link
the project.
  • Loading branch information
tindzk committed Jul 25, 2019
1 parent 37b376c commit c28e5ae
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 9 deletions.
31 changes: 23 additions & 8 deletions src/main/scala/seed/generation/Bloop.scala
Original file line number Diff line number Diff line change
Expand Up @@ -359,11 +359,13 @@ object Bloop {
}

def moduleOutputPath(buildPath: Path,
module: Option[Module],
module: Module,
defaultName: String): Path =
module.flatMap(_.output) match {
case Some(p) if p.isAbsolute => p
case Some(p) => buildPath.toAbsolutePath.resolve(p).normalize()
module.output match {
case Some(p) if Paths.get(p).isAbsolute => Paths.get(p)
case Some(p) =>
val base = buildPath.toAbsolutePath.resolve(p).normalize()
if (!p.endsWith("/")) base else base.resolve(defaultName)
case None => buildPath.toAbsolutePath.resolve(defaultName)
}

Expand All @@ -380,12 +382,25 @@ object Bloop {
): Unit = {
val isCrossBuild = module.targets.toSet.size > 1

val jsOutputPath = moduleOutputPath(buildPath, module.js, name + ".js")
val jsOutputPath = module.js
.orElse(if (!module.targets.contains(JavaScript)) None else Some(Module()))
.map(js => moduleOutputPath(buildPath, js, name + ".js"))

val nativeOutputPath =
moduleOutputPath(buildPath, module.native, name + ".run")
module.native
.orElse(if (!module.targets.contains(Native)) None else Some(Module()))
.map(native => moduleOutputPath(buildPath, native, name + ".run"))

jsOutputPath.foreach { path =>
if (!Files.exists(path.getParent)) Files.createDirectories(path.getParent)
}

nativeOutputPath.foreach { path =>
if (!Files.exists(path.getParent)) Files.createDirectories(path.getParent)
}

writeJsModule(build, if (!isCrossBuild) name else name + "-js",
projectPath, bloopPath, bloopBuildPath, Some(jsOutputPath),
projectPath, bloopPath, bloopBuildPath, jsOutputPath,
module.copy(scalaDeps = collectJsDeps(build, module)),
collectJsClassPath(bloopBuildPath, build, module),
module.js, build.project, resolution, compilerResolution,
Expand All @@ -402,7 +417,7 @@ object Bloop {
module.jvm, build.project, resolution, compilerResolution, test = false,
log)
writeNativeModule(build, if (!isCrossBuild) name else name + "-native",
projectPath, bloopPath, bloopBuildPath, Some(nativeOutputPath),
projectPath, bloopPath, bloopBuildPath, nativeOutputPath,
module.copy(scalaDeps = collectNativeDeps(build, module)),
collectJvmClassPath(bloopBuildPath, build, module),
module.native, build.project, resolution, compilerResolution, test = false,
Expand Down
5 changes: 4 additions & 1 deletion src/main/scala/seed/model/Build.scala
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,10 @@ object Build {
moduleDeps: List[String] = List(),
mainClass: Option[String] = None,
targets: List[Platform] = List(),
output: Option[Path] = None,

// If this was a Path, it would include the directory
// relative to the build file.
output: Option[String] = None,

// JavaScript
jsdom: Boolean = false,
Expand Down
18 changes: 18 additions & 0 deletions src/test/scala/seed/generation/BloopIntegrationSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,24 @@ object BloopIntegrationSpec extends TestSuite[Unit] {
compileAndRun(buildPath)
}

testAsync("Link JavaScript modules with custom target path") { _ =>
val BuildConfig.Result(build, projectPath, _) = BuildConfig.load(
Paths.get("test/submodule-output-path"), Log.urgent).get
val buildPath = tempPath.resolve("submodule-output-path")
Files.createDirectory(buildPath)
val packageConfig = PackageConfig(tmpfs = false, silent = false,
ivyPath = None, cachePath = None)
cli.Generate.ui(Config(), projectPath, buildPath, build,
Command.Bloop(packageConfig), Log.urgent)
TestProcessHelper.runBloop(buildPath)("run", "app", "base", "base2")
.map { x =>
assertEquals(x.split("\n").count(_ == "hello"), 3)
assert(Files.exists(buildPath.resolve("build").resolve("js").resolve("app.js")))
assert(Files.exists(buildPath.resolve("build").resolve("js").resolve("base.js")))
assert(Files.exists(buildPath.resolve("build").resolve("base2.js")))
}
}

testAsync("Build project with overridden compiler plug-in version") { _ =>
val projectPath = Paths.get("test/example-paradise-versions")
val BuildConfig.Result(build, _, _) =
Expand Down
10 changes: 10 additions & 0 deletions test/submodule-output-path/build.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import = ["submodule"]

[project]
scalaVersion = "2.13.0"
scalaJsVersion = "0.6.28"

[module.app.js]
moduleDeps = ["base"]
sources = ["src"]
output = "js/app.js"
1 change: 1 addition & 0 deletions test/submodule-output-path/src/Main.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
object Main extends App { println("hello") }
10 changes: 10 additions & 0 deletions test/submodule-output-path/submodule/build.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[project]
scalaVersion = "2.13.0"
scalaJsVersion = "0.6.28"

[module.base.js]
sources = ["src"]
output = "js/"

[module.base2.js]
sources = ["src"]
1 change: 1 addition & 0 deletions test/submodule-output-path/submodule/src/Main.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
object Main extends App { println("hello") }

0 comments on commit c28e5ae

Please sign in to comment.