-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Caleb Froese
committed
Oct 11, 2019
0 parents
commit e4b8074
Showing
11 changed files
with
149 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
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,37 @@ | ||
# rules_yaml | ||
|
||
This project provides [Bazel](http://bazel.build) rules for operating with YAML. | ||
|
||
## Setup | ||
|
||
In your WORKSPACE file, include the following snippets. | ||
This will tell Bazel to fetch the YAML rules archive, extract it, and prepare it for use in your application. | ||
|
||
```bzl | ||
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") | ||
|
||
http_archive( | ||
name = "rules_yaml", | ||
sha256 = "<see github releases page for checksum>", | ||
url = "https://github.com/calebfroese/rules_yaml/archive/0.0.1.zip", | ||
) | ||
|
||
load("@rules_yaml//:defs.bzl", "yaml_repositories") | ||
|
||
# This sets up the dependencies for rules_yaml | ||
yaml_repositories() | ||
``` | ||
|
||
## Usage | ||
|
||
You can pull elements into new YAML files using the `yaml_excerpt` rule. | ||
|
||
```bzl | ||
# Extract elements nested under the path into a new file | ||
yaml_excerpt( | ||
name = "first_car_info", | ||
src = "template.yml", | ||
path = "config.properties.cars[0].Details", | ||
) | ||
# Which can then be passed in to other rules as ":first_car_info" | ||
``` |
Empty file.
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,5 @@ | ||
load("//internal/rules:yaml_excerpt.bzl", _yaml_excerpt = "yaml_excerpt") | ||
load("//internal/repository:yaml_repositories.bzl", _yaml_repositories = "yaml_repositories") | ||
|
||
yaml_excerpt = _yaml_excerpt | ||
yaml_repositories = _yaml_repositories |
Empty file.
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 @@ | ||
package(default_visibility = ["//internal:__subpackages__"]) |
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,9 @@ | ||
def select_compiler(compiler): | ||
"""Will return the correct YAML yq executable for the host machine.""" | ||
if compiler != None: | ||
return compiler | ||
return select({ | ||
"@bazel_tools//src/conditions:darwin": "@yq_binary_darwin//file", | ||
"@bazel_tools//src/conditions:windows": "@yq_binary_windows//file", | ||
"//conditions:default": "@yq_binary_linux//file", | ||
}) |
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 @@ | ||
package(default_visibility = ["//internal:__subpackages__"]) |
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,39 @@ | ||
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_file") | ||
|
||
_YQ_SOURCES = { | ||
"darwin_amd64": ( | ||
["https://github.com/mikefarah/yq/releases/download/2.4.0/yq_darwin_amd64"], | ||
"f09d3db259a0073a441362e94c7973a921c58903c7dd7cb7894672037687322c", | ||
), | ||
"windows_amd64": ( | ||
["https://github.com/mikefarah/yq/releases/download/2.4.0/yq_windows_amd64.exe"], | ||
"793dac701765fce832afff85104cb208c1392a94c369474222fe42a119e1bcd1", | ||
), | ||
"linux_amd64": ( | ||
["https://github.com/mikefarah/yq/releases/download/2.4.0/yq_linux_amd64"], | ||
"99a01ae32f0704773c72103adb7050ef5c5cad14b517a8612543821ef32d6cc9", | ||
), | ||
} | ||
|
||
def yaml_repositories(yq_sources = _YQ_SOURCES): | ||
"""Declares workspaces that the YAML rules depends on. All consumers of rules_yaml should call this in their | ||
WORKSPACE.""" | ||
|
||
http_file( | ||
name = "yq_binary_darwin", | ||
executable = True, | ||
urls = yq_sources["darwin_amd64"][0], | ||
sha256 = yq_sources["darwin_amd64"][1], | ||
) | ||
http_file( | ||
name = "yq_binary_windows", | ||
executable = True, | ||
urls = yq_sources["windows_amd64"][0], | ||
sha256 = yq_sources["windows_amd64"][1], | ||
) | ||
http_file( | ||
name = "yq_binary_linux", | ||
executable = True, | ||
urls = yq_sources["linux_amd64"][0], | ||
sha256 = yq_sources["linux_amd64"][1], | ||
) |
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 @@ | ||
package(default_visibility = ["//internal:__subpackages__"]) |
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,56 @@ | ||
load("//internal/common:utils.bzl", "select_compiler") | ||
|
||
def yaml_excerpt(name, src, path, compiler = None): | ||
"""Macro to wrap the yaml_excerpt rule getting around the restriction of running Starlarks `select` in rules.""" | ||
_yaml_excerpt( | ||
name = name, | ||
src = src, | ||
path = path, | ||
compiler = select_compiler(compiler), | ||
) | ||
|
||
def _yaml_excerpt_impl(ctx): | ||
executable = ctx.executable.compiler | ||
sourcefile = ctx.file.src | ||
outputfile = ctx.outputs.excerpt | ||
print("Exec", executable) | ||
|
||
ctx.actions.run_shell( | ||
tools = [executable], | ||
outputs = [outputfile], | ||
inputs = [sourcefile], | ||
command = "{executable} read {yaml_file} {path} > {output}".format( | ||
executable = executable.path, | ||
yaml_file = sourcefile.path, | ||
path = ctx.attr.path, | ||
output = outputfile.path, | ||
), | ||
) | ||
|
||
_yaml_excerpt = rule( | ||
doc = """Extracts YAML at a path into a new file | ||
Uses JSON path syntax to reference a YAML element and output a file with the selected contents. A custom | ||
yq executable can optionally be provided.""", | ||
implementation = _yaml_excerpt_impl, | ||
attrs = { | ||
"src": attr.label( | ||
doc = """The source YAML file to perform operations on""", | ||
mandatory = True, | ||
allow_single_file = True, | ||
), | ||
"path": attr.string( | ||
doc = """JSON path reference to the element, for example `a.b.c`. `$` can be specified to reference the root.""", | ||
mandatory = True, | ||
), | ||
"compiler": attr.label( | ||
doc = """yq executable""", | ||
allow_files = True, | ||
executable = True, | ||
cfg = "host", | ||
), | ||
}, | ||
outputs = { | ||
"excerpt": "%{name}.yaml", | ||
}, | ||
) |