Search in sources :

Example 41 with UncheckedIOException

use of org.gradle.api.UncheckedIOException in project gradle by gradle.

the class IdePlugin method addWorkspace.

protected void addWorkspace(final IdeWorkspace workspace) {
    lifecycleTask.doLast(new Action<Task>() {

        @Override
        public void execute(Task task) {
            System.out.println(String.format("Generated %s at %s", workspace.getDisplayName(), new ConsoleRenderer().asClickableFileUrl(workspace.getLocation().get().getAsFile())));
        }
    });
    Task openTask = project.getTasks().create("open" + StringUtils.capitalize(getLifecycleTaskName()));
    openTask.dependsOn(lifecycleTask);
    openTask.setGroup("IDE");
    openTask.setDescription("Opens the " + workspace.getDisplayName());
    openTask.doLast(new Action<Task>() {

        @Override
        public void execute(Task task) {
            if (OperatingSystem.current().isMacOsX()) {
                project.exec(new Action<ExecSpec>() {

                    @Override
                    public void execute(ExecSpec execSpec) {
                        execSpec.commandLine("open", workspace.getLocation().get());
                    }
                });
            } else {
                try {
                    Desktop.getDesktop().open(workspace.getLocation().get().getAsFile());
                } catch (IOException e) {
                    throw new UncheckedIOException(e);
                }
            }
        }
    });
}
Also used : Task(org.gradle.api.Task) Action(org.gradle.api.Action) UncheckedIOException(org.gradle.api.UncheckedIOException) ConsoleRenderer(org.gradle.internal.logging.ConsoleRenderer) IOException(java.io.IOException) UncheckedIOException(org.gradle.api.UncheckedIOException) ExecSpec(org.gradle.process.ExecSpec)

Example 42 with UncheckedIOException

use of org.gradle.api.UncheckedIOException in project gradle by gradle.

the class AbstractGradleExecuter method getResultAssertion.

protected Action<ExecutionResult> getResultAssertion() {
    return new Action<ExecutionResult>() {

        int expectedDeprecationWarnings = AbstractGradleExecuter.this.expectedDeprecationWarnings;

        boolean expectStackTraces = !AbstractGradleExecuter.this.stackTraceChecksOn;

        boolean checkDeprecations = AbstractGradleExecuter.this.checkDeprecations;

        @Override
        public void execute(ExecutionResult executionResult) {
            String normalizedOutput = executionResult.getNormalizedOutput();
            String error = executionResult.getError();
            boolean executionFailure = isExecutionFailure(executionResult);
            // for tests using rich console standard out and error are combined in output of execution result
            if (executionFailure) {
                normalizedOutput = removeExceptionStackTraceForFailedExecution(normalizedOutput);
            }
            validate(normalizedOutput, "Standard output");
            if (executionFailure) {
                error = removeExceptionStackTraceForFailedExecution(error);
            }
            validate(error, "Standard error");
            if (expectedDeprecationWarnings > 0) {
                throw new AssertionError(String.format("Expected %d more deprecation warnings", expectedDeprecationWarnings));
            }
        }

        private boolean isErrorOutEmpty(String error) {
            // See: https://github.com/gradle/performance/issues/375#issuecomment-315103861
            return Strings.isNullOrEmpty(error.replaceAll("(?m)^SLF4J: .*", "").trim());
        }

        private boolean isExecutionFailure(ExecutionResult executionResult) {
            return executionResult instanceof ExecutionFailure;
        }

        // Axe everything after the expected exception
        private String removeExceptionStackTraceForFailedExecution(String text) {
            int pos = text.indexOf("* Exception is:");
            if (pos >= 0) {
                text = text.substring(0, pos);
            }
            return text;
        }

        private void validate(String output, String displayName) {
            List<String> lines;
            try {
                lines = CharSource.wrap(output).readLines();
            } catch (IOException e) {
                throw new UncheckedIOException(e);
            }
            int i = 0;
            while (i < lines.size()) {
                String line = lines.get(i);
                if (line.matches(".*use(s)? or override(s)? a deprecated API\\.")) {
                    // A javac warning, ignore
                    i++;
                } else if (line.contains(UnsupportedJavaRuntimeException.JAVA7_DEPRECATION_WARNING)) {
                    if (!java7DeprecationWarningShouldExist()) {
                        throw new AssertionError(String.format("%s line %d contains unexpected deprecation warning: %s%n=====%n%s%n=====%n", displayName, i + 1, line, output));
                    }
                    // skip over stack trace
                    i++;
                    while (i < lines.size() && STACK_TRACE_ELEMENT.matcher(lines.get(i)).matches()) {
                        i++;
                    }
                } else if (isJava7DeprecationTruncatedDaemonLog(lines, i, line)) {
                    // skip over stack trace
                    i++;
                    while (i < lines.size() && STACK_TRACE_ELEMENT.matcher(lines.get(i)).matches()) {
                        i++;
                    }
                } else if (isDeprecationMessageInHelpDescription(line)) {
                    i++;
                } else if (line.matches(".*\\s+deprecated.*")) {
                    if (checkDeprecations && expectedDeprecationWarnings <= 0) {
                        throw new AssertionError(String.format("%s line %d contains a deprecation warning: %s%n=====%n%s%n=====%n", displayName, i + 1, line, output));
                    }
                    expectedDeprecationWarnings--;
                    // skip over stack trace
                    i++;
                    while (i < lines.size() && STACK_TRACE_ELEMENT.matcher(lines.get(i)).matches()) {
                        i++;
                    }
                } else if (!expectStackTraces && STACK_TRACE_ELEMENT.matcher(line).matches() && i < lines.size() - 1 && STACK_TRACE_ELEMENT.matcher(lines.get(i + 1)).matches()) {
                    // 2 or more lines that look like stack trace elements
                    throw new AssertionError(String.format("%s line %d contains an unexpected stack trace: %s%n=====%n%s%n=====%n", displayName, i + 1, line, output));
                } else {
                    i++;
                }
            }
        }

        private boolean isDeprecationMessageInHelpDescription(String s) {
            return s.matches(".*\\[deprecated.*]");
        }
    };
}
Also used : ClosureBackedAction(org.gradle.api.internal.ClosureBackedAction) Action(org.gradle.api.Action) UncheckedIOException(org.gradle.api.UncheckedIOException) UncheckedIOException(org.gradle.api.UncheckedIOException) IOException(java.io.IOException)

Example 43 with UncheckedIOException

use of org.gradle.api.UncheckedIOException in project gradle by gradle.

the class GroupedOutputFixture method stripAnsiCodes.

private String stripAnsiCodes(String output) {
    try {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        IOUtils.copy(new StringReader(output), new AnsiOutputStream(baos));
        return baos.toString();
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    }
}
Also used : AnsiOutputStream(org.fusesource.jansi.AnsiOutputStream) StringReader(java.io.StringReader) UncheckedIOException(org.gradle.api.UncheckedIOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) UncheckedIOException(org.gradle.api.UncheckedIOException)

Example 44 with UncheckedIOException

use of org.gradle.api.UncheckedIOException in project gradle by gradle.

the class GradleVsMavenBuildExperimentRunner method executeWithFileLogging.

private void executeWithFileLogging(MavenBuildExperimentSpec experiment, ExecAction mavenInvocation) {
    File log = new File(experiment.getWorkingDirectory(), "log.txt");
    OutputStream out = null;
    try {
        out = new FileOutputStream(log, true);
        mavenInvocation.setErrorOutput(out);
        mavenInvocation.setStandardOutput(out);
        mavenInvocation.execute();
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    } finally {
        CompositeStoppable.stoppable(out).stop();
    }
}
Also used : OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) UncheckedIOException(org.gradle.api.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(org.gradle.api.UncheckedIOException) File(java.io.File)

Example 45 with UncheckedIOException

use of org.gradle.api.UncheckedIOException in project gradle by gradle.

the class RhinoWorkerUtils method parse.

public static Scriptable parse(File source, String encoding, Action<Context> contextConfig) {
    Context context = Context.enter();
    if (contextConfig != null) {
        contextConfig.execute(context);
    }
    Scriptable scope = context.initStandardObjects();
    try {
        Reader reader = new InputStreamReader(new FileInputStream(source), encoding);
        try {
            context.evaluateReader(scope, reader, source.getName(), 0, null);
        } finally {
            reader.close();
        }
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    } finally {
        Context.exit();
    }
    return scope;
}
Also used : Context(org.mozilla.javascript.Context) InputStreamReader(java.io.InputStreamReader) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) UncheckedIOException(org.gradle.api.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(org.gradle.api.UncheckedIOException) Scriptable(org.mozilla.javascript.Scriptable) FileInputStream(java.io.FileInputStream)

Aggregations

UncheckedIOException (org.gradle.api.UncheckedIOException)101 IOException (java.io.IOException)79 File (java.io.File)32 InputStream (java.io.InputStream)9 FileOutputStream (java.io.FileOutputStream)7 OutputStream (java.io.OutputStream)7 BufferedReader (java.io.BufferedReader)6 StringReader (java.io.StringReader)6 FileInputStream (java.io.FileInputStream)5 Matcher (java.util.regex.Matcher)5 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 URI (java.net.URI)4 URL (java.net.URL)4 ArrayList (java.util.ArrayList)4 Manifest (java.util.jar.Manifest)4 ZipInputStream (java.util.zip.ZipInputStream)4 FileVisitDetails (org.gradle.api.file.FileVisitDetails)4 ImmutableSortedMap (com.google.common.collect.ImmutableSortedMap)3 ByteArrayInputStream (java.io.ByteArrayInputStream)3 FileReader (java.io.FileReader)3