|
| 1 | +load("@build_bazel_rules_nodejs//:providers.bzl", "run_node") |
| 2 | + |
| 3 | +def _extract_api_to_json(ctx): |
| 4 | + """Implementation of the extract_api_to_json rule""" |
| 5 | + |
| 6 | + # Define arguments that will be passed to the underlying nodejs program. |
| 7 | + args = ctx.actions.args() |
| 8 | + |
| 9 | + # Use a param file because we may have a large number of inputs. |
| 10 | + args.set_param_file_format("multiline") |
| 11 | + args.use_param_file("%s", use_always = True) |
| 12 | + |
| 13 | + # Pass the module_name for the extracted APIs. This will be something like "@angular/core". |
| 14 | + args.add(ctx.attr.module_name) |
| 15 | + |
| 16 | + # Pass the module_label for the extracted APIs, This is something like core for "@angular/core". |
| 17 | + args.add(ctx.attr.module_label) |
| 18 | + |
| 19 | + # Pass the set of private modules that should not be included in the API reference. |
| 20 | + args.add_joined(ctx.attr.private_modules, join_with = ",") |
| 21 | + |
| 22 | + # Pass the entry_point for from which to extract public symbols. |
| 23 | + args.add(ctx.file.entry_point) |
| 24 | + |
| 25 | + # Pass the set of source files from which API reference data will be extracted. |
| 26 | + args.add_joined(ctx.files.srcs, join_with = ",") |
| 27 | + |
| 28 | + # Pass the name of the output JSON file. |
| 29 | + json_output = ctx.outputs.output_name |
| 30 | + args.add(json_output.path) |
| 31 | + |
| 32 | + # Pass the import path map |
| 33 | + # TODO: consider module_mappings_aspect to deal with path mappings instead of manually |
| 34 | + # specifying them |
| 35 | + # https://github.com/bazelbuild/rules_nodejs/blob/5.x/internal/linker/link_node_modules.bzl#L236 |
| 36 | + path_map = {} |
| 37 | + for target, path in ctx.attr.import_map.items(): |
| 38 | + files = target.files.to_list() |
| 39 | + if len(files) != 1: |
| 40 | + fail("Expected a single file in import_map target %s" % target.label) |
| 41 | + path_map[path] = files[0].path |
| 42 | + args.add(json.encode(path_map)) |
| 43 | + |
| 44 | + # Pass the set of (optional) extra entries |
| 45 | + args.add_joined(ctx.files.extra_entries, join_with = ",") |
| 46 | + |
| 47 | + # Define an action that runs the nodejs_binary executable. This is |
| 48 | + # the main thing that this rule does. |
| 49 | + run_node( |
| 50 | + ctx = ctx, |
| 51 | + inputs = depset(ctx.files.srcs + ctx.files.extra_entries), |
| 52 | + executable = "_extract_api_to_json", |
| 53 | + outputs = [json_output], |
| 54 | + arguments = [args], |
| 55 | + ) |
| 56 | + |
| 57 | + # The return value describes what the rule is producing. In this case we need to specify |
| 58 | + # the "DefaultInfo" with the output JSON files. |
| 59 | + return [DefaultInfo(files = depset([json_output]))] |
| 60 | + |
| 61 | +extract_api_to_json = rule( |
| 62 | + # Point to the starlark function that will execute for this rule. |
| 63 | + implementation = _extract_api_to_json, |
| 64 | + doc = """Rule that extracts Angular API reference information from TypeScript |
| 65 | + sources and write it to a JSON file""", |
| 66 | + |
| 67 | + # The attributes that can be set to this rule. |
| 68 | + attrs = { |
| 69 | + "srcs": attr.label_list( |
| 70 | + doc = """The source files for this rule. This must include one or more |
| 71 | + TypeScript files.""", |
| 72 | + allow_empty = False, |
| 73 | + allow_files = True, |
| 74 | + ), |
| 75 | + "output_name": attr.output( |
| 76 | + doc = """Name of the JSON output file.""", |
| 77 | + ), |
| 78 | + "entry_point": attr.label( |
| 79 | + doc = """Source file entry-point from which to extract public symbols""", |
| 80 | + mandatory = True, |
| 81 | + allow_single_file = True, |
| 82 | + ), |
| 83 | + "private_modules": attr.string_list( |
| 84 | + doc = """List of private modules that should not be included in the API symbol linking""", |
| 85 | + ), |
| 86 | + "import_map": attr.label_keyed_string_dict( |
| 87 | + doc = """Map of import path to the index.ts file for that import""", |
| 88 | + allow_files = True, |
| 89 | + ), |
| 90 | + "module_name": attr.string( |
| 91 | + doc = """JS Module name to be used for the extracted symbols""", |
| 92 | + mandatory = True, |
| 93 | + ), |
| 94 | + "module_label": attr.string( |
| 95 | + doc = """Module label to be used for the extracted symbols. To be used as display name, for example in API docs""", |
| 96 | + ), |
| 97 | + "extra_entries": attr.label_list( |
| 98 | + doc = """JSON files that contain extra entries to append to the final collection.""", |
| 99 | + allow_files = True, |
| 100 | + ), |
| 101 | + |
| 102 | + # The executable for this rule (private). |
| 103 | + "_extract_api_to_json": attr.label( |
| 104 | + default = Label("//tools/adev-api-extraction:extract_api_to_json"), |
| 105 | + executable = True, |
| 106 | + cfg = "exec", |
| 107 | + ), |
| 108 | + }, |
| 109 | +) |
0 commit comments