-
Notifications
You must be signed in to change notification settings - Fork 522
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: preserve legacy "$(rlocation)" usage to make custom variabl…
…e expansion non-breaking
- Loading branch information
1 parent
5358d56
commit 1211a42
Showing
5 changed files
with
84 additions
and
3 deletions.
There are no files selected for viewing
This file contains 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,36 @@ | ||
# Copyright 2017 The Bazel Authors. All rights reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
"""Helper functions to preserve legacy `$(rlocation ` usage | ||
""" | ||
|
||
def preserve_legacy_rlocation(input): | ||
"""Converts legacy `$(rlocation ` to `$(rlocation ` which is preserved when expanding make | ||
variables with ctx.expand_make_variables. | ||
Args: | ||
input: String to be modified | ||
Returns: | ||
The modified string | ||
""" | ||
result = "" | ||
length = len(input) | ||
for i in range(length): | ||
if input[i:].startswith("$(rlocation "): | ||
if i == 0 or input[i - 1] != "$": | ||
# insert an additional "$" | ||
result += "$" | ||
result += input[i] | ||
return result |
This file contains 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
This file contains 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,31 @@ | ||
"Unit tests for //internal/common:preserve_legacy_rlocation.bzl" | ||
|
||
load("@bazel_skylib//lib:unittest.bzl", "asserts", "unittest") | ||
load("//internal/common:preserve_legacy_rlocation.bzl", "preserve_legacy_rlocation") | ||
|
||
def _impl(ctx): | ||
env = unittest.begin(ctx) | ||
|
||
conversions = { | ||
"$$(rlocation foobar)": "$$(rlocation foobar)", | ||
"$(rlocation foobar)": "$$(rlocation foobar)", | ||
"$(rlocation! foobar)": "$(rlocation! foobar)", | ||
} | ||
|
||
for key in conversions: | ||
asserts.equals(env, "%s" % conversions[key], preserve_legacy_rlocation("%s" % key)) | ||
asserts.equals(env, " %s " % conversions[key], preserve_legacy_rlocation(" %s " % key)) | ||
asserts.equals(env, "%s%s" % (conversions[key], conversions[key]), preserve_legacy_rlocation("%s%s" % (key, key))) | ||
asserts.equals(env, "%s %s" % (conversions[key], conversions[key]), preserve_legacy_rlocation("%s %s" % (key, key))) | ||
asserts.equals(env, " %s %s " % (conversions[key], conversions[key]), preserve_legacy_rlocation(" %s %s " % (key, key))) | ||
asserts.equals(env, "a%sb%sc" % (conversions[key], conversions[key]), preserve_legacy_rlocation("a%sb%sc" % (key, key))) | ||
|
||
return unittest.end(env) | ||
|
||
preserve_legacy_rlocation_test = unittest.make( | ||
impl = _impl, | ||
attrs = {}, | ||
) | ||
|
||
def preserve_legacy_rlocation_test_suite(): | ||
unittest.suite("preserve_legacy_rlocation_tests", preserve_legacy_rlocation_test) |
This file contains 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
This file contains 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