Skip to content

Commit

Permalink
Export resources as unmanaged resources directories in sbt
Browse files Browse the repository at this point in the history
  • Loading branch information
lwronski committed Dec 29, 2021
1 parent c74e28f commit 192b8c4
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 21 deletions.
59 changes: 39 additions & 20 deletions modules/cli/src/main/scala/scala/cli/export/Sbt.scala
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,20 @@ final case class Sbt(
)
}

val resourceDirPaths =
options.classPathOptions.resourcesDir ++ options.classPathOptions.resourcesVirtualDir

val customResourceSettings =
if (resourceDirPaths.isEmpty) Nil
else {
val resources = resourceDirPaths.map(p => s"""file("$p")""")
Seq(
s"""Compile / unmanagedResourceDirectories ++= Seq(${resources.mkString(", ")})"""
)
}

SbtProject(
settings = Seq(customCompileOnlyJarsSettings, customJarsSettings)
settings = Seq(customCompileOnlyJarsSettings, customJarsSettings, customResourceSettings)
)
}

Expand Down Expand Up @@ -235,7 +247,7 @@ final case class Sbt(
)
}

private def dependencySettings(options: BuildOptions): SbtProject = {
private def dependencySettings(options: BuildOptions, scope: Scope): SbtProject = {

val depSettings = {
val depStrings = options.classPathOptions
Expand All @@ -255,12 +267,13 @@ final case class Sbt(
if (s.fullCrossVersion.getOrElse(false)) Some(".cross(CrossVersion.full)")
else None
val sep =
if (s.platform.getOrElse(false)) "%%%"
else "%%"
if (s.platform.getOrElse(false)) "%%"
else "%"
(sep, suffixOpt0)
}
val scope0 = if (scope == Scope.Test) "% Test" else ""

val baseDep = s"""$q$org$q $sep $q$name$q % $q$ver$q"""
val baseDep = s"""$q$org$q $sep $q$name$q % $q$ver$q $scope0"""
suffixOpt.fold(baseDep)(suffix => s"($baseDep)$suffix")
}

Expand All @@ -287,31 +300,37 @@ final case class Sbt(
)
}

def export(options: BuildOptions, sources: Sources): SbtProject = {
def export(
optionsMain: BuildOptions,
optionsTest: BuildOptions,
sourcesMain: Sources,
sourcesTest: Sources
): SbtProject = {

// TODO Handle Scala CLI cross-builds

val projectChunks = Seq(
SbtProject(settings = Seq(extraSettings)),
mainSources(sources),
sources(sourcesMain, sourcesTest),
sbtVersionProject,
scalaVersionSettings(options),
scalacOptionsSettings(options),
mainClassSettings(options),
pureJavaSettings(options, sources),
javaOptionsSettings(options),
if (options.platform.value == Platform.JS)
scalaJsSettings(options.scalaJsOptions)
scalaVersionSettings(optionsMain),
scalacOptionsSettings(optionsMain),
mainClassSettings(optionsMain),
pureJavaSettings(optionsMain, sourcesMain),
javaOptionsSettings(optionsMain),
if (optionsMain.platform.value == Platform.JS)
scalaJsSettings(optionsMain.scalaJsOptions)
else
SbtProject(),
if (options.platform.value == Platform.Native)
scalaNativeSettings(options.scalaNativeOptions)
if (optionsMain.platform.value == Platform.Native)
scalaNativeSettings(optionsMain.scalaNativeOptions)
else
SbtProject(),
customJarsSettings(options),
testFrameworkSettings(options),
repositorySettings(options),
dependencySettings(options)
customJarsSettings(optionsMain),
testFrameworkSettings(optionsMain),
repositorySettings(optionsMain),
dependencySettings(optionsMain, Scope.Main),
dependencySettings(optionsTest, Scope.Test)
)

projectChunks.foldLeft(SbtProject())(_ + _)
Expand Down
3 changes: 2 additions & 1 deletion modules/cli/src/main/scala/scala/cli/export/SbtProject.scala
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ final case class SbtProject(

val buildSbtImportsContent = imports.map(_ + nl).mkString
val buildSbtSettingsContent = settings
.filter(_.nonEmpty)
.map { settings0 =>
settings0.map(s => s + nl).mkString + nl
}
Expand All @@ -53,7 +54,7 @@ final case class SbtProject(
}
for ((path, language, content) <- testSources) {
val path0 = dir / "src" / "test" / language / path
os.write(path0, content)
os.write(path0, content, createFolders = true)
}
}
}
Expand Down

0 comments on commit 192b8c4

Please sign in to comment.