|
| 1 | +""" |
| 2 | + Implementation of the "package_docs_content" rule. The implementation runs the |
| 3 | + packager executable in order to group all specified files into the given sections. |
| 4 | +""" |
| 5 | +def _package_docs_content(ctx): |
| 6 | + # Directory that will contain all grouped input files. This directory will be created |
| 7 | + # relatively to the current target package. (e.g. "bin/src/material-examples/docs-content") |
| 8 | + output_dir = ctx.attr.name; |
| 9 | + |
| 10 | + # Arguments that will be passed to the packager executable. |
| 11 | + args = ctx.actions.args() |
| 12 | + |
| 13 | + # List of outputs that should be generated by the packager action. Bazel will automatically |
| 14 | + # throw an error if any output has not been generated properly. |
| 15 | + expected_outputs = []; |
| 16 | + |
| 17 | + # Support passing arguments through a parameter file. This is necessary because on Windows |
| 18 | + # there is an argument limit and we need to handle a large amount of input files. Bazel |
| 19 | + # switches between parameter file and normal argument passing based on the operating system. |
| 20 | + # Read more here: https://docs.bazel.build/versions/master/skylark/lib/Args.html#use_param_file |
| 21 | + args.use_param_file(param_file_arg = "--param-file=%s") |
| 22 | + |
| 23 | + # Walk through each defined input target and the associated section and compute the |
| 24 | + # output file which will be added to the executable arguments. |
| 25 | + for input_target, section_name in ctx.attr.srcs.items(): |
| 26 | + section_files = input_target.files.to_list() |
| 27 | + |
| 28 | + for input_file in section_files: |
| 29 | + # For each input file, we want to create a copy that is stored in the output directory |
| 30 | + # within its specified section. e.g. "pkg_bin/docs-content/guides/getting-started.html" |
| 31 | + output_file = ctx.actions.declare_file( |
| 32 | + "%s/%s/%s" % (output_dir, section_name, input_file.basename)) |
| 33 | + |
| 34 | + # Add the output file to the expected outputs so that Bazel throws an error if the file |
| 35 | + # hasn't been generated properly. |
| 36 | + expected_outputs += [output_file] |
| 37 | + |
| 38 | + # Pass the input file path and the output file path to the packager executable. We need |
| 39 | + # to do this for each file because we cannot determine the general path to the output |
| 40 | + # directory in a reliable way because Bazel targets cannot just "declare" a directory. |
| 41 | + # See: https://docs.bazel.build/versions/master/skylark/lib/actions.html |
| 42 | + args.add("%s,%s" % (input_file.path, output_file.path)) |
| 43 | + |
| 44 | + # Do nothing if there are no input files. Bazel will throw if we schedule an action |
| 45 | + # that returns no outputs. |
| 46 | + if not ctx.files.srcs: |
| 47 | + return None |
| 48 | + |
| 49 | + # Run the packager executable that groups the specified source files and writes them |
| 50 | + # to the given output directory. |
| 51 | + ctx.actions.run( |
| 52 | + inputs = ctx.files.srcs, |
| 53 | + executable = ctx.executable._packager, |
| 54 | + outputs = expected_outputs, |
| 55 | + arguments = [args], |
| 56 | + ) |
| 57 | + |
| 58 | + return DefaultInfo(files = depset(expected_outputs)) |
| 59 | + |
| 60 | +""" |
| 61 | + Rule definition for the "package_docs_content" rule that can accept arbritary source files |
| 62 | + that will be grouped into specified sections. This is being used to package the docs |
| 63 | + content into a desired folder structure that can be shared with the docs application. |
| 64 | +""" |
| 65 | +package_docs_content = rule( |
| 66 | + implementation = _package_docs_content, |
| 67 | + attrs = { |
| 68 | + # This defines the sources for the "package_docs_content" rule. Instead of just |
| 69 | + # accepting a list of labels, this rule requires the developer to specify a label |
| 70 | + # keyed dictionary. This allows developers to specify where specific targets |
| 71 | + # should be grouped into. This helpful when publishing the docs content because |
| 72 | + # the docs repository does not about the directory structure of the generated files. |
| 73 | + "srcs": attr.label_keyed_string_dict(allow_files = True), |
| 74 | + |
| 75 | + # Executable for this rule that is responsible for packaging the specified |
| 76 | + # targets into the associated sections. |
| 77 | + "_packager": attr.label( |
| 78 | + default = Label("//tools/package-docs-content"), |
| 79 | + executable = True, |
| 80 | + cfg = "host" |
| 81 | + )}, |
| 82 | +) |
0 commit comments