diff --git a/pride-core/src/main/groovy/com/prezi/gradle/pride/PrideInitializer.groovy b/pride-core/src/main/groovy/com/prezi/gradle/pride/PrideInitializer.groovy index 91e8113..2e81741 100644 --- a/pride-core/src/main/groovy/com/prezi/gradle/pride/PrideInitializer.groovy +++ b/pride-core/src/main/groovy/com/prezi/gradle/pride/PrideInitializer.groovy @@ -1,5 +1,6 @@ package com.prezi.gradle.pride +import com.prezi.gradle.pride.internal.LoggerOutputStream import org.gradle.tooling.GradleConnector import org.gradle.tooling.model.gradle.GradleBuild import org.slf4j.Logger @@ -58,7 +59,12 @@ class PrideInitializer { def relativePath = prideDirectory.toURI().relativize(moduleDirectory.toURI()).toString() // Load the model for the build - GradleBuild build = connection.getModel(GradleBuild) + def builder = connection.model(GradleBuild) + // Redirect output to loggers + // Won't work until http://issues.gradle.org/browse/GRADLE-2687 + builder.standardError = new LoggerOutputStream({ log.error("{}", it) }) + builder.standardOutput = new LoggerOutputStream({ log.info("{}", it) }) + GradleBuild build = builder.get() // Merge settings settingsFile << "\n// Settings from project in directory /${relativePath}\n\n" diff --git a/pride-core/src/main/groovy/com/prezi/gradle/pride/internal/LoggerOutputStream.groovy b/pride-core/src/main/groovy/com/prezi/gradle/pride/internal/LoggerOutputStream.groovy new file mode 100644 index 0000000..47d600d --- /dev/null +++ b/pride-core/src/main/groovy/com/prezi/gradle/pride/internal/LoggerOutputStream.groovy @@ -0,0 +1,70 @@ +package com.prezi.gradle.pride.internal + +/** + * Based on Ant's LineOrientedOutputStream. + */ +public class LoggerOutputStream extends OutputStream { + + private static final int CR = 0x0d + private static final int LF = 0x0a + + private final def buffer = new ByteArrayOutputStream(256) + private boolean skip = false + + private final Closure logLine + + public LoggerOutputStream(Closure logLine) { + this.logLine = logLine + } + + protected void processLine() { + try { + logLine(new String(buffer.toByteArray())) + } finally { + buffer.reset(); + } + } + + public void close() { + if (buffer.size() > 0) { + processLine() + } + super.close() + } + + public final void write(int cc) { + final byte c = (byte) cc + if ((c == LF) || (c == CR)) { + if (!skip) { + processLine() + } + } else { + buffer.write(cc) + } + skip = (c == CR) + } + + public final void write(byte[] b, int off, int len) throws IOException { + // find the line breaks and pass other chars through in blocks + int offset = off + int blockStartOffset = offset + int remaining = len + while (remaining > 0) { + while (remaining > 0 && b[offset] != LF && b[offset] != CR) { + offset++ + remaining-- + } + // either end of buffer or a line separator char + int blockLength = offset - blockStartOffset + if (blockLength > 0) { + buffer.write(b, blockStartOffset, blockLength) + } + while (remaining > 0 && (b[offset] == LF || b[offset] == CR)) { + write(b[offset]) + offset++ + remaining-- + } + blockStartOffset = offset + } + } +}