Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
eed3si9n committed Oct 6, 2024
1 parent f0bf4b8 commit f62cb51
Show file tree
Hide file tree
Showing 17 changed files with 52 additions and 30 deletions.
3 changes: 3 additions & 0 deletions src/main/scala-2.12/PluginCompat.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import sbt.util.Tracked.{ inputChanged, lastOutput }
import xsbti.FileConverter

private[sbtassembly] object PluginCompat {
type Out = java.io.File
type MainClass = sbt.Package.MainClass

object CollectionConverters
Expand All @@ -21,6 +22,8 @@ private[sbtassembly] object PluginCompat {
a.data.toPath()
def toFile(a: Attributed[File])(implicit conv: FileConverter): File =
a.data
def toOutput(x: File)(implicit conv: FileConverter): File =
x
def toNioPaths(cp: Seq[Attributed[File]])(implicit conv: FileConverter): Vector[NioPath] =
cp.map(_.data.toPath()).toVector
def toFiles(cp: Seq[Attributed[File]])(implicit conv: FileConverter): Vector[File] =
Expand Down
7 changes: 5 additions & 2 deletions src/main/scala-3/PluginCompat.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import sbt.librarymanagement.ModuleID
import xsbti.{ FileConverter, HashedVirtualFileRef }

object PluginCompat:
type Out = HashedVirtualFileRef
type JarManifest = PackageOption.JarManifest
type MainClass = PackageOption.MainClass
type ManifestAttributes = PackageOption.ManifestAttributes
Expand All @@ -23,6 +24,8 @@ object PluginCompat:
conv.toPath(a.data)
inline def toFile(a: Attributed[HashedVirtualFileRef])(implicit conv: FileConverter): File =
toNioPath(a).toFile()
def toOutput(x: File)(implicit conv: FileConverter): HashedVirtualFileRef =
conv.toVirtualFile(x.toPath())
def toNioPaths(cp: Seq[Attributed[HashedVirtualFileRef]])(implicit conv: FileConverter): Vector[NioPath] =
cp.map(toNioPath).toVector
inline def toFiles(cp: Seq[Attributed[HashedVirtualFileRef]])(implicit conv: FileConverter): Vector[File] =
Expand Down Expand Up @@ -56,7 +59,7 @@ object PluginCompat:
): CacheKey = 0

private[sbtassembly] def cachedAssembly(inputs: CacheKey, cacheDir: File, scalaVersion: String, log: Logger)(
buildAssembly: () => File
): File =
buildAssembly: () => PluginCompat.Out
): PluginCompat.Out =
buildAssembly()
end PluginCompat
8 changes: 4 additions & 4 deletions src/main/scala/sbtassembly/Assembly.scala
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ object Assembly {
val jarName: String = s"$name${if (version.nonEmpty) "-" else ""}$version.jar"
}

def assemblyTask(key: TaskKey[File]): Initialize[Task[File]] = Def.task {
def assemblyTask(key: TaskKey[PluginCompat.Out]): Initialize[Task[PluginCompat.Out]] = Def.task {
val t = (key / test).value
val s = (key / streams).value
val conv = fileConverter.value
Expand Down Expand Up @@ -212,7 +212,7 @@ object Assembly {
conv: FileConverter,
cacheDir: File,
log: Logger
): File = {
): PluginCompat.Out = {
def timed[A](level: Level.Value, desc: String)(f: => A): A = {
log.log(level, desc + " start:")
val start = Instant.now().toEpochMilli
Expand Down Expand Up @@ -322,7 +322,7 @@ object Assembly {
if (mergeStrategy.name == MergeStrategy.rename.name) Option.empty
else Option(mergeStrategy)
}
val buildAssembly = () => {
val buildAssembly: () => PluginCompat.Out = () => {
val mergedEntries = timed(Level.Debug, "Merge all conflicting jar entries (including those renamed)") {
merge(renamedDependencies ++ others, secondPassMergeStrategy, log)
}
Expand Down Expand Up @@ -391,7 +391,7 @@ object Assembly {
}
log.info("Built: " + builtAssemblyJar.toPath)
log.info("Jar hash: " + fullSha1)
builtAssemblyJar
toOutput(builtAssemblyJar)
}
val mergeStrategiesByPathList = timed(Level.Debug, "Collect all merge strategies for cache check") {
// collect all
Expand Down
10 changes: 5 additions & 5 deletions src/main/scala/sbtassembly/AssemblyKeys.scala
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package sbtassembly

import com.eed3si9n.jarjarabrams
import sbt.Keys._
import sbt._
import sbt.Keys.*
import sbt.*

trait AssemblyKeys {
lazy val assembly = taskKey[File]("Builds a deployable über JAR")
lazy val assembly = taskKey[PluginCompat.Out]("Builds a deployable über JAR")
lazy val assembleArtifact = settingKey[Boolean]("Enables (true) or disables (false) assembling an artifact")
lazy val assemblyOption = taskKey[AssemblyOption]("Configuration for making a deployable über JAR")
lazy val assemblyPackageScala = taskKey[File]("Produces the Scala artifact")
lazy val assemblyPackageDependency = taskKey[File]("Produces the dependency artifact")
lazy val assemblyPackageScala = taskKey[PluginCompat.Out]("Produces the Scala artifact")
lazy val assemblyPackageDependency = taskKey[PluginCompat.Out]("Produces the dependency artifact")
lazy val assemblyJarName = taskKey[String]("name of the über jar")
lazy val assemblyDefaultJarName = taskKey[String]("default name of the über jar")
lazy val assemblyOutputPath = taskKey[File]("output path of the über jar")
Expand Down
6 changes: 5 additions & 1 deletion src/main/scala/sbtassembly/AssemblyPlugin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,10 @@ object AssemblyPlugin extends sbt.AutoPlugin {
def assemblyOptionSettings: Seq[Setting[_]] = Seq(
assemblyOption := {
val s = streams.value
val sr = assemblyShadeRules.value
if (sr.nonEmpty && exportJars.value) {
sys.error("exportJars must be set to false for the shading to work")
}
AssemblyOption()
.withIncludeBin((packageBin / assembleArtifact).value)
.withIncludeScala((assemblyPackageScala / assembleArtifact).value)
Expand All @@ -118,7 +122,7 @@ object AssemblyPlugin extends sbt.AutoPlugin {
.withAppendContentHash(assemblyAppendContentHash.value)
.withPrependShellScript(assemblyPrependShellScript.value)
.withMaxHashLength(assemblyMaxHashLength.?.value)
.withShadeRules(assemblyShadeRules.value)
.withShadeRules(sr)
.withScalaVersion(scalaVersion.value)
.withLevel(logLevel.?.value.getOrElse(Level.Info))
.withRepeatableBuild(assemblyRepeatableBuild.value)
Expand Down
14 changes: 8 additions & 6 deletions src/sbt-test/merging/merging/build.sbt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
ThisBuild / version := "0.1"
ThisBuild / scalaVersion := "2.12.18"
ThisBuild / assemblyMergeStrategy := {
version := "0.1"
scalaVersion := "2.12.18"
assemblyMergeStrategy := {
case "a" => MergeStrategy.concat
case "b" => MergeStrategy.first
case "c" => MergeStrategy.last
Expand All @@ -9,9 +9,11 @@ ThisBuild / assemblyMergeStrategy := {
case "f" => MergeStrategy.discard
case PathList("x", "y") => MergeStrategy.discard
case x =>
val oldStrategy = (ThisBuild / assemblyMergeStrategy).value
val oldStrategy = assemblyMergeStrategy.value
oldStrategy(x)
}
name := "foo"
exportJars := true

lazy val testmerge = (project in file("."))
.settings(
Expand All @@ -25,9 +27,9 @@ lazy val testmerge = (project in file("."))
mustContain(dir / "d", Seq("1", "2", "3"))
mustContain(dir / "e", Seq("1"))
mustNotExist(dir / "f")
mustContain(dir / "README_foo", Seq("resources"))
mustContain(dir / "README_foo-0.1", Seq("resources"))
mustContain(dir / "README_1", Seq("1"))
mustContain(dir / "LICENSE_foo", Seq("resources"))
mustContain(dir / "LICENSE_foo-0.1", Seq("resources"))
mustContain(dir / "LICENSE" / "a", Seq("1"))
// 80f5a06 -- don't rename License.class
mustExist(dir / "com" / "example" / "License.class")
Expand Down
1 change: 1 addition & 0 deletions src/sbt-test/merging/merging/test
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# check if the file gets created
> debug
> assembly

# check if it says hello
Expand Down
6 changes: 3 additions & 3 deletions src/sbt-test/shading/directories/build.sbt
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
ThisBuild / scalaVersion := "2.12.18"

ThisBuild / assemblyShadeRules := Seq(
scalaVersion := "2.12.18"
assemblyShadeRules := Seq(
ShadeRule.rename("somepackage.**" -> "shaded.@1").inAll
)
exportJars := false

lazy val root = (project in file("."))
.settings(
Expand Down
1 change: 1 addition & 0 deletions src/sbt-test/shading/jdk11/build.sbt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
exportJars := false
scalaVersion := scala212

lazy val scala212 = "2.12.18"
Expand Down
1 change: 1 addition & 0 deletions src/sbt-test/shading/jdk17/build.sbt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
exportJars := false
scalaVersion := "2.13.12"

assembly / assemblyShadeRules := Seq(
Expand Down
3 changes: 2 additions & 1 deletion src/sbt-test/shading/keeponly/build.sbt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
exportJars := false
lazy val scala212 = "2.12.18"
lazy val scala213 = "2.13.11"

Expand All @@ -16,7 +17,7 @@ lazy val testkeep = (project in file(".")).
IO.unzip(crossTarget.value / "foo.jar", dir)
mustNotExist(dir / "removed" / "ShadeClass.class")
mustNotExist(dir / "removed" / "ShadePackage.class")
mustExist(dir / "keep" / "Keeped.class")
mustExist(dir / "keep" / "Kept.class")
}
})

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package keep

class Keeped
class Kept
12 changes: 7 additions & 5 deletions src/sbt-test/shading/scalasigannot/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ ThisBuild / crossScalaVersions := List(scala212, scala213)


val shadingSettings: Seq[Def.Setting[_]] = Seq(
exportJars := false,
assembly / assemblyShadeRules := Seq(
ShadeRule.rename(
"to.be.shaded.**" -> "shade.@1"
Expand All @@ -18,7 +19,7 @@ val shadingSettings: Seq[Def.Setting[_]] = Seq(
assembly / assemblyExcludedJars := {
val cp = (assembly / fullClasspath).value
cp.filterNot {p =>
p.data.getName.startsWith("tobeshaded")
p.data.toString.contains("tobeshaded")
}
},

Expand All @@ -39,10 +40,10 @@ lazy val fatLib = project.in(file("fatlib"))
Seq(
name := "fatlib",
(Compile / unmanagedJars) := {
val tbs: File = (toBeShaded / Compile / packageBin).value
val tbs = (toBeShaded / Compile / packageBin).value
//Seq(sbt.internal.util.Attributed.blank[java.io.File](tbs))

Seq(Attributed.blank[java.io.File](tbs))
Seq(Attributed.blank(tbs))
}
)
)
Expand All @@ -62,8 +63,9 @@ lazy val root = project.in(file("."))
//Seq(sbt.internal.util.Attributed.blank[java.io.File](tbs))

val x = (fatLib / Compile / assembly).value
Seq(Attributed.blank[java.io.File](x))
}
Seq(Attributed.blank(x))
},
fgRun / aggregate := false,
)
)
.aggregate(fatLib, toBeShaded)
2 changes: 1 addition & 1 deletion src/sbt-test/shading/scalasigannot/project/plugins.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
if(pluginVersion == null)
throw new RuntimeException("""|The system property 'plugin.version' is not defined.
|Specify this property using the scriptedLaunchOpts -D.""".stripMargin)
else addSbtPlugin("com.eed3si9n" % "sbt-assembly" % pluginVersion changing())
else addSbtPlugin("com.eed3si9n" % "sbt-assembly" % pluginVersion)
}
2 changes: 1 addition & 1 deletion src/sbt-test/shading/scalasigannot/test
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# Run the main project

> +root/run
> +root/fgRun
2 changes: 2 additions & 0 deletions src/sbt-test/shading/shading/build.sbt
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
exportJars := false

lazy val testshade = (project in file(".")).
settings(
version := "0.1",
Expand Down
2 changes: 2 additions & 0 deletions src/sbt-test/shading/unmanagedjars/build.sbt
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
exportJars := false

lazy val testshade = (project in file(".")).
settings(
version := "0.1",
Expand Down

0 comments on commit f62cb51

Please sign in to comment.