Skip to content

Commit

Permalink
Reformat files with Buildifier
Browse files Browse the repository at this point in the history
  • Loading branch information
laurentlb committed May 22, 2018
1 parent 100965e commit 8fd1f5a
Show file tree
Hide file tree
Showing 21 changed files with 319 additions and 259 deletions.
2 changes: 1 addition & 1 deletion java-tutorial/src/main/java/com/example/cmdline/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ java_binary(
name = "runner",
srcs = ["Runner.java"],
main_class = "com.example.cmdline.Runner",
deps = ["//:greeter"]
deps = ["//:greeter"],
)
38 changes: 22 additions & 16 deletions rules/actions_run/execute.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,28 @@ and users cannot redefine it.
"""

def _impl(ctx):
# The list of arguments we pass to the script.
args = [ctx.outputs.out.path] + [f.path for f in ctx.files.chunks]
# Action to call the script.
ctx.actions.run(
inputs=ctx.files.chunks,
outputs=[ctx.outputs.out],
arguments=args,
progress_message="Merging into %s" % ctx.outputs.out.short_path,
executable=ctx.executable._merge_tool)
# The list of arguments we pass to the script.
args = [ctx.outputs.out.path] + [f.path for f in ctx.files.chunks]

# Action to call the script.
ctx.actions.run(
inputs = ctx.files.chunks,
outputs = [ctx.outputs.out],
arguments = args,
progress_message = "Merging into %s" % ctx.outputs.out.short_path,
executable = ctx.executable._merge_tool,
)

concat = rule(
implementation=_impl,
attrs={
"chunks": attr.label_list(allow_files=True),
"out": attr.output(mandatory=True),
"_merge_tool": attr.label(executable=True, cfg="host", allow_files=True,
default=Label("//actions_run:merge"))
}
implementation = _impl,
attrs = {
"chunks": attr.label_list(allow_files = True),
"out": attr.output(mandatory = True),
"_merge_tool": attr.label(
executable = True,
cfg = "host",
allow_files = True,
default = Label("//actions_run:merge"),
),
},
)
10 changes: 5 additions & 5 deletions rules/actions_write/file.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ large files with a lot of static content, consider using
"""

def _impl(ctx):
output = ctx.outputs.out
ctx.actions.write(output=output, content=ctx.attr.content)
output = ctx.outputs.out
ctx.actions.write(output = output, content = ctx.attr.content)

file = rule(
implementation=_impl,
attrs={"content": attr.string()},
outputs={"out": "%{name}.txt"},
implementation = _impl,
attrs = {"content": attr.string()},
outputs = {"out": "%{name}.txt"},
)
64 changes: 34 additions & 30 deletions rules/aspect/file_collector.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -5,51 +5,55 @@ For more information about aspects, see the documentation:
"""

FileCollector = provider(
fields = {"files" : "collected files"}
fields = {"files": "collected files"},
)

def _file_collector_aspect_impl(target, ctx):
# This function is executed for each dependency the aspect visits.
# This function is executed for each dependency the aspect visits.

# Collect files from the srcs
direct = [f
for f in ctx.rule.files.srcs
if ctx.attr.extension == "*" or ctx.attr.extension == f.extension]
# Collect files from the srcs
direct = [
f
for f in ctx.rule.files.srcs
if ctx.attr.extension == "*" or ctx.attr.extension == f.extension
]

# Combine direct files with the files from the dependencies.
files = depset(
direct=direct,
transitive=[dep[FileCollector].files for dep in ctx.rule.attr.deps])
# Combine direct files with the files from the dependencies.
files = depset(
direct = direct,
transitive = [dep[FileCollector].files for dep in ctx.rule.attr.deps],
)

return [FileCollector(files=files)]
return [FileCollector(files = files)]

file_collector_aspect = aspect(
implementation=_file_collector_aspect_impl,
attr_aspects=["deps"],
attrs={
"extension": attr.string(values=["*", "h", "cc"]),
}
implementation = _file_collector_aspect_impl,
attr_aspects = ["deps"],
attrs = {
"extension": attr.string(values = ["*", "h", "cc"]),
},
)

def _file_collector_rule_impl(ctx):
# This function is executed once per `file_collector`.
# This function is executed once per `file_collector`.

# Write the collected information to the output file.
content = []
for dep in ctx.attr.deps:
files = [f.short_path for f in dep[FileCollector].files.to_list()]
content.append("files from {}: {}".format(dep.label, files))
content += [""] # trailing newline
# Write the collected information to the output file.
content = []
for dep in ctx.attr.deps:
files = [f.short_path for f in dep[FileCollector].files.to_list()]
content.append("files from {}: {}".format(dep.label, files))
content += [""] # trailing newline

ctx.actions.write(
output=ctx.outputs.out,
content="\n".join(content))
ctx.actions.write(
output = ctx.outputs.out,
content = "\n".join(content),
)

file_collector = rule(
implementation=_file_collector_rule_impl,
attrs={
"deps": attr.label_list(aspects=[file_collector_aspect]),
"extension": attr.string(default="*"),
implementation = _file_collector_rule_impl,
attrs = {
"deps": attr.label_list(aspects = [file_collector_aspect]),
"extension": attr.string(default = "*"),
},
outputs = {"out": "%{name}.files"},
)
30 changes: 16 additions & 14 deletions rules/attributes/printer.bzl
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
"""Example of a rule that accesses its attributes."""

def _impl(ctx):
# Print debug information about the target.
print("Target {} has {} deps".format(ctx.label, len(ctx.attr.deps)))
# Print debug information about the target.
print("Target {} has {} deps".format(ctx.label, len(ctx.attr.deps)))

# For each target in deps, print its label and files.
for i, d in enumerate(ctx.attr.deps):
print(" {}. label = {}".format(i+1, d.label))
# A label can represent any number of files (possibly 0).
print(" files = " + str([f.path for f in d.files.to_list()]))
# For each target in deps, print its label and files.
for i, d in enumerate(ctx.attr.deps):
print(" {}. label = {}".format(i + 1, d.label))

# For debugging, consider using `dir` to explore the existing fields.
print(dir(ctx)) # prints all the fields and methods of ctx
print(dir(ctx.attr)) # prints all the attributes of the rule
# A label can represent any number of files (possibly 0).
print(" files = " + str([f.path for f in d.files.to_list()]))

# For debugging, consider using `dir` to explore the existing fields.
print(dir(ctx)) # prints all the fields and methods of ctx
print(dir(ctx.attr)) # prints all the attributes of the rule

printer = rule(
implementation=_impl,
attrs={
implementation = _impl,
attrs = {
# Do not declare "name": It is added automatically.
"number": attr.int(default = 1),
"deps": attr.label_list(allow_files=True),
})
"deps": attr.label_list(allow_files = True),
},
)
68 changes: 36 additions & 32 deletions rules/computed_dependencies/hash.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -18,40 +18,44 @@ _filters = {
"none": None,
}

def _get_filter(filter): # requires attribute "filter"
# Return the value for the attribute "_filter_bin"
# It can be a label or None.
return _filters[filter]
def _get_filter(filter): # requires attribute "filter"
# Return the value for the attribute "_filter_bin"
# It can be a label or None.
return _filters[filter]

def _impl(ctx):
src = ctx.file.src

if not ctx.attr._filter_bin:
# Skip the processing
processed = src
else:
# The temporary file is based on 'ctx.label.name' to avoid conflicts.
processed = ctx.actions.declare_file(ctx.label.name + "_processed")
# Run the selected binary
ctx.actions.run(
outputs = [processed],
inputs = [ctx.file.src],
progress_message="Apply filter '%s'" % ctx.attr.filter,
arguments = [ctx.file.src.path, processed.path],
executable = ctx.executable._filter_bin)

# Compute the hash
out = ctx.outputs.text
ctx.actions.run_shell(
outputs = [out],
inputs = [processed],
command = "md5sum < %s > %s" % (processed.path, out.path))
src = ctx.file.src

if not ctx.attr._filter_bin:
# Skip the processing
processed = src
else:
# The temporary file is based on 'ctx.label.name' to avoid conflicts.
processed = ctx.actions.declare_file(ctx.label.name + "_processed")

# Run the selected binary
ctx.actions.run(
outputs = [processed],
inputs = [ctx.file.src],
progress_message = "Apply filter '%s'" % ctx.attr.filter,
arguments = [ctx.file.src.path, processed.path],
executable = ctx.executable._filter_bin,
)

# Compute the hash
out = ctx.outputs.text
ctx.actions.run_shell(
outputs = [out],
inputs = [processed],
command = "md5sum < %s > %s" % (processed.path, out.path),
)

md5_sum = rule(
implementation=_impl,
attrs={
"filter": attr.string(values=_filters.keys(), default="none"),
"src": attr.label(mandatory=True, single_file=True, allow_files=True),
"_filter_bin": attr.label(default=_get_filter, executable=True, cfg="host"),
implementation = _impl,
attrs = {
"filter": attr.string(values = _filters.keys(), default = "none"),
"src": attr.label(mandatory = True, single_file = True, allow_files = True),
"_filter_bin": attr.label(default = _get_filter, executable = True, cfg = "host"),
},
outputs = {"text": "%{name}.txt"})
outputs = {"text": "%{name}.txt"},
)
21 changes: 16 additions & 5 deletions rules/depsets/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,35 @@ py_binary(

foo_library(
name = "a",
srcs = ["a.foo", "a_impl.foo"],
srcs = [
"a.foo",
"a_impl.foo",
],
)

foo_library(
name = "b",
srcs = ["b.foo", "b_impl.foo"],
srcs = [
"b.foo",
"b_impl.foo",
],
deps = [":a"],
)

foo_library(
name = "c",
srcs = ["c.foo", "c_impl.foo"],
srcs = [
"c.foo",
"c_impl.foo",
],
deps = [":a"],
)

foo_binary(
name = "d",
srcs = ["d.foo"],
deps = [":b", ":c"],
deps = [
":b",
":c",
],
)

54 changes: 30 additions & 24 deletions rules/depsets/foo.bzl
Original file line number Diff line number Diff line change
@@ -1,48 +1,54 @@
FooFiles = provider("transitive_sources")

def get_transitive_srcs(srcs, deps):
"""Obtain the source files for a target and its transitive dependencies.
"""Obtain the source files for a target and its transitive dependencies.
Args:
srcs: a list of source files
deps: a list of targets that are direct dependencies
Returns:
a collection of the transitive sources
"""
return depset(
Args:
srcs: a list of source files
deps: a list of targets that are direct dependencies
Returns:
a collection of the transitive sources
"""
return depset(
srcs,
transitive = [dep[FooFiles].transitive_sources for dep in deps])
transitive = [dep[FooFiles].transitive_sources for dep in deps],
)

def _foo_library_impl(ctx):
trans_srcs = get_transitive_srcs(ctx.files.srcs, ctx.attr.deps)
return [FooFiles(transitive_sources=trans_srcs)]
trans_srcs = get_transitive_srcs(ctx.files.srcs, ctx.attr.deps)
return [FooFiles(transitive_sources = trans_srcs)]

foo_library = rule(
implementation = _foo_library_impl,
attrs = {
"srcs": attr.label_list(allow_files=True),
"srcs": attr.label_list(allow_files = True),
"deps": attr.label_list(),
},
)

def _foo_binary_impl(ctx):
foocc = ctx.executable._foocc
out = ctx.outputs.out
trans_srcs = get_transitive_srcs(ctx.files.srcs, ctx.attr.deps)
srcs_list = trans_srcs.to_list()
ctx.actions.run(executable = foocc,
arguments = [out.path] + [src.path for src in srcs_list],
inputs = srcs_list + [foocc],
outputs = [out])
foocc = ctx.executable._foocc
out = ctx.outputs.out
trans_srcs = get_transitive_srcs(ctx.files.srcs, ctx.attr.deps)
srcs_list = trans_srcs.to_list()
ctx.actions.run(
executable = foocc,
arguments = [out.path] + [src.path for src in srcs_list],
inputs = srcs_list + [foocc],
outputs = [out],
)

foo_binary = rule(
implementation = _foo_binary_impl,
attrs = {
"srcs": attr.label_list(allow_files=True),
"srcs": attr.label_list(allow_files = True),
"deps": attr.label_list(),
"_foocc": attr.label(default=Label("//depsets:foocc"),
allow_files=True, executable=True, cfg="host")
"_foocc": attr.label(
default = Label("//depsets:foocc"),
allow_files = True,
executable = True,
cfg = "host",
),
},
outputs = {"out": "%{name}.out"},
)

Loading

0 comments on commit 8fd1f5a

Please sign in to comment.