Skip to content
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

Fix resource_strip_prefix of scala_libary for external repositories #944

Merged
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
5 changes: 5 additions & 0 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,11 @@ filegroup(
path = "third_party/test/new_local_repo",
)

local_repository(
name = "strip_resource_external_workspace",
path = "third_party/test/strip_resource_external_workspace",
)

load("@io_bazel_rules_scala//scala:toolchains.bzl", "scala_register_unused_deps_toolchains")

scala_register_unused_deps_toolchains()
Expand Down
21 changes: 12 additions & 9 deletions scala/private/phases/phase_compile.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#
# DOCUMENT THIS
#
load("@bazel_skylib//lib:paths.bzl", _paths = "paths")
load("@bazel_tools//tools/jdk:toolchain_utils.bzl", "find_java_runtime_toolchain", "find_java_toolchain")
load(
"@io_bazel_rules_scala//scala/private:coverage_replacements_provider.bzl",
Expand Down Expand Up @@ -480,17 +481,17 @@ def _try_to_compile_java_jar(
java_compilation_provider = provider,
)

def _adjust_resources_path(path, resource_strip_prefix):
def _adjust_resources_path(resource, resource_strip_prefix):
if resource_strip_prefix:
return _adjust_resources_path_by_strip_prefix(path, resource_strip_prefix)
return _adjust_resources_path_by_strip_prefix(resource, resource_strip_prefix)
else:
return _adjust_resources_path_by_default_prefixes(path)
return _adjust_resources_path_by_default_prefixes(resource.path)

def _add_resources_cmd(ctx):
res_cmd = []
for f in ctx.files.resources:
c_dir, res_path = _adjust_resources_path(
f.short_path,
f,
ctx.attr.resource_strip_prefix,
)
target_path = res_path
Expand All @@ -504,12 +505,14 @@ def _add_resources_cmd(ctx):
res_cmd.extend([line])
return "".join(res_cmd)

def _adjust_resources_path_by_strip_prefix(path, resource_strip_prefix):
if not path.startswith(resource_strip_prefix):
fail("Resource file %s is not under the specified prefix to strip" % path)
def _adjust_resources_path_by_strip_prefix(resource, resource_strip_prefix):
path = resource.path
prefix = _paths.join(resource.owner.workspace_root, resource_strip_prefix)
Copy link
Member

Choose a reason for hiding this comment

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

Nice, thanks!

if not path.startswith(prefix):
fail("Resource file %s is not under the specified prefix %s to strip" % (path, prefix))

clean_path = path[len(resource_strip_prefix):]
return resource_strip_prefix, clean_path
clean_path = path[len(prefix):]
return prefix, clean_path

def _collect_java_providers_of(deps):
providers = []
Expand Down
9 changes: 9 additions & 0 deletions test/src/main/scala/scalarules/test/resources/strip/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,12 @@ scala_specs2_junit_test(
unused_dependency_checker_mode = "off",
deps = [":noSrcsWithResources"],
)

scala_specs2_junit_test(
name = "resouceStripPrefixFromExternalRepoTest",
size = "small",
srcs = ["ResourceStripPrefixTest.scala"],
suffixes = ["Test"],
unused_dependency_checker_mode = "off",
deps = ["@strip_resource_external_workspace//strip:noSrcsWithResources"],
)
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
workspace(name = "strip_resource_external_workspace")
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
load("@io_bazel_rules_scala//scala:scala.bzl", "scala_library")

scala_library(
name = "noSrcsWithResources",
resource_strip_prefix = "strip",
resources = ["nosrc_jar_resource.txt"],
visibility = ["//visibility:public"],
)
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
I am a text resource!