Skip to content

Commit

Permalink
Implement altenative caching stategies
Browse files Browse the repository at this point in the history
  • Loading branch information
romainreuillon committed Nov 9, 2023
1 parent abcba3b commit 0b5d97d
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 28 deletions.
52 changes: 28 additions & 24 deletions src/main/scala/com/typesafe/sbt/osgi/Osgi.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,19 @@

package com.typesafe.sbt.osgi

import java.nio.file.{ FileVisitOption, Files, Path }

import java.nio.file.{FileVisitOption, Files, Path}
import aQute.bnd.osgi.Builder
import aQute.bnd.osgi.Constants._
import aQute.bnd.osgi.Constants.*
import com.typesafe.sbt.osgi.OsgiKeys.CacheStrategy

import java.util.Properties
import java.util.function.Predicate
import java.util.stream.Collectors

import sbt._
import sbt.Keys._
import sbt.*
import sbt.Keys.*
import sbt.Package.ManifestAttributes

import scala.collection.JavaConverters._
import scala.collection.JavaConverters.*
import scala.language.implicitConversions

private object Osgi {
Expand All @@ -45,12 +45,19 @@ private object Osgi {
sourceDirectories: Seq[File],
packageOptions: scala.Seq[sbt.PackageOption],
useJVMJar: Boolean,
cacheBundle: Boolean): Option[File] = {
cacheStrategy: Option[CacheStrategy]): Option[File] = cacheStrategy.flatMap { strategy =>

def fileFootprint(file: File) = {
def footprint(f: File) =
strategy match {
case CacheStrategy.LastModified => FileInfo.lastModified(f).lastModified.toString
case CacheStrategy.Hash => Hash.toHex(FileInfo.hash(f).hash.toArray)
}

def fileFootprint(file: File) =
if (!file.exists()) Seq()
else if (file.isDirectory) Files.walk(file.toPath).iterator().asScala.map(f => f.toAbsolutePath.toString -> Hash.toHex(FileInfo.hash(f.toFile).hash.toArray)).toSeq
else Seq(file.absolutePath -> Hash.toHex(FileInfo.hash(file).hash.toArray))
else if (file.isDirectory) Files.walk(file.toPath).iterator().asScala.map(f => f.toAbsolutePath.toString -> footprint(f.toFile).toSeq)
else Seq(file.absolutePath -> footprint(file))
}

def serialized =
s"""${headers}
Expand All @@ -68,16 +75,13 @@ private object Osgi {

def footprint = Hash.apply(serialized).mkString("")

if (!cacheBundle) None
else {
val footprintValue = footprint
val bundleCacheFootprint = file(artifactPath.absolutePath + "_footprint")
val footprintValue = footprint
val bundleCacheFootprint = file(artifactPath.absolutePath + "_footprint")

if (!bundleCacheFootprint.exists() || IO.read(bundleCacheFootprint) != footprintValue) {
IO.write(bundleCacheFootprint, footprintValue)
None
} else if (artifactPath.exists()) Some(artifactPath) else None
}
if (!bundleCacheFootprint.exists() || IO.read(bundleCacheFootprint) != footprintValue) {
IO.write(bundleCacheFootprint, footprintValue)
None
} else if (artifactPath.exists()) Some(artifactPath) else None
}
def withCache(
headers: OsgiManifestHeaders,
Expand All @@ -91,7 +95,7 @@ private object Osgi {
sourceDirectories: Seq[File],
packageOptions: scala.Seq[sbt.PackageOption],
useJVMJar: Boolean,
cacheBundle: Boolean)(produce: => File): File =
cacheStrategy: Option[CacheStrategy])(produce: => File): File =
cachedBundle(
headers,
additionalHeaders,
Expand All @@ -104,7 +108,7 @@ private object Osgi {
sourceDirectories,
packageOptions,
useJVMJar,
cacheBundle
cacheStrategy
).getOrElse(produce)

def bundleTask(
Expand All @@ -119,7 +123,7 @@ private object Osgi {
sourceDirectories: Seq[File],
packageOptions: scala.Seq[sbt.PackageOption],
useJVMJar: Boolean,
cacheBundle: Boolean,
cacheStrategy: Option[CacheStrategy],
streams: TaskStreams): File =
withCache(headers,
additionalHeaders,
Expand All @@ -132,7 +136,7 @@ private object Osgi {
sourceDirectories,
packageOptions,
useJVMJar,
cacheBundle) {
cacheStrategy) {
val builder = new Builder

if (failOnUndecidedPackage) {
Expand Down
11 changes: 9 additions & 2 deletions src/main/scala/com/typesafe/sbt/osgi/OsgiKeys.scala
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,17 @@ object OsgiKeys {
SettingKey[Boolean](prefix("PackageWithJVMJar"), "Use the JVM jar tools to craft the bundle instead of the one from BND." +
"Without this setting the produced bundle are detected as corrupted by recent JVMs")

val cacheBundle: SettingKey[Boolean] =
SettingKey[Boolean](prefix("CacheBundle"), "Do not build a new bundle if a bundle already exists and has been crafted from identical inputs")
val cacheStrategy: SettingKey[Option[CacheStrategy]] =
SettingKey[Option[CacheStrategy]](prefix("CacheBundle"), "Do not build a new bundle if a bundle already exists and has been crafted from identical inputs")


private def prefix(key: String) = "osgi" + key


sealed trait CacheStrategy

object CacheStrategy {
object Hash extends CacheStrategy
object LastModified extends CacheStrategy
}
}
4 changes: 2 additions & 2 deletions src/main/scala/com/typesafe/sbt/osgi/SbtOsgi.scala
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ object SbtOsgi extends AutoPlugin {
(sourceDirectories in Compile).value,
(packageOptions in (Compile, packageBin)).value,
packageWithJVMJar.value,
cacheBundle.value,
cacheStrategy.value,
streams.value),
Compile / sbt.Keys.packageBin := bundle.value,
manifestHeaders := OsgiManifestHeaders(
Expand Down Expand Up @@ -89,6 +89,6 @@ object SbtOsgi extends AutoPlugin {
embeddedJars := Nil,
explodedJars := Nil,
packageWithJVMJar := false,
cacheBundle := false)
cacheStrategy := None)
}
}

0 comments on commit 0b5d97d

Please sign in to comment.