Skip to content

Commit

Permalink
fix: PathRef in universalMappings
Browse files Browse the repository at this point in the history
  • Loading branch information
hoangmaihuy committed Aug 6, 2024
1 parent b10a731 commit ce4a6be
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,20 @@ trait JavaAppPackagingModule extends UniversalPackagerModule with BashStartScrip

def bundledJvmLocation: T[Option[String]] = T { Option.empty[String] }

private def moduleDepMappings: T[Seq[(os.Path, os.SubPath)]] = T.traverse(transitiveModuleDeps.distinct) { module =>
private def moduleDepMappings: T[Seq[(PathRef, os.SubPath)]] = T.traverse(transitiveModuleDeps.distinct) { module =>
T.task {
module.jar().path -> os.sub / "lib" / (module.artifactName() + ".jar")
module.jar() -> os.sub / "lib" / (module.artifactName() + ".jar")
}
}

private def ivyDepMappings: T[Seq[(os.Path, os.SubPath)]] = T {
private def ivyDepMappings: T[Seq[(PathRef, os.SubPath)]] = T {
resolvedRunIvyDeps().toSeq.map { ivyDep =>
ivyDep.path -> (os.sub / "lib" / ivyDep.path.last)
ivyDep -> (os.sub / "lib" / ivyDep.path.last)
}
}

/** The order of the classpath used at runtime for the bat/bash scripts. */
def classpathMappings: T[Seq[(os.Path, os.SubPath)]] = T {
def classpathMappings: T[Seq[(PathRef, os.SubPath)]] = T {
ivyDepMappings() ++ moduleDepMappings()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ trait BashStartScriptModule extends Module with CommonStartScriptGenerator { sel

def bashScriptEnvConfigLocation: T[Option[String]] = T { Option.empty[String] }

def bashScriptMappings: T[Seq[(os.Path, os.SubPath)]] = T {
def bashScriptMappings: T[Seq[(PathRef, os.SubPath)]] = T {
val discoveredMainClasses = zincWorker().worker().discoverMainClasses(compile())
generateStartScripts(
BashScriptConfig(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ package io.github.hoangmaihuy.mill.packager.archetypes.scripts
import java.io.File
import java.net.URL

import mill.api.Logger

import mill.api._
import io.github.hoangmaihuy.mill.packager.archetypes.TemplateWriter

trait CommonStartScriptGenerator {
Expand Down Expand Up @@ -61,7 +60,7 @@ trait CommonStartScriptGenerator {
discoveredMainClasses: Seq[String],
targetDir: os.Path,
log: Logger
): Seq[(os.Path, os.SubPath)] =
): Seq[(PathRef, os.SubPath)] =
StartScriptMainClassConfig.from(mainClass, discoveredMainClasses) match {
case NoMain =>
log.error("You have no main class in your project. No start script will be generated.")
Expand Down Expand Up @@ -103,7 +102,7 @@ trait CommonStartScriptGenerator {
config: SpecializedScriptConfig,
targetDir: os.Path,
log: Logger
): Seq[(os.Path, os.SubPath)] = {
): Seq[(PathRef, os.SubPath)] = {
val classAndScriptNames = ScriptUtils.createScriptNames(discoveredMainClasses)
ScriptUtils.warnOnScriptNameCollision(classAndScriptNames, log)

Expand Down Expand Up @@ -143,7 +142,7 @@ trait CommonStartScriptGenerator {
config: SpecializedScriptConfig,
targetDir: os.Path,
mainClasses: Seq[String]
): (os.Path, os.SubPath) = {
): (PathRef, os.SubPath) = {
val template = resolveTemplate(config.templateLocation)
val replacements = createReplacementsForMainScript(
mainClass,
Expand All @@ -162,7 +161,7 @@ trait CommonStartScriptGenerator {
os.write.over(script, scriptContent, createFolders = true)
// TODO - Better control over this!
script.toIO.setExecutable(executableBitValue)
script -> os.sub / scriptTargetFolder / scriptNameWithSuffix
PathRef(script) -> os.sub / scriptTargetFolder / scriptNameWithSuffix
}

private[this] def resolveTemplate(defaultTemplateLocation: File): URL = {
Expand All @@ -175,7 +174,7 @@ trait CommonStartScriptGenerator {
targetDir: os.Path,
config: ScriptConfig,
log: Logger
): Seq[(os.Path, os.SubPath)] = {
): Seq[(PathRef, os.SubPath)] = {
val tmp = targetDir / scriptTargetFolder
val forwarderTemplate = getClass.getResource(s"/packager/scripts/$forwarderTemplateName")
val classAndScriptNames = ScriptUtils.createScriptNames(discoveredMainClasses)
Expand All @@ -194,7 +193,7 @@ trait CommonStartScriptGenerator {

os.write.over(file, scriptContent, createFolders = true)
file.toIO.setExecutable(executableBitValue)
file -> os.sub / scriptTargetFolder / scriptName
PathRef(file) -> os.sub / scriptTargetFolder / scriptName
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,26 +29,21 @@ trait UniversalPackagerModule extends PackagerModule {

def universalSources: T[PathRef] = T.source(millSourcePath / "universal")

def universalMappings: T[Seq[(os.Path, os.SubPath)]] = T {
def universalMappings: T[Seq[(PathRef, os.SubPath)]] = T {
val sourceDirectory = universalSources().path
if (os.exists(sourceDirectory)) {
os
.walk(sourceDirectory)
.map { source =>
source -> source.subRelativeTo(sourceDirectory)
PathRef(source) -> source.subRelativeTo(sourceDirectory)
}
} else {
Seq.empty
}
}

private def universalMappingSources = T.sources(
universalMappings().map(mapping => PathRef(mapping._1))
)

// mappings with or without the configured topLevelDirectory
private def universalPackageMappings: T[Seq[(os.Path, os.SubPath)]] = T {
universalMappingSources()
private def universalPackageMappings: T[Seq[(PathRef, os.SubPath)]] = T {
val mappings = universalMappings()
topLevelDirectory().map { dir =>
mappings.map { case (f, p) => f -> (os.sub / dir / p) }
Expand Down Expand Up @@ -84,7 +79,7 @@ trait UniversalPackagerModule extends PackagerModule {
os.remove.all(T.dest)
universalMappings().foreach { case (f, p) =>
os.copy(
from = f,
from = f.path,
to = Path(p, T.dest),
createFolders = true,
followLinks = true,
Expand Down Expand Up @@ -164,7 +159,7 @@ trait UniversalPackagerModule extends PackagerModule {
*/
def universalPackageZip: T[PathRef] = T {
val zip = T.dest / (packageName() + ".zip")
ZipHelper.zip(universalPackageMappings(), zip)
ZipHelper.zip(universalPackageMappings().map(m => m._1.path -> m._2), zip)
T.log.info(s"Generated package: $zip")
PathRef(zip)
}
Expand All @@ -173,7 +168,7 @@ trait UniversalPackagerModule extends PackagerModule {
*/
def universalPackageTarZstd: T[PathRef] = T {
val out: Path = T.dest / (packageName() + universalTarZstdExt())
Archiver().mkTarball(universalMappings(), out, universalTopLevelPath())(
Archiver().mkTarball(universalPackageMappings().map(m => m._1.path -> m._2), out, universalTopLevelPath())(
new ZstdCompressorOutputStream(_, universalZstdCompressLevel())
)
T.log.info(s"Generated package: $out")
Expand All @@ -186,7 +181,7 @@ trait UniversalPackagerModule extends PackagerModule {
val out: Path = T.dest / (packageName() + universalTarGZExt())
val parameter = new GzipParameters()
parameter.setCompressionLevel(universalGzipCompressLevel())
Archiver().mkTarball(universalMappings(), out, universalTopLevelPath())(new GzipCompressorOutputStream(_, parameter))
Archiver().mkTarball(universalPackageMappings().map(m => m._1.path -> m._2), out, universalTopLevelPath())(new GzipCompressorOutputStream(_, parameter))
T.log.info(s"Generated package: $out")
PathRef(out)
}
Expand All @@ -195,15 +190,15 @@ trait UniversalPackagerModule extends PackagerModule {
*/
def universalPackageTarBzip2: T[PathRef] = T {
val out: Path = T.dest / (packageName() + universalTarBzip2Ext())
Archiver().mkTarball(universalMappings(), out, universalTopLevelPath())(new BZip2CompressorOutputStream(_))
Archiver().mkTarball(universalPackageMappings().map(m => m._1.path -> m._2), out, universalTopLevelPath())(new BZip2CompressorOutputStream(_))
PathRef(out)
}

/** Create a .tar.xz package file.
*/
def universalPackageTarXz: T[PathRef] = T {
val out: Path = T.dest / (packageName() + universalTarXZExt())
Archiver().mkTarball(universalMappings(), out, universalTopLevelPath())(new XZCompressorOutputStream(_))
Archiver().mkTarball(universalPackageMappings().map(m => m._1.path -> m._2), out, universalTopLevelPath())(new XZCompressorOutputStream(_))
T.log.info(s"Generated package: $out")
PathRef(out)
}
Expand Down

0 comments on commit ce4a6be

Please sign in to comment.