Add own Node downloader to use V8 Canary. Remove duplicate downloader code.

This commit is contained in:
Volker Berlin 2020-06-07 11:57:56 +02:00
parent 2b46b6dbdf
commit 42c68257b6
3 changed files with 153 additions and 51 deletions

View File

@ -0,0 +1,99 @@
/*
* Copyright 2020 Volker Berlin (i-net software)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package de.inetsoftware.jwebassembly;
import static de.inetsoftware.jwebassembly.SpiderMonkey.extractStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.MessageFormat;
import java.time.Instant;
/**
* Download the node tool.
*
* @author Volker Berlin
*/
public class Node {
private String command;
private static final String BASE_URL = "https://nodejs.org/download/v8-canary/";
private static final String REVISION = "15.0.0-v8-canary20200605aeeaa178a0";
/**
* Check if there is a new version of the script engine
*
* @throws IOException
* if any error occur
*/
private void download() throws IOException {
String fileName;
String ext;
final String os = System.getProperty( "os.name", "" ).toLowerCase();
if( os.contains( "windows" ) ) {
boolean is32 = "32".equals( System.getProperty( "sun.arch.data.model" ) );
fileName = is32 ? "win-x86" : "win-x64";
ext = "zip";
} else if( os.contains( "mac" ) ) {
fileName = "darwin-x64";
ext = "tar.gz";
} else if( os.contains( "linux" ) ) {
fileName = "linux-x64";
ext = "tar.gz";
} else {
throw new IllegalStateException( "Unknown OS: " + os );
}
String urlStr = MessageFormat.format( "{0}v{1}/node-v{1}-{2}.{3}", BASE_URL, REVISION, fileName, ext );
File target = new File( System.getProperty( "java.io.tmpdir" ) + "/node" );
File commandDir = new File( target.getAbsolutePath() + MessageFormat.format( "/node-v{1}-{2}", BASE_URL, REVISION, fileName, ext ) );
if( commandDir.isDirectory() && commandDir.listFiles().length > 0 ) {
// no download needed
System.out.println( "\tUP-TP-DATE, use version from " + commandDir );
} else {
URL url = new URL( urlStr );
System.out.println( "\tDownload: " + url );
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setConnectTimeout( 5000 );
InputStream input = conn.getInputStream();
extractStream( input, "tar.gz".equals( ext ), target );
}
command = commandDir.getAbsolutePath();
}
/**
* Get the node command. If file not exists then download it.
*
* @return the path to the executable
* @throws IOException
* if any I/O error occur
*/
public String getNodeDir() throws IOException {
if( command == null ) {
download();
}
return command;
}
}

View File

@ -23,8 +23,12 @@ import java.net.URL;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import java.time.Instant;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.GZIPInputStream;
import org.apache.commons.compress.archivers.ArchiveEntry;
import org.apache.commons.compress.archivers.ArchiveInputStream;
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream;
/**
* Download the JavaScript engine SpiderMonkey.
@ -55,8 +59,7 @@ public class SpiderMonkey {
throw new IllegalStateException( "Unknown OS: " + os );
}
File target = new File( System.getProperty( "java.io.tmpdir" ) + "/SpiderMonkey" );
URL url = new URL( "https://archive.mozilla.org/pub/firefox/nightly/latest-mozilla-central/jsshell-" + fileName
+ ".zip" );
URL url = new URL( "https://archive.mozilla.org/pub/firefox/nightly/latest-mozilla-central/jsshell-" + fileName + ".zip" );
System.out.println( "\tDownload: " + url );
command = target.getAbsolutePath() + "/js";
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
@ -78,10 +81,49 @@ public class SpiderMonkey {
System.out.println( "\tUP-TP-DATE, use version from " + Instant.ofEpochMilli( target.lastModified() ) );
return;
}
ZipInputStream zip = new ZipInputStream( input );
long lastModfied = conn.getLastModified();
extractStream( input, false, target );
target.setLastModified( lastModfied );
System.out.println( "\tUse Version from " + Instant.ofEpochMilli( lastModfied ) );
}
/**
* Get the SpeiderMonkey command. If file not exists then download it.
*
* @return the path to the executable
* @throws IOException
* if any I/O error occur
*/
public String getCommand() throws IOException {
if( command == null ) {
download();
}
return command;
}
/**
* Extract a compressed stream
*
* @param input
* the input stream
* @param tarGz
* true: *.tar.gz else *.zip
* @param target
* the location to extract
* @throws IOException
* if any I/O error occur
*/
static void extractStream( InputStream input, boolean tarGz, File target ) throws IOException {
ArchiveInputStream archiv;
if( tarGz ) {
input = new GZIPInputStream( input );
archiv = new TarArchiveInputStream( input );
} else {
archiv = new ZipArchiveInputStream( input );
}
do {
ZipEntry entry = zip.getNextEntry();
ArchiveEntry entry = archiv.getNextEntry();
if( entry == null ) {
break;
}
@ -91,21 +133,9 @@ public class SpiderMonkey {
File file = new File( target, entry.getName() );
file.getParentFile().mkdirs();
Files.copy( zip, file.toPath(), StandardCopyOption.REPLACE_EXISTING );
file.setLastModified( entry.getTime() );
if( "js".equals( file.getName() ) ) {
file.setExecutable( true );
}
Files.copy( archiv, file.toPath(), StandardCopyOption.REPLACE_EXISTING );
file.setLastModified( entry.getLastModifiedDate().getTime() );
file.setExecutable( true );
} while( true );
target.setLastModified( lastModfied );
System.out.println( "\tUse Version from " + Instant.ofEpochMilli( lastModfied ) );
}
public String getCommand() throws IOException {
if( command == null ) {
download();
}
return command;
}
}

View File

@ -15,24 +15,19 @@
*/
package de.inetsoftware.jwebassembly;
import static de.inetsoftware.jwebassembly.SpiderMonkey.extractStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import java.time.Instant;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.zip.GZIPInputStream;
import javax.annotation.Nonnull;
import org.apache.commons.compress.archivers.ArchiveEntry;
import org.apache.commons.compress.archivers.ArchiveInputStream;
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream;
import org.junit.Assert;
/**
@ -93,29 +88,7 @@ class Wat2Wasm {
long lastModfied = conn.getLastModified();
ArchiveInputStream archiv;
if( fileName.endsWith( ".tar.gz" ) ) {
input = new GZIPInputStream( input );
archiv = new TarArchiveInputStream( input );
} else {
archiv = new ZipArchiveInputStream( input );
}
do {
ArchiveEntry entry = archiv.getNextEntry();
if( entry == null ) {
break;
}
if( entry.isDirectory() ) {
continue;
}
File file = new File( target, entry.getName() );
file.getParentFile().mkdirs();
Files.copy( archiv, file.toPath(), StandardCopyOption.REPLACE_EXISTING );
file.setLastModified( entry.getLastModifiedDate().getTime() );
file.setExecutable( true );
} while( true );
extractStream( input, fileName.endsWith( ".tar.gz" ), target );
// target.setLastModified( lastModfied );
System.out.println( "\tUse Version from " + Instant.ofEpochMilli( lastModfied ) );