Skip to content

docs: generate adev-compatible api json #30857

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
"integration-tests": "bazel test --test_tag_filters=-linker-integration-test --build_tests_only -- //integration/...",
"test-linker-aot": "bazel test --partial_compilation --test_tag_filters=partial-compilation-integration,-firefox --build_tests_only -- //integration/... //src/...",
"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/...",
"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",
"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",
"tsc": "node ./node_modules/typescript/bin/tsc",
"ci-push-deploy-docs-app": "node --no-warnings=ExperimentalWarning --loader ts-node/esm/transpile-only scripts/docs-deploy/deploy-ci-push.mts",
"ci-docs-monitor-test": "node --no-warnings=ExperimentalWarning --loader ts-node/esm/transpile-only scripts/docs-deploy/monitoring/ci-test.mts",
Expand Down
18 changes: 18 additions & 0 deletions src/cdk/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
load("//src/cdk:config.bzl", "CDK_ENTRYPOINTS", "CDK_ENTRYPOINTS_WITH_STYLES", "CDK_SCSS_LIBS", "CDK_TARGETS")
load("//tools:defaults.bzl", "ng_package", "sass_library", "ts_project")
load("@npm//:defs.bzl", "npm_link_all_packages")
load("@aspect_bazel_lib//lib:copy_to_directory.bzl", "copy_to_directory")

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

Expand Down Expand Up @@ -59,7 +60,11 @@ ng_package(
] + prebuiltStyleTargets + CDK_SCSS_LIBS,
nested_packages = [
"//src/cdk/schematics:npm_package",
":adev_assets",
],
replace_prefixes = {
"adev_assets/": "_adev_assets/",
},
tags = ["release-package"],
visibility = [
"//:__pkg__",
Expand All @@ -75,3 +80,16 @@ filegroup(
# which contain a slash are not in the top-level and do not have an overview.
srcs = ["//src/cdk/%s:overview" % ep for ep in CDK_ENTRYPOINTS if not "/" in ep],
)

copy_to_directory(
name = "adev_assets",
srcs = [
"//src/cdk/testing:json_api",
"//src/cdk/testing/protractor:json_api",
"//src/cdk/testing/selenium-webdriver:json_api",
"//src/cdk/testing/testbed:json_api",
],
replace_prefixes = {
"**/": "",
},
)
12 changes: 12 additions & 0 deletions src/cdk/testing/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
load("//src/e2e-app:test_suite.bzl", "e2e_test_suite")
load("//tools:defaults.bzl", "markdown_to_html", "ng_web_test_suite", "ts_project")
load("//src/cdk/testing/tests:webdriver-test.bzl", "webdriver_test")
load("//tools/adev-api-extraction:extract_api_to_json.bzl", "extract_api_to_json")

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

Expand Down Expand Up @@ -47,3 +48,14 @@ webdriver_test(
"//src/cdk/testing/tests:webdriver_test_sources",
],
)

extract_api_to_json(
name = "json_api",
srcs = [
":source-files",
],
entry_point = ":index.ts",
module_name = "@angular/cdk/testing",
output_name = "cdk_testing.json",
private_modules = [""],
)
12 changes: 12 additions & 0 deletions src/cdk/testing/protractor/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
load("//tools:defaults.bzl", "ts_project")
load("//tools/adev-api-extraction:extract_api_to_json.bzl", "extract_api_to_json")

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

Expand All @@ -20,3 +21,14 @@ filegroup(
name = "source-files",
srcs = glob(["**/*.ts"]),
)

extract_api_to_json(
name = "json_api",
srcs = [
":source-files",
],
entry_point = ":index.ts",
module_name = "@angular/cdk/testing/protractor",
output_name = "cdk_testing_protractor.json",
private_modules = [""],
)
12 changes: 12 additions & 0 deletions src/cdk/testing/selenium-webdriver/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
load("//tools:defaults.bzl", "ts_project")
load("//tools/adev-api-extraction:extract_api_to_json.bzl", "extract_api_to_json")

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

Expand All @@ -19,3 +20,14 @@ filegroup(
name = "source-files",
srcs = glob(["**/*.ts"]),
)

extract_api_to_json(
name = "json_api",
srcs = [
":source-files",
],
entry_point = ":index.ts",
module_name = "@angular/cdk//selenium-webdriver",
output_name = "cdk_testing_selenium_webdriver.json",
private_modules = [""],
)
12 changes: 12 additions & 0 deletions src/cdk/testing/testbed/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
load("//tools:defaults.bzl", "ng_web_test_suite", "ts_project")
load("//tools/adev-api-extraction:extract_api_to_json.bzl", "extract_api_to_json")

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

Expand Down Expand Up @@ -35,3 +36,14 @@ ng_web_test_suite(
name = "unit_tests",
deps = [":unit_test_sources"],
)

extract_api_to_json(
name = "json_api",
srcs = [
":source-files",
],
entry_point = ":index.ts",
module_name = "@angular/cdk/testing/testbed",
output_name = "cdk_testing_testbed.json",
private_modules = [""],
)
47 changes: 47 additions & 0 deletions tools/adev-api-extraction/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
load("@aspect_rules_js//js:defs.bzl", "js_binary")
load("//tools:defaults.bzl", "ts_project")
load("@aspect_rules_ts//ts:defs.bzl", "ts_config")

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

ts_project(
name = "extract_api_to_json_lib",
srcs = glob(
["**/*.mts"],
exclude = [
"**/*.spec.ts",
],
),
resolve_json_module = True,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are we reading JSON? or can this be dropped now?

tsconfig = ":tsconfig",
deps = [
"//:node_modules/@angular/compiler",
"//:node_modules/@angular/compiler-cli",
"//:node_modules/@bazel/runfiles",
"//:node_modules/@types/node",
"//:node_modules/typescript",
],
)

# Action binary for the api_gen bazel rule.
js_binary(
name = "extract_api_to_json",
data = [
":extract_api_to_json_lib",
"//:node_modules/typescript",
],
entry_point = "index.mjs",
visibility = ["//visibility:public"],
)

# Expose the sources in the dev-infra NPM package.
filegroup(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: might not be needed

name = "files",
srcs = glob(["**/*"]),
)

ts_config(
name = "tsconfig",
src = "tsconfig.json",
deps = ["//:node_modules/@types/node"],
)
3 changes: 3 additions & 0 deletions tools/adev-api-extraction/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Copied from https://github.com/angular/angular/tree/main/adev/shared-docs/pipeline/api-gen/extraction

TODO: share this script between angular/angular & angular/components
112 changes: 112 additions & 0 deletions tools/adev-api-extraction/extract_api_to_json.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
load("@build_bazel_rules_nodejs//:providers.bzl", "run_node")

def _extract_api_to_json(ctx):
"""Implementation of the extract_api_to_json rule"""

# Define arguments that will be passed to the underlying nodejs program.
args = ctx.actions.args()

# Use a param file because we may have a large number of inputs.
args.set_param_file_format("multiline")
args.use_param_file("%s", use_always = True)

# Pass the module_name for the extracted APIs. This will be something like "@angular/core".
args.add(ctx.attr.module_name)

# Pass the module_label for the extracted APIs, This is something like core for "@angular/core".
args.add(ctx.attr.module_label)

# Pass the set of private modules that should not be included in the API reference.
args.add_joined(ctx.attr.private_modules, join_with = ",")

# Pass the entry_point for from which to extract public symbols.
args.add(ctx.file.entry_point)

# Pass the set of source files from which API reference data will be extracted.
args.add_joined(ctx.files.srcs, join_with = ",")

# Pass the name of the output JSON file.
json_output = ctx.outputs.output_name
args.add(json_output.path)

# Pass the import path map
# TODO: consider module_mappings_aspect to deal with path mappings instead of manually
# specifying them
# https://github.com/bazelbuild/rules_nodejs/blob/5.x/internal/linker/link_node_modules.bzl#L236
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: update this TODO. With our new Bazel toolchain, this whole concept no longer exists (probably module_name attributes still need to be cleaned up though).

This way seems correct, but even better would be just leveraging the real tsconfig from src/bazel-tsconfig-build with the "real path mappings".

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed the TODO, not really sure how to do the leveraging the real tsconfig thing but if you explain over slack I can change it

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can follow-up in the future. It would mostly be about reading the tsconfig from src simply. btw: I still see the TODO, but not a big deal

path_map = {}
for target, path in ctx.attr.import_map.items():
files = target.files.to_list()
if len(files) != 1:
fail("Expected a single file in import_map target %s" % target.label)
path_map[path] = files[0].path
args.add(json.encode(path_map))

# Pass the set of (optional) extra entries
args.add_joined(ctx.files.extra_entries, join_with = ",")

# Define an action that runs the nodejs_binary executable. This is
# the main thing that this rule does.
run_node(
ctx = ctx,
inputs = depset(ctx.files.srcs + ctx.files.extra_entries),
executable = "_extract_api_to_json",
outputs = [json_output],
arguments = [args],
env = {
"BAZEL_BINDIR": ".",
},
)

# The return value describes what the rule is producing. In this case we need to specify
# the "DefaultInfo" with the output JSON files.
return [DefaultInfo(files = depset([json_output]))]

extract_api_to_json = rule(
# Point to the starlark function that will execute for this rule.
implementation = _extract_api_to_json,
doc = """Rule that extracts Angular API reference information from TypeScript
sources and write it to a JSON file""",

# The attributes that can be set to this rule.
attrs = {
"srcs": attr.label_list(
doc = """The source files for this rule. This must include one or more
TypeScript files.""",
allow_empty = False,
allow_files = True,
),
"output_name": attr.output(
doc = """Name of the JSON output file.""",
),
"entry_point": attr.label(
doc = """Source file entry-point from which to extract public symbols""",
mandatory = True,
allow_single_file = True,
),
"private_modules": attr.string_list(
doc = """List of private modules that should not be included in the API symbol linking""",
),
"import_map": attr.label_keyed_string_dict(
doc = """Map of import path to the index.ts file for that import""",
allow_files = True,
),
"module_name": attr.string(
doc = """JS Module name to be used for the extracted symbols""",
mandatory = True,
),
"module_label": attr.string(
doc = """Module label to be used for the extracted symbols. To be used as display name, for example in API docs""",
),
"extra_entries": attr.label_list(
doc = """JSON files that contain extra entries to append to the final collection.""",
allow_files = True,
),

# The executable for this rule (private).
"_extract_api_to_json": attr.label(
default = Label("//tools/adev-api-extraction:extract_api_to_json"),
executable = True,
cfg = "exec",
),
},
)
Loading
Loading