/** * Runs the pub command. * * @return the result of running the pub command */ public IStatus runSilent(IProgressMonitor monitor) { try { // Build the process description to run pub DartSdk sdk = DartSdkManager.getManager().getSdk(); File pubFile = sdk.getPubExecutable(); ProcessBuilder builder = new ProcessBuilder(); builder.directory(container.getLocation().toFile()); builder.redirectErrorStream(true); List<String> args = new ArrayList<String>(); if (DartCore.isMac()) { args.add("/bin/bash"); args.add("--login"); args.add("-c"); args.add("\"" + pubFile.getAbsolutePath() + "\"" + " " + command); } else { args.add(pubFile.getAbsolutePath()); args.add(command); } builder.command(args); // Run the pub command as an external process. ProcessRunner runner = newProcessRunner(builder); try { runner.runSync(monitor); } catch (IOException e) { String message = NLS.bind(PubMessages.RunPubJob_failed, command, e.toString()); return new Status(IStatus.CANCEL, DartCore.PLUGIN_ID, message, e); } StringBuilder stringBuilder = new StringBuilder(); if (!runner.getStdOut().isEmpty()) { stringBuilder.append(runner.getStdOut().trim() + "\n"); // $NON-NLS-1$ } int exitCode = runner.getExitCode(); if (exitCode != 0) { String output = "[" + exitCode + "] " + stringBuilder.toString(); String message = NLS.bind(PubMessages.RunPubJob_failed, command, output); return new Status(IStatus.ERROR, DartCore.PLUGIN_ID, message); } try { // Refresh the Eclipse resources container.refreshLocal(IResource.DEPTH_INFINITE, monitor); } catch (CoreException e) { // Log the exception and move on DartCore.logError("Exception refreshing " + container, e); } return new Status(IStatus.OK, DartCore.PLUGIN_ID, stringBuilder.toString()); } catch (OperationCanceledException exception) { String message = NLS.bind(PubMessages.RunPubJob_canceled, command); return new Status(IStatus.CANCEL, DartCore.PLUGIN_ID, message, exception); } finally { monitor.done(); } }
/** * Run the formatter on the given input source. * * @param source the source to pass to the formatter * @param selection the selection info to pass into the formatter * @param monitor the monitor for displaying progress * @throws IOException if an exception was thrown during execution * @throws CoreException if an exception occurs in file refresh * @return the formatted source (or null in case formatting could not be executed) */ public static FormattedSource format( final String source, final Point selection, IProgressMonitor monitor) throws IOException, CoreException { File dartfmt = DartSdkManager.getManager().getSdk().getDartFmtExecutable(); if (!dartfmt.canExecute()) { return null; } if (source.length() == 0) { FormattedSource result = new FormattedSource(); result.source = source; return result; } ProcessBuilder builder = new ProcessBuilder(); List<String> args = new ArrayList<String>(); args.add(dartfmt.getPath()); if (selection != null) { args.add(ARGS_SOURCE_FLAG + " " + selection.x + "," + selection.y); } args.add(ARGS_MAX_LINE_LEN_FLAG); if (getMaxLineLengthEnabled() && getMaxLineLength().length() > 0) { args.add(getMaxLineLength()); } else { args.add("Infinity"); } args.add(ARGS_INDENT_FLAG); args.add(getInsertSpacesForTabs() ? getSpacesPerIndent() : "tab"); if (getPerformTransforms()) { args.add(ARGS_TRANSFORMS_FLAG); } args.add(ARGS_MACHINE_FORMAT_FLAG); builder.command(args); builder.redirectErrorStream(true); ProcessRunner runner = new ProcessRunner(builder) { @Override protected void processStarted(Process process) throws IOException { BufferedWriter writer = new BufferedWriter( new OutputStreamWriter(process.getOutputStream(), "UTF-8"), source.length()); writer.append(source); writer.close(); } }; runner.runSync(monitor); StringBuilder sb = new StringBuilder(); if (!runner.getStdOut().isEmpty()) { sb.append(runner.getStdOut()); } // TODO (pquitslund): better error handling if (runner.getExitCode() != 0) { sb.append(runner.getStdErr()); throw new IOException(sb.toString()); } String formattedSource = sb.toString(); try { JSONObject json = new JSONObject(formattedSource); String sourceString = (String) json.get(JSON_SOURCE_KEY); JSONObject selectionJson = (JSONObject) json.get(JSON_SELECTION_KEY); // TODO (pquitslund): figure out why we (occasionally) need to remove an extra trailing // NEWLINE if (sourceString.endsWith("\n\n")) { sourceString = sourceString.substring(0, sourceString.length() - 1); } FormattedSource result = new FormattedSource(); result.source = sourceString; result.selectionOffset = selectionJson.getInt(JSON_OFFSET_KEY); result.selectionLength = selectionJson.getInt(JSON_LENGTH_KEY); return result; } catch (JSONException e) { DartToolsPlugin.log(e); throw new IOException(e); } }