diff --git a/scala/scala.bzl b/scala/scala.bzl index ea5ea939b..a942d39a5 100644 --- a/scala/scala.bzl +++ b/scala/scala.bzl @@ -165,6 +165,7 @@ Files: {files} EnableIjar: {enableijar} ijarOutput: {ijar_out} ijarCmdPath: {ijar_cmd_path} +SourceJars: {srcjars} """.format( out=ctx.outputs.jar.path, # 0 manifest=ctx.outputs.manifest.path, # 1 @@ -175,6 +176,7 @@ ijarCmdPath: {ijar_cmd_path} enableijar=buildijar, ijar_out=ijar_output_path, ijar_cmd_path=ijar_cmd_path, + srcjars=",".join([f.path for f in all_srcjars]), ) ctx.file_action(output=scalac_args_file, content=scalac_args) javac_sources_cmd = "" @@ -199,9 +201,6 @@ ijarCmdPath: {ijar_cmd_path} javac_args=javac_args_file.path, javac=ctx.file._javac.path ) - if len(all_srcjars) > 0: - print("Got source jars: " + ",".join(all_srcjars)) - # srcjar_cmd = "" # if len(all_srcjars) > 0: diff --git a/src/java/io/bazel/rulesscala/scalac/ScalaCInvoker.java b/src/java/io/bazel/rulesscala/scalac/ScalaCInvoker.java index 91724aed4..dc4104d74 100644 --- a/src/java/io/bazel/rulesscala/scalac/ScalaCInvoker.java +++ b/src/java/io/bazel/rulesscala/scalac/ScalaCInvoker.java @@ -62,6 +62,36 @@ * the DOS epoch. All Jar entries are sorted alphabetically. */ public class ScalaCInvoker { + private static List extractJar(String jarPath, String outputFolder) throws IOException, FileNotFoundException{ + List outputPaths = new ArrayList(); + java.util.jar.JarFile jar = new java.util.jar.JarFile(jarPath); +java.util.Enumeration e = jar.entries(); +while (e.hasMoreElements()) { + java.util.jar.JarEntry file = (java.util.jar.JarEntry) e.nextElement(); + java.io.File f = new java.io.File(outputFolder + java.io.File.separator + file.getName()); + + if (file.isDirectory()) { // if its a directory, create it + f.mkdirs(); + continue; + } + + File parent = f.getParentFile(); + System.out.println("Mkdir: " + parent); + parent.mkdirs(); + outputPaths.add(f); + + java.io.InputStream is = jar.getInputStream(file); // get the input stream + java.io.FileOutputStream fos = new java.io.FileOutputStream(f); + while (is.available() > 0) { // write contents of 'is' to 'fos' + fos.write(is.read()); + } + fos.close(); + is.close(); +} +return outputPaths; +} + + // A UUID that uniquely identifies this running worker process. static final UUID workerUuid = UUID.randomUUID(); @@ -201,7 +231,8 @@ private static HashMap buildArgMap(List lines) { 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]); + String k = lSplit[0].substring(0, lSplit[0].length() - 1); + hm.put(k, lSplit[1]); } } return hm; @@ -261,7 +292,33 @@ private static void processRequest(List args) throws Exception { 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[] files = getOrEmpty(argMap, "Files").split(","); + Path outputPath = FileSystems.getDefault().getPath(outputName); + + String[] sourceJars = getOrEmpty(argMap, "SourceJars").split(","); + List sourceFiles = new ArrayList(); + + for(String jarPath : sourceJars) { + if(jarPath.length() > 0){ + Path tmpPath = Files.createTempDirectory(outputPath.getParent(), "tmp"); + sourceFiles.addAll(extractJar(jarPath, tmpPath.toString())); + } + } + + int sourceFilesSize = sourceFiles.size(); + String[] tmpFiles = new String[files.length + sourceFilesSize]; + System.arraycopy(files, 0, tmpFiles, 0, files.length); + int baseIdx = files.length; + for(File p: sourceFiles) { + tmpFiles[baseIdx] = p.toString(); + baseIdx += 1; + } + files = tmpFiles; + + if(files.length == 0) { + throw new Exception("Must have input files from either source jars or local files."); + } + boolean iJarEnabled = booleanGetOrFalse(argMap, "EnableIjar"); String ijarOutput = null; @@ -271,7 +328,6 @@ private static void processRequest(List args) throws Exception { ijarCmdPath = getOrError(argMap, "ijarCmdPath", "Missing required arg ijarCmdPath when ijar enabled"); } - Path outputPath = FileSystems.getDefault().getPath(outputName); Path tmpPath = Files.createTempDirectory(outputPath.getParent(),"tmp");