forked from detekt/detekt
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add multiplex workers support (detekt#46)
- Loading branch information
1 parent
ec4784a
commit db1903f
Showing
24 changed files
with
411 additions
and
123 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 32 additions & 34 deletions
66
detekt/wrapper/src/main/kotlin/io/buildfoundation/bazel/rulesdetekt/wrapper/Application.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,42 @@ | ||
package io.buildfoundation.bazel.rulesdetekt.wrapper | ||
|
||
import bazel.worker.WorkerProtocol | ||
import kotlin.system.exitProcess | ||
|
||
interface Application { | ||
|
||
fun run(arguments: Array<String>) | ||
|
||
class Worker(private val executor: SandboxExecutor) : Application { | ||
|
||
override fun run(arguments: Array<String>) { | ||
while (true) { | ||
val result = WorkerProtocol.WorkRequest.parseDelimitedFrom(System.`in`).let { | ||
executor.execute(it.argumentsList.toTypedArray()) | ||
} | ||
|
||
WorkerProtocol.WorkResponse.newBuilder() | ||
.apply { | ||
if (result.code != 0) { | ||
output = listOf(result.stdout, result.stderr).joinToString(separator = "\n") | ||
} | ||
} | ||
.setExitCode(result.code) | ||
.build() | ||
.writeDelimitedTo(System.out) | ||
} | ||
} | ||
} | ||
import io.reactivex.Scheduler | ||
|
||
internal interface Application { | ||
|
||
class OneShot(private val executor: SandboxExecutor) : Application { | ||
fun run(args: Array<String>) | ||
|
||
override fun run(arguments: Array<String>) { | ||
val result = executor.execute(arguments) | ||
class OneShot( | ||
private val executable: Executable, | ||
private val streams: Streams, | ||
private val platform: Platform | ||
) : Application { | ||
|
||
if (result.code != 0) { | ||
System.out.println(result.stdout) | ||
System.err.println(result.stderr) | ||
override fun run(args: Array<String>) { | ||
val result = executable.execute(args) | ||
|
||
if (result is Result.Failure) { | ||
streams.error.println(result.description) | ||
} | ||
|
||
exitProcess(result.code) | ||
platform.exit(result.consoleStatusCode) | ||
} | ||
} | ||
|
||
class Worker( | ||
private val scheduler: Scheduler, | ||
private val executable: WorkerExecutable, | ||
private val streams: WorkerStreams | ||
) : Application { | ||
|
||
override fun run(args: Array<String>) { | ||
streams.request | ||
.subscribeOn(scheduler) | ||
.parallel() | ||
.runOn(scheduler) | ||
.map { executable.execute(it) } | ||
.sequential() | ||
.blockingSubscribe(streams.response) | ||
} | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
detekt/wrapper/src/main/kotlin/io/buildfoundation/bazel/rulesdetekt/wrapper/Detekt.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package io.buildfoundation.bazel.rulesdetekt.wrapper | ||
|
||
import io.gitlab.arturbosch.detekt.cli.buildRunner | ||
import java.io.PrintStream | ||
|
||
internal interface Detekt { | ||
|
||
fun execute(args: Array<String>, outputPrinter: PrintStream, errorPrinter: PrintStream) | ||
|
||
class Impl : Detekt { | ||
|
||
override fun execute(args: Array<String>, outputPrinter: PrintStream, errorPrinter: PrintStream) { | ||
buildRunner(args, outputPrinter, errorPrinter).execute() | ||
} | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
detekt/wrapper/src/main/kotlin/io/buildfoundation/bazel/rulesdetekt/wrapper/Executable.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package io.buildfoundation.bazel.rulesdetekt.wrapper | ||
|
||
import java.io.ByteArrayOutputStream | ||
import java.io.PrintStream | ||
import java.nio.charset.Charset | ||
|
||
internal interface Executable { | ||
|
||
fun execute(args: Array<String>): Result | ||
|
||
class DetektImpl(private val detekt: Detekt) : Executable { | ||
|
||
override fun execute(args: Array<String>): Result { | ||
val outputPrinter = PrintStream(Streams.DevNullOutputStream().buffered()) | ||
|
||
val errorPrinterBuffer = ByteArrayOutputStream() | ||
val errorPrinter = PrintStream(errorPrinterBuffer.buffered()) | ||
|
||
return try { | ||
detekt.execute(args, outputPrinter, errorPrinter) | ||
|
||
Result.Success | ||
} catch (e: Exception) { | ||
e.printStackTrace(errorPrinter) | ||
errorPrinter.flush() | ||
|
||
Result.Failure(errorPrinterBuffer.toString(Charset.defaultCharset())) | ||
} finally { | ||
outputPrinter.close() | ||
errorPrinter.close() | ||
} | ||
} | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
detekt/wrapper/src/main/kotlin/io/buildfoundation/bazel/rulesdetekt/wrapper/Platform.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package io.buildfoundation.bazel.rulesdetekt.wrapper | ||
|
||
import kotlin.system.exitProcess | ||
|
||
internal interface Platform { | ||
|
||
fun exit(code: Int) | ||
|
||
class Impl : Platform { | ||
|
||
override fun exit(code: Int) = exitProcess(code) | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
detekt/wrapper/src/main/kotlin/io/buildfoundation/bazel/rulesdetekt/wrapper/Result.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package io.buildfoundation.bazel.rulesdetekt.wrapper | ||
|
||
internal sealed class Result { | ||
object Success : Result() | ||
data class Failure(val description: String) : Result() | ||
|
||
val consoleStatusCode = when (this) { | ||
is Success -> 0 | ||
is Failure -> 1 | ||
} | ||
} |
60 changes: 0 additions & 60 deletions
60
...t/wrapper/src/main/kotlin/io/buildfoundation/bazel/rulesdetekt/wrapper/SandboxExecutor.kt
This file was deleted.
Oops, something went wrong.
21 changes: 0 additions & 21 deletions
21
...wrapper/src/main/kotlin/io/buildfoundation/bazel/rulesdetekt/wrapper/SandboxedExecutor.kt
This file was deleted.
Oops, something went wrong.
25 changes: 25 additions & 0 deletions
25
detekt/wrapper/src/main/kotlin/io/buildfoundation/bazel/rulesdetekt/wrapper/Streams.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package io.buildfoundation.bazel.rulesdetekt.wrapper | ||
|
||
import java.io.InputStream | ||
import java.io.OutputStream | ||
import java.io.PrintStream | ||
|
||
internal data class Streams(val input: InputStream, val output: PrintStream, val error: PrintStream) { | ||
|
||
companion object { | ||
fun system(): Streams { | ||
val systemInput = System.`in` | ||
val systemOutput = System.out | ||
val systemError = System.err | ||
|
||
// Suppressing system singleton stream to avoid accidental output from the tool. | ||
System.setOut(PrintStream(DevNullOutputStream())) | ||
|
||
return Streams(systemInput, systemOutput, systemError) | ||
} | ||
} | ||
|
||
class DevNullOutputStream : OutputStream() { | ||
override fun write(byte: Int) = Unit | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
.../wrapper/src/main/kotlin/io/buildfoundation/bazel/rulesdetekt/wrapper/WorkerExecutable.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package io.buildfoundation.bazel.rulesdetekt.wrapper | ||
|
||
import bazel.worker.WorkerProtocol.WorkRequest | ||
import bazel.worker.WorkerProtocol.WorkResponse | ||
|
||
internal interface WorkerExecutable { | ||
|
||
fun execute(request: WorkRequest): WorkResponse | ||
|
||
class Impl(private val executable: Executable) : WorkerExecutable { | ||
|
||
override fun execute(request: WorkRequest): WorkResponse { | ||
val result = executable.execute(request.argumentsList.toTypedArray()) | ||
|
||
val resultOutput = when (result) { | ||
is Result.Success -> WorkResponse.getDefaultInstance().output | ||
is Result.Failure -> result.description | ||
} | ||
|
||
return WorkResponse.newBuilder() | ||
.setRequestId(request.requestId) | ||
.setOutput(resultOutput) | ||
.setExitCode(result.consoleStatusCode) | ||
.build() | ||
} | ||
} | ||
} |
Oops, something went wrong.