Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove Scrimage classloader hack #553

Merged
merged 2 commits into from
Feb 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ lazy val pluginSettings: Seq[Def.Setting[_]] = Seq(
"org.http4s" %% "http4s-blaze-client" % "0.21.18",
"net.jcazevedo" %% "moultingyaml" % "0.4.2",
"com.lihaoyi" %% "scalatags" % "0.9.3",
"com.sksamuel.scrimage" %% "scrimage-scala" % "4.0.16",
"com.sksamuel.scrimage" %% "scrimage-scala" % "4.0.17",
"org.scalatest" %% "scalatest" % "3.2.3" % Test,
"org.scalatestplus" %% "scalacheck-1-15" % "3.2.3.0" % Test
),
Expand Down
2 changes: 1 addition & 1 deletion project/dependencies.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ libraryDependencies ++= Seq(
"com.47deg" %% "github4s" % "0.24.0",
"net.jcazevedo" %% "moultingyaml" % "0.4.2",
"com.lihaoyi" %% "scalatags" % "0.9.3",
"com.sksamuel.scrimage" %% "scrimage-scala" % "4.0.16"
"com.sksamuel.scrimage" %% "scrimage-scala" % "4.0.17"
)
33 changes: 14 additions & 19 deletions src/main/scala/microsites/util/MicrositeHelper.scala
Original file line number Diff line number Diff line change
Expand Up @@ -260,26 +260,21 @@ class MicrositeHelper(config: MicrositeSettings) {

createFile(sourceFile)

//This is a dirty classloader hack to allow the latest version of Scrimage to work.
//This plugin's default classloader is limited and cannot load ImageReader instances.
//We get a different ClassLoader that will work, replace it, and then set it back after we're done.
//Will be fixed in Scrimage 4.1.0, see: https://github.com/sksamuel/scrimage/issues/217
//Scrimage needs a different classloader as our plugin's default classloader is restricted
//See: https://github.com/sksamuel/scrimage/issues/217
val desiredCL: ClassLoader = classOf[ImageReader].getClassLoader
val currentCL = Thread.currentThread.getContextClassLoader()

try {
Thread.currentThread.setContextClassLoader(desiredCL)

(faviconFilenames zip faviconSizes)
.map { case (name, size) =>
(new File(s"$targetDir$jekyllDir/img/$name"), size)
}
.map { case (file, (width, height)) =>
ImmutableImage.loader.fromFile(sourceFile.toFile).scaleTo(width, height).output(file)
}
} finally
//Reset the classloader to what it was previously
Thread.currentThread.setContextClassLoader(currentCL)

(faviconFilenames zip faviconSizes)
.map { case (name, size) =>
(new File(s"$targetDir$jekyllDir/img/$name"), size)
}
.map { case (file, (width, height)) =>
ImmutableImage.loader
.withClassLoader(desiredCL)
.fromFile(sourceFile.toFile)
.scaleTo(width, height)
.output(file)
}
}

def copyConfigurationFile(sourceDir: File, targetDir: File): Unit = {
Expand Down