Skip to content

Commit 4ab6810

Browse files
committed
wip
1 parent cdc4995 commit 4ab6810

File tree

2 files changed

+61
-6
lines changed

2 files changed

+61
-6
lines changed

scala/scala.bzl

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ Files: {files}
165165
EnableIjar: {enableijar}
166166
ijarOutput: {ijar_out}
167167
ijarCmdPath: {ijar_cmd_path}
168+
SourceJars: {srcjars}
168169
""".format(
169170
out=ctx.outputs.jar.path, # 0
170171
manifest=ctx.outputs.manifest.path, # 1
@@ -175,6 +176,7 @@ ijarCmdPath: {ijar_cmd_path}
175176
enableijar=buildijar,
176177
ijar_out=ijar_output_path,
177178
ijar_cmd_path=ijar_cmd_path,
179+
srcjars=",".join([f.path for f in all_srcjars]),
178180
)
179181
ctx.file_action(output=scalac_args_file, content=scalac_args)
180182
javac_sources_cmd = ""
@@ -199,9 +201,6 @@ ijarCmdPath: {ijar_cmd_path}
199201
javac_args=javac_args_file.path,
200202
javac=ctx.file._javac.path
201203
)
202-
if len(all_srcjars) > 0:
203-
print("Got source jars: " + ",".join(all_srcjars))
204-
205204

206205
# srcjar_cmd = ""
207206
# if len(all_srcjars) > 0:

src/java/io/bazel/rulesscala/scalac/ScalaCInvoker.java

Lines changed: 59 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,36 @@
6262
* the DOS epoch. All Jar entries are sorted alphabetically.
6363
*/
6464
public class ScalaCInvoker {
65+
private static List<File> extractJar(String jarPath, String outputFolder) throws IOException, FileNotFoundException{
66+
List<File> outputPaths = new ArrayList<File>();
67+
java.util.jar.JarFile jar = new java.util.jar.JarFile(jarPath);
68+
java.util.Enumeration e = jar.entries();
69+
while (e.hasMoreElements()) {
70+
java.util.jar.JarEntry file = (java.util.jar.JarEntry) e.nextElement();
71+
java.io.File f = new java.io.File(outputFolder + java.io.File.separator + file.getName());
72+
73+
if (file.isDirectory()) { // if its a directory, create it
74+
f.mkdirs();
75+
continue;
76+
}
77+
78+
File parent = f.getParentFile();
79+
System.out.println("Mkdir: " + parent);
80+
parent.mkdirs();
81+
outputPaths.add(f);
82+
83+
java.io.InputStream is = jar.getInputStream(file); // get the input stream
84+
java.io.FileOutputStream fos = new java.io.FileOutputStream(f);
85+
while (is.available() > 0) { // write contents of 'is' to 'fos'
86+
fos.write(is.read());
87+
}
88+
fos.close();
89+
is.close();
90+
}
91+
return outputPaths;
92+
}
93+
94+
6595
// A UUID that uniquely identifies this running worker process.
6696
static final UUID workerUuid = UUID.randomUUID();
6797

@@ -201,7 +231,8 @@ private static HashMap<String, String> buildArgMap(List<String> lines) {
201231
throw new RuntimeException("Bad arg, should have at most 1 space/2 spans. arg: " + line);
202232
}
203233
if(lSplit.length > 1) {
204-
hm.put(lSplit[0].substring(0, lSplit[0].length() - 1), lSplit[1]);
234+
String k = lSplit[0].substring(0, lSplit[0].length() - 1);
235+
hm.put(k, lSplit[1]);
205236
}
206237
}
207238
return hm;
@@ -261,7 +292,33 @@ private static void processRequest(List<String> args) throws Exception {
261292
String[] scalaOpts = getOrEmpty(argMap, "ScalacOpts").split(",");
262293
String[] pluginArgs = buildPluginArgs(getOrEmpty(argMap, "Plugins"));
263294
String classpath = getOrError(argMap, "Classpath", "Must supply the classpath arg");
264-
String[] files = getOrError(argMap, "Files", "Must supply files to operate on").split(",");
295+
String[] files = getOrEmpty(argMap, "Files").split(",");
296+
Path outputPath = FileSystems.getDefault().getPath(outputName);
297+
298+
String[] sourceJars = getOrEmpty(argMap, "SourceJars").split(",");
299+
List<File> sourceFiles = new ArrayList<File>();
300+
301+
for(String jarPath : sourceJars) {
302+
if(jarPath.length() > 0){
303+
Path tmpPath = Files.createTempDirectory(outputPath.getParent(), "tmp");
304+
sourceFiles.addAll(extractJar(jarPath, tmpPath.toString()));
305+
}
306+
}
307+
308+
int sourceFilesSize = sourceFiles.size();
309+
String[] tmpFiles = new String[files.length + sourceFilesSize];
310+
System.arraycopy(files, 0, tmpFiles, 0, files.length);
311+
int baseIdx = files.length;
312+
for(File p: sourceFiles) {
313+
tmpFiles[baseIdx] = p.toString();
314+
baseIdx += 1;
315+
}
316+
files = tmpFiles;
317+
318+
if(files.length == 0) {
319+
throw new Exception("Must have input files from either source jars or local files.");
320+
}
321+
265322

266323
boolean iJarEnabled = booleanGetOrFalse(argMap, "EnableIjar");
267324
String ijarOutput = null;
@@ -271,7 +328,6 @@ private static void processRequest(List<String> args) throws Exception {
271328
ijarCmdPath = getOrError(argMap, "ijarCmdPath", "Missing required arg ijarCmdPath when ijar enabled");
272329
}
273330

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

277333

0 commit comments

Comments
 (0)