Skip to content

Commit 99c998d

Browse files
committed
Initial Checkin
0 parents  commit 99c998d

11 files changed

+399
-0
lines changed

.classpath

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="src" output="bin/main" path="src">
4+
<attributes>
5+
<attribute name="gradle_scope" value="main"/>
6+
<attribute name="gradle_used_by_scope" value="main,test"/>
7+
</attributes>
8+
</classpathentry>
9+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8/"/>
10+
<classpathentry kind="con" path="org.eclipse.buildship.core.gradleclasspathcontainer"/>
11+
<classpathentry kind="output" path="bin/default"/>
12+
</classpath>

.gitattributes

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Auto detect text files and perform LF normalization
2+
* text=auto
3+
4+
*.java text eol=lf
5+
*.java diff=java

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/.gradle/
2+
/bin/
3+
/build/

.project

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>JWebAssembly-Gradle</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.eclipse.jdt.core.javabuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
<buildCommand>
14+
<name>org.eclipse.buildship.core.gradleprojectbuilder</name>
15+
<arguments>
16+
</arguments>
17+
</buildCommand>
18+
</buildSpec>
19+
<natures>
20+
<nature>org.eclipse.jdt.core.javanature</nature>
21+
<nature>org.eclipse.buildship.core.gradleprojectnature</nature>
22+
</natures>
23+
</projectDescription>
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
connection.project.dir=
2+
eclipse.preferences.version=1

.settings/org.eclipse.jdt.core.prefs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
eclipse.preferences.version=1
2+
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
3+
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
4+
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
5+
org.eclipse.jdt.core.compiler.compliance=1.8
6+
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
7+
org.eclipse.jdt.core.compiler.debug.localVariable=generate
8+
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
9+
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
10+
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
11+
org.eclipse.jdt.core.compiler.source=1.8
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
implementation-class=de.inetsoftware.jwebassembly.gradle.WasmPlugin
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Copyright 2018 Volker Berlin (i-net software)
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package de.inetsoftware.jwebassembly.gradle;
17+
18+
/**
19+
* Possible output formats
20+
*
21+
* @author Volker Berlin
22+
*
23+
*/
24+
public enum OutputFormat {
25+
Binary, Text;
26+
}
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
/*
2+
* Copyright 2018 Volker Berlin (i-net software)
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package de.inetsoftware.jwebassembly.gradle;
17+
18+
import java.io.File;
19+
import java.io.FileOutputStream;
20+
import java.io.OutputStreamWriter;
21+
import java.lang.reflect.InvocationTargetException;
22+
import java.lang.reflect.Method;
23+
import java.net.URL;
24+
import java.net.URLClassLoader;
25+
import java.nio.charset.StandardCharsets;
26+
import java.util.ArrayList;
27+
28+
import javax.annotation.Nonnull;
29+
30+
import org.gradle.api.Project;
31+
import org.gradle.api.artifacts.Configuration;
32+
import org.gradle.api.tasks.TaskExecutionException;
33+
34+
/**
35+
* A wrapper to the downloaded JWebAssembly via reflection.
36+
*
37+
* @author Volker Berlin
38+
*/
39+
class WasmCompiler {
40+
41+
private static Class<?> jWebAssemblyClass;
42+
43+
private static Method addFile;
44+
45+
private static Method compileToBinary;
46+
47+
private static Method compileToText;
48+
49+
private WasmTask task;
50+
51+
private Object instance;
52+
53+
/**
54+
* Create a new instance of the compiler. To download the right version this must occur lazy after the
55+
* configuration.
56+
*
57+
* @param task
58+
* current task with configuration
59+
*/
60+
WasmCompiler( WasmTask task ) {
61+
this.task = task;
62+
try {
63+
instance = getCompilerClass( task ).newInstance();
64+
} catch( Exception ex ) {
65+
throw new TaskExecutionException( task, ex );
66+
}
67+
}
68+
69+
/**
70+
* Get the class from JWebAssembly.
71+
*/
72+
private Class<?> getCompilerClass( WasmTask task ) throws Exception {
73+
if( jWebAssemblyClass == null ) {
74+
Project project = task.getProject();
75+
76+
project.getDependencies().add( WasmPlugin.CONFIGURATION_NAME, "de.inetsoftware:jwebassembly-compiler:" + task.getCompilerVersion() );
77+
Configuration config = project.getConfigurations().getByName( WasmPlugin.CONFIGURATION_NAME );
78+
ArrayList<URL> urls = new ArrayList<>();
79+
for( File file : config.getFiles() ) {
80+
task.getLogger().lifecycle( "\tcompiler: " + file.getName() );
81+
urls.add( file.toURI().toURL() );
82+
}
83+
URLClassLoader cl = new URLClassLoader( urls.toArray( new URL[0] ) );
84+
85+
jWebAssemblyClass = cl.loadClass( "de.inetsoftware.jwebassembly.JWebAssembly" );
86+
87+
addFile = getMethod( "addFile", File.class );
88+
compileToBinary = getMethod( "compileToBinary", File.class );
89+
compileToText = getMethod( "compileToText", Appendable.class );
90+
}
91+
return jWebAssemblyClass;
92+
}
93+
94+
/**
95+
* Load a method via reflection for later use.
96+
*
97+
* @param name
98+
* the method name
99+
* @param parameterTypes
100+
* the parameters
101+
* @return the requested method
102+
*/
103+
private static Method getMethod( String name, Class<?>... parameterTypes ) throws Exception {
104+
Method method = jWebAssemblyClass.getMethod( name, parameterTypes );
105+
method.setAccessible( true );
106+
return method;
107+
}
108+
109+
/**
110+
* Add a single file to the compiler
111+
*
112+
* @param file
113+
* the file
114+
*/
115+
void addFile( @Nonnull File file ) {
116+
try {
117+
addFile.invoke( instance, file );
118+
} catch( InvocationTargetException ex ) {
119+
throw new TaskExecutionException( task, ex.getTargetException() );
120+
} catch( Exception ex ) {
121+
throw new TaskExecutionException( task, ex );
122+
}
123+
}
124+
125+
/**
126+
* Do the compiling to a wasm file.
127+
*/
128+
void compile() {
129+
try {
130+
if( task.getFormat() == OutputFormat.Binary ) {
131+
compileToBinary.invoke( instance, task.getArchivePath() );
132+
} else {
133+
try (OutputStreamWriter output = new OutputStreamWriter( new FileOutputStream( task.getArchivePath() ), StandardCharsets.UTF_8 )) {
134+
compileToText.invoke( instance, output );
135+
}
136+
}
137+
} catch( InvocationTargetException ex ) {
138+
Throwable targetException = ex.getTargetException();
139+
String msg = "WasmException".equals( targetException.getClass().getSimpleName() ) ? targetException.getMessage() : targetException.toString();
140+
task.getLogger().error( "> WASM compile failed with: " + msg );
141+
throw new TaskExecutionException( task, targetException );
142+
} catch( Exception ex ) {
143+
throw new TaskExecutionException( task, ex );
144+
}
145+
}
146+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Copyright 2018 Volker Berlin (i-net software)
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package de.inetsoftware.jwebassembly.gradle;
17+
18+
import org.gradle.api.Plugin;
19+
import org.gradle.api.Project;
20+
import org.gradle.api.Task;
21+
import org.gradle.api.artifacts.Configuration;
22+
import org.gradle.api.plugins.BasePlugin;
23+
import org.gradle.api.plugins.JavaPlugin;
24+
import org.gradle.api.plugins.JavaPluginConvention;
25+
import org.gradle.api.tasks.SourceSet;
26+
27+
/**
28+
* The WASM Gradle Plugin class.
29+
*
30+
* @author Volker Berlin
31+
*/
32+
public class WasmPlugin implements Plugin<Project> {
33+
34+
static final String CONFIGURATION_NAME = "wasmCompiler";
35+
36+
/**
37+
* {@inheritDoc}
38+
*/
39+
@Override
40+
public void apply( Project project ) {
41+
project.getPlugins().apply( JavaPlugin.class );
42+
JavaPluginConvention javaConvention = project.getConvention().getPlugin( JavaPluginConvention.class );
43+
SourceSet main = javaConvention.getSourceSets().getByName( SourceSet.MAIN_SOURCE_SET_NAME );
44+
45+
WasmTask wasm = project.getTasks().create( "wasm", WasmTask.class );
46+
wasm.setDescription( "Assembles a jar archive containing the main classes." );
47+
wasm.setGroup( BasePlugin.BUILD_GROUP );
48+
wasm.from( main.getOutput() );
49+
50+
// Create dependencies
51+
wasm.dependsOn( "classes" );
52+
Task build = project.getTasks().getByName( "build" );
53+
build.dependsOn( wasm );
54+
55+
// Creates the configurations used by plugin.
56+
Configuration config = project.getConfigurations().create( CONFIGURATION_NAME );
57+
config.setVisible( false );
58+
config.setTransitive( true );
59+
config.setDescription( "The WASM Compiler dependency." );
60+
}
61+
}

0 commit comments

Comments
 (0)