|
| 1 | +""" |
| 2 | + Gets a path relative to the specified label. This is achieved by just removing the label |
| 3 | + package path from the specified path. e.g. the path is "guides/test/my-text.md" and the |
| 4 | + label package is "guides/". The expected path would be "test/my-text.md". |
| 5 | +""" |
| 6 | +def _relative_to_label(label, short_path): |
| 7 | + # TODO(devversion): extract into generic utility under tools/ |
| 8 | + return short_path[len(label.package) + 1:] |
| 9 | + |
| 10 | +""" |
| 11 | + Implementation of the "highlight_files" rule. The implementation runs the |
| 12 | + highlight-files executable in order to highlight the specified source files. |
| 13 | +""" |
| 14 | +def _highlight_files(ctx): |
| 15 | + input_files = ctx.files.srcs; |
| 16 | + args = ctx.actions.args() |
| 17 | + expected_outputs = []; |
| 18 | + |
| 19 | + # Do nothing if there are no input files. Bazel will throw if we schedule an action |
| 20 | + # that returns no outputs. |
| 21 | + if not input_files: |
| 22 | + return None |
| 23 | + |
| 24 | + # Support passing arguments through a parameter file. This is necessary because on Windows |
| 25 | + # there is an argument limit and we need to handle a large amount of input files. Bazel |
| 26 | + # switches between parameter file and normal argument passing based on the operating system. |
| 27 | + # Read more here: https://docs.bazel.build/versions/master/skylark/lib/Args.html#use_param_file |
| 28 | + args.use_param_file(param_file_arg = "--param-file=%s") |
| 29 | + |
| 30 | + # Add the bazel bin directory to the command arguments. The script needs to know about |
| 31 | + # the output directory because the input files are not in the same location as the bazel |
| 32 | + # bin directory. |
| 33 | + args.add(ctx.bin_dir.path) |
| 34 | + |
| 35 | + for input_file in input_files: |
| 36 | + # Extension of the input file (e.g. "ts" or "css") |
| 37 | + file_extension = input_file.extension |
| 38 | + |
| 39 | + # Determine the input file path relatively to the current package path. This is necessary |
| 40 | + # because we want to preserve directories for the input files and `declare_file` expects a |
| 41 | + # path that is relative to the current package. We remove the file extension including the dot |
| 42 | + # because we will constructo an output file using a different extension. |
| 43 | + relative_basepath = _relative_to_label(ctx.label, input_file.short_path)[ |
| 44 | + :-len(file_extension) - 1] |
| 45 | + |
| 46 | + # Construct the output path from the relative basepath and file extension. For example: |
| 47 | + # "autocomplete.ts" should result in "autocomplete-ts.html". |
| 48 | + expected_outputs += [ |
| 49 | + ctx.actions.declare_file("%s-%s.html" % (relative_basepath, file_extension)), |
| 50 | + ] |
| 51 | + |
| 52 | + # Add the path for the input file to the command line arguments, so that the executable |
| 53 | + # can process it. |
| 54 | + args.add(input_file.path) |
| 55 | + |
| 56 | + # Run the highlight-files executable that highlights the specified source files. |
| 57 | + ctx.actions.run( |
| 58 | + inputs = input_files, |
| 59 | + executable = ctx.executable._highlight_files, |
| 60 | + outputs = expected_outputs, |
| 61 | + arguments = [args], |
| 62 | + ) |
| 63 | + |
| 64 | + return DefaultInfo(files = depset(expected_outputs)) |
| 65 | + |
| 66 | +""" |
| 67 | + Rule definition for the "highlight_files" rule that can accept arbritary source files |
| 68 | + that will be transformed into HTML files which reflect the highlighted source code of |
| 69 | + the given files. The outputs can be referenced through the default output provider. |
| 70 | +""" |
| 71 | +highlight_files = rule( |
| 72 | + implementation = _highlight_files, |
| 73 | + attrs = { |
| 74 | + "srcs": attr.label_list(allow_files = True), |
| 75 | + |
| 76 | + # Executable for this rule that is responsible for highlighting the specified |
| 77 | + # input files. |
| 78 | + "_highlight_files": attr.label( |
| 79 | + default = Label("//tools/highlight-files"), |
| 80 | + executable = True, |
| 81 | + cfg = "host" |
| 82 | + )}, |
| 83 | +) |
0 commit comments