/** * This method is the main manager of the linking process. It should only be run if an "outfile" * has been provided in the configuration. It will attempt to link all the files in the objdir * into a simple executable/library. */ private void link() { // generate the command line Commandline commandline = generateLinkCommand(); // create the execution object Execute runner = new Execute( new LogStreamHandler(configuration.getTask(), Project.MSG_INFO, Project.MSG_WARN)); runner.setCommandline(commandline.getCommandline()); // run the command try { task.log("Starting Link "); task.log(commandline.toString(), Project.MSG_DEBUG); int exitValue = runner.execute(); if (exitValue != 0) throw new BuildException("Link Failed, (exit value: " + exitValue + ")"); } catch (IOException e) { String msg = "There was a problem running the linker, this usually occurs when the " + "linker can't be found, make sure it is on your path. full error: " + e.getMessage(); throw new BuildException(msg, e); } task.log("Link complete. Library in directory: " + configuration.getOutputDirectory()); task.log(""); // a little bit of space }
private void runForked(String[] commandLine) { // use the main method in FileSystemCompiler final Execute executor = new Execute(); // new LogStreamHandler ( attributes , Project.MSG_INFO , Project.MSG_WARN ) // ) ; executor.setAntRun(getProject()); executor.setWorkingDirectory(getProject().getBaseDir()); executor.setCommandline(commandLine); try { executor.execute(); } catch (final IOException ioe) { throw new BuildException("Error running forked groovyc.", ioe); } final int returnCode = executor.getExitValue(); if (returnCode != 0) { taskSuccess = false; if (errorProperty != null) { getProject().setNewProperty(errorProperty, "true"); } if (failOnError) { throw new BuildException("Forked groovyc returned error code: " + returnCode); } else { log.error("Forked groovyc returned error code: " + returnCode); } } }
protected Flags runDiff( SourceFile vmFile, SourceFile cpFile, String diffFileName, Map<String, String> packageDiffs) throws IOException, InterruptedException { final String[] cmd = { "diff", "-b", // Ignore white space change // "-E", // Ignore changes due to tab expansion // "-w", // Ignore all white space change // "-B", // Ignore changes whose lines are all blank // "-N", // Treat absent files as empty "-au", "-I", ".*$" + "Id:.*$.*", // Avoid cvs keyword // expansion in this string vmFile.getFileName(), cpFile.getFile().getAbsolutePath() }; final ByteArrayOutputStream out = new ByteArrayOutputStream(); final ByteArrayOutputStream err = new ByteArrayOutputStream(); final PumpStreamHandler streamHandler = new PumpStreamHandler(out, err); final Execute exe = new Execute(streamHandler); exe.setCommandline(cmd); exe.setWorkingDirectory(vmFile.getBaseDir()); final int rc = exe.execute(); if ((rc != 0) && (out.size() > 0)) { File diffFile = new File(destDir, diffFileName); FileOutputStream os = new FileOutputStream(diffFile); try { final byte[] diff = out.toByteArray(); os.write(diff); os.flush(); final String diffStr = new String(diff); final String pkg = vmFile.getPackageName(); String pkgDiff; if (packageDiffs.containsKey(pkg)) { pkgDiff = packageDiffs.get(pkg); pkgDiff = pkgDiff + "diff\n" + diffStr; } else { pkgDiff = diffStr; } packageDiffs.put(pkg, pkgDiff); Flags flags = getFlags(diffStr); if (!vmFile.getTarget().equals(cpFile.getTarget())) { flags.set(FLAG_TARGET_DIFF); } flags.set(NEEDS_MERGE); return flags; } finally { os.close(); } } else { return new Flags(NO_CHANGE); } }
private int run(String[] command) throws BuildException { Execute exe = new Execute(new LogStreamHandler(this, Project.MSG_INFO, Project.MSG_WARN), null); exe.setAntRun(project); exe.setWorkingDirectory(dir); exe.setCommandline(command); try { return exe.execute(); } catch (IOException e) { throw new BuildException(e, location); } }
public String execute(String[] commandLine, File sourceCodeDirectory) throws IOException { StringOutputStream stringOutputStream = new StringOutputStream(); Execute execute = new Execute(new PumpStreamHandler(stringOutputStream)); execute.setWorkingDirectory(sourceCodeDirectory); execute.setCommandline(commandLine); execute.execute(); String output = stringOutputStream.toString(); stringOutputStream.close(); return output; }
public void run() { Commandline theCommand; // get the name of the output file File ofile = helper.getOFile(objectDirectory, sourceFile); if (sourceFile.getName().endsWith(".rc")) { // Is this a win32 resource file? // create the full command theCommand = new Commandline(); theCommand.setExecutable("windres"); theCommand.createArgument().setValue("-i"); theCommand.createArgument().setFile(sourceFile); theCommand.createArgument().setValue("-J"); theCommand.createArgument().setValue("rc"); theCommand.createArgument().setValue("-o"); theCommand.createArgument().setFile(ofile); theCommand.createArgument().setValue("-O"); theCommand.createArgument().setValue("coff"); } else { // create the full command theCommand = (Commandline) command.clone(); theCommand.createArgument().setFile(sourceFile); theCommand.createArgument().setValue("-o"); theCommand.createArgument().setFile(ofile); } // create the execution object Execute runner = new Execute( new LogStreamHandler(configuration.getTask(), Project.MSG_INFO, Project.MSG_WARN)); // runner.setAntRun( project ); runner.setCommandline(theCommand.getCommandline()); // run the command try { task.log(" " + sourceFile.getName()); task.log(theCommand.toString(), Project.MSG_DEBUG); int exitValue = runner.execute(); if (exitValue != 0) { throw new BuildException("Compile Failed, (exit value: " + exitValue + ")"); } } catch (IOException e) { String msg = "There was a problem running the compiler, this usually occurs when " + "it can't be found, make sure it is on your path. full error: " + e.getMessage(); throw new BuildException(msg, e); } }
private int executeRunnerClassAsForked() throws BuildException { CommandlineJava cmd = initializeJavaCommand(); Execute execute = new Execute(new LogStreamHandler(this, Project.MSG_INFO, Project.MSG_WARN)); execute.setCommandline(cmd.getCommandline()); execute.setNewenvironment(false); execute.setAntRun(getProject()); log(cmd.describeCommand(), Project.MSG_VERBOSE); int retVal; try { retVal = execute.execute(); } catch (IOException e) { throw new BuildException("Process fork failed.", e, getLocation()); } return retVal; }
/** execute in a forked VM */ private int run(String[] command) throws BuildException { PumpStreamHandler psh = new PumpStreamHandler( new LogOutputStream(this, Project.MSG_INFO), new TeeOutputStream(new LogOutputStream(this, Project.MSG_WARN), bos)); Execute exe = new Execute(psh, null); exe.setAntRun(getProject()); if (workingdir != null) { exe.setWorkingDirectory(workingdir); } exe.setCommandline(command); try { return exe.execute(); } catch (IOException e) { throw new BuildException(e, getLocation()); } finally { FileUtils.close(bos); } }
private void executeCommand(Commandline command) { try { Execute exe; if (this.xml != null) { String xml = getProject().replaceProperties(this.xml); InputStream bis = new ByteArrayInputStream(xml.getBytes()); ExecuteStreamHandler sh = new PumpStreamHandler(System.out, System.err, bis); exe = new Execute(sh); } else { exe = new Execute(); } exe.setAntRun(getProject()); exe.setWorkingDirectory(getProject().getBaseDir()); exe.setCommandline(command.getCommandline()); log(command.toString()); int r = exe.execute(); if (r < 0) { throw new BuildException("Processing error!", getLocation()); } } catch (IOException e) { throw new BuildException( "Error running " + command.getCommandline()[0] + " compiler.", e, getLocation()); } }
@Override public void execute() throws BuildException { // Source directory defaults to current directory if (_sourceDir == null) { _sourceDir = getProject().getBaseDir(); } // Destination directory defaults to source directory if (_destDir == null) { _destDir = _sourceDir; } // Check the directories checkDir("Source directory", _sourceDir, true, false); checkDir("Destination directory", _destDir, false, true); // Create a watch dog, if there is a time-out configured ExecuteWatchdog watchdog = (_timeOut > 0L) ? new ExecuteWatchdog(_timeOut) : null; // Determine what command to execute String command = (_command == null || _command.length() < 1) ? DEFAULT_COMMAND : _command; // Check that the command is executable Buffer buffer = new Buffer(); Execute execute = new Execute(buffer, watchdog); String[] cmdline = new String[] {command, "-v"}; execute.setAntRun(getProject()); execute.setCommandline(cmdline); try { if (execute.execute() != 0) { throw new BuildException( "Unable to execute LessCSS command " + quote(command) + ". Running '" + command + " -v' resulted in exit code " + execute.getExitValue() + '.'); } } catch (IOException cause) { throw new BuildException("Unable to execute LessCSS command " + quote(command) + '.', cause); } // Display the command and version number String versionString = buffer.getOutString().trim(); if (versionString.startsWith(command)) { versionString = versionString.substring(command.length()).trim(); } if (versionString.startsWith("v")) { versionString = versionString.substring(1).trim(); } log( "Using command " + quote(command) + ", version is " + quote(versionString) + '.', MSG_VERBOSE); // TODO: Improve this, detect kind of command boolean createOutputFile = command.indexOf("plessc") >= 0; // Preparations done, consider each individual file for processing log( "Transforming from " + _sourceDir.getPath() + " to " + _destDir.getPath() + '.', MSG_VERBOSE); long start = System.currentTimeMillis(); int failedCount = 0, successCount = 0, skippedCount = 0; for (String inFileName : getDirectoryScanner(_sourceDir).getIncludedFiles()) { // Make sure the input file exists File inFile = new File(_sourceDir, inFileName); if (!inFile.exists()) { continue; } // Determine if the file type is supported if (!matches(inFileName.toLowerCase(), "\\.less$")) { log( "Skipping " + quote(inFileName) + " because the file does not end in \".less\" (case-insensitive).", MSG_VERBOSE); skippedCount++; continue; } // Some preparations related to the input file and output file long thisStart = System.currentTimeMillis(); String outFileName = inFile.getName().replaceFirst("\\.less$", ".css"); File outFile = new File(_destDir, outFileName); String outFilePath = outFile.getPath(); String inFilePath = inFile.getPath(); if (!_overwrite) { // Skip this file is the output file exists and is newer if (outFile.exists() && (outFile.lastModified() > inFile.lastModified())) { log("Skipping " + quote(inFileName) + " because output file is newer.", MSG_VERBOSE); skippedCount++; continue; } } // Prepare for the command execution buffer = new Buffer(); watchdog = (_timeOut > 0L) ? new ExecuteWatchdog(_timeOut) : null; execute = new Execute(buffer, watchdog); cmdline = createOutputFile ? new String[] {command, inFilePath} : new String[] {command, inFilePath, outFilePath}; execute.setAntRun(getProject()); execute.setCommandline(cmdline); // Execute the command boolean failure; try { execute.execute(); failure = execute.isFailure(); } catch (IOException cause) { failure = true; } // Output to stderr or stdout indicates a failure String errorOutput = buffer.getErrString(); errorOutput = isEmpty(errorOutput) ? buffer.getOutString() : errorOutput; failure = failure ? true : !isEmpty(errorOutput); // Create the output file if the command just sent everything to // standard out if (createOutputFile) { try { buffer.writeOutTo(new FileOutputStream(outFile)); } catch (IOException cause) { throw new BuildException( "Failed to write output to file \"" + outFile.getPath() + "\".", cause); } } // Log the result for this individual file long thisDuration = System.currentTimeMillis() - thisStart; if (failure) { String logMessage = "Failed to transform " + quote(inFilePath); if (isEmpty(errorOutput)) { logMessage += '.'; } else { logMessage += ':' + System.getProperty("line.separator") + errorOutput; } log(logMessage, MSG_ERR); failedCount++; } else { log("Transformed " + quote(inFileName) + " in " + thisDuration + " ms.", MSG_VERBOSE); successCount++; } } // Log the total result long duration = System.currentTimeMillis() - start; if (failedCount > 0) { throw new BuildException( "" + failedCount + " file(s) failed to transform, while " + successCount + " succeeded. Total duration is " + duration + " ms."); } else { log( "" + successCount + " file(s) transformed in " + duration + " ms; " + skippedCount + " file(s) skipped."); } }