Skip to content

Commit 30b0efc

Browse files
committed
address review
1 parent b28dcd1 commit 30b0efc

File tree

2 files changed

+65
-67
lines changed

2 files changed

+65
-67
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
11
package io.bazel.rulesscala.scalac;
22

3-
import java.io.File;
4-
import java.io.FileNotFoundException;
5-
import java.io.IOException;
6-
import java.nio.file.FileSystems;
7-
import java.nio.file.Files;
8-
import java.nio.file.Path;
93
import java.util.ArrayList;
104
import java.util.HashMap;
115
import java.util.List;
@@ -18,14 +12,12 @@ public class CompileOptions {
1812
final public String[] pluginArgs;
1913
final public String classpath;
2014
final public String[] files;
21-
final public Path outputPath;
2215
final public String[] sourceJars;
2316
final public boolean iJarEnabled;
2417
final public String ijarOutput;
2518
final public String ijarCmdPath;
26-
final public Path tmpPath;
2719

28-
public CompileOptions(List<String> args) throws IOException, FileNotFoundException {
20+
public CompileOptions(List<String> args) {
2921
Map<String, String> argMap = buildArgMap(args);
3022

3123
outputName = getOrError(argMap, "JarOutput", "Missing required arg JarOutput");
@@ -34,21 +26,9 @@ public CompileOptions(List<String> args) throws IOException, FileNotFoundExcepti
3426
scalaOpts = getOrEmpty(argMap, "ScalacOpts").split(",");
3527
pluginArgs = buildPluginArgs(getOrEmpty(argMap, "Plugins"));
3628
classpath = getOrError(argMap, "Classpath", "Must supply the classpath arg");
37-
outputPath = FileSystems.getDefault().getPath(outputName);
29+
files = getOrEmpty(argMap, "Files").split(",");
3830

3931
sourceJars = getOrEmpty(argMap, "SourceJars").split(",");
40-
List<File> sourceFiles = new ArrayList<File>();
41-
42-
for(String jarPath : sourceJars) {
43-
if(jarPath.length() > 0){
44-
Path tmpPath = Files.createTempDirectory(outputPath.getParent(), "tmp");
45-
sourceFiles.addAll(extractJar(jarPath, tmpPath.toString()));
46-
}
47-
}
48-
files = appendToString(getOrEmpty(argMap, "Files").split(","), sourceFiles);
49-
if(files.length == 0) {
50-
throw new RuntimeException("Must have input files from either source jars or local files.");
51-
}
5232
iJarEnabled = booleanGetOrFalse(argMap, "EnableIjar");
5333
if(iJarEnabled) {
5434
ijarOutput = getOrError(argMap, "ijarOutput", "Missing required arg ijarOutput when ijar enabled");
@@ -58,18 +38,6 @@ public CompileOptions(List<String> args) throws IOException, FileNotFoundExcepti
5838
ijarOutput = null;
5939
ijarCmdPath = null;
6040
}
61-
tmpPath = Files.createTempDirectory(outputPath.getParent(),"tmp");
62-
}
63-
64-
private static <T> String[] appendToString(String[] init, List<T> rest) {
65-
String[] tmp = new String[init.length + rest.size()];
66-
System.arraycopy(init, 0, tmp, 0, init.length);
67-
int baseIdx = init.length;
68-
for(T t : rest) {
69-
tmp[baseIdx] = t.toString();
70-
baseIdx += 1;
71-
}
72-
return tmp;
7341
}
7442

7543
private static HashMap<String, String> buildArgMap(List<String> lines) {
@@ -129,33 +97,4 @@ public static String[] buildPluginArgs(String packedPlugins) {
12997
}
13098
return result;
13199
}
132-
private static List<File> extractJar(String jarPath,
133-
String outputFolder) throws IOException, FileNotFoundException {
134-
135-
List<File> outputPaths = new ArrayList<File>();
136-
java.util.jar.JarFile jar = new java.util.jar.JarFile(jarPath);
137-
java.util.Enumeration e = jar.entries();
138-
while (e.hasMoreElements()) {
139-
java.util.jar.JarEntry file = (java.util.jar.JarEntry) e.nextElement();
140-
File f = new File(outputFolder + java.io.File.separator + file.getName());
141-
142-
if (file.isDirectory()) { // if its a directory, create it
143-
f.mkdirs();
144-
continue;
145-
}
146-
147-
File parent = f.getParentFile();
148-
parent.mkdirs();
149-
outputPaths.add(f);
150-
151-
java.io.InputStream is = jar.getInputStream(file); // get the input stream
152-
java.io.FileOutputStream fos = new java.io.FileOutputStream(f);
153-
while (is.available() > 0) { // write contents of 'is' to 'fos'
154-
fos.write(is.read());
155-
}
156-
fos.close();
157-
is.close();
158-
}
159-
return outputPaths;
160-
}
161100
}

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

+63-4
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import java.io.IOException;
3232
import java.io.PrintStream;
3333
import java.lang.reflect.Field;
34+
import java.nio.file.FileSystems;
3435
import java.nio.file.Files;
3536
import java.nio.file.Path;
3637
import java.nio.file.Paths;
@@ -135,6 +136,62 @@ private static void runPersistentWorker(WorkerOptions workerOptions) throws IOEx
135136
}
136137
}
137138

139+
static private String[] extractSourceJars(CompileOptions opts, Path tmpParent) throws IOException {
140+
List<File> sourceFiles = new ArrayList<File>();
141+
142+
for(String jarPath : opts.sourceJars) {
143+
if (jarPath.length() > 0){
144+
Path tmpPath = Files.createTempDirectory(tmpParent, "tmp");
145+
sourceFiles.addAll(extractJar(jarPath, tmpPath.toString()));
146+
}
147+
}
148+
String[] files = appendToString(opts.files, sourceFiles);
149+
if(files.length == 0) {
150+
throw new RuntimeException("Must have input files from either source jars or local files.");
151+
}
152+
return files;
153+
}
154+
155+
private static List<File> extractJar(String jarPath,
156+
String outputFolder) throws IOException, FileNotFoundException {
157+
158+
List<File> outputPaths = new ArrayList<File>();
159+
java.util.jar.JarFile jar = new java.util.jar.JarFile(jarPath);
160+
java.util.Enumeration e = jar.entries();
161+
while (e.hasMoreElements()) {
162+
java.util.jar.JarEntry file = (java.util.jar.JarEntry) e.nextElement();
163+
File f = new File(outputFolder + java.io.File.separator + file.getName());
164+
165+
if (file.isDirectory()) { // if its a directory, create it
166+
f.mkdirs();
167+
continue;
168+
}
169+
170+
File parent = f.getParentFile();
171+
parent.mkdirs();
172+
outputPaths.add(f);
173+
174+
java.io.InputStream is = jar.getInputStream(file); // get the input stream
175+
java.io.FileOutputStream fos = new java.io.FileOutputStream(f);
176+
while (is.available() > 0) { // write contents of 'is' to 'fos'
177+
fos.write(is.read());
178+
}
179+
fos.close();
180+
is.close();
181+
}
182+
return outputPaths;
183+
}
184+
185+
static <T> String[] appendToString(String[] init, List<T> rest) {
186+
String[] tmp = new String[init.length + rest.size()];
187+
System.arraycopy(init, 0, tmp, 0, init.length);
188+
int baseIdx = init.length;
189+
for(T t : rest) {
190+
tmp[baseIdx] = t.toString();
191+
baseIdx += 1;
192+
}
193+
return tmp;
194+
}
138195
public static String[] merge(String[]... arrays) {
139196
int totalLength = 0;
140197
for(String[] arr:arrays){
@@ -170,18 +227,20 @@ private static void processRequest(List<String> args) throws Exception {
170227
}
171228
CompileOptions ops = new CompileOptions(args);
172229

230+
Path outputPath = FileSystems.getDefault().getPath(ops.outputName);
231+
Path tmpPath = Files.createTempDirectory(outputPath.getParent(), "tmp");
173232
String[] constParams = {
174233
"-classpath",
175234
ops.classpath,
176235
"-d",
177-
ops.tmpPath.toString()
236+
tmpPath.toString()
178237
};
179238

180239
String[] compilerArgs = merge(
181240
ops.scalaOpts,
182241
ops.pluginArgs,
183242
constParams,
184-
ops.files);
243+
extractSourceJars(ops, outputPath.getParent()));
185244

186245
MainClass comp = new MainClass();
187246
long start = System.currentTimeMillis();
@@ -199,8 +258,8 @@ private static void processRequest(List<String> args) throws Exception {
199258
String[] jarCreatorArgs = {
200259
"-m",
201260
ops.manifestPath,
202-
ops.outputPath.toString(),
203-
ops.tmpPath.toString()
261+
outputPath.toString(),
262+
tmpPath.toString()
204263
};
205264
JarCreator.buildJar(jarCreatorArgs);
206265

0 commit comments

Comments
 (0)