Simplify get_java_package implementation.
Root should be calculated by giving preference to fist occurrence not the last. Existing implementation is also incorrect.
"""Utility functions for the j2cl_* build rules / macros"""
-def _get_last_dir_occurrence_of(path, name):
- """Returns the index of the last occurrence of directory name in path."""
- if path == name:
- return 0
- if path.endswith("/" + name):
- return path.rfind(name)
- index = path.rfind("/" + name + "/")
- if index != -1:
- return index + 1
- if path.startswith(name + "/"):
- return 0
- else:
- return -1
+def get_java_package(path):
+ """Extract the java package from path"""
+ segments = path.split("/")
+ start_index = min([_find(segments, root) for root in ["java", "javatests"]])
+ if start_index == len(segments):
+ fail("Cannot find java root: " + path)
-def _get_java_root_index(pkg_name):
- """Returns the index of the java_root within a build package"""
- # Find the java folder in the beginning, middle or end of a path.
- java_index = _get_last_dir_occurrence_of(pkg_name, "java")
+ return ".".join(segments[start_index + 1:])
- # Find the javatests folder in the beginning, middle or end of a path.
- javatests_index = _get_last_dir_occurrence_of(pkg_name, "javatests")
-
- if java_index == -1 and javatests_index == -1:
- fail("can not find java root: " + pkg_name)
-
- if java_index > javatests_index:
- index = java_index + len("java/")
- else:
- index = javatests_index + len("javatests/")
- return index
-
-def get_java_root(pkg_name):
- """Extract the path to java root from the build package"""
- return pkg_name[:_get_java_root_index(pkg_name)]
-
-def get_java_path(pkg_name):
- """Extract the java path from the build package"""
- return pkg_name[len(get_java_root(pkg_name)):]
-
-def get_java_package(pkg_name):
- """Extract the java package from the build package"""
- return get_java_path(pkg_name).replace("/", ".")
+def _find(segments, s):
+ return segments.index(s) if s in segments else len(segments)
load(
"//javascript/tools/jscompiler/builddefs:flags.bzl",
