enso/project/StdBits.scala

117 lines
4.1 KiB
Scala
Raw Normal View History

import sbt.Keys._
import sbt._
2021-07-20 14:32:19 +03:00
import sbt.internal.util.ManagedLogger
import sbt.io.IO
import sbt.librarymanagement.{ConfigurationFilter, DependencyFilter}
2021-07-20 14:32:19 +03:00
import scala.sys.process.Process
object StdBits {
/** Discovers dependencies of a project and copies them into the destination
* directory.
*
* @param destination location where to put the dependencies
* @param baseJarName name of the base generated JAR (if any); unexpected
* (old) files are removed, so this task needs to know
* this file's name to avoid removing it
* @param ignoreScalaLibrary whether to ignore Scala dependencies that are
* added by default be SBT and are not relevant in
* pure-Java projects
*/
def copyDependencies(
destination: File,
baseJarName: Option[String],
2021-07-20 14:32:19 +03:00
ignoreScalaLibrary: Boolean,
unpackedDeps: Set[String] = Set()
): Def.Initialize[Task[Unit]] =
Def.task {
val libraryUpdates = (Compile / update).value
val log = streams.value.log
val ignoredConfigurations: NameFilter =
if (ignoreScalaLibrary)
new ExactFilter(Configurations.ScalaTool.name)
else NothingFilter
2021-07-20 14:32:19 +03:00
val configFilter: ConfigurationFilter =
DependencyFilter.configurationFilter(-ignoredConfigurations)
2021-07-20 14:32:19 +03:00
val graalOrg = new ExactFilter("org.graalvm.sdk")
val relevantFiles =
libraryUpdates
.select(
configuration = configFilter,
module = DependencyFilter.moduleFilter(organization = -graalOrg),
artifact = DependencyFilter.artifactFilter()
)
val dependencyStore =
streams.value.cacheStoreFactory.make("std-bits-dependencies")
Tracked.diffInputs(dependencyStore, FileInfo.hash)(relevantFiles.toSet) {
report =>
val expectedFileNames =
2021-07-20 14:32:19 +03:00
report.checked.map(
getDestinationFileName(_, unpackedDeps)
) ++ baseJarName.toSeq
for (existing <- IO.listFiles(destination)) {
if (!expectedFileNames.contains(existing.getName)) {
log.info(
s"Removing outdated std-bits dependency ${existing.getName}."
)
IO.delete(existing)
}
}
for (changed <- report.modified -- report.removed) {
log.info(
s"Updating changed std-bits dependency ${changed.getName}."
)
2021-07-20 14:32:19 +03:00
updateDependency(changed, unpackedDeps, destination, log)
}
2020-12-09 16:58:11 +03:00
for (file <- report.unmodified) {
2021-07-20 14:32:19 +03:00
val dest = destination / getDestinationFileName(file, unpackedDeps)
if (!dest.exists()) {
log.info(s"Adding missing std-bits dependency ${file.getName}.")
2021-07-20 14:32:19 +03:00
updateDependency(file, unpackedDeps, destination, log)
}
}
}
}
2021-07-20 14:32:19 +03:00
private def shouldUnpack(jar: File, unpacked: Set[String]): Boolean = {
// Maven stores dependencies like this:
// .../repo1.maven.org/maven2/org/apache/xmlbeans/xmlbeans/5.0.1/xmlbeans-5.0.1.jar
// therefore, the parent of the parent of the jar file is the directory
// named with the un-versioned library name.
unpacked.contains(jar.getParentFile.getParentFile.getName)
}
private def updateDependency(
jar: File,
unpacked: Set[String],
destinationDir: File,
logger: ManagedLogger
): Unit = {
val destination = destinationDir / getDestinationFileName(jar, unpacked)
if (shouldUnpack(jar, unpacked)) {
destination.mkdirs()
val exitCode = Process(s"jar xf ${jar.getAbsolutePath}", destination).!
if (exitCode != 0) {
logger.err(s"Could not unpack a dependency jar: $jar.")
throw new RuntimeException(s"Could not unpack a dependency jar: $jar.")
}
} else {
IO.copyFile(jar, destination)
}
}
private def getDestinationFileName(
file: File,
unpacked: Set[String]
): String = {
if (shouldUnpack(file, unpacked)) {
val name = file.getName
name.stripSuffix(".jar")
} else file.getName
}
}