Skip to content

Commit 338743e

Browse files
devversionjosephperrott
authored andcommitted
build: generate api docs for cdk using bazel (angular#14427)
1 parent ec2e3ad commit 338743e

File tree

29 files changed

+349
-28
lines changed

29 files changed

+349
-28
lines changed

src/BUILD.bazel

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package(default_visibility = ["//visibility:public"])
22

3+
load("//:packages.bzl", "CDK_PACKAGES")
34
load("//tools:defaults.bzl", "ts_library")
5+
load("//tools/dgeni:index.bzl", "dgeni_api_docs")
46

57
exports_files([
68
"bazel-tsconfig-build.json",
@@ -12,3 +14,16 @@ ts_library(
1214
name = 'module-typings',
1315
srcs = [":module-typings.d.ts"]
1416
)
17+
18+
dgeni_api_docs(
19+
name = "api-docs",
20+
srcs = ["//src/cdk/%s:source-files" % name for name in CDK_PACKAGES] + [
21+
# Add all Angular packages to the sources because some Material exports use
22+
# Angular exports and these should not cause any warnings when Dgeni uses the
23+
# type checker to parse our TypeScript sources.
24+
"@matdeps//@angular"
25+
],
26+
entry_points = {
27+
"cdk": CDK_PACKAGES,
28+
},
29+
)

src/cdk/BUILD.bazel

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package(default_visibility=["//visibility:public"])
22

3-
load("//:packages.bzl", "CDK_TARGETS", "ROLLUP_GLOBALS")
3+
load("//:packages.bzl", "CDK_TARGETS", "CDK_PACKAGES", "ROLLUP_GLOBALS")
44
load("//tools:defaults.bzl", "ng_module", "ng_package")
55

66
# Root "@angular/cdk" entry-point that does not re-export individual entry-points.
@@ -13,6 +13,11 @@ ng_module(
1313
],
1414
)
1515

16+
filegroup(
17+
name = "overviews",
18+
srcs = ["//src/cdk/%s:overview" % name for name in CDK_PACKAGES]
19+
)
20+
1621
# Creates the @angular/cdk package published to npm.
1722
ng_package(
1823
name = "npm_package",

src/cdk/a11y/BUILD.bazel

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,8 @@ markdown_to_html(
5555
name = "overview",
5656
srcs = [":a11y.md"],
5757
)
58+
59+
filegroup(
60+
name = "source-files",
61+
srcs = glob(["**/*.ts"]),
62+
)

src/cdk/accordion/BUILD.bazel

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,9 @@ markdown_to_html(
3434
name = "overview",
3535
srcs = [],
3636
)
37+
38+
filegroup(
39+
name = "source-files",
40+
srcs = glob(["**/*.ts"]),
41+
)
42+

src/cdk/bidi/BUILD.bazel

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,8 @@ markdown_to_html(
3131
name = "overview",
3232
srcs = [":bidi.md"],
3333
)
34+
35+
filegroup(
36+
name = "source-files",
37+
srcs = glob(["**/*.ts"]),
38+
)

src/cdk/coercion/BUILD.bazel

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,8 @@ markdown_to_html(
2727
name = "overview",
2828
srcs = [":coercion.md"],
2929
)
30+
31+
filegroup(
32+
name = "source-files",
33+
srcs = glob(["**/*.ts"]),
34+
)

src/cdk/collections/BUILD.bazel

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,8 @@ markdown_to_html(
3131
name = "overview",
3232
srcs = [":collections.md"],
3333
)
34+
35+
filegroup(
36+
name = "source-files",
37+
srcs = glob(["**/*.ts"]),
38+
)

src/cdk/drag-drop/BUILD.bazel

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,8 @@ markdown_to_html(
3838
name = "overview",
3939
srcs = [":drag-drop.md"],
4040
)
41+
42+
filegroup(
43+
name = "source-files",
44+
srcs = glob(["**/*.ts"]),
45+
)

src/cdk/keycodes/BUILD.bazel

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,8 @@ markdown_to_html(
2828
name = "overview",
2929
srcs = [":keycodes.md"],
3030
)
31+
32+
filegroup(
33+
name = "source-files",
34+
srcs = glob(["**/*.ts"]),
35+
)

src/cdk/layout/BUILD.bazel

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,8 @@ markdown_to_html(
3434
name = "overview",
3535
srcs = [":layout.md"],
3636
)
37+
38+
filegroup(
39+
name = "source-files",
40+
srcs = glob(["**/*.ts"]),
41+
)

src/cdk/observers/BUILD.bazel

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,8 @@ markdown_to_html(
3030
name = "overview",
3131
srcs = [":observers.md"],
3232
)
33+
34+
filegroup(
35+
name = "source-files",
36+
srcs = glob(["**/*.ts"]),
37+
)

src/cdk/overlay/BUILD.bazel

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,7 @@ markdown_to_html(
6363
srcs = [":overlay.md"],
6464
)
6565

66+
filegroup(
67+
name = "source-files",
68+
srcs = glob(["**/*.ts"]),
69+
)

src/cdk/platform/BUILD.bazel

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,8 @@ markdown_to_html(
1616
name = "overview",
1717
srcs = [":platform.md"],
1818
)
19+
20+
filegroup(
21+
name = "source-files",
22+
srcs = glob(["**/*.ts"]),
23+
)

src/cdk/portal/BUILD.bazel

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,8 @@ markdown_to_html(
3131
srcs = [":portal.md"],
3232
)
3333

34+
filegroup(
35+
name = "source-files",
36+
srcs = glob(["**/*.ts"]),
37+
)
38+

src/cdk/scrolling/BUILD.bazel

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,8 @@ markdown_to_html(
4646
name = "overview",
4747
srcs = [":scrolling.md"],
4848
)
49+
50+
filegroup(
51+
name = "source-files",
52+
srcs = glob(["**/*.ts"]),
53+
)

src/cdk/stepper/BUILD.bazel

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,8 @@ markdown_to_html(
2323
name = "overview",
2424
srcs = [":stepper.md"],
2525
)
26+
27+
filegroup(
28+
name = "source-files",
29+
srcs = glob(["**/*.ts"]),
30+
)

src/cdk/table/BUILD.bazel

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,8 @@ markdown_to_html(
4040
name = "overview",
4141
srcs = [":table.md"],
4242
)
43+
44+
filegroup(
45+
name = "source-files",
46+
srcs = glob(["**/*.ts"]),
47+
)

src/cdk/text-field/BUILD.bazel

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,8 @@ markdown_to_html(
5252
name = "overview",
5353
srcs = [":text-field.md"],
5454
)
55+
56+
filegroup(
57+
name = "source-files",
58+
srcs = glob(["**/*.ts"]),
59+
)

src/cdk/tree/BUILD.bazel

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,8 @@ markdown_to_html(
3939
name = "overview",
4040
srcs = [":tree.md"],
4141
)
42+
43+
filegroup(
44+
name = "source-files",
45+
srcs = glob(["**/*.ts"]),
46+
)

src/lib/BUILD.bazel

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ sass_bundle(
2424
output_name = "_theming.scss",
2525
)
2626

27+
filegroup(
28+
name = "overviews",
29+
srcs = ["//src/lib/%s:overview" % name for name in MATERIAL_PACKAGES]
30+
)
31+
2732
# Creates the @angular/material package published to npm.
2833
ng_package(
2934
name = "npm_package",

src/material-examples/BUILD.bazel

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package(default_visibility=["//visibility:public"])
22

3-
load("//:packages.bzl", "CDK_TARGETS", "MATERIAL_TARGETS", "ROLLUP_GLOBALS", "MATERIAL_PACKAGES",
4-
"CDK_PACKAGES")
3+
load("//:packages.bzl", "CDK_TARGETS", "MATERIAL_TARGETS", "ROLLUP_GLOBALS")
54
load("//tools:defaults.bzl", "ng_module", "ng_package")
65
load("//tools/highlight-files:index.bzl", "highlight_files")
76
load("//tools/package-docs-content:index.bzl", "package_docs_content")
@@ -28,16 +27,6 @@ filegroup(
2827
srcs = glob(["*/*.html", "*/*.css", "*/*.ts"])
2928
)
3029

31-
filegroup(
32-
name = "material-overviews",
33-
srcs = ["//src/lib/%s:overview" % name for name in MATERIAL_PACKAGES]
34-
)
35-
36-
filegroup(
37-
name = "cdk-overviews",
38-
srcs = ["//src/cdk/%s:overview" % name for name in CDK_PACKAGES]
39-
)
40-
4130
highlight_files(
4231
name = "highlighted-source-files",
4332
srcs = [":example-source-files"]
@@ -59,8 +48,8 @@ package_docs_content(
5948
":example-source-files": "examples-source",
6049

6150
# Package the overviews for "@angular/material" and "@angular/cdk" into the docs content
62-
":material-overviews": "overviews/material",
63-
":cdk-overviews": "overviews/cdk",
51+
"//src/lib:overviews": "overviews/material",
52+
"//src/cdk:overviews": "overviews/cdk",
6453

6554
# TODO(devversion): we need to also package the API html files here
6655
}

tools/dgeni/BUILD.bazel

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package(default_visibility = ["//visibility:public"])
2+
3+
load("@build_bazel_rules_nodejs//:defs.bzl", "nodejs_binary")
4+
load("//tools:defaults.bzl", "ts_library")
5+
6+
nodejs_binary(
7+
name = "dgeni",
8+
entry_point = "angular_material/tools/dgeni/bazel-bin.js",
9+
data = [
10+
"@matdeps//source-map-support",
11+
"@matdeps//dgeni",
12+
"@matdeps//dgeni-packages",
13+
":sources",
14+
],
15+
)
16+
17+
ts_library(
18+
name = "sources",
19+
srcs = glob(["**/*.ts"]),
20+
deps = [
21+
"@matdeps//@types/node",
22+
"//tools/highlight-files:sources",
23+
],
24+
tsconfig = ":tsconfig.json",
25+
)

tools/dgeni/bazel-bin.ts

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
import {Dgeni} from 'dgeni';
2+
import {ReadTypeScriptModules} from 'dgeni-packages/typescript/processors/readTypeScriptModules';
3+
import {TsParser} from 'dgeni-packages/typescript/services/TsParser';
4+
import {readFileSync} from 'fs';
5+
import {join, relative} from 'path';
6+
import {apiDocsPackage} from './index';
7+
8+
/**
9+
* Determines the command line arguments for the current Bazel action. Since this action can
10+
* have a large set of input files, Bazel may write the arguments into a parameter file.
11+
* This function is responsible for handling normal argument passing or Bazel parameter files.
12+
* Read more here: https://docs.bazel.build/versions/master/skylark/lib/Args.html#use_param_file
13+
*/
14+
function getBazelActionArguments() {
15+
const args = process.argv.slice(2);
16+
17+
// If Bazel uses a parameter file, we've specified that it passes the file in the following
18+
// format: "arg0 arg1 --param-file={path_to_param_file}"
19+
if (args[0].startsWith('--param-file=')) {
20+
return readFileSync(args[0].split('=')[1], 'utf8').trim().split('\n');
21+
}
22+
23+
return args;
24+
}
25+
26+
if (require.main === module) {
27+
const [
28+
// Path that refers to the package where the current Bazel target is defined.
29+
bazelLabelPackagePath,
30+
// Path that is relative to the execroot and is the output directory for the docs.
31+
outputDirPath,
32+
// Remaining arguments that will be used to compute the entry points that need to be parsed.
33+
...entryPointArgs
34+
] = getBazelActionArguments();
35+
36+
const execRootPath = process.cwd();
37+
const packagePath = join(execRootPath, bazelLabelPackagePath);
38+
39+
// Configure the Dgeni docs package to respect our passed options from the Bazel rule.
40+
apiDocsPackage.config((readTypeScriptModules: ReadTypeScriptModules,
41+
tsParser: TsParser,
42+
templateFinder: any,
43+
writeFilesProcessor: any,
44+
readFilesProcessor: any) => {
45+
46+
// Set the base path for the "readFilesProcessor" to the execroot. This is necessary because
47+
// otherwise the "writeFilesProcessor" is not able to write to the specified output path.
48+
readFilesProcessor.basePath = execRootPath;
49+
50+
// Set the base path for parsing the TypeScript source files to the directory that includes
51+
// all sources (also known as the path to the current Bazel target). This makes it easier for
52+
// custom processors (such as the `entry-point-grouper) to compute entry-point paths.
53+
readTypeScriptModules.basePath = packagePath;
54+
55+
// For each package we want to setup all entry points in Dgeni so that their API
56+
// will be generated. Packages and their associated entry points are passed in pairs.
57+
// The first argument will be always the package name, and the second argument will be a
58+
// joined string containing names of all entry points for that specific package.
59+
// e.g. "cdk" "platform,bidi,a11y"
60+
for (let i = 0; i + 1 < entryPointArgs.length; i += 2) {
61+
const packageName = entryPointArgs[i];
62+
const entryPoints = entryPointArgs[i + 1].split(',');
63+
64+
// Walk through each entry point of the current package and add it to the
65+
// "readTypeScriptModules" processor so that it will parse it. Additionally we want
66+
// to setup path mapping for that entry-point, so that we are able to merge
67+
// inherited class members across entry points or packages.
68+
entryPoints.forEach(entryPointName => {
69+
const entryPointPath = `${packageName}/${entryPointName}`;
70+
const entryPointIndexPath = `${entryPointPath}/index.ts`;
71+
72+
tsParser.options.paths![`@angular/${entryPointPath}`] = [entryPointIndexPath];
73+
readTypeScriptModules.sourceFiles.push(entryPointIndexPath);
74+
});
75+
}
76+
77+
// Base URL for the `tsParser`. The base URL refer to the directory that includes all
78+
// package sources that need to be processed by Dgeni.
79+
tsParser.options.baseUrl = packagePath;
80+
81+
// This is ensures that the Dgeni TypeScript processor is able to parse node modules such
82+
// as the Angular packages which might be needed for doc items. e.g. if a class implements
83+
// the "AfterViewInit" interface from "@angular/core". This needs to be relative to the
84+
// "baseUrl" that has been specified for the "tsParser" compiler options.
85+
tsParser.options.paths!['*'] = [relative(packagePath, 'external/matdeps/node_modules/*')];
86+
87+
// Since our base directory is the Bazel execroot, we need to make sure that Dgeni can
88+
// find all templates needed to output the API docs.
89+
templateFinder.templateFolders = [join(execRootPath, 'tools/dgeni/templates/')];
90+
91+
// The output path for files will be computed by joining the output folder with the base path
92+
// from the "readFilesProcessors". Since the base path is the execroot, we can just use
93+
// the output path passed from Bazel (e.g. $EXECROOT/bazel-out/bin/src/docs-content)
94+
writeFilesProcessor.outputFolder = outputDirPath;
95+
});
96+
97+
// Run the docs generation. The process will be automatically kept alive until Dgeni
98+
// completed. In case the returned promise has been rejected, we need to manually exit the
99+
// process with the proper exit code because Dgeni doesn't use native promises which would
100+
// automatically cause the error to propagate. The error message will be automatically
101+
// printed internally by Dgeni (so we don't want to repeat here)
102+
new Dgeni([apiDocsPackage]).generate().catch(() => process.exit(1));
103+
}
104+
105+
106+

0 commit comments

Comments
 (0)