Skip to content

Commit b2ee3fc

Browse files
committed
docs: generate adev-compatible api json (#30857)
* docs: generate adev-compatible api json * fixup! docs: generate adev-compatible api json * fixup! docs: generate adev-compatible api json * fixup! docs: generate adev-compatible api json * fixup! docs: generate adev-compatible api json (cherry picked from commit 2c078ac)
1 parent 5efd099 commit b2ee3fc

File tree

13 files changed

+574
-2
lines changed

13 files changed

+574
-2
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
"integration-tests": "bazel test --test_tag_filters=-linker-integration-test --build_tests_only -- //integration/...",
4747
"test-linker-aot": "bazel test --partial_compilation --test_tag_filters=partial-compilation-integration,-firefox --build_tests_only -- //integration/... //src/...",
4848
"test-linker-jit": "bazel test --partial_compilation --test_tag_filters=partial-compilation-integration,-firefox --build_tests_only --//tools:force_partial_jit_compilation=True -- //integration/... //src/...",
49-
"check-tooling-setup": "pnpm tsc --project tools/tsconfig.json --noEmit && pnpm tsc --project scripts/tsconfig.json --noEmit && pnpm tsc --project .ng-dev/tsconfig.json --noEmit",
49+
"check-tooling-setup": "pnpm tsc --project tools/tsconfig.json --noEmit && pnpm tsc --project tools/adev-api-extraction/tsconfig.json --noEmit && pnpm tsc --project scripts/tsconfig.json --noEmit && pnpm tsc --project .ng-dev/tsconfig.json --noEmit",
5050
"tsc": "node ./node_modules/typescript/bin/tsc",
5151
"ci-push-deploy-docs-app": "node --no-warnings=ExperimentalWarning --loader ts-node/esm/transpile-only scripts/docs-deploy/deploy-ci-push.mts",
5252
"ci-docs-monitor-test": "node --no-warnings=ExperimentalWarning --loader ts-node/esm/transpile-only scripts/docs-deploy/monitoring/ci-test.mts",

src/cdk/BUILD.bazel

+18
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
load("//src/cdk:config.bzl", "CDK_ENTRYPOINTS", "CDK_ENTRYPOINTS_WITH_STYLES", "CDK_SCSS_LIBS", "CDK_TARGETS")
22
load("//tools:defaults.bzl", "ng_package", "sass_library", "ts_project")
33
load("@npm//:defs.bzl", "npm_link_all_packages")
4+
load("@aspect_bazel_lib//lib:copy_to_directory.bzl", "copy_to_directory")
45

56
package(default_visibility = ["//visibility:public"])
67

@@ -59,7 +60,11 @@ ng_package(
5960
] + prebuiltStyleTargets + CDK_SCSS_LIBS,
6061
nested_packages = [
6162
"//src/cdk/schematics:npm_package",
63+
":adev_assets",
6264
],
65+
replace_prefixes = {
66+
"adev_assets/": "_adev_assets/",
67+
},
6368
tags = ["release-package"],
6469
visibility = [
6570
"//:__pkg__",
@@ -75,3 +80,16 @@ filegroup(
7580
# which contain a slash are not in the top-level and do not have an overview.
7681
srcs = ["//src/cdk/%s:overview" % ep for ep in CDK_ENTRYPOINTS if not "/" in ep],
7782
)
83+
84+
copy_to_directory(
85+
name = "adev_assets",
86+
srcs = [
87+
"//src/cdk/testing:json_api",
88+
"//src/cdk/testing/protractor:json_api",
89+
"//src/cdk/testing/selenium-webdriver:json_api",
90+
"//src/cdk/testing/testbed:json_api",
91+
],
92+
replace_prefixes = {
93+
"**/": "",
94+
},
95+
)

src/cdk/testing/BUILD.bazel

+12
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
load("//src/e2e-app:test_suite.bzl", "e2e_test_suite")
22
load("//tools:defaults.bzl", "markdown_to_html", "ng_web_test_suite", "ts_project")
33
load("//src/cdk/testing/tests:webdriver-test.bzl", "webdriver_test")
4+
load("//tools/adev-api-extraction:extract_api_to_json.bzl", "extract_api_to_json")
45

56
package(default_visibility = ["//visibility:public"])
67

@@ -47,3 +48,14 @@ webdriver_test(
4748
"//src/cdk/testing/tests:webdriver_test_sources",
4849
],
4950
)
51+
52+
extract_api_to_json(
53+
name = "json_api",
54+
srcs = [
55+
":source-files",
56+
],
57+
entry_point = ":index.ts",
58+
module_name = "@angular/cdk/testing",
59+
output_name = "cdk_testing.json",
60+
private_modules = [""],
61+
)

src/cdk/testing/protractor/BUILD.bazel

+12
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
load("//tools:defaults.bzl", "ts_project")
2+
load("//tools/adev-api-extraction:extract_api_to_json.bzl", "extract_api_to_json")
23

34
package(default_visibility = ["//visibility:public"])
45

@@ -20,3 +21,14 @@ filegroup(
2021
name = "source-files",
2122
srcs = glob(["**/*.ts"]),
2223
)
24+
25+
extract_api_to_json(
26+
name = "json_api",
27+
srcs = [
28+
":source-files",
29+
],
30+
entry_point = ":index.ts",
31+
module_name = "@angular/cdk/testing/protractor",
32+
output_name = "cdk_testing_protractor.json",
33+
private_modules = [""],
34+
)

src/cdk/testing/selenium-webdriver/BUILD.bazel

+12
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
load("//tools:defaults.bzl", "ts_project")
2+
load("//tools/adev-api-extraction:extract_api_to_json.bzl", "extract_api_to_json")
23

34
package(default_visibility = ["//visibility:public"])
45

@@ -19,3 +20,14 @@ filegroup(
1920
name = "source-files",
2021
srcs = glob(["**/*.ts"]),
2122
)
23+
24+
extract_api_to_json(
25+
name = "json_api",
26+
srcs = [
27+
":source-files",
28+
],
29+
entry_point = ":index.ts",
30+
module_name = "@angular/cdk//selenium-webdriver",
31+
output_name = "cdk_testing_selenium_webdriver.json",
32+
private_modules = [""],
33+
)

src/cdk/testing/testbed/BUILD.bazel

+12
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
load("//tools:defaults.bzl", "ng_web_test_suite", "ts_project")
2+
load("//tools/adev-api-extraction:extract_api_to_json.bzl", "extract_api_to_json")
23

34
package(default_visibility = ["//visibility:public"])
45

@@ -37,3 +38,14 @@ ng_web_test_suite(
3738
name = "unit_tests",
3839
deps = [":unit_test_sources"],
3940
)
41+
42+
extract_api_to_json(
43+
name = "json_api",
44+
srcs = [
45+
":source-files",
46+
],
47+
entry_point = ":index.ts",
48+
module_name = "@angular/cdk/testing/testbed",
49+
output_name = "cdk_testing_testbed.json",
50+
private_modules = [""],
51+
)

tools/adev-api-extraction/BUILD.bazel

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
load("@aspect_rules_js//js:defs.bzl", "js_binary")
2+
load("//tools:defaults.bzl", "ts_project")
3+
load("@aspect_rules_ts//ts:defs.bzl", "ts_config")
4+
5+
package(default_visibility = ["//visibility:public"])
6+
7+
ts_project(
8+
name = "extract_api_to_json_lib",
9+
srcs = glob(
10+
["**/*.mts"],
11+
exclude = [
12+
"**/*.spec.ts",
13+
],
14+
),
15+
resolve_json_module = True,
16+
tsconfig = ":tsconfig",
17+
deps = [
18+
"//:node_modules/@angular/compiler",
19+
"//:node_modules/@angular/compiler-cli",
20+
"//:node_modules/@bazel/runfiles",
21+
"//:node_modules/@types/node",
22+
"//:node_modules/typescript",
23+
],
24+
)
25+
26+
# Action binary for the api_gen bazel rule.
27+
js_binary(
28+
name = "extract_api_to_json",
29+
data = [
30+
":extract_api_to_json_lib",
31+
"//:node_modules/typescript",
32+
],
33+
entry_point = "index.mjs",
34+
visibility = ["//visibility:public"],
35+
)
36+
37+
# Expose the sources in the dev-infra NPM package.
38+
filegroup(
39+
name = "files",
40+
srcs = glob(["**/*"]),
41+
)
42+
43+
ts_config(
44+
name = "tsconfig",
45+
src = "tsconfig.json",
46+
deps = ["//:node_modules/@types/node"],
47+
)

tools/adev-api-extraction/README.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Copied from https://github.com/angular/angular/tree/main/adev/shared-docs/pipeline/api-gen/extraction
2+
3+
TODO: share this script between angular/angular & angular/components
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
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+
env = {
56+
"BAZEL_BINDIR": ".",
57+
},
58+
)
59+
60+
# The return value describes what the rule is producing. In this case we need to specify
61+
# the "DefaultInfo" with the output JSON files.
62+
return [DefaultInfo(files = depset([json_output]))]
63+
64+
extract_api_to_json = rule(
65+
# Point to the starlark function that will execute for this rule.
66+
implementation = _extract_api_to_json,
67+
doc = """Rule that extracts Angular API reference information from TypeScript
68+
sources and write it to a JSON file""",
69+
70+
# The attributes that can be set to this rule.
71+
attrs = {
72+
"srcs": attr.label_list(
73+
doc = """The source files for this rule. This must include one or more
74+
TypeScript files.""",
75+
allow_empty = False,
76+
allow_files = True,
77+
),
78+
"output_name": attr.output(
79+
doc = """Name of the JSON output file.""",
80+
),
81+
"entry_point": attr.label(
82+
doc = """Source file entry-point from which to extract public symbols""",
83+
mandatory = True,
84+
allow_single_file = True,
85+
),
86+
"private_modules": attr.string_list(
87+
doc = """List of private modules that should not be included in the API symbol linking""",
88+
),
89+
"import_map": attr.label_keyed_string_dict(
90+
doc = """Map of import path to the index.ts file for that import""",
91+
allow_files = True,
92+
),
93+
"module_name": attr.string(
94+
doc = """JS Module name to be used for the extracted symbols""",
95+
mandatory = True,
96+
),
97+
"module_label": attr.string(
98+
doc = """Module label to be used for the extracted symbols. To be used as display name, for example in API docs""",
99+
),
100+
"extra_entries": attr.label_list(
101+
doc = """JSON files that contain extra entries to append to the final collection.""",
102+
allow_files = True,
103+
),
104+
105+
# The executable for this rule (private).
106+
"_extract_api_to_json": attr.label(
107+
default = Label("//tools/adev-api-extraction:extract_api_to_json"),
108+
executable = True,
109+
cfg = "exec",
110+
),
111+
},
112+
)

0 commit comments

Comments
 (0)