153 lines
5.0 KiB
Java
Raw Permalink Normal View History

2019-02-12 21:16:34 +01:00
/*
* Copyright 2019 - 2022 Volker Berlin (i-net software)
2019-02-12 21:16:34 +01:00
*
* 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;
2019-02-12 21:16:34 +01:00
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.time.Instant;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.annotation.Nonnull;
2019-02-12 21:16:34 +01:00
import org.junit.Assert;
/**
* Download the wat2wasm tool from github
*
* @author Volker Berlin
*/
class Wat2Wasm {
private String command;
private IOException error;
2019-02-12 21:16:34 +01:00
/**
* Check if there is a new version of the script engine
*
* @param target
* the target directory
2019-02-12 21:16:34 +01:00
* @throws IOException
* if any error occur
*/
private void download( File target ) throws IOException {
//boolean is32 = "32".equals( System.getProperty( "sun.arch.data.model" ) );
2019-02-12 21:16:34 +01:00
String fileName;
final String os = System.getProperty( "os.name", "" ).toLowerCase();
if( os.contains( "windows" ) ) {
fileName = "windows.tar.gz";
2019-02-12 21:16:34 +01:00
} else if( os.contains( "mac" ) ) {
fileName = "macos.tar.gz";
2019-02-12 21:16:34 +01:00
} else if( os.contains( "linux" ) ) {
fileName = "ubuntu.tar.gz";
2019-02-12 21:16:34 +01:00
} else {
throw new IllegalStateException( "Unknown OS: " + os );
}
URL url = new URL( "https://api.github.com/repos/WebAssembly/wabt/releases/latest" );
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
2019-02-12 21:16:34 +01:00
InputStream input = conn.getInputStream();
String data = WasmRule.readStream( input, true );
2019-02-12 21:16:34 +01:00
Pattern pattern = Pattern.compile( "/WebAssembly/wabt/releases/download/[0-9.]*/wabt-[0-9.]*-" + fileName );
Matcher matcher = pattern.matcher( data );
Assert.assertTrue( data, matcher.find() );
2019-02-12 21:16:34 +01:00
String downloadUrl = matcher.group();
url = new URL( "https://github.com" + downloadUrl );
System.out.println( "\tDownload: " + url );
2019-02-12 21:16:34 +01:00
conn = (HttpURLConnection)url.openConnection();
// if( target.exists() ) {
// conn.setIfModifiedSince( target.lastModified() );
// }
2019-02-12 21:16:34 +01:00
input = conn.getInputStream();
if( conn.getResponseCode() == HttpURLConnection.HTTP_NOT_MODIFIED ) {
System.out.println( "\tUP-TO-DATE, use version from " + Instant.ofEpochMilli( target.lastModified() ) );
return;
}
long lastModfied = conn.getLastModified();
extractStream( input, fileName.endsWith( ".tar.gz" ), target );
2019-02-12 21:16:34 +01:00
// target.setLastModified( lastModfied );
2019-02-12 21:16:34 +01:00
System.out.println( "\tUse Version from " + Instant.ofEpochMilli( lastModfied ) );
}
/**
* Search the tool in the directory recursively and set the command.
*
* @param dir
* the directory
*/
private void searchExecuteable( File dir ) {
File[] list = dir.listFiles();
if( list != null ) {
for( File file : list ) {
if( file.isDirectory() ) {
searchExecuteable( file );
} else {
2020-06-18 22:39:30 +02:00
String name = file.getName();
2021-01-12 22:40:43 +01:00
if( name.equals( "wat2wasm" ) || name.equals( "wat2wasm.exe" ) || name.equals( "wat2wasm.bat" ) ) {
2019-02-12 21:16:34 +01:00
command = file.getAbsolutePath();
}
}
if( command != null ) {
return;
}
}
}
}
/**
* Get the executable command
*
* @return the command
* @throws IOException
* if any I/O error occur
*/
@Nonnull
2019-02-12 21:16:34 +01:00
public String getCommand() throws IOException {
if( error != null ) {
throw error;
}
2019-02-12 21:16:34 +01:00
if( command == null ) {
try {
File target = new File( System.getProperty( "java.io.tmpdir" ) + "/wabt" );
searchExecuteable( target );
if( command == null ) {
download( target );
searchExecuteable( target );
if( command == null ) {
throw new IOException("Wabt was not download or sved to: " + target );
}
}
} catch( IOException ex ) {
error = ex;
throw ex;
}
2019-02-12 21:16:34 +01:00
}
return command;
}
}