Skip to content

Address inconsistent behavior of generated resources files #297

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 2 commits into from
Oct 5, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions scala/scala.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def _adjust_resources_path(path, resource_strip_prefix):
def _add_resources_cmd(ctx):
res_cmd = []
for f in ctx.files.resources:
c_dir, res_path = _adjust_resources_path(f.path, ctx.attr.resource_strip_prefix)
c_dir, res_path = _adjust_resources_path(f.short_path, ctx.attr.resource_strip_prefix)
target_path = res_path
if target_path[0] == "/":
target_path = target_path[1:]
Expand Down Expand Up @@ -197,6 +197,7 @@ PrintCompileTime: {print_compile_time}
ResourceDests: {resource_dest}
ResourceJars: {resource_jars}
ResourceSrcs: {resource_src}
ResourceShortPaths: {resource_short_paths}
ResourceStripPrefix: {resource_strip_prefix}
ScalacOpts: {scala_opts}
SourceJars: {srcjars}
Expand All @@ -215,8 +216,9 @@ DependencyAnalyzerMode: {dependency_analyzer_mode}
srcjars=",".join([f.path for f in all_srcjars]),
java_files=",".join([f.path for f in java_srcs]),
resource_src=",".join([f.path for f in ctx.files.resources]),
resource_short_paths=",".join([f.short_path for f in ctx.files.resources]),
resource_dest=",".join(
[_adjust_resources_path_by_default_prefixes(f.path)[1] for f in ctx.files.resources]
[_adjust_resources_path_by_default_prefixes(f.short_path)[1] for f in ctx.files.resources]
),
resource_strip_prefix=ctx.attr.resource_strip_prefix,
resource_jars=",".join([f.path for f in ctx.files.resource_jars]),
Expand Down
1 change: 1 addition & 0 deletions src/java/io/bazel/rulesscala/scalac/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ java_binary(
"CompileOptions.java",
"ScalaCInvoker.java",
"ScalacProcessor.java",
"Resource.java",
],
main_class = "io.bazel.rulesscala.scalac.ScalaCInvoker",
visibility = ["//visibility:public"],
Expand Down
22 changes: 15 additions & 7 deletions src/java/io/bazel/rulesscala/scalac/CompileOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class CompileOptions {
final public String ijarOutput;
final public String ijarCmdPath;
final public String[] javaFiles;
final public Map<String, String> resourceFiles;
final public Map<String, Resource> resourceFiles;
final public String resourceStripPrefix;
final public String[] resourceJars;
final public String[] directJars;
Expand Down Expand Up @@ -62,15 +62,23 @@ public CompileOptions(List<String> args) {
currentTarget = getOrElse(argMap, "CurrentTarget", "NA");
}

private static Map<String, String> getResources(Map<String, String> args) {
private static Map<String, Resource> getResources(Map<String, String> args) {
String[] keys = getCommaList(args, "ResourceSrcs");
String[] vals = getCommaList(args, "ResourceDests");
if (keys.length != vals.length)
throw new RuntimeException(String.format("mismatch in resources: keys: %s vals: %s",
String[] dests = getCommaList(args, "ResourceDests");
String[] shortPaths = getCommaList(args, "ResourceShortPaths");

if (keys.length != dests.length)
throw new RuntimeException(String.format("mismatch in resources: keys: %s dests: %s",
getOrEmpty(args, "ResourceSrcs"), getOrEmpty(args, "ResourceDests")));
HashMap<String, String> res = new HashMap();

if (keys.length != shortPaths.length)
throw new RuntimeException(String.format("mismatch in resources: keys: %s shortPaths: %s",
getOrEmpty(args, "ResourceSrcs"), getOrEmpty(args, "ResourceShortPaths")));

HashMap<String, Resource> res = new HashMap();
for(int idx = 0; idx < keys.length; idx++) {
res.put(keys[idx], vals[idx]);
Resource resource = new Resource(dests[idx], shortPaths[idx]);
res.put(keys[idx], resource);
}
return res;
}
Expand Down
11 changes: 11 additions & 0 deletions src/java/io/bazel/rulesscala/scalac/Resource.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package io.bazel.rulesscala.scalac;

public class Resource {
public final String destination;
public final String shortPath;

public Resource(String destination, String shortPath) {
this.destination = destination;
this.shortPath = shortPath;
}
}
10 changes: 6 additions & 4 deletions src/java/io/bazel/rulesscala/scalac/ScalacProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -275,11 +275,13 @@ public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOEx
}
}
private static void copyResources(
Map<String, String> resources,
Map<String, Resource> resources,
String resourceStripPrefix,
Path dest) throws IOException {
for(Entry<String, String> e : resources.entrySet()) {
for(Entry<String, Resource> e : resources.entrySet()) {
Path source = Paths.get(e.getKey());
Resource resource = e.getValue();
Path shortPath = Paths.get(resource.shortPath);
String dstr;
// Check if we need to modify resource destination path
if (!"".equals(resourceStripPrefix)) {
Expand All @@ -292,9 +294,9 @@ private static void copyResources(
* from the Source Path and use that as the new destination path
* Refer Bazel -> BazelJavaRuleClasses.java#L227 for details
*/
dstr = getResourcePath(source, resourceStripPrefix);
dstr = getResourcePath(shortPath, resourceStripPrefix);
} else {
dstr = e.getValue();
dstr = resource.destination;
}
if (dstr.charAt(0) == '/') dstr = dstr.substring(1);
Path target = dest.resolve(dstr);
Expand Down
3 changes: 2 additions & 1 deletion test/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,8 @@ scala_library(
"//test/data:more.txt",
"//test/data:foo.txt",
"//test/src/main/resources/scala/test:more-hellos",
"//test/src/main/resources/scala/test:more-byes"
"//test/src/main/resources/scala/test:more-byes",
"//test/src/main/resources/scala/test:generated-hello",
],
resource_strip_prefix = "test/",
)
Expand Down
7 changes: 7 additions & 0 deletions test/src/main/resources/scala/test/BUILD
Original file line number Diff line number Diff line change
@@ -1 +1,8 @@
exports_files(["byes", "hellos", "hellos-and-byes.jar", "more-byes", "more-hellos"])

genrule(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getting fancy: generated resources...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah admittedly this is a subtle corner case... We somehow still managed to hit it though

name = "generated-hello",
outs = ["generated-hello.txt"],
cmd = "echo 'hello' > $@",
visibility = ["//visibility:public"],
)
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@ object ResourcesStripScalaBinary {
ResourcesStripScalaLib.getGreetings foreach println
ResourcesStripScalaLib.getFarewells foreach println
ResourcesStripScalaLib.getData foreach println
ResourcesStripScalaLib.getGeneratedHello foreach println
}
}
2 changes: 2 additions & 0 deletions test/src/main/scala/scala/test/ResourcesStripScalaLib.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ object ResourcesStripScalaLib {

def getData = get("/data/more.txt")

def getGeneratedHello = get("/src/main/resources/scala/test/generated-hello.txt")

private def get(s: String): List[String] =
scala.io.Source
.fromInputStream(getClass.getResourceAsStream(s))
Expand Down