Skip to content

Commit

Permalink
feat: write_source_files
Browse files Browse the repository at this point in the history
  • Loading branch information
kormide authored and gregmagolan committed Feb 7, 2022
1 parent 8a287de commit 8e6a15c
Show file tree
Hide file tree
Showing 14 changed files with 443 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
docs/*.md
lib/tests/jq/*.json
lib/tests/write_source_files/a2.js
lib/tests/write_source_files/b2.js
5 changes: 5 additions & 0 deletions docs/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ stardoc_with_diff_test(
out_label = "//docs:jq.md",
)

stardoc_with_diff_test(
bzl_library_target = "//lib:write_source_files",
out_label = "//docs:write_source_files.md",
)

update_docs(
name = "update",
docs_folder = "docs",
Expand Down
61 changes: 61 additions & 0 deletions docs/write_source_files.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<!-- Generated with Stardoc: http://skydoc.bazel.build -->

Public API for write_source_files

<a id="#write_source_files"></a>

## write_source_files

<pre>
write_source_files(<a href="#write_source_files-name">name</a>, <a href="#write_source_files-files">files</a>, <a href="#write_source_files-kwargs">kwargs</a>)
</pre>

Write to one or more files in the source tree. Stamp out tests that ensure the files exists and are up to date.

Usage:

```starlark
load("@aspect_bazel_lib//lib:write_source_files.bzl", "write_source_files")

write_source_files(
name = "write_foobar",
files = {
"foobar.json": "//some/generated:file",
},
)
```

To update the source file, run:
```bash
bazel run //:write_foobar
```

A test will fail if the source file doesn't exist
```bash
bazel test //...

//:foobar.json does not exist. To create & update this file, run:

bazel run //:write_foobar
```

...or if it's out of date.
```bash
bazel test //...

//:foobar.json is out-of-date. To update this file, run:

bazel run //:write_foobar
```


**PARAMETERS**


| Name | Description | Default Value |
| :------------- | :------------- | :------------- |
| <a id="write_source_files-name"></a>name | Name of the executable target that creates or updates the source file | none |
| <a id="write_source_files-files"></a>files | A dict where the keys are source files to write to and the values are labels pointing to the desired content. Source files must be within the same bazel package as the target. | none |
| <a id="write_source_files-kwargs"></a>kwargs | Other common named parameters such as <code>tags</code> or <code>visibility</code> | none |


12 changes: 12 additions & 0 deletions lib/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,15 @@ toolchain_type(
name = "jq_toolchain_type",
visibility = ["//visibility:public"],
)

bzl_library(
name = "write_source_files",
srcs = ["write_source_files.bzl"],
visibility = ["//visibility:public"],
deps = [
":utils",
"//lib/private:fail_with_message_test",
"//lib/private:write_source_files",
"@bazel_skylib//rules:diff_test",
],
)
15 changes: 15 additions & 0 deletions lib/private/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,18 @@ bzl_library(
srcs = ["jq.bzl"],
visibility = ["//lib:__subpackages__"],
)

bzl_library(
name = "write_source_files",
srcs = ["write_source_files.bzl"],
visibility = ["//lib:__subpackages__"],
deps = [
"//lib:utils",
],
)

bzl_library(
name = "fail_with_message_test",
srcs = ["fail_with_message_test.bzl"],
visibility = ["//lib:__subpackages__"],
)
12 changes: 12 additions & 0 deletions lib/private/fail_with_message_test.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
"Test rule that always fails and prints a message"

def _fail_with_message_test_impl(ctx):
fail(ctx.attr.message)

fail_with_message_test = rule(
attrs = {
"message": attr.string(mandatory = True),
},
implementation = _fail_with_message_test_impl,
test = True,
)
65 changes: 65 additions & 0 deletions lib/private/write_source_files.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
"write_source_file implementation"

load("//lib:utils.bzl", "is_external_label")

_write_source_files_attrs = {
"in_files": attr.label_list(allow_files = True, allow_empty = False, mandatory = True),
"out_files": attr.label_list(allow_files = True, allow_empty = False, mandatory = True),
"is_windows": attr.bool(mandatory = True),
}

def _write_source_files_impl(ctx):
if ctx.attr.is_windows:
fail("write_source_file is not yet implemented for windows")

if (len(ctx.attr.in_files) != len(ctx.attr.out_files)):
fail("in_files and out_files must be the same length")

for i in range(len(ctx.attr.in_files)):
out_file_label = ctx.attr.out_files[i].label
if is_external_label(out_file_label):
fail("out file %s must be a source file in the user workspace" % out_file_label)

if not ctx.files.out_files[i].is_source:
fail("out file %s must be a source file, not a generated file" % out_file_label)

if out_file_label.package != ctx.label.package:
fail("out file %s (in package '%s') must be a source file within the target's package: '%s'" % (out_file_label, out_file_label.package, ctx.label.package))

updater = ctx.actions.declare_file(
ctx.label.name + "_update.sh",
)

ctx.actions.write(
output = updater,
content = """
#!/usr/bin/env bash
set -o errexit -o nounset -o pipefail
runfiles_dir=$PWD
# BUILD_WORKSPACE_DIRECTORY not set when running as a test, uses the sandbox instead
if [[ ! -z "${BUILD_WORKSPACE_DIRECTORY:-}" ]]; then
cd "$BUILD_WORKSPACE_DIRECTORY"
fi
""" + "\n".join([
"""
in=$runfiles_dir/{in_file}
out={out_file}
mkdir -p "$(dirname "$out")"
echo "Copying $in to $out in $PWD"
cp -f "$in" "$out"
chmod 644 "$out"
""".format(in_file = ctx.files.in_files[i].short_path, out_file = ctx.files.out_files[i].short_path)
for i in range(len(ctx.attr.in_files))
]),
)

return DefaultInfo(
executable = updater,
runfiles = ctx.runfiles(files = ctx.files.in_files),
)

write_source_files_lib = struct(
attrs = _write_source_files_attrs,
implementation = _write_source_files_impl,
)
34 changes: 34 additions & 0 deletions lib/tests/write_source_files/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
load("//lib/tests/write_source_files:write_source_files_test.bzl", "write_source_files_test")
load("//lib:write_source_files.bzl", "write_source_files")

genrule(
name = "a-desired",
outs = ["a-desired.js"],
cmd = "echo 'console.log(\"a*\")' > $@",
)

genrule(
name = "b-desired",
outs = ["b-desired.js"],
cmd = "echo 'console.log(\"b*\")' > $@",
)

write_source_files_test(
name = "write_to_source_files_test",
in_files = [
":a-desired",
":b-desired",
],
out_files = [
"a.js",
"b.js",
],
)

write_source_files(
name = "macro_smoke_test",
files = {
"a2.js": ":a-desired",
"b2.js": ":b-desired",
},
)
1 change: 1 addition & 0 deletions lib/tests/write_source_files/a.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log("a");
1 change: 1 addition & 0 deletions lib/tests/write_source_files/a2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log("a*")
1 change: 1 addition & 0 deletions lib/tests/write_source_files/b.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log("b");
1 change: 1 addition & 0 deletions lib/tests/write_source_files/b2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log("b*")
102 changes: 102 additions & 0 deletions lib/tests/write_source_files/write_source_files_test.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
"""Tests for write_source_files"""
# Inspired by https://github.com/cgrindel/bazel-starlib/blob/main/updatesrc/private/updatesrc_update_test.bzl

load("//lib/private:write_source_files.bzl", _lib = "write_source_files_lib")

_write_source_files = rule(
attrs = _lib.attrs,
implementation = _lib.implementation,
executable = True,
)

def _impl(ctx):
test = ctx.actions.declare_file(
ctx.label.name + "_test.sh",
)

ctx.actions.write(
output = test,
is_executable = True,
content = """
#!/usr/bin/env bash
set -o errexit -o nounset -o pipefail
assert_different() {
local in_file="${1}"
local out_file="${2}"
diff "${in_file}" "${out_file}" > /dev/null && (echo >&2 "Expected files to differ. in: ${in_file}, out: ${out_file}" && return -1)
return 0
}
assert_same() {
local in_file="${1}"
local out_file="${2}"
diff "${in_file}" "${out_file}" || (echo >&2 "Expected files to be same. in: ${in_file}, out: ${out_file}" && return -1)
}
# Check that in and out files are different
""" + "\n".join([
"assert_different {in_file} {out_file}".format(
in_file = ctx.files.in_files[i].short_path,
out_file = ctx.files.out_files[i].short_path,
)
for i in range(len(ctx.files.in_files))
]) + """
# Write to the source files
{write_source_files}
# Check that in and out files are the same
""".format(write_source_files = ctx.file.write_source_files_target.short_path) + "\n".join([
"assert_same {in_file} {out_file}".format(
in_file = ctx.files.in_files[i].short_path,
out_file = ctx.files.out_files[i].short_path,
)
for i in range(len(ctx.files.in_files))
]),
)

return DefaultInfo(
executable = test,
runfiles = ctx.runfiles(files = [ctx.file.write_source_files_target] + ctx.files.in_files + ctx.files.out_files),
)

_write_source_files_test = rule(
implementation = _impl,
attrs = {
"write_source_files_target": attr.label(
allow_single_file = True,
executable = True,
cfg = "exec",
),
"out_files": attr.label_list(
allow_files = True,
allow_empty = False,
mandatory = True,
),
"in_files": attr.label_list(
allow_files = True,
allow_empty = False,
mandatory = True,
),
},
test = True,
)

def write_source_files_test(name, in_files, out_files):
"""Stamp a write_source_files executable and a test to run against it"""

_write_source_files(
name = name + "_updater",
out_files = out_files,
in_files = in_files,
is_windows = False,
)

# Note that for testing we update the source files in the sandbox,
# not the actual source tree.
_write_source_files_test(
name = name,
write_source_files_target = name + "_updater",
out_files = out_files,
in_files = in_files,
)
Loading

0 comments on commit 8e6a15c

Please sign in to comment.