forked from bazel-contrib/rules_scala
-
Notifications
You must be signed in to change notification settings - Fork 0
Factor out ScalaCInvoker option parsing #1
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,13 @@ | ||
load("//scala:scala.bzl", "scala_library") | ||
|
||
|
||
java_import( | ||
name = "scala_xml", | ||
jars = ["@scala//:lib/scala-xml_2.11-1.0.4.jar"] | ||
) | ||
|
||
scala_library(name = "test_reporter", | ||
srcs = ["JUnitXmlReporter.scala"], | ||
deps = ["@scalatest//file"], | ||
deps = ["@scalatest//file", ":scala_xml"], | ||
visibility = ["//visibility:public"], | ||
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
161 changes: 161 additions & 0 deletions
161
src/java/io/bazel/rulesscala/scalac/CompileOptions.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,161 @@ | ||
package io.bazel.rulesscala.scalac; | ||
|
||
import java.io.File; | ||
import java.io.FileNotFoundException; | ||
import java.io.IOException; | ||
import java.nio.file.FileSystems; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class CompileOptions { | ||
final public String outputName; | ||
final public String manifestPath; | ||
final public String[] scalaOpts; | ||
final public String[] pluginArgs; | ||
final public String classpath; | ||
final public String[] files; | ||
final public Path outputPath; | ||
final public String[] sourceJars; | ||
final public boolean iJarEnabled; | ||
final public String ijarOutput; | ||
final public String ijarCmdPath; | ||
final public Path tmpPath; | ||
|
||
public CompileOptions(List<String> args) throws IOException, FileNotFoundException { | ||
Map<String, String> argMap = buildArgMap(args); | ||
|
||
outputName = getOrError(argMap, "JarOutput", "Missing required arg JarOutput"); | ||
manifestPath = getOrError(argMap, "Manifest", "Missing required arg Manifest"); | ||
|
||
scalaOpts = getOrEmpty(argMap, "ScalacOpts").split(","); | ||
pluginArgs = buildPluginArgs(getOrEmpty(argMap, "Plugins")); | ||
classpath = getOrError(argMap, "Classpath", "Must supply the classpath arg"); | ||
outputPath = FileSystems.getDefault().getPath(outputName); | ||
|
||
sourceJars = getOrEmpty(argMap, "SourceJars").split(","); | ||
List<File> sourceFiles = new ArrayList<File>(); | ||
|
||
for(String jarPath : sourceJars) { | ||
if(jarPath.length() > 0){ | ||
Path tmpPath = Files.createTempDirectory(outputPath.getParent(), "tmp"); | ||
sourceFiles.addAll(extractJar(jarPath, tmpPath.toString())); | ||
} | ||
} | ||
files = appendToString(getOrEmpty(argMap, "Files").split(","), sourceFiles); | ||
if(files.length == 0) { | ||
throw new RuntimeException("Must have input files from either source jars or local files."); | ||
} | ||
iJarEnabled = booleanGetOrFalse(argMap, "EnableIjar"); | ||
if(iJarEnabled) { | ||
ijarOutput = getOrError(argMap, "ijarOutput", "Missing required arg ijarOutput when ijar enabled"); | ||
ijarCmdPath = getOrError(argMap, "ijarCmdPath", "Missing required arg ijarCmdPath when ijar enabled"); | ||
} | ||
else { | ||
ijarOutput = null; | ||
ijarCmdPath = null; | ||
} | ||
tmpPath = Files.createTempDirectory(outputPath.getParent(),"tmp"); | ||
} | ||
|
||
private static <T> String[] appendToString(String[] init, List<T> rest) { | ||
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 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], lSplit[1]); | ||
} | ||
} | ||
return hm; | ||
} | ||
|
||
private static String getOrEmpty(Map<String, String> m, String k) { | ||
if(m.containsKey(k)) { | ||
return m.get(k); | ||
} else { | ||
return ""; | ||
} | ||
} | ||
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 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; | ||
} | ||
public static String[] buildPluginArgs(String packedPlugins) { | ||
String[] pluginElements = packedPlugins.split(","); | ||
int numPlugins = 0; | ||
for(int i =0; i< pluginElements.length; i++){ | ||
if(pluginElements[i].length() > 0) { | ||
numPlugins += 1; | ||
} | ||
} | ||
|
||
String[] result = new String[numPlugins]; | ||
int idx = 0; | ||
for(int i =0; i< pluginElements.length; i++){ | ||
if(pluginElements[i].length() > 0) { | ||
result[idx] = "-Xplugin:" + pluginElements[i]; | ||
idx += 1; | ||
} | ||
} | ||
return result; | ||
} | ||
private static List<File> extractJar(String jarPath, | ||
String outputFolder) throws IOException, FileNotFoundException { | ||
|
||
List<File> outputPaths = new ArrayList<File>(); | ||
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(); | ||
File f = new File(outputFolder + java.io.File.separator + file.getName()); | ||
|
||
if (file.isDirectory()) { // if its a directory, create it | ||
f.mkdirs(); | ||
continue; | ||
} | ||
|
||
File parent = f.getParentFile(); | ||
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; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should probably be in another class/file entirely. Doesn't seem at home in compile options?