Skip to content

Commit

Permalink
plovr now works end-to-end: can start up the executable jar with one …
Browse files Browse the repository at this point in the history
…or more config files
  • Loading branch information
bolinfest committed Jun 1, 2010
1 parent 34018a1 commit 4c5772d
Show file tree
Hide file tree
Showing 11 changed files with 300 additions and 24 deletions.
71 changes: 60 additions & 11 deletions build.xml
Expand Up @@ -53,32 +53,81 @@
<fail if="hasFailingTests" />
</target>

<target name="release" depends="compile" description="build a release">
<target name="externs">
<mkdir dir="${build.dir}" />
<zip destfile="${build.dir}/externs.zip" basedir="${closure-compiler.dir}/externs" />
<zip destfile="${build.dir}/library.zip">
<zipfileset dir="${closure-library.dir}/closure/goog"
<exec executable="sh"
output="${build.dir}/externs_manifest.txt"
failonerror="true">
<arg value="${basedir}/listfiles.sh" />
<arg value="${closure-compiler.dir}/externs" />
</exec>
</target>

<target name="library">
<mkdir dir="${build.dir}" />
<exec executable="sh"
output="${build.dir}/library_manifest.txt"
failonerror="true">
<arg value="${basedir}/listfiles.sh" />
<arg value="${closure-library.dir}/closure/goog" />
</exec>
</target>

<target name="third_party">
<mkdir dir="${build.dir}" />
<exec executable="sh"
output="${build.dir}/third_party_manifest.txt"
failonerror="true">
<arg value="${basedir}/listfiles.sh" />
<arg value="${closure-library.dir}/third_party/closure/goog" />
</exec>
</target>

<target name="release"
depends="compile, externs, library, third_party"
description="build a release">
<mkdir dir="${build.dir}" />
<jar destfile="${build.dir}/plovr.jar">
<zipfileset dir="${closure-compiler.dir}/externs/"
includes="**/*.js"
prefix="externs"
/>
<zipfileset dir="${closure-library.dir}/closure/goog/"
includes="**/*.js"
excludes="deps.js"
prefix="closure/goog" />
<zipfileset dir="${closure-library.dir}/third_party/closure/goog"
prefix="closure/goog"
/>
<zipfileset dir="${closure-library.dir}/third_party/closure/goog/"
includes="**/*.js"
excludes="deps.js"
prefix="third_party/closure/goog" />
prefix="third_party/closure/goog"
/>
<zipfileset dir="${closure-templates.dir}/javascript/"
includes="soyutils_usegoog.js"
prefix="soy" />
</zip>
<jar destfile="${build.dir}/plovr.jar">
<fileset dir="${build.dir}" includes="externs_manifest.txt,
library_manifest.txt,
third_party_manifest.txt"
/>

<zipgroupfileset dir="${lib.dir}" includes="*.jar" />
<fileset dir="${classes.dir}" />
<fileset dir="${build.dir}" includes="externs.zip, library.zip" />
<manifest>
<attribute name="Main-Class" value="org.plovr.Main" />
<attribute name="Main-Class" value="org.plovr.Main" />
</manifest>
</jar>
</target>

<target name="integration-test"
depends="release"
description="builds the jar and runs it on localhost:9810">
<java jar="${build.dir}/plovr.jar" fork="true">
<arg value="${test.dir}/org/plovr/integration-test-config.js" />
<jvmarg value="-Xdebug" />
<jvmarg value="-Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=8000"/>
</java>
</target>

<target name="build" depends="release" />

</project>
8 changes: 8 additions & 0 deletions listfiles.sh
@@ -0,0 +1,8 @@
# Creating a separate script to list files in a directory is much easier than
# trying to accomplish the equivalent thing in pure Ant.

# Change to the directory to eliminate any relative directory prefix.
cd $1

# Remove the "./" from the beginning of each entry, and exclude deps.js files.
find . -name '*.js' | grep -v deps.js | cut -b 3-
6 changes: 6 additions & 0 deletions src/org/plovr/AbstractJsInput.java
Expand Up @@ -7,6 +7,12 @@
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;

/**
* {@link AbstractJsInput} provides the default logic for extracting
* {@code goog.provide()} and {@code goog.require()} information.
*
* @author bolinfest@gmail.com (Michael Bolin)
*/
abstract class AbstractJsInput implements JsInput {

private static final Pattern GOOG_PROVIDE_OR_REQUIRE =
Expand Down
13 changes: 11 additions & 2 deletions src/org/plovr/CompilationServer.java
Expand Up @@ -10,6 +10,7 @@
import java.util.List;
import java.util.Map;
import java.util.concurrent.Executors;
import java.util.logging.Logger;

import com.google.common.collect.LinkedListMultimap;
import com.google.common.collect.Maps;
Expand All @@ -27,6 +28,8 @@

class CompilationServer implements Runnable {

private static final Logger logger = Logger.getLogger("org.plovr.CompilationServer");

private final Map<String, Config> configMap;

private final int port;
Expand Down Expand Up @@ -112,8 +115,14 @@ private void compile(Config config, QueryData data, Appendable builder) throws I
CompilerOptions options = new CompilerOptions();
level.setOptionsForCompilationLevel(options);

Result result = compiler.compile(compilerArguments.getExterns(),
compilerArguments.getInputs(), options);
Result result;
try {
result = compiler.compile(compilerArguments.getExterns(),
compilerArguments.getInputs(), options);
} catch (Throwable t) {
logger.severe(t.getMessage());
result = null;
}

if (result.success) {
builder.append(compiler.toSource());
Expand Down
4 changes: 2 additions & 2 deletions src/org/plovr/ConfigParser.java
Expand Up @@ -51,7 +51,7 @@ public static Config parseFile(File file) throws IOException {
List<String> deps = getAsStringList(map, "deps");
List<String> inputs = getAsStringList(map, "inputs");
JsonElement externsEl = map.get("externs");
List<File> externs = externsEl.isJsonNull()
List<File> externs = externsEl == null || externsEl.isJsonNull()
? null
: Lists.transform(getAsStringList(map, "externs"), STRING_TO_FILE);
Manifest manifest = new Manifest(closureLibraryDirectory,
Expand All @@ -62,7 +62,7 @@ public static Config parseFile(File file) throws IOException {
// Extract the Compiler options.
CompilationLevel level = CompilationLevel.SIMPLE_OPTIMIZATIONS;
JsonElement optionsEl = map.get("options");
if (optionsEl.isJsonObject()) {
if (optionsEl != null && optionsEl.isJsonObject()) {
// JsonObject options = optionsEl.getAsJsonObject();
// JsonElement levelEl = options.get("level");
// if (levelEl.isJsonPrimitive() && levelEl.getAsJsonPrimitive().isString()) {
Expand Down
7 changes: 7 additions & 0 deletions src/org/plovr/Main.java
Expand Up @@ -2,6 +2,8 @@

import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
* {@link Main} kicks off the plovr buildr.
Expand All @@ -10,6 +12,8 @@
*/
public class Main {

private Main() {}

/**
* Runs on port 9810 by default. (Eventually there will be a real flags
* architecture behind this.)
Expand All @@ -21,6 +25,9 @@ public static void main(String[] args) throws IOException {
System.exit(1);
return;
}

// The Compiler logging statements produce too much output.
Logger.getLogger("com.google.javascript.jscomp").setLevel(Level.OFF);

// Register all of the configs.
CompilationServer server = new CompilationServer(9810);
Expand Down
20 changes: 12 additions & 8 deletions src/org/plovr/Manifest.java
Expand Up @@ -21,7 +21,8 @@

/**
* {@link Manifest} represents an ordered list of JavaScript inputs to the
* Closure Compiler, along with a set of externs.
* Closure Compiler, along with a set of externs. This list is derived from the
* transitive closure of the dependencies from a set of input files.
*
* @author bolinfest@gmail.com (Michael Bolin)
*/
Expand Down Expand Up @@ -88,9 +89,8 @@ public CompilerArguments getCompilerArguments() {
}

private List<JSSourceFile> getDefaultExterns() {
// TODO(bolinfest): Implement this.
logger.info("Returning default externs");
return null;
logger.fine("Using default externs");
return ResourceReader.getDefaultExterns();
}

private List<JsInput> getInputsInCompilationOrder() {
Expand All @@ -111,10 +111,12 @@ private List<JsInput> getInputsInCompilationOrder() {
}

LinkedHashSet<JsInput> compilerInputs = new LinkedHashSet<JsInput>();
if (closureLibraryDirectory != null) {
if (closureLibraryDirectory == null) {
compilerInputs.add(ResourceReader.getBaseJs());
} else {
String path = "base.js";
JsInput base = new JsSourceFile(path, new File(closureLibraryDirectory, path));
compilerInputs.add(base);
compilerInputs.add(base);
}
for (JsInput requiredInput : requiredInputs) {
buildDependencies(provideToSource, compilerInputs, requiredInput);
Expand All @@ -138,8 +140,10 @@ private void buildDependencies(Map<String, JsInput> provideToSource,
private Set<JsInput> getAllDependencies() {
Set<JsInput> allDependencies = Sets.newHashSet();
final boolean includeSoy = true;
if (closureLibraryDirectory != null) {
allDependencies.addAll(getFiles(closureLibraryDirectory, includeSoy));
if (closureLibraryDirectory == null) {
allDependencies.addAll(ResourceReader.getClosureLibrarySources());
} else {
allDependencies.addAll(getFiles(closureLibraryDirectory, includeSoy));
}
allDependencies.addAll(getFiles(dependencies, includeSoy));
allDependencies.addAll(requiredInputs);
Expand Down
46 changes: 46 additions & 0 deletions src/org/plovr/ResourceJsInput.java
@@ -0,0 +1,46 @@
package org.plovr;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.logging.Logger;

import com.google.common.io.LineReader;

/**
* {@link ResourceJsInput} represents a JavaScript file loaded from a JAR, so
* it will never change, so its dependencies must only be read once.
*
* @author bolinfest@gmail.com (Michael Bolin)
*/
class ResourceJsInput extends AbstractJsInput {

private static final Logger logger = Logger.getLogger("org.plovr.ResourceJsInput");

private final String pathToResource;

ResourceJsInput(String pathToResource) {
super(pathToResource);
this.pathToResource = pathToResource;
}

@Override
public String getCode() {
try {
InputStream input = ResourceReader.class.getResourceAsStream(
pathToResource);
Readable readable = new InputStreamReader(input);
LineReader lineReader = new LineReader(readable);
StringBuilder builder = new StringBuilder();
String line;
while ((line = lineReader.readLine()) != null) {
builder.append(line + "\n");
}
return builder.toString();
} catch (IOException e) {
logger.severe(e.getMessage());
throw new RuntimeException(e);
}
}

}

0 comments on commit 4c5772d

Please sign in to comment.