Example usage for com.google.gwt.dev.util Util recursiveDelete

List of usage examples for com.google.gwt.dev.util Util recursiveDelete

Introduction

In this page you can find the example usage for com.google.gwt.dev.util Util recursiveDelete.

Prototype

public static void recursiveDelete(File file, boolean childrenOnly) 

Source Link

Document

Deletes a file or recursively deletes a directory.

Usage

From source file:gwt.ns.webworker.linker.WorkerCompiler.java

License:Apache License

/**
 * Compiles requests in new process.//from  w ww.  jav  a2 s .  co m
 * 
 * @param logger
 * @param requests
 * @return Compiled worker scripts.
 * @throws UnableToCompleteException
 */
private static SortedMap<WorkerRequestArtifact, String> runCompiler(TreeLogger logger,
        SortedSet<WorkerRequestArtifact> requests) throws UnableToCompleteException {

    List<String> commands = new ArrayList<String>();
    commands.add("java");

    // flag child process as recursive. value unimportant
    commands.add("-D" + RECURSION_FLAG_PROP + "=" + "true");

    //inherit classpath from this process
    commands.add("-cp");
    commands.add(System.getProperty("java.class.path"));

    commands.add("com.google.gwt.dev.Compiler");

    // TODO: best practices for fixed directory that will be erased?
    // destination war directory
    commands.add("-war");
    commands.add(TEMP_WAR_DIR_NAME);

    // TODO: user specified compiler options...workers, output style, etc
    // are these even visible?

    for (WorkerRequestArtifact req : requests) {
        commands.add(req.getCanonicalName());
    }

    ProcessBuilder compileBuilder = new ProcessBuilder(commands);
    compileBuilder.redirectErrorStream(true);

    TreeLogger compLogger = logger.branch(TreeLogger.INFO, "Recursively compiling Worker modules...");

    // default print command so build system mismatches are obvious
    StringBuilder buf = new StringBuilder();
    for (String com : commands) {
        buf.append(com + " ");
    }
    logger.log(TreeLogger.INFO, "Executing cmd: \"" + buf.toString() + "\"");

    Process compile;
    try {
        compile = compileBuilder.start();
    } catch (IOException e) {
        compLogger.log(TreeLogger.ERROR, "Unable to compile.", e);
        throw new UnableToCompleteException();
    }

    // new thread for piping compiler output to logger
    PipeOutput pipe = new PipeOutput(compLogger, compile.getInputStream());
    new Thread(pipe).start();

    int exitValue;
    try {
        // block until compiler finished
        exitValue = compile.waitFor();
    } catch (InterruptedException e) {
        compLogger.log(TreeLogger.ERROR, "Thread interrupted while waiting for compilation.", e);
        throw new UnableToCompleteException();
    }
    if (exitValue != 0) {
        compLogger.log(TreeLogger.ERROR, "Error in compilation. See previous error.");
        throw new UnableToCompleteException();
    }

    SortedMap<WorkerRequestArtifact, String> workerScripts = new TreeMap<WorkerRequestArtifact, String>();

    for (WorkerRequestArtifact req : requests) {
        assert (!workerScripts.containsKey(req)) : "Module " + req.getName() + " was likely compiled twice.";

        // TODO: requires knowledge of linker used to package worker
        // query that linker to find file? needs to be more robust if
        // allowing more perms, etc, anyway
        String name = req.getName();
        File scriptFile = new File(TEMP_WAR_DIR,
                name + File.separator + name + WorkerRequestArtifact.WORKER_EXTENSION);
        if (!scriptFile.isFile()) {
            compLogger.log(TreeLogger.ERROR, "Script file " + scriptFile.getPath()
                    + " not found as expected. This is likely because the build system is not flexible enough to fit your needs. File an issue!");
            throw new UnableToCompleteException();
        }
        String script = Util.readFileAsString(scriptFile);
        workerScripts.put(req, script);
    }

    // delete temp directory
    // TODO: if this was a temp directory, this would feel a lot safer
    Util.recursiveDelete(TEMP_WAR_DIR, false);

    if (workerScripts.isEmpty()) {
        workerScripts = null;
    }

    return workerScripts;
}