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

Develop #17

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
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
6 changes: 5 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,10 @@
<artifactId>testng</artifactId>
<version>6.2.1</version>
</dependency>

<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.theoryinpractise.coffeescript;

import com.theoryinpractise.coffeescript.compiler.CompilerFactory;
import com.theoryinpractise.coffeescript.compiler.CoffeeScriptCompiler;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Charsets;
import com.google.common.base.Function;
Expand All @@ -12,6 +14,7 @@

import javax.annotation.Nullable;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

Expand Down Expand Up @@ -104,7 +107,7 @@ public void execute() throws MojoExecutionException {
}

getLog().info(String.format("coffee-maven-plugin using coffee script version %s", version));
CoffeeScriptCompiler coffeeScriptCompiler = new CoffeeScriptCompiler(version, bare);
CoffeeScriptCompiler coffeeScriptCompiler = CompilerFactory.newInstance(version, bare);

try {
if (compileIndividualFiles) {
Expand All @@ -115,7 +118,7 @@ public void execute() throws MojoExecutionException {
for (File file : joinSet.getFiles()) {
getLog().info("Compiling File " + file.getName() + " in JoinSet:" + joinSet.getId());
compiled
.append(coffeeScriptCompiler.compile(Files.toString(file, Charsets.UTF_8)))
.append(coffeeScriptCompiler.compile(file))
.append("\n");
}
write(joinSet.getCoffeeOutputDirectory(), joinSet.getId(), compiled.toString());
Expand All @@ -124,7 +127,10 @@ public void execute() throws MojoExecutionException {
for (JoinSet joinSet : findJoinSets()) {
getLog().info("Compiling JoinSet: " + joinSet.getId() + " with files: " + joinSet.getFileNames());

String compiled = coffeeScriptCompiler.compile(joinSet.getConcatenatedStringOfFiles());
File tempFile = File.createTempFile("coffee-", ".js");
new FileOutputStream(tempFile).write(joinSet.getConcatenatedStringOfFiles().getBytes());
String compiled = coffeeScriptCompiler.compile(tempFile);
tempFile.delete();

write(joinSet.getCoffeeOutputDirectory(), joinSet.getId(), compiled);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.theoryinpractise.coffeescript.compiler;

import java.io.File;

/**
*
* @author thrykol
*/
public interface CoffeeScriptCompiler {

/**
* Compiles the source coffee file to javascript.
*
* @param source CoffeeScript file to compile
* @return The compiled javascript
*/
public String compile(File source);

/**
* System commands which must exist for this compiler to be available
* @return System commands required by the compiler
*/
public String[] commands();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.theoryinpractise.coffeescript.compiler;

import com.theoryinpractise.coffeescript.compiler.NodeCompiler;
import com.theoryinpractise.coffeescript.compiler.RhinoCompiler;
import com.theoryinpractise.coffeescript.compiler.CoffeeScriptCompiler;
import java.io.IOException;
import org.apache.commons.lang.SystemUtils;

/**
*
* @author thrykol
*/
public abstract class CompilerFactory {

/**
* Create a new compiler instance.
*
* @param version
* @param bare
* @return
*/
public static CoffeeScriptCompiler newInstance(String version, boolean bare) {
CoffeeScriptCompiler compiler = null;

try {
if (SystemUtils.IS_OS_LINUX && _linux(NodeCompiler.class)) {
compiler = new NodeCompiler("coffee", bare);
}
} catch (Exception ex) {
ex.printStackTrace();
}

if (compiler == null) {
compiler = new RhinoCompiler(version, bare);
}

return compiler;
}

private static boolean _linux(Class<? extends CoffeeScriptCompiler> klass) throws IOException, InterruptedException, InstantiationException, IllegalAccessException {
boolean result = true;
for (String cmd : klass.newInstance().commands()) {
Process p = Runtime.getRuntime().exec(
new String[]{"which", cmd});

p.waitFor();
result &= p.exitValue() == 0;
}

return result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.theoryinpractise.coffeescript.compiler;

import com.theoryinpractise.coffeescript.CoffeeScriptException;
import java.io.File;
import java.io.IOException;
import java.io.StringWriter;
import org.apache.commons.io.IOUtils;

/**
*
* @author thrykol
*/
public class NodeCompiler implements CoffeeScriptCompiler {

boolean bare;

public NodeCompiler() {
this("", false);
}

public NodeCompiler(String version, boolean bare) {
this.bare = bare;
}

public String compile(File source) {
Process p;

String command = "cat " + source.getAbsolutePath() + " | coffee -sc";

try {
p = Runtime.getRuntime().exec(
new String[]{"sh", "-c", command});

p.waitFor();

if (p.exitValue() != 0) {
StringWriter writer = new StringWriter();
IOUtils.copy(p.getErrorStream(), writer);
throw new CoffeeScriptException(writer.toString());
}

StringWriter writer = new StringWriter();
IOUtils.copy(p.getInputStream(), writer);

return writer.toString();
} catch (InterruptedException ex) {
throw new CoffeeScriptException(ex.getMessage(), ex);
} catch (IOException ex) {
throw new CoffeeScriptException(ex.getMessage(), ex);
}
}

public String[] commands() {
return new String[]{"sh","cat","coffee"};
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package com.theoryinpractise.coffeescript;
package com.theoryinpractise.coffeescript.compiler;

import com.theoryinpractise.coffeescript.compiler.CoffeeScriptCompiler;
import com.google.common.base.Charsets;
import com.google.common.io.Files;
import com.google.common.io.InputSupplier;
import com.google.common.io.Resources;
import com.theoryinpractise.coffeescript.CoffeeScriptException;
import java.io.File;
import org.mozilla.javascript.Context;
import org.mozilla.javascript.JavaScriptException;
import org.mozilla.javascript.Scriptable;
Expand Down Expand Up @@ -37,14 +41,14 @@
* <p/>
* Wrapper around the coffee-script compiler from https://github.com/jashkenas/coffee-script/
*/
public class CoffeeScriptCompiler {
public class RhinoCompiler implements CoffeeScriptCompiler {

private boolean bare;
private String version;
private final Scriptable globalScope;
private Scriptable coffeeScript;

public CoffeeScriptCompiler(String version, boolean bare) {
public RhinoCompiler(String version, boolean bare) {
this.bare = bare;
this.version = version;

Expand All @@ -66,28 +70,33 @@ private void compileFile(Context context, String sourcePath, String fileName) th
context.evaluateReader(globalScope, supplier.getInput(), fileName, 0, null);
}

public String compile(String coffeeScriptSource) {
public String compile(File source) {
Context context = Context.enter();
try {
Scriptable compileScope = context.newObject(coffeeScript);
compileScope.setParentScope(coffeeScript);
compileScope.put("coffeeScript", compileScope, coffeeScriptSource);
try {

String options = bare ? "{bare: true}" : "{}";

return (String) context.evaluateString(
compileScope,
String.format("compile(coffeeScript, %s);", options),
"source", 0, null);
} catch (JavaScriptException e) {
throw new CoffeeScriptException(e.getMessage(), e);
}
String coffeeScriptSource = Files.toString(source, Charsets.UTF_8);
Scriptable compileScope = context.newObject(coffeeScript);
compileScope.setParentScope(coffeeScript);
compileScope.put("coffeeScript", compileScope, coffeeScriptSource);

String options = bare ? "{bare: true}" : "{}";

return (String) context.evaluateString(
compileScope,
String.format("compile(coffeeScript, %s);", options),
"source", 0, null);
} catch (IOException e) {
throw new CoffeeScriptException(e.getMessage(), e);
} catch (JavaScriptException e) {
throw new CoffeeScriptException(e.getMessage(), e);
} finally {
Context.exit();
}
}

public String[] commands() {
return new String[]{};
}

private Context createContext() {
Context context = Context.enter();
context.setOptimizationLevel(9); // Enable optimization
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.theoryinpractise.coffeescript;

import com.google.common.base.Function;
import com.google.common.collect.Iterators;
import com.theoryinpractise.coffeescript.compiler.CoffeeScriptCompiler;
import com.theoryinpractise.coffeescript.compiler.CompilerFactory;
import com.theoryinpractise.coffeescript.compiler.NodeCompiler;
import java.io.File;
import java.util.Iterator;
import javax.annotation.Nullable;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

/**
*
* @author thrykol
*/
public class NodeCompilerTest {

@DataProvider
public Iterator<Object[]> provideVersions() {

return Iterators.transform(new CoffeeScriptCompilerMojo().acceptableVersions.iterator(), new Function<String, Object[]>() {

public Object[] apply(@Nullable String s) {
return new Object[]{s};
}
});

}

public NodeCompilerTest() {
}

@Test(dataProvider = "provideVersions")
public void testCompile(final String version) throws Exception {
CoffeeScriptCompiler compiler = CompilerFactory.newInstance(version, false);

if (compiler instanceof NodeCompiler) {
File file = new File("target/test-classes/sample.coffee");
String output = compiler.compile(file);

System.out.println("Output: [" + output + "]");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

import com.google.common.base.Function;
import com.google.common.collect.Iterators;
import com.theoryinpractise.coffeescript.CoffeeScriptCompilerMojo;
import com.theoryinpractise.coffeescript.compiler.RhinoCompiler;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import javax.annotation.Nullable;
import java.util.Iterator;

public class CoffeeScriptCompilerTest {
public class RhinoCompilerTest {

@DataProvider
public Iterator<Object[]> provideVersions() {
Expand All @@ -23,7 +25,7 @@ public Object[] apply(@Nullable String s) {

@Test(dataProvider = "provideVersions")
public void testCompiler(final String version) {
CoffeeScriptCompiler compiler = new CoffeeScriptCompiler(version, true);
RhinoCompiler compiler = new RhinoCompiler(version, true);

}

Expand Down
2 changes: 2 additions & 0 deletions src/test/resources/sample.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Existence:
alert "I knew it!" if elvis?