Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

clean up workers #1005

Merged
merged 17 commits into from
Apr 29, 2020
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions scala/scalafmt/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ filegroup(

scala_binary(
name = "scalafmt",
srcs = ["scalafmt/ScalafmtRunner.scala"],
main_class = "io.bazel.rules_scala.scalafmt.ScalafmtRunner",
srcs = ["scalafmt/ScalafmtWorker.scala"],
main_class = "io.bazel.rules_scala.scalafmt.ScalafmtWorker",
visibility = ["//visibility:public"],
deps = [
"//src/java/io/bazel/rulesscala/worker",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package io.bazel.rules_scala.scalafmt

import io.bazel.rulesscala.worker.{GenericWorker, Processor};
import io.bazel.rulesscala.worker.Worker
import java.io.File
import java.nio.file.Files
import org.scalafmt.Scalafmt
Expand All @@ -10,21 +10,13 @@ import scala.annotation.tailrec
import scala.collection.JavaConverters._
import scala.io.Codec

object ScalafmtRunner extends GenericWorker(new ScalafmtProcessor) {
def main(args: Array[String]) {
try run(args)
catch {
case x: Exception =>
x.printStackTrace()
System.exit(1)
}
}
}
object ScalafmtWorker extends Worker.Interface {

def main(args: Array[String]): Unit = Worker.workerMain(args, ScalafmtWorker)

class ScalafmtProcessor extends Processor {
def processRequest(args: java.util.List[String]) {
def work(args: Array[String]) {
val argName = List("config", "input", "output")
val argFile = args.asScala.map{x => new File(x)}
val argFile = args.map{x => new File(x)}
val namespace = argName.zip(argFile).toMap

val source = FileOps.readFile(namespace.getOrElse("input", new File("")))(Codec.UTF8)
Expand Down
2 changes: 1 addition & 1 deletion scala_proto/scala_proto_toolchain.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ scala_proto_toolchain = rule(
"code_generator": attr.label(
executable = True,
cfg = "host",
default = Label("@io_bazel_rules_scala//src/scala/scripts:scalapb_generator"),
default = Label("@io_bazel_rules_scala//src/scala/scripts:scalapb_worker"),
allow_files = True,
),
"named_generators": attr.string_dict(),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package io.bazel.rulesscala.coverage.instrumenter;

import io.bazel.rulesscala.jar.JarCreator;
import io.bazel.rulesscala.worker.GenericWorker;
import io.bazel.rulesscala.worker.Processor;
import io.bazel.rulesscala.worker.Worker;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
Expand All @@ -26,28 +25,18 @@
import org.jacoco.core.instr.Instrumenter;
import org.jacoco.core.runtime.OfflineInstrumentationAccessGenerator;

public final class JacocoInstrumenter implements Processor {
public final class JacocoInstrumenter implements Worker.Interface {

public static void main(String[] args) throws Exception {
(new Worker()).run(args);
}

private static final class Worker extends GenericWorker {
public Worker() {
super(new JacocoInstrumenter());
}
Worker.workerMain(args, new JacocoInstrumenter());
}

@Override
public void processRequest(List < String > args) {
public void work(String[] args) throws Exception {
Instrumenter jacoco = new Instrumenter(new OfflineInstrumentationAccessGenerator());
args.forEach(arg -> {
try {
processArg(jacoco, arg);
} catch (final Exception e) {
throw new RuntimeException(e);
}
});
for (String arg : args) {
processArg(jacoco, arg);
}
}

private void processArg(Instrumenter jacoco, String arg) throws Exception {
Expand Down
5 changes: 2 additions & 3 deletions src/java/io/bazel/rulesscala/scalac/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ java_binary(
"-source 1.8",
"-target 1.8",
],
main_class = "io.bazel.rulesscala.scalac.ScalaCInvoker",
main_class = "io.bazel.rulesscala.scalac.ScalacWorker",
visibility = ["//visibility:public"],
deps = [
":exported_scalac_repositories_from_toolchain_to_jvm",
Expand All @@ -33,8 +33,7 @@ filegroup(
srcs = [
"CompileOptions.java",
"Resource.java",
"ScalaCInvoker.java",
"ScalacProcessor.java",
"ScalacWorker.java",
],
visibility = ["//visibility:public"],
)
47 changes: 0 additions & 47 deletions src/java/io/bazel/rulesscala/scalac/ScalaCInvoker.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package io.bazel.rulesscala.scalac;

import io.bazel.rulesscala.jar.JarCreator;
import io.bazel.rulesscala.worker.GenericWorker;
import io.bazel.rulesscala.worker.Processor;
import io.bazel.rulesscala.worker.Worker;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
Expand All @@ -28,7 +27,7 @@
import scala.tools.nsc.MainClass;
import scala.tools.nsc.reporters.ConsoleReporter;

class ScalacProcessor implements Processor {
class ScalacWorker implements Worker.Interface {
private static boolean isWindows = System.getProperty("os.name").toLowerCase().contains("windows");

/** This is the reporter field for scalac, which we want to access */
Expand All @@ -43,11 +42,15 @@ class ScalacProcessor implements Processor {
}
}

public static void main(String[] args) throws Exception {
Worker.workerMain(args, new ScalacWorker());
}

@Override
public void processRequest(List<String> args) throws Exception {
public void work(String[] args) throws Exception {
Path tmpPath = null;
try {
CompileOptions ops = new CompileOptions(args);
CompileOptions ops = new CompileOptions(Arrays.asList(args));

Path outputPath = FileSystems.getDefault().getPath(ops.outputName);
tmpPath = Files.createTempDirectory(outputPath.getParent(), "tmp");
Expand All @@ -63,7 +66,7 @@ public void processRequest(List<String> args) throws Exception {

String[] scalaSources = collectSrcJarSources(ops.files, scalaJarFiles, javaJarFiles);

String[] javaSources = GenericWorker.appendToString(ops.javaFiles, javaJarFiles);
String[] javaSources = appendToString(ops.javaFiles, javaJarFiles);
if (scalaSources.length == 0 && javaSources.length == 0) {
throw new RuntimeException("Must have input files from either source jars or local files.");
}
Expand Down Expand Up @@ -95,8 +98,8 @@ public void processRequest(List<String> args) throws Exception {

private static String[] collectSrcJarSources(
String[] files, List<File> scalaJarFiles, List<File> javaJarFiles) {
String[] scalaSources = GenericWorker.appendToString(files, scalaJarFiles);
return GenericWorker.appendToString(scalaSources, javaJarFiles);
String[] scalaSources = appendToString(files, scalaJarFiles);
return appendToString(scalaSources, javaJarFiles);
}

private static List<File> filterFilesByExtension(List<File> files, String extension) {
Expand Down Expand Up @@ -166,7 +169,7 @@ private static boolean matchesFileExtensions(String fileName, String[] extension
}

private static String[] encodeBazelTargets(String[] targets) {
return Arrays.stream(targets).map(ScalacProcessor::encodeBazelTarget).toArray(String[]::new);
return Arrays.stream(targets).map(ScalacWorker::encodeBazelTarget).toArray(String[]::new);
}

private static String encodeBazelTarget(String target) {
Expand Down Expand Up @@ -223,7 +226,7 @@ private static void compileScalaSources(CompileOptions ops, String[] scalaSource
String[] constParams = {"-classpath", ops.classpath, "-d", tmpPath.toString()};

String[] compilerArgs =
GenericWorker.merge(ops.scalaOpts, ops.pluginArgs, constParams, pluginParams, scalaSources);
merge(ops.scalaOpts, ops.pluginArgs, constParams, pluginParams, scalaSources);

MainClass comp = new MainClass();
long start = System.currentTimeMillis();
Expand Down Expand Up @@ -312,4 +315,30 @@ private static void copyResourceJars(String[] resourceJars, Path dest) throws IO
extractJar(jarPath, dest.toString(), null);
}
}

private static <T> String[] appendToString(String[] init, List<T> rest) {
andyscott marked this conversation as resolved.
Show resolved Hide resolved
String[] tmp = new String[init.length + rest.size()];
System.arraycopy(init, 0, tmp, 0, init.length);
int baseIdx = init.length;
for (T t : rest) {
tmp[baseIdx] = t.toString();
baseIdx += 1;
}
return tmp;
}

private static String[] merge(String[]... arrays) {
int totalLength = 0;
for (String[] arr : arrays) {
totalLength += arr.length;
}

String[] result = new String[totalLength];
int offset = 0;
for (String[] arr : arrays) {
System.arraycopy(arr, 0, result, offset, arr.length);
offset += arr.length;
}
return result;
}
}
18 changes: 12 additions & 6 deletions src/java/io/bazel/rulesscala/worker/BUILD
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
load("@rules_java//java:defs.bzl", "java_library")

java_library(
name = "worker",
srcs = ["Worker.java"],
visibility = ["//:__subpackages__"],
andyscott marked this conversation as resolved.
Show resolved Hide resolved
deps = [
"//third_party/bazel/src/main/protobuf:worker_protocol_java_proto",
],
)

java_test(
name = "worker_test",
srcs = [
"GenericWorker.java",
"Processor.java",
"WorkerTest.java",
],
visibility = ["//visibility:public"],
test_class = "io.bazel.rulesscala.worker.WorkerTest",
deps = [
"//third_party/bazel/src/main/protobuf:worker_protocol_java_proto",
":worker",
],
)
Loading