Skip to content

Commit

Permalink
Issue bertramdev#287: Simplify native loader to use Javet OS utils
Browse files Browse the repository at this point in the history
  • Loading branch information
longwa committed Dec 8, 2021
1 parent f7a6093 commit 9700d91
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 119 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ asset-pipeline-site/src/assets/html/apidoc/
.jruby-container
.sass-cache
.sass-work
.shelf/
11 changes: 4 additions & 7 deletions asset-pipeline-core.ipr
Original file line number Diff line number Diff line change
Expand Up @@ -360,9 +360,12 @@
<library name="Gradle: com.caoccao.javet:javet-core:1.0.6">
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/com/caoccao/javet/javet-core/1.0.6/javet-core-1.0.6.jar!/" />
<root url="jar://$USER_HOME$/.gradle/caches/modules-2/files-2.1/com.caoccao.javet/javet-core/1.0.6/47c0f9e768c833b00cdf051928bcd7b97b30701a/javet-core-1.0.6.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
<SOURCES>
<root url="jar://$USER_HOME$/.gradle/caches/modules-2/files-2.1/com.caoccao.javet/javet-core/1.0.6/3d4b6655e8e4d11e678099046e7dd5647777fc82/javet-core-1.0.6-sources.jar!/" />
</SOURCES>
</library>
<library name="Gradle: com.fasterxml.jackson.core:jackson-annotations:2.11.2">
<CLASSES>
Expand Down Expand Up @@ -1476,7 +1479,6 @@
<library name="Gradle: javax.activation:javax.activation-api:1.2.0">
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/javax/activation/javax.activation-api/1.2.0/javax.activation-api-1.2.0.jar!/" />
<root url="jar://$USER_HOME$/.gradle/caches/modules-2/files-2.1/javax.activation/javax.activation-api/1.2.0/85262acf3ca9816f9537ca47d5adeabaead7cb16/javax.activation-api-1.2.0.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES>
Expand Down Expand Up @@ -1567,7 +1569,6 @@
<library name="Gradle: javax.xml.bind:jaxb-api:2.3.1">
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/javax/xml/bind/jaxb-api/2.3.1/jaxb-api-2.3.1.jar!/" />
<root url="jar://$USER_HOME$/.gradle/caches/modules-2/files-2.1/javax.xml.bind/jaxb-api/2.3.1/8531ad5ac454cc2deb9d4d32c40c4d7451939b5d/jaxb-api-2.3.1.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES>
Expand Down Expand Up @@ -1699,7 +1700,6 @@
<library name="Gradle: org.apache.commons:commons-lang3:3.8.1">
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/org/apache/commons/commons-lang3/3.8.1/commons-lang3-3.8.1.jar!/" />
<root url="jar://$USER_HOME$/.gradle/caches/modules-2/files-2.1/org.apache.commons/commons-lang3/3.8.1/6505a72a097d9270f7a9e7bf42c4238283247755/commons-lang3-3.8.1.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES>
Expand All @@ -1709,7 +1709,6 @@
<library name="Gradle: org.apache.commons:commons-lang3:3.9">
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/org/apache/commons/commons-lang3/3.9/commons-lang3-3.9.jar!/" />
<root url="jar://$USER_HOME$/.gradle/caches/modules-2/files-2.1/org.apache.commons/commons-lang3/3.9/122c7cee69b53ed4a7681c03d4ee4c0e2765da5/commons-lang3-3.9.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES>
Expand Down Expand Up @@ -2214,7 +2213,6 @@
<library name="Gradle: org.fusesource.jansi:jansi:1.18">
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/org/fusesource/jansi/jansi/1.18/jansi-1.18.jar!/" />
<root url="jar://$USER_HOME$/.gradle/caches/modules-2/files-2.1/org.fusesource.jansi/jansi/1.18/d9205bbcd4b5f9cd1effe752d18f73bd942d783f/jansi-1.18.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES>
Expand Down Expand Up @@ -2925,7 +2923,6 @@
<library name="Gradle: org.slf4j:slf4j-api:1.7.30">
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/org/slf4j/slf4j-api/1.7.30/slf4j-api-1.7.30.jar!/" />
<root url="jar://$USER_HOME$/.gradle/caches/modules-2/files-2.1/org.slf4j/slf4j-api/1.7.30/b5a4b6d16ab13e34a88fae84c35cd5d68cac922c/slf4j-api-1.7.30.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package asset.pipeline.dart

import asset.pipeline.AssetPipelineConfigHolder
import com.caoccao.javet.enums.JSRuntimeType
import com.caoccao.javet.interop.loader.JavetLibLoader
import groovy.transform.CompileStatic
import groovy.util.logging.Slf4j

import java.nio.channels.Channels
import java.nio.channels.FileChannel
import java.nio.channels.ReadableByteChannel
import java.util.jar.JarEntry
import java.util.jar.JarFile

import static com.caoccao.javet.utils.JavetOSUtils.*

@Slf4j
@CompileStatic
class NativeLibraryLoader {
static final String JAVET_VERSION = NativeLibraryLoader.class.classLoader.getResource("javet-version.txt").openStream().text
static final String BASE_URL = AssetPipelineConfigHolder.config?.javetBaseUrl ?: "https://repo1.maven.org/maven2/com/caoccao/javet"

final JavetLibLoader javetLibLoader

NativeLibraryLoader(JSRuntimeType jsRuntimeType) {
javetLibLoader = new JavetLibLoader(jsRuntimeType)
}

private URL getJavetJARUrl() {
new URL("${BASE_URL}/${javetPackageName}/${JAVET_VERSION}/${getJavetFileName()}")
}

private String getJavetPackageName() {
IS_MACOS ? "javet-macos" : "javet"
}

private String getJavetFileName() {
"${javetPackageName}-${JAVET_VERSION}.jar"
}

/**
* The native libraries are packed in the Javet platform specific jars. First we need to download the correct JAR
* for this platform and then we will extract the native library from it and into a temporary directory for loading.
*
* @return the downloaded Javet jar file for this platform
*/
private File downloadJavetJar() {
File jarFile = new File(TEMP_DIRECTORY, javetFileName)
if (jarFile.exists()) {
log.debug("Jar file ${jarFile.path} exists, skipping download")
return jarFile
}

// Download the file
URL jarUrl = getJavetJARUrl()
log.info("Downloading sass-dart native library integration: ${jarUrl}")

copyToFile(jarUrl.openStream(), jarFile)

log.debug("Downloaded $jarUrl to $jarFile (${jarFile.size()} bytes)")
jarFile
}

private void copyToFile(InputStream inputStream, File targetFile) {
ReadableByteChannel readableByteChannel = Channels.newChannel(inputStream)
FileOutputStream fileOutputStream = new FileOutputStream(targetFile)
FileChannel fileChannel = fileOutputStream.getChannel()
fileChannel.transferFrom(readableByteChannel, 0, Long.MAX_VALUE)
}

/**
* A file reference to the platform and arch specific native library needed.
* @return the required native library for v8 integration
*/
File extractNativeLibrary() {
File file = downloadJavetJar()
if (!file.exists()) {
throw new IllegalStateException("Javet jar file $file does not exist, perhaps the download failed")
}

String libraryName = javetLibLoader.libFileName
File jniLibrary = new File(TEMP_DIRECTORY, libraryName)
if (jniLibrary.exists()) {
log.debug("Native library $libraryName already exists, skipping extract")
return jniLibrary
}

// Extract from the JAR file, should be in the root
JarFile jarFile = new JarFile(file)
JarEntry jarEntry = jarFile.getJarEntry(libraryName)
if (!jarEntry) {
throw new IllegalStateException("Could not load native library: $libraryName")
}

copyToFile(jarFile.getInputStream(jarEntry), jniLibrary)

log.debug("Extracted native library $libraryName to ${jniLibrary.path}")
jniLibrary
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class SassProcessor extends AbstractProcessor {
final String sassCompiler

static {
File nativeLibrary = NativeLibraryUtil.extractNativeLibrary(JSRuntimeType.Node)
File nativeLibrary = new NativeLibraryLoader(JSRuntimeType.Node).extractNativeLibrary()

// Override Javet to use the library that we downloaded for this platform
JavetLibLoader.setLibLoadingListener(new IJavetLibLoadingListener() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,13 @@ import spock.lang.Specification
*/
class SassProcessorSpec extends Specification {

void setup() {
AssetPipelineConfigHolder.config = [:]
}

void "should compile sass into css"() {
given:
AssetPipelineConfigHolder.resolvers = []
AssetPipelineConfigHolder.config = [:]
AssetPipelineConfigHolder.registerResolver(new FileSystemAssetResolver('test','assets'))
def assetFile = AssetHelper.fileForFullName('test.scss')
def processor = new SassProcessor()
Expand All @@ -42,7 +45,6 @@ class SassProcessorSpec extends Specification {
void "should compile nested sass into css"() {
given:
AssetPipelineConfigHolder.resolvers = []
AssetPipelineConfigHolder.config = [:]
AssetPipelineConfigHolder.registerResolver(new FileSystemAssetResolver('test','assets'))
def assetFile = AssetHelper.fileForFullName('partials/forms.scss')
def processor = new SassProcessor()
Expand All @@ -55,7 +57,6 @@ class SassProcessorSpec extends Specification {
void "should compile nested foo into css"() {
given:
AssetPipelineConfigHolder.resolvers = []
AssetPipelineConfigHolder.config = [:]
AssetPipelineConfigHolder.registerResolver(new FileSystemAssetResolver('test','assets'))
def assetFile = AssetHelper.fileForFullName('foo/foo.scss')
def processor = new SassProcessor()
Expand All @@ -68,7 +69,6 @@ class SassProcessorSpec extends Specification {
void "should compile Bourbon"() {
given:
AssetPipelineConfigHolder.resolvers = []
AssetPipelineConfigHolder.config = [:]
AssetPipelineConfigHolder.registerResolver(new FileSystemAssetResolver('test', 'assets'))
def assetFile = AssetHelper.fileForFullName('bourbon-test.scss')
def processor = new SassProcessor()
Expand All @@ -81,7 +81,6 @@ class SassProcessorSpec extends Specification {
void "should compile Bootstrap v4"() {
given:
AssetPipelineConfigHolder.resolvers = []
AssetPipelineConfigHolder.config = [:]
AssetPipelineConfigHolder.registerResolver(new FileSystemAssetResolver('test', 'assets'))
def assetFile = AssetHelper.fileForFullName('bootstrap/bootstrap.scss')
def processor = new SassProcessor()
Expand Down

0 comments on commit 9700d91

Please sign in to comment.