Skip to content

Commit

Permalink
Wip
Browse files Browse the repository at this point in the history
  • Loading branch information
ianoc-stripe committed Sep 12, 2016
1 parent 1f9600c commit 1cc779c
Show file tree
Hide file tree
Showing 3 changed files with 142 additions and 102 deletions.
61 changes: 33 additions & 28 deletions scala/scala.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -131,20 +131,19 @@ def _compile(ctx, _jars, dep_srcjars, buildijar):
cp_resources = _add_resources_cmd(
ctx, "{out}_tmp".format(out=ctx.outputs.jar.path)
)
ijar_cmd = ""
ijar_output_path = ""
ijar_cmd_path = ""
if buildijar:
ijar_cmd = "\n{ijar} {out} {ijar_out}".format(
ijar=ctx.file._ijar.path,
out=ctx.outputs.jar.path,
ijar_out=ctx.outputs.ijar.path)
ijar_output_path = ctx.outputs.ijar.path
ijar_cmd_path = ctx.file._ijar.path

java_srcs = _java_filetype.filter(ctx.files.srcs)
sources = _scala_filetype.filter(ctx.files.srcs) + java_srcs
srcjars = _srcjar_filetype.filter(ctx.files.srcs)
all_srcjars = set(srcjars + list(dep_srcjars))
# look for any plugins:
plugins = _collect_plugin_paths(ctx.attr.plugins)
plugin_arg = "Plugins: " + ",".join(list(plugins))
plugin_arg = ",".join(list(plugins))

# Set up the args to pass to scalac because they can be too long for bash
scalac_args_file = ctx.new_file(ctx.outputs.jar, ctx.label.name + "_scalac_args") # noqa
Expand All @@ -157,19 +156,25 @@ def _compile(ctx, _jars, dep_srcjars, buildijar):
)

scalac_args = """
{out}
{manifest}
{scala_opts}
{plugin_arg}
{cp}
{files}
JarOutput: {out}
Manifest: {manifest}
ScalacOpts: {scala_opts}
Plugins: {plugin_arg}
Classpath: {cp}
Files: {files}
EnableIjar: {enableijar}
ijarOutput: {ijar_out}
ijarCmdPath: {ijar_cmd_path}
""".format(
out=ctx.outputs.jar.path, # 0
manifest=ctx.outputs.manifest.path, # 1
scala_opts="ScalaOpts: " + ",".join(ctx.attr.scalacopts), # 2
scala_opts=",".join(ctx.attr.scalacopts), # 2
plugin_arg=plugin_arg, # 3
cp=compiler_classpath, # 4
files=",".join([f.path for f in sources]), # 5
enableijar=buildijar,
ijar_out=ijar_output_path,
ijar_cmd_path=ijar_cmd_path,
)
ctx.file_action(output=scalac_args_file, content=scalac_args)
javac_sources_cmd = ""
Expand Down Expand Up @@ -229,21 +234,21 @@ def _compile(ctx, _jars, dep_srcjars, buildijar):
)
ctx.file_action(output=argfile, content=scalac_args)

cmd = """
cat {scalac_args} > {out}_args/scala_args
{java} -jar {scalac} {jvm_flags} @{out}_args/scala_args""" + javac_sources_cmd + """
""" + ijar_cmd
cmd = cmd.format(
cp_resources=cp_resources,
java=ctx.file._java.path,
jvm_flags=" ".join(["-J" + flag for flag in ctx.attr.jvm_flags]),
scalac=_get_scalac_jar_path(ctx.files._scalac),
scalac_args=scalac_args_file.path,
out=ctx.outputs.jar.path,
manifest=ctx.outputs.manifest.path,
jar=_get_jar_path(ctx.files._jar),
ijar=ctx.file._ijar.path,
)
# cmd = """
# cat {scalac_args} > {out}_args/scala_args
# {java} -jar {scalac} {jvm_flags} @{out}_args/scala_args""" + javac_sources_cmd + """
# """ + ijar_cmd
# cmd = cmd.format(
# cp_resources=cp_resources,
# java=ctx.file._java.path,
# jvm_flags=" ".join(["-J" + flag for flag in ctx.attr.jvm_flags]),
# scalac=_get_scalac_jar_path(ctx.files._scalac),
# scalac_args=scalac_args_file.path,
# out=ctx.outputs.jar.path,
# manifest=ctx.outputs.manifest.path,
# jar=_get_jar_path(ctx.files._jar),
# ijar=ctx.file._ijar.path,
# )

outs = [ctx.outputs.jar]
if buildijar:
Expand Down
167 changes: 97 additions & 70 deletions src/java/io/bazel/rulesscala/scalac/ScalaCInvoker.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.io.IOException;
import java.util.Collection;
import java.util.Map;
import java.util.HashMap;
import java.util.TreeMap;
import java.util.jar.Attributes;
import java.util.jar.JarOutputStream;
Expand Down Expand Up @@ -183,53 +184,95 @@ public static String[] merge(String[]... arrays) {
// processRequest(ImmutableList.copyOf(args));
// }
// }
private static String getOptionalNamedArg(String line) {
String[] lSplit = line.split(" ");
if(lSplit.length > 1) {
return lSplit[1];
} else {
return "";
private static HashMap<String, String> buildArgMap(List<String> lines) {
HashMap hm = new HashMap();
for(String line: lines) {
String[] lSplit = line.split(" ");
if(lSplit.length > 2) {
throw new RuntimeException("Bad arg, should have at most 1 space/2 spans. arg: " + line);
}
if(lSplit.length > 1) {
hm.put(lSplit[0].substring(0, lSplit[0].length() - 1), lSplit[1]);
}
}
return hm;
}
private static void processRequest(List<String> args) throws Exception {
System.out.println("\n\n\n___ARGS_START____\n");

private static String getOrError(Map<String, String> m, String k, String errorMessage) {
if(m.containsKey(k)) {
return m.get(k);
} else {
throw new RuntimeException(errorMessage);
}
}

private static String getOrEmpty(Map<String, String> m, String k) {
if(m.containsKey(k)) {
return m.get(k);
} else {
return "";
}
}

for (int i = 0; i < args.size(); i++) {
System.out.println("'''" + args.get(i) + "'''");
private static boolean booleanGetOrFalse(Map<String, String> m, String k) {
if(m.containsKey(k)) {
String v = m.get(k);
if(v.trim().equals("True") || v.trim().equals("true")) {
return true;
}
}
return false;
}

// JarOutput: {out}
// Manifest: {manifest}
// ScalacOpts: {scala_opts}
// Plugins: {plugin_arg}
// Classpath: {cp}
// Files: {files}
// EnableIjar: {enableijar}
// ijarOutput: {ijar_out}
// ijarCmdPath: {ijar_cmd_path}
private static Field reporterField;
static {
try {
reporterField = Driver.class.getDeclaredField("reporter"); //NoSuchFieldException
reporterField.setAccessible(true);
}
catch (Exception ex){
throw new RuntimeException("nope", ex);
}
}

private static void processRequest(List<String> args) throws Exception {
if (args.size() == 1 && args.get(0).startsWith("@")) {
args = Files.readAllLines(Paths.get(args.get(0).substring(1)), UTF_8);
}

List<String> trimmedArgs = new ArrayList<String>();
for(String arg : args) {
if(arg.trim().length() > 0){
trimmedArgs.add(arg);
}
Map<String, String> argMap = buildArgMap(args);
for (Map.Entry<String, String> entry : argMap.entrySet()) {
System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
}
args = trimmedArgs;

for (int i = 0; i < args.size(); i++) {
System.out.println(args.get(i));
}
String outputName = getOrError(argMap, "JarOutput", "Missing required arg JarOutput");
String manifestPath = getOrError(argMap, "Manifest", "Missing required arg Manifest");

String outputName = args.get(0);
String manifestPath = args.get(1);
String[] scalaOpts = getOrEmpty(argMap, "ScalacOpts").split(",");
String[] pluginArgs = buildPluginArgs(getOrEmpty(argMap, "Plugins"));
String classpath = getOrError(argMap, "Classpath", "Must supply the classpath arg");
String[] files = getOrError(argMap, "Files", "Must supply files to operate on").split(",");

String[] scalaOpts = getOptionalNamedArg(args.get(2)).split(",");
String[] pluginArgs = buildPluginArgs(getOptionalNamedArg(args.get(3)));
String classpath = args.get(4);
String[] files = args.get(5).split(",");
boolean iJarEnabled = booleanGetOrFalse(argMap, "EnableIjar");
String ijarOutput = null;
String ijarCmdPath = null;
if(iJarEnabled) {
ijarOutput = getOrError(argMap, "ijarOutput", "Missing required arg ijarOutput when ijar enabled");
ijarCmdPath = getOrError(argMap, "ijarCmdPath", "Missing required arg ijarCmdPath when ijar enabled");
}

Path outputPath = FileSystems.getDefault().getPath(outputName);
Path tmpPath = Files.createTempDirectory(outputPath.getParent(),"tmp");

System.out.println("Output path will be: " + outputName);
System.out.println("Manifest path will be: " + manifestPath);
System.out.println("tmpPath path will be: " + tmpPath);

String[] constParams = {
"-classpath",
Expand All @@ -244,50 +287,34 @@ private static void processRequest(List<String> args) throws Exception {
constParams,
files);

MainClass comp = new MainClass();
comp.process(compilerArgs);

ConsoleReporter reporter = (ConsoleReporter) reporterField.get(comp);

if (reporter.hasErrors()) {
// reportErrors(reporter);
reporter.flush();
} else {
// reportSuccess();
String[] jarCreatorArgs = {
"-m",
manifestPath,
outputPath.toString(),
tmpPath.toString()
};
JarCreator.buildJar(jarCreatorArgs);

for (int i = 0; i < compilerArgs.length; i++) {
System.out.println(compilerArgs[i]);
if(iJarEnabled) {
Process iostat = new ProcessBuilder().command(ijarCmdPath, outputName, ijarOutput).inheritIO().start();
int exitCode = iostat.waitFor();
if(exitCode != 0) {
throw new RuntimeException("ijar process failed!");
}
System.out.println("exitCode = " + exitCode);
}




// System.out.println("\n\n___ARGS_END____\n");

// for (int i = 0; i < newArgs.length; i++) {
// System.out.println(newArgs[i]);
// }

// System.out.println("SASDF");
// System.out.println("SASDF");
// System.out.println("SASDF");
// System.out.println("SASDF");
// System.out.println("SASDF");
MainClass comp = new MainClass();
comp.process(compilerArgs);


// System.out.println("SASDF");

Field f = Driver.class.getDeclaredField("reporter"); //NoSuchFieldException
f.setAccessible(true);
ConsoleReporter reporter = (ConsoleReporter) f.get(comp); //IllegalAccessException

if (reporter.hasErrors()) {
// reportErrors(reporter);
reporter.flush();
} else {
// reportSuccess();
String[] jarCreatorArgs = {
"-m",
manifestPath,
outputPath.toString(),
tmpPath.toString()
};
JarCreator.buildJar(jarCreatorArgs);

System.out.println("Success");
}
System.out.println("Success");
}
}

public static void main(String[] args) {
Expand Down
16 changes: 12 additions & 4 deletions src/scala/scripts/BUILD
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
load("//scala:scala.bzl", "scala_binary")
load("//scala:scala.bzl", "scala_binary", "scala_library")

scala_binary(
name = "generator",
scala_library(
name = "generator_lib",
srcs = ["TwitterScroogeGenerator.scala"],
main_class = "scripts.ScroogeGenerator",
deps = [
"@scrooge_generator//jar",
"@util_core//jar",
Expand All @@ -13,6 +12,15 @@ scala_binary(
visibility = ["//visibility:public"],
)

scala_binary(
name = "generator",
main_class = "scripts.ScroogeGenerator",
deps = [
":generator_lib",
],
visibility = ["//visibility:public"],
)

java_import(
name = "scala_parsers",
jars = ["@scala//:lib/scala-parser-combinators_2.11-1.0.4.jar"],
Expand Down

0 comments on commit 1cc779c

Please sign in to comment.