Skip to content

Commit a0aa103

Browse files
mmalerbajosephperrott
authored andcommitted
docs: generate adev-compatible api json
1 parent 0e39170 commit a0aa103

File tree

13 files changed

+611
-2
lines changed

13 files changed

+611
-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_json_apis",
6264
],
65+
replace_prefixes = {
66+
"adev_json_apis/": "_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_json_apis",
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

@@ -35,3 +36,14 @@ ng_web_test_suite(
3536
name = "unit_tests",
3637
deps = [":unit_test_sources"],
3738
)
39+
40+
extract_api_to_json(
41+
name = "json_api",
42+
srcs = [
43+
":source-files",
44+
],
45+
entry_point = ":index.ts",
46+
module_name = "@angular/cdk/testing/testbed",
47+
output_name = "cdk_testing_testbed.json",
48+
private_modules = [""],
49+
)

tools/adev-api-extraction/BUILD.bazel

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
load("@build_bazel_rules_nodejs//:index.bzl", "nodejs_binary")
2+
load("@aspect_rules_esbuild//esbuild:defs.bzl", "esbuild")
3+
load("//tools:defaults2.bzl", "ts_project")
4+
load("@aspect_rules_ts//ts:defs.bzl", rules_js_tsconfig = "ts_config")
5+
6+
package(default_visibility = ["//visibility:public"])
7+
8+
esbuild(
9+
name = "bin",
10+
bundle = True,
11+
entry_point = ":index.ts",
12+
external = [
13+
"typescript",
14+
],
15+
format = "esm",
16+
output = "bin.mjs",
17+
platform = "node",
18+
target = "es2022",
19+
deps = [
20+
":extract_api_to_json_lib",
21+
"//:node_modules/@angular/compiler-cli",
22+
],
23+
)
24+
25+
ts_project(
26+
name = "extract_api_to_json_lib",
27+
srcs = glob(
28+
["**/*.ts"],
29+
exclude = [
30+
"**/*.spec.ts",
31+
],
32+
),
33+
resolve_json_module = True,
34+
tsconfig = ":tsconfig",
35+
deps = [
36+
"//:node_modules/@angular/compiler",
37+
"//:node_modules/@angular/compiler-cli",
38+
"//:node_modules/@bazel/runfiles",
39+
"//:node_modules/@types/node",
40+
"//:node_modules/typescript",
41+
],
42+
)
43+
44+
# Action binary for the api_gen bazel rule.
45+
nodejs_binary(
46+
name = "extract_api_to_json",
47+
data = [
48+
":bin",
49+
"//:node_modules/typescript",
50+
],
51+
entry_point = "bin.mjs",
52+
# Note: Using the linker here as we need it for ESM. The linker is not
53+
# super reliably when running concurrently on Windows- but we have existing
54+
# actions using the linker. An alternative would be to:
55+
# - bundle the Angular compiler into a CommonJS bundle
56+
# - use the patched resolution- but also patch the ESM imports (similar to how FW does it).
57+
visibility = ["//visibility:public"],
58+
)
59+
60+
# Expose the sources in the dev-infra NPM package.
61+
filegroup(
62+
name = "files",
63+
srcs = glob(["**/*"]),
64+
)
65+
66+
rules_js_tsconfig(
67+
name = "tsconfig",
68+
src = "tsconfig.json",
69+
deps = ["//:node_modules/@types/node"],
70+
)

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,109 @@
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

Comments
 (0)