Skip to content

Commit ba76d1c

Browse files
committed
add more descriptive comments for changes in bazel
1 parent bd7a1a3 commit ba76d1c

File tree

7 files changed

+130
-112
lines changed

7 files changed

+130
-112
lines changed

src/components-examples/BUILD.bazel

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,8 @@ ng_package(
108108
name = "npm_package",
109109
srcs = ["package.json"],
110110
entry_point = ":public-api.ts",
111+
// this is a workaround to store a tree artifact in the ng_package.
112+
// ng_package does not properly handle tree artifacts currently so we escalate to nested_packages
111113
nested_packages = [":docs-content"],
112114
tags = ["docs-package"],
113115
deps = [":components-examples"] + ALL_EXAMPLES,

tools/highlight-files/highlight-files.ts

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,30 @@ function getBazelActionArguments() {
2626
return args;
2727
}
2828

29+
function _detectAndHighlightRegionBlocks(parsed:
30+
{ contents: string, regions: { [p: string]: string } },
31+
basePath: string,
32+
outDir: string,
33+
fileExtension: string) {
34+
for (const [regionName, regionSnippet] of Object.entries(parsed.regions)) {
35+
// Create files for each found region
36+
if (!regionName) {
37+
continue;
38+
}
39+
const highlightedRegion = highlightCodeBlock(regionSnippet, fileExtension);
40+
// Convert "my-component-example.ts" into "my-component-example_region-ts.html"
41+
const regionBaseOutputPath = basePath.replace(`.${fileExtension}`,
42+
`_${regionName}-${fileExtension}.html`);
43+
const regionOutputPath = join(outDir, regionBaseOutputPath);
44+
ensureDirSync(dirname(regionOutputPath));
45+
writeFileSync(regionOutputPath, highlightedRegion);
46+
}
47+
}
48+
2949
if (require.main === module) {
3050
// The script expects the output directory as first argument. Second is the name of the
3151
// package where this the highlight target is declared. All remaining arguments will be
3252
// considered as markdown input files that need to be transformed.
33-
3453
const [outDir, packageName, ...inputFiles] = getBazelActionArguments();
3554

3655
// Walk through each input file and write transformed markdown output
@@ -41,17 +60,7 @@ if (require.main === module) {
4160
const basePath = relative(packageName, execPath);
4261
const fileExtension = extname(basePath).substring(1);
4362
const parsed = regionParser(readFileSync(execPath, 'utf8'), fileExtension);
44-
for (const [regionName, regionSnippet] of Object.entries(parsed.regions)) {
45-
// Create files for each found region
46-
if (!regionName) { continue; }
47-
const highlightedRegion = highlightCodeBlock(regionSnippet, fileExtension);
48-
// Convert "my-component-example.ts" into "my-component-example_region-ts.html"
49-
const regionBaseOutputPath = basePath.replace(`.${fileExtension}`,
50-
`_${regionName}-${fileExtension}.html`);
51-
const regionOutputPath = join(outDir, regionBaseOutputPath);
52-
ensureDirSync(dirname(regionOutputPath));
53-
writeFileSync(regionOutputPath, highlightedRegion);
54-
}
63+
_detectAndHighlightRegionBlocks(parsed, basePath, outDir, fileExtension);
5564
// Convert "my-component-example.ts" into "my-component-example-ts.html"
5665
const baseOutputPath = basePath.replace(`.${fileExtension}`, `-${fileExtension}.html`);
5766
const outputPath = join(outDir, baseOutputPath);

tools/package-docs-content/index.bzl

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,12 @@ def _package_docs_content(ctx):
1010
# Directory that will contain all grouped input files. This directory will be
1111
# created relatively to the current target package. For example:
1212
# "bin/src/components-examples/docs-content/docs-content". The reason we need to
13-
# repeat `docs-content` is for
13+
# repeat `docs-content` is that the ng_package rule does not properly handle tree
14+
# artifacts in data. Instead, we create a tree artifact that can be put into nested_packages.
15+
# Nested packages do not preserve the tree artifact name (i.e. the directory name),
16+
# so all contents of the docs-content would be put directly into the @angular/components-examples package.
17+
# To avoid that, we create another folder like docs-content in the tree artifact that
18+
# is preserved as content of the tree artifact.
1419
output_dir = ctx.actions.declare_directory("%s/%s" % (ctx.attr.name, ctx.attr.name))
1520

1621
# Support passing arguments through a parameter file. This is necessary because on Windows
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// These kind of comments are used CSS and other languages that do not support inline comments
2-
module.exports = {
2+
export const blockC = {
33
regionStartMatcher: /^\s*\/\*\s*#docregion\s*(.*)\s*\*\/\s*$/,
44
regionEndMatcher: /^\s*\/\*\s*#enddocregion\s*(.*)\s*\*\/\s*$/,
55
};
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// These kind of comments are used in HTML
2-
module.exports = {
2+
export const html = {
33
regionStartMatcher: /^\s*<!--\s*#docregion\s*(.*?)\s*(?:-->)?\s*$/,
44
regionEndMatcher: /^\s*<!--\s*#enddocregion\s*(.*?)\s*-->\s*$/,
55
};
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// This comment type is used in C like languages such as JS, TS, Dart, etc
2-
module.exports = {
2+
export const inlineC = {
33
regionStartMatcher: /^\s*\/\/\s*#docregion\s*(.*)\s*$/,
44
regionEndMatcher: /^\s*\/\/\s*#enddocregion\s*(.*)\s*$/,
55
};

tools/region-parser/region-parser.ts

Lines changed: 98 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
const blockC = require('./region-matchers/block-c');
2-
const html = require('./region-matchers/html');
3-
const inlineC = require('./region-matchers/inline-c');
1+
import {blockC} from './region-matchers/block-c';
2+
import {html} from './region-matchers/html';
3+
import {inlineC} from './region-matchers/inline-c';
44

5-
export type Region = {lines: string[], open: boolean};
6-
export type RegionMap = {[regionName: string]: Region};
5+
export type Region = { lines: string[], open: boolean };
6+
export type RegionMap = { [regionName: string]: Region };
77

88
export function regionParser(contents: string, fileType: string) {
9-
return regionParserImpl(contents, fileType);
9+
return regionParserImpl(contents, fileType);
1010
}
1111

1212
/**
@@ -15,113 +15,115 @@ export function regionParser(contents: string, fileType: string) {
1515
* @returns {contents: string, regions: {[regionName: string]: string}}
1616
*/
1717
function regionParserImpl(contents: string, fileType: string)
18-
: {contents: string, regions: {[regionName: string]: string}} {
19-
const regionMatchers: {[fileType: string]: {[region: string]: RegExp}} = {
20-
ts: inlineC,
21-
js: inlineC,
22-
es6: inlineC,
23-
html: html,
24-
css: blockC,
25-
json: inlineC,
26-
'json.annotated': inlineC
27-
};
28-
const regionMatcher = regionMatchers[fileType];
29-
const openRegions: string[] = [];
30-
const regionMap: RegionMap = {};
18+
: { contents: string, regions: { [regionName: string]: string } } {
19+
const regionMatchers: { [fileType: string]: { [region: string]: RegExp } } = {
20+
ts: inlineC,
21+
js: inlineC,
22+
es6: inlineC,
23+
html: html,
24+
css: blockC,
25+
json: inlineC,
26+
'json.annotated': inlineC
27+
};
28+
const regionMatcher = regionMatchers[fileType];
29+
const openRegions: string[] = [];
30+
const regionMap: RegionMap = {};
3131

32-
if (regionMatcher) {
33-
const lines = contents.split(/\r?\n/).filter((line) => {
34-
// debugger;
35-
const startRegion = line.match(regionMatcher.regionStartMatcher);
36-
const endRegion = line.match(regionMatcher.regionEndMatcher);
32+
if (regionMatcher) {
33+
const lines = contents.split(/\r?\n/).filter((line) => {
34+
// debugger;
35+
const startRegion = line.match(regionMatcher.regionStartMatcher);
36+
const endRegion = line.match(regionMatcher.regionEndMatcher);
3737

38-
// start region processing
39-
if (startRegion) {
40-
// open up the specified region
41-
const regionNames = getRegionNames(startRegion[1]);
42-
if (regionNames.length === 0) {
43-
regionNames.push('');
44-
}
45-
regionNames.forEach(regionName => {
46-
const region = regionMap[regionName];
47-
if (region) {
48-
if (region.open) {
49-
throw new Error(
50-
`Tried to open a region, named "${regionName}", that is already open`
51-
);
52-
}
53-
region.open = true;
54-
} else {
55-
regionMap[regionName] = {lines: [], open: true};
56-
}
57-
openRegions.push(regionName);
58-
});
38+
// start region processing
39+
if (startRegion) {
40+
// open up the specified region
41+
const regionNames = getRegionNames(startRegion[1]);
42+
if (regionNames.length === 0) {
43+
regionNames.push('');
44+
}
45+
regionNames.forEach(regionName => {
46+
const region = regionMap[regionName];
47+
if (region) {
48+
if (region.open) {
49+
throw new Error(
50+
`Tried to open a region, named "${regionName}", that is already open`
51+
);
52+
}
53+
region.open = true;
54+
} else {
55+
regionMap[regionName] = {lines: [], open: true};
56+
}
57+
openRegions.push(regionName);
58+
});
5959

60-
// end region processing
61-
} else if (endRegion) {
62-
if (openRegions.length === 0) {
63-
throw new Error('Tried to close a region when none are open');
64-
}
65-
// close down the specified region (or most recent if no name is given)
66-
const regionNames = getRegionNames(endRegion[1]);
67-
if (regionNames.length === 0) {
68-
regionNames.push(openRegions[openRegions.length - 1]);
69-
}
60+
// end region processing
61+
} else if (endRegion) {
62+
if (openRegions.length === 0) {
63+
throw new Error('Tried to close a region when none are open');
64+
}
65+
// close down the specified region (or most recent if no name is given)
66+
const regionNames = getRegionNames(endRegion[1]);
67+
if (regionNames.length === 0) {
68+
regionNames.push(openRegions[openRegions.length - 1]);
69+
}
7070

71-
regionNames.forEach(regionName => {
72-
const region = regionMap[regionName];
73-
if (!region || !region.open) {
74-
throw new Error(
75-
`Tried to close a region, named "${regionName}", that is not open`);
76-
}
77-
region.open = false;
78-
removeLast(openRegions, regionName);
79-
});
71+
regionNames.forEach(regionName => {
72+
const region = regionMap[regionName];
73+
if (!region || !region.open) {
74+
throw new Error(
75+
`Tried to close a region, named "${regionName}", that is not open`);
76+
}
77+
region.open = false;
78+
removeLast(openRegions, regionName);
79+
});
8080

81-
} else {
82-
openRegions.forEach(regionName => regionMap[regionName].lines.push(line));
83-
// do not filter out this line from the content
84-
return true;
85-
}
81+
} else {
82+
openRegions.forEach(regionName => regionMap[regionName].lines.push(line));
83+
// do not filter out this line from the content
84+
return true;
85+
}
8686

87-
// this line contained an annotation so let's filter it out
88-
return false;
89-
});
90-
if (!regionMap['']) {
91-
regionMap[''] = {lines, open: false};
92-
}
93-
return {
94-
contents: lines.join('\n'),
95-
regions: mapObject(regionMap, (regionName: string, region: Region) =>
96-
leftAlign(region.lines).join('\n'))
97-
};
98-
} else {
99-
return {contents, regions: {}};
87+
// this line contained an annotation so let's filter it out
88+
return false;
89+
});
90+
if (!regionMap['']) {
91+
regionMap[''] = {lines, open: false};
10092
}
93+
return {
94+
contents: lines.join('\n'),
95+
regions: mapObject(regionMap, (regionName: string, region: Region) =>
96+
leftAlign(region.lines).join('\n'))
97+
};
98+
} else {
99+
return {contents, regions: {}};
100+
}
101101
}
102102

103103
function mapObject(obj: RegionMap, mapper: (regionName: string, region: Region) => string) {
104-
const mappedObj: {[regionName: string]: string} = {};
105-
Object.keys(obj).forEach((key: string) => { mappedObj[key] = mapper(key, obj[key]); });
106-
return mappedObj;
104+
const mappedObj: { [regionName: string]: string } = {};
105+
Object.keys(obj).forEach((key: string) => {
106+
mappedObj[key] = mapper(key, obj[key]);
107+
});
108+
return mappedObj;
107109
}
108110

109111
function getRegionNames(input: string): string[] {
110-
return (input.trim() === '') ? [] : input.split(',').map(name => name.trim());
112+
return (input.trim() === '') ? [] : input.split(',').map(name => name.trim());
111113
}
112114

113115
function removeLast(array: string[], item: string) {
114-
const index = array.lastIndexOf(item);
115-
array.splice(index, 1);
116+
const index = array.lastIndexOf(item);
117+
array.splice(index, 1);
116118
}
117119

118120
function leftAlign(lines: string[]): string[] {
119-
let indent = Number.MAX_VALUE;
120-
lines.forEach(line => {
121-
const lineIndent = line.search(/\S/);
122-
if (lineIndent !== -1) {
123-
indent = Math.min(lineIndent, indent);
124-
}
125-
});
126-
return lines.map(line => line.substr(indent));
121+
let indent = Number.MAX_VALUE;
122+
lines.forEach(line => {
123+
const lineIndent = line.search(/\S/);
124+
if (lineIndent !== -1) {
125+
indent = Math.min(lineIndent, indent);
126+
}
127+
});
128+
return lines.map(line => line.substr(indent));
127129
}

0 commit comments

Comments
 (0)