Skip to content

Commit

Permalink
Virtualize packageBin, part 3
Browse files Browse the repository at this point in the history
  • Loading branch information
eed3si9n committed Nov 12, 2023
1 parent cc822f3 commit 2b15927
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 32 deletions.
25 changes: 16 additions & 9 deletions main-actions/src/main/scala/sbt/Pkg.scala
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,19 @@ import sjsonnew.{
}

import sbt.util.Logger

import sbt.util.{ CacheStoreFactory, FilesInfo, ModifiedFileInfo, PlainFileInfo }
import sbt.util.{
CacheStoreFactory,
FilesInfo,
HashedVirtualFileRef,
ModifiedFileInfo,
PlainFileInfo
}
import sbt.util.FileInfo.{ exists, lastModified }
import sbt.util.CacheImplicits._
import sbt.util.Tracked.{ inputChanged, outputChanged }
import scala.sys.process.Process
import xsbti.FileConverter
import xsbti.VirtualFile
import xsbti.VirtualFileRef

/**
Expand Down Expand Up @@ -106,22 +112,23 @@ object Pkg:
* @param options additional package information, e.g. jar manifest, main class or manifest attributes
*/
final class Configuration(
val sources: Seq[(VirtualFileRef, String)],
val sources: Seq[(HashedVirtualFileRef, String)],
val jar: VirtualFileRef,
val options: Seq[PackageOption]
)

object Configuration:
given IsoLList.Aux[
Configuration,
Vector[(VirtualFileRef, String)] :*: VirtualFileRef :*: Seq[PackageOption] :*: LNil
Vector[(HashedVirtualFileRef, String)] :*: VirtualFileRef :*: Seq[PackageOption] :*: LNil
] =
import sbt.util.CacheImplicits.given
import sbt.util.PathHashWriters.given
LList.iso(
(c: Configuration) =>
("sources", c.sources.toVector) :*: ("jar", c.jar) :*: ("options", c.options) :*: LNil,
(in: Vector[(VirtualFileRef, String)] :*: VirtualFileRef :*: Seq[PackageOption] :*: LNil) =>
Configuration(in.head, in.tail.head, in.tail.tail.head),
(in: Vector[(HashedVirtualFileRef, String)] :*: VirtualFileRef :*: Seq[PackageOption] :*:
LNil) => Configuration(in.head, in.tail.head, in.tail.tail.head),
)
given JsonFormat[Configuration] = summon[JsonFormat[Configuration]]
end Configuration
Expand All @@ -131,7 +138,7 @@ object Pkg:
* @param cacheStoreFactory used for jar caching. We try to avoid rebuilds as much as possible
* @param log feedback for the user
*/
def apply(conf: Configuration, converter: FileConverter, log: Logger): Unit =
def apply(conf: Configuration, converter: FileConverter, log: Logger): VirtualFile =
apply(conf, converter, log, timeFromConfiguration(conf))

/**
Expand All @@ -145,14 +152,14 @@ object Pkg:
converter: FileConverter,
log: Logger,
time: Option[Long]
): Unit = {
): VirtualFile =
val manifest = toManifest(conf, log)
val out = converter.toPath(conf.jar).toFile()
val sources = conf.sources.map { case (vf, path) =>
converter.toPath(vf).toFile() -> path
}
makeJar(sources, out, manifest, log, time)
}
converter.toVirtualFile(out.toPath())

def toManifest(conf: Configuration, log: Logger): Manifest =
val manifest = new Manifest
Expand Down
59 changes: 39 additions & 20 deletions main/src/main/scala/sbt/Defaults.scala
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ import xsbti.compile.{
Setup,
TransactionalManagerType
}
import sbt.util.HashedVirtualFileRef

object Defaults extends BuildCommon {
final val CacheDirectoryName = "cache"
Expand Down Expand Up @@ -421,7 +422,7 @@ object Defaults extends BuildCommon {
val coursierCache = csrCacheDirectory.value
val javaHome = Paths.get(sys.props("java.home"))
val out = rootOutputDirectory.value
Map(
ListMap(
"OUT" -> out,
"BASE" -> base.toPath,
"SBT_BOOT" -> boot.toPath,
Expand Down Expand Up @@ -1752,39 +1753,54 @@ object Defaults extends BuildCommon {
packageTaskSettings(packageDoc, packageDocMappings) ++
Seq(Keys.`package` := packageBin.value)

def packageBinMappings: Initialize[Task[Seq[(VirtualFileRef, String)]]] =
def packageBinMappings: Initialize[Task[Seq[(HashedVirtualFileRef, String)]]] =
Def.task {
val converter = fileConverter.value
val xs = products.value
xs
.flatMap(Path.allSubpaths)
.map { case (p, path) => converter.toVirtualFile(p.toPath()) -> path }
.filter(_._1.isFile())
.map { case (p, path) =>
val vf = converter.toVirtualFile(p.toPath())
HashedVirtualFileRef.of(vf) -> path
}
}

def packageDocMappings: Initialize[Task[Seq[(VirtualFileRef, String)]]] =
def packageDocMappings: Initialize[Task[Seq[(HashedVirtualFileRef, String)]]] =
Def.task {
val converter = fileConverter.value
val d = doc.value
Path.allSubpaths(d).toSeq.map { case (p, path) =>
converter.toVirtualFile(p.toPath()) -> path
}
Path
.allSubpaths(d)
.toSeq
.filter(_._1.isFile())
.map { case (p, path) =>
val vf = converter.toVirtualFile(p.toPath())
HashedVirtualFileRef.of(vf) -> path
}
}

def packageSrcMappings: Initialize[Task[Seq[(VirtualFileRef, String)]]] =
def packageSrcMappings: Initialize[Task[Seq[(HashedVirtualFileRef, String)]]] =
concatMappings(resourceMappings, sourceMappings)

private type Mappings = Initialize[Task[Seq[(VirtualFileRef, String)]]]
private type Mappings = Initialize[Task[Seq[(HashedVirtualFileRef, String)]]]
def concatMappings(as: Mappings, bs: Mappings): Mappings =
as.zipWith(bs) {
(a: Task[Seq[(VirtualFileRef, String)]], b: Task[Seq[(VirtualFileRef, String)]]) =>
(
a: Task[Seq[(HashedVirtualFileRef, String)]],
b: Task[Seq[(HashedVirtualFileRef, String)]]
) =>
(a, b).mapN {
case (seq1: Seq[(VirtualFileRef, String)], seq2: Seq[(VirtualFileRef, String)]) =>
case (
seq1: Seq[(HashedVirtualFileRef, String)],
seq2: Seq[(HashedVirtualFileRef, String)]
) =>
seq1 ++ seq2
}
}

// drop base directories, since there are no valid mappings for these
def sourceMappings: Initialize[Task[Seq[(VirtualFileRef, String)]]] =
def sourceMappings: Initialize[Task[Seq[(HashedVirtualFileRef, String)]]] =
Def.task {
val converter = fileConverter.value
val sdirs = unmanagedSourceDirectories.value
Expand All @@ -1797,17 +1813,18 @@ object Defaults extends BuildCommon {
case _ => None
}
.map { case (p, path) =>
converter.toVirtualFile(p.toPath()) -> path
val vf = converter.toVirtualFile(p.toPath())
HashedVirtualFileRef.of(vf) -> path
}
}

def resourceMappings: Initialize[Task[Seq[(VirtualFileRef, String)]]] =
def resourceMappings: Initialize[Task[Seq[(HashedVirtualFileRef, String)]]] =
relativeMappings(unmanagedResources, unmanagedResourceDirectories)

def relativeMappings(
files: Taskable[Seq[File]],
dirs: Taskable[Seq[File]]
): Initialize[Task[Seq[(VirtualFileRef, String)]]] =
): Initialize[Task[Seq[(HashedVirtualFileRef, String)]]] =
Def.task {
val converter = fileConverter.value
val rdirs = dirs.toTask.value.toSet
Expand All @@ -1818,7 +1835,8 @@ object Defaults extends BuildCommon {
case _ => None
}
.map { case (p, path) =>
converter.toVirtualFile(p.toPath()) -> path
val vf = converter.toVirtualFile(p.toPath())
HashedVirtualFileRef.of(vf) -> path
}
}

Expand All @@ -1834,7 +1852,7 @@ object Defaults extends BuildCommon {
def relativeMappings( // forward to widened variant
files: ScopedTaskable[Seq[File]],
dirs: ScopedTaskable[Seq[File]]
): Initialize[Task[Seq[(VirtualFileRef, String)]]] =
): Initialize[Task[Seq[(HashedVirtualFileRef, String)]]] =
relativeMappings(files: Taskable[Seq[File]], dirs)

def collectFiles( // forward to widened variant
Expand Down Expand Up @@ -1933,7 +1951,7 @@ object Defaults extends BuildCommon {

def packageTaskSettings(
key: TaskKey[VirtualFileRef],
mappingsTask: Initialize[Task[Seq[(VirtualFileRef, String)]]]
mappingsTask: Initialize[Task[Seq[(HashedVirtualFileRef, String)]]]
) =
inTask(key)(
Seq(
Expand All @@ -1951,13 +1969,14 @@ object Defaults extends BuildCommon {
val config = packageConfiguration.value
val s = streams.value
val converter = fileConverter.value
Pkg(
val out = Pkg(
config,
converter,
s.log,
Pkg.timeFromConfiguration(config)
)
config.jar
Def.declareOutput(out)
out
}

def packageConfigurationTask: Initialize[Task[Pkg.Configuration]] =
Expand Down
3 changes: 2 additions & 1 deletion main/src/main/scala/sbt/Keys.scala
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import xsbti.compile.analysis.ReadStamps

import scala.concurrent.duration.{ Duration, FiniteDuration }
import scala.xml.{ NodeSeq, Node => XNode }
import sbt.util.HashedVirtualFileRef

// format: off

Expand Down Expand Up @@ -281,7 +282,7 @@ object Keys {
val artifact = settingKey[Artifact]("Describes an artifact.").withRank(BMinusSetting)
val artifactClassifier = settingKey[Option[String]]("Sets the classifier used by the default artifact definition.").withRank(BSetting)
val artifactName = settingKey[(ScalaVersion, ModuleID, Artifact) => String]("Function that produces the artifact name from its definition.").withRank(CSetting)
val mappings = taskKey[Seq[(VirtualFileRef, String)]]("Defines the mappings from a file to a path, used by packaging, for example.").withRank(BTask)
val mappings = taskKey[Seq[(HashedVirtualFileRef, String)]]("Defines the mappings from a file to a path, used by packaging, for example.").withRank(BTask)
val fileMappings = taskKey[Seq[(File, File)]]("Defines the mappings from a file to a file, used for copying files, for example.").withRank(BMinusTask)

// Run Keys
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,10 @@ class DiskActionCacheStore(base: Path) extends ActionCacheStore:
refs.flatMap: r =>
val casFile = casBase.toFile / r.contentHash
if casFile.exists then
val outPath = outputDirectory.resolve(r.id)
val id =
if r.id.startsWith("${OUT}/") then r.id.drop(7)
else r.id
val outPath = outputDirectory.resolve(id)
Files.createDirectories(outPath.getParent())
if outPath.toFile().exists() then IO.delete(outPath.toFile())
Some(Files.createSymbolicLink(outPath, casFile.toPath))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package sbt.util

import xsbti.VirtualFile
import xsbti.VirtualFileRef
import sjsonnew.*

Expand All @@ -26,6 +27,9 @@ object HashedVirtualFileRef:
if xs.size != 2 then sys.error(s"invalid HashedVirtualFileRef $str")
else HashedVirtualFileRef(xs(1), xs(0))

def of(vf: VirtualFile): HashedVirtualFileRef =
val contentHash = s"farm64-${vf.contentHash.toHexString}"
HashedVirtualFileRef(vf.id, contentHash)
given IsoLList.Aux[HashedVirtualFileRef, String :*: String :*: LNil] =
LList.iso(
{ (r: HashedVirtualFileRef) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,4 @@ trait BasicCacheImplicits:
SingletonCache.basicSingletonCache(asSingleton(t))

given IsoString[VirtualFileRef] = IsoString.iso(_.id, VirtualFileRef.of)
given JsonFormat[VirtualFileRef] = summon[JsonFormat[VirtualFileRef]]
end BasicCacheImplicits

0 comments on commit 2b15927

Please sign in to comment.