diff options
author | Damien Martin-Guillerez <dmarting@google.com> | 2016-02-26 10:36:35 +0000 |
---|---|---|
committer | Damien Martin-Guillerez <dmarting@google.com> | 2016-02-28 17:04:12 +0000 |
commit | cff5c43c9cb9d63efe38d2e77dae4c5655a53feb (patch) | |
tree | f8afddf221fc58920c335c03ca676cd14c6e4d88 | |
parent | 76139f29ca42f68f282f54531f4752d6091a831f (diff) |
Adds an executable argument to repository_ctx.file and repository_ctx.template
We sometime want to execute the created file. The executable argument
permit to control the executable bit on the created file.
Issue #893
--
MOS_MIGRATED_REVID=115653127
-rw-r--r-- | src/main/java/com/google/devtools/build/lib/bazel/repository/skylark/SkylarkRepositoryContext.java | 41 | ||||
-rwxr-xr-x | src/test/shell/bazel/skylark_repository_test.sh | 22 |
2 files changed, 56 insertions, 7 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/bazel/repository/skylark/SkylarkRepositoryContext.java b/src/main/java/com/google/devtools/build/lib/bazel/repository/skylark/SkylarkRepositoryContext.java index e2500dab2b..2817cab15d 100644 --- a/src/main/java/com/google/devtools/build/lib/bazel/repository/skylark/SkylarkRepositoryContext.java +++ b/src/main/java/com/google/devtools/build/lib/bazel/repository/skylark/SkylarkRepositoryContext.java @@ -184,12 +184,21 @@ public class SkylarkRepositoryContext { public void createFile(Object path) throws RepositoryFunctionException, EvalException { createFile(path, ""); } + @SkylarkCallable( + name = "file", + documented = false + ) + public void createFile(Object path, String content) + throws RepositoryFunctionException, EvalException { + createFile(path, content, true); + } @SkylarkCallable( name = "file", - doc = "Generate a file in the output directory with the provided content" + doc = "Generate a file in the output directory with the provided content. An optional third " + + "argument set the executable bit to on or off (default to True)." ) - public void createFile(Object path, String content) + public void createFile(Object path, String content, Boolean executable) throws RepositoryFunctionException, EvalException { SkylarkPath p = getPath("file()", path); try { @@ -198,21 +207,36 @@ public class SkylarkRepositoryContext { try (OutputStream stream = p.path.getOutputStream()) { stream.write(content.getBytes(StandardCharsets.UTF_8)); } + if (executable) { + p.path.setExecutable(true); + } } catch (IOException e) { throw new RepositoryFunctionException(e, Transience.TRANSIENT); } } @SkylarkCallable( - name = "template", - doc = - "Generate a new file using a <code>template</code>. Every occurrence in " - + "<code>template</code> of a key of <code>substitutions</code> will be replaced by " - + "the corresponding value. The result is written in <code>path</code>." + name = "template", + documented = false ) public void createFileFromTemplate( Object path, Object template, Map<String, String> substitutions) throws RepositoryFunctionException, EvalException { + createFileFromTemplate(path, template, substitutions, true); + } + + @SkylarkCallable( + name = "template", + doc = + "Generate a new file using a <code>template</code>. Every occurrence in " + + "<code>template</code> of a key of <code>substitutions</code> will be replaced by " + + "the corresponding value. The result is written in <code>path</code>. An optional" + + "<code>executable</code> argument (default to true) can be set to turn on or off" + + "the executable bit." + ) + public void createFileFromTemplate( + Object path, Object template, Map<String, String> substitutions, Boolean executable) + throws RepositoryFunctionException, EvalException { SkylarkPath p = getPath("template()", path); SkylarkPath t = getPath("template()", template); try { @@ -226,6 +250,9 @@ public class SkylarkRepositoryContext { try (OutputStream stream = p.path.getOutputStream()) { stream.write(tpl.getBytes(StandardCharsets.UTF_8)); } + if (executable) { + p.path.setExecutable(true); + } } catch (IOException e) { throw new RepositoryFunctionException(e, Transience.TRANSIENT); } diff --git a/src/test/shell/bazel/skylark_repository_test.sh b/src/test/shell/bazel/skylark_repository_test.sh index 64590ae856..7d49598e2f 100755 --- a/src/test/shell/bazel/skylark_repository_test.sh +++ b/src/test/shell/bazel/skylark_repository_test.sh @@ -343,6 +343,28 @@ EOF expect_log "BOZ" } +function test_skylark_repository_executable_flag() { + setup_skylark_repository + + # Our custom repository rule + cat >test.bzl <<EOF +def _impl(ctx): + ctx.file("test.sh", "exit 0") + ctx.file("BUILD", "sh_binary(name='bar',srcs=['test.sh'])", False) + ctx.template("test2", Label("//:bar"), {}, False) + ctx.template("test2.sh", Label("//:bar"), {}, True) +repo = repository_rule(implementation=_impl, local=True) +EOF + cat >bar + + bazel run @foo//:bar >& $TEST_log || fail "Execution of @foo//:bar failed" + output_base=$(bazel info output_base) + test -x "${output_base}/external/foo/test.sh" || fail "test.sh is not executable" + test -x "${output_base}/external/foo/test2.sh" || fail "test2.sh is not executable" + test ! -x "${output_base}/external/foo/BUILD" || fail "BUILD is executable" + test ! -x "${output_base}/external/foo/test2" || fail "test2 is executable" +} + function tear_down() { true } |