Skip to content

Commit 7c09469

Browse files
kyliaualexeagle
authored andcommitted
test(npm_install): Add angular deps and update golden files
1 parent a1c1f78 commit 7c09469

22 files changed

Lines changed: 5408 additions & 42 deletions

‎internal/common/BUILD.bazel‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ bzl_library(
2222
name = "bzl",
2323
srcs = glob(["*.bzl"]),
2424
visibility = ["//visibility:public"],
25+
deps = [
26+
"//internal/ng_apf_library:bzl",
27+
],
2528
)
2629

2730
# Exported to be consumed for generating skydoc.

‎internal/common/dev_scripts_aspect.bzl‎

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,24 @@
1616
"""Aspect to collect dev scripts from `deps` attribute.
1717
"""
1818

19+
load("//internal/ng_apf_library:ng_apf_library.bzl", "ScriptsProvider")
20+
21+
DevScriptsProvider = provider(fields = ["dev_scripts"])
22+
1923
def _dev_scripts_aspect_impl(target, ctx):
2024
result = depset()
2125

2226
# If target is a node module it'd provide a list of `scripts`.
23-
if hasattr(target, "scripts"):
24-
result = depset(transitive = [result, target.scripts])
27+
if ScriptsProvider in target:
28+
result = depset(transitive = [result, target[ScriptsProvider].scripts])
2529

2630
# Recursively collect transitive `dev_scripts` from the deps.
2731
if hasattr(ctx.rule.attr, "deps"):
2832
for dep in ctx.rule.attr.deps:
29-
if hasattr(dep, "dev_scripts"):
30-
result = depset(transitive = [result, dep.dev_scripts])
33+
if DevScriptsProvider in dep:
34+
result = depset(transitive = [result, dep[DevScriptsProvider].dev_scripts])
3135

32-
return struct(dev_scripts = result)
36+
return [DevScriptsProvider(dev_scripts = result)]
3337

3438
dev_scripts_aspect = aspect(
3539
_dev_scripts_aspect_impl,

‎internal/ng_apf_library/BUILD.bazel‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,12 @@ filegroup(
2929
srcs = [
3030
"BUILD.bazel",
3131
"ng_apf_library.bzl",
32+
"ng_apf_library.js",
3233
],
3334
visibility = ["//:__pkg__"],
3435
)
3536

36-
exports_files(["ng_apf_library.bzl"])
37+
exports_files([
38+
"ng_apf_library.bzl",
39+
"ng_apf_library.js",
40+
])

‎internal/ng_apf_library/ng_apf_library.bzl‎

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,34 +12,39 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
ScriptsProvider = provider(fields = ["scripts"])
16+
1517
def _ng_apf_library_impl(ctx):
1618
umds = []
1719
factories = []
20+
summaries = []
1821

1922
for file in ctx.files.srcs:
2023
if file.basename.endswith(".umd.js"):
2124
umds.append(file)
2225
elif file.basename.endswith(".ngfactory.js"):
2326
factories.append(file)
27+
elif file.basename.endswith(".ngsummary.js"):
28+
summaries.append(file)
2429

25-
return struct(
26-
files = depset(
30+
return [
31+
DefaultInfo(files = depset(
2732
transitive = [src.files for src in ctx.attr.srcs] + [dep.files for dep in ctx.attr.deps],
33+
)),
34+
ScriptsProvider(
35+
scripts = depset(umds + factories + summaries),
2836
),
29-
scripts = depset(umds + factories),
30-
)
37+
]
3138

32-
"""Provides a replacement for the default filegroup target, with the addition of
33-
`scripts` provider.
34-
"""
3539
ng_apf_library = rule(
3640
implementation = _ng_apf_library_impl,
3741
attrs = {
3842
"srcs": attr.label_list(
3943
doc = "The list of files that comprise the package",
4044
),
4145
"deps": attr.label_list(
42-
doc = "Flatten dependencies of the package",
46+
doc = "Flattened dependencies of the package",
4347
),
4448
},
49+
doc = "Provides a replacement for the default filegroup target, with the addition of `scripts` provider.",
4550
)
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,11 @@ function isNgApfPackage(pkg) {
3535
}
3636

3737
/**
38-
* Prints a `ng_apf_library` rule that replaces the default filegroup target.
38+
* Prints a `ng_apf_library` rule that replaces the generated filegroup target
39+
* that the user will depend on, e.g. `@npm//@angular/core` will be an
40+
* `ng_apf_library`.
3941
*/
40-
function printNgApfLibrary(pkg) {
41-
const pkgDeps = pkg._dependencies.filter(dep => dep !== pkg && !dep._isNested);
42+
function printNgApfLibrary(pkg, pkgDeps) {
4243
return `
4344
load("@build_bazel_rules_nodejs//internal/ng_apf_library:ng_apf_library.bzl", "ng_apf_library")
4445
ng_apf_library(

‎internal/npm_install/BUILD.bazel‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ filegroup(
1313
name = "generate_build_file",
1414
srcs = [
1515
"generate_build_file.js",
16-
"ng_apf_library.js",
16+
"//internal/ng_apf_library:ng_apf_library.js",
1717
],
1818
visibility = ["//internal:__subpackages__"],
1919
)

‎internal/npm_install/generate_build_file.js‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@
4141

4242
const fs = require('fs');
4343
const path = require('path');
44+
// When ng_apf_library.js is executed at installation time the script is copied
45+
// from internal/ng_apf_library to external/npm so import path here is relative
46+
// to current directory.
4447
const apf = require('./ng_apf_library.js');
4548

4649
const BUILD_FILE_HEADER = `# Generated file from yarn_install/npm_install rule.
@@ -671,8 +674,7 @@ function filterFilesForFilegroup(files, exts = []) {
671674
* Given a `pkg`, return the skylark `filegroup` target which is the default
672675
* target for the alias `@npm//${pkg.name}`.
673676
*/
674-
function printPkgTarget(pkg) {
675-
const pkgDeps = pkg._dependencies.filter(dep => dep !== pkg && !dep._isNested);
677+
function printPkgTarget(pkg, pkgDeps) {
676678
return `
677679
filegroup(
678680
name = "${pkg._name}__pkg",
@@ -689,17 +691,18 @@ filegroup(
689691
}
690692

691693
/**
692-
* Given a pkg, return the skylark `filegroup` targets for the package.
694+
* Given a pkg, return the skylark `filegroup` and/or `ng_apf_library` targets for the package.
693695
*/
694696
function printPackage(pkg) {
695697
const sources = filterFilesForFilegroup(pkg._files, INCLUDED_FILES);
696698
const dtsSources = filterFilesForFilegroup(pkg._files, ['.d.ts']);
699+
const pkgDeps = pkg._dependencies.filter(dep => dep !== pkg && !dep._isNested);
697700

698701
let result = `
699702
# Generated targets for npm package "${pkg._dir}"
700703
${printJson(pkg)}
701704
702-
${apf.isNgApfPackage(pkg) ? apf.printNgApfLibrary(pkg) : printPkgTarget(pkg)}
705+
${apf.isNgApfPackage(pkg) ? apf.printNgApfLibrary(pkg, pkgDeps) : printPkgTarget(pkg, pkgDeps)}
703706
704707
filegroup(
705708
name = "${pkg._name}__files",

‎internal/npm_install/npm_install.bzl‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ def _add_scripts(repository_ctx):
109109

110110
repository_ctx.template(
111111
"ng_apf_library.js",
112-
repository_ctx.path(Label("//internal/npm_install:ng_apf_library.js")),
112+
repository_ctx.path(Label("//internal/ng_apf_library:ng_apf_library.js")),
113113
{},
114114
)
115115

‎internal/npm_install/test/BUILD.bazel‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@ filegroup(
1313
jasmine_node_test(
1414
name = "test",
1515
srcs = glob(["*.spec.js"]),
16+
bootstrap = ["build_bazel_rules_nodejs/internal/npm_install/test/copy_ng_apf_library.js"],
1617
data = [
1718
":check.js",
19+
":copy_ng_apf_library.js",
1820
":goldens",
1921
":package",
2022
"//internal/npm_install:generate_build_file",
@@ -25,8 +27,10 @@ jasmine_node_test(
2527

2628
nodejs_binary(
2729
name = "test.accept",
30+
bootstrap = ["build_bazel_rules_nodejs/internal/npm_install/test/copy_ng_apf_library.js"],
2831
data = [
2932
":check.js",
33+
":copy_ng_apf_library.js",
3034
":goldens",
3135
":package",
3236
":update_golden.js",

‎internal/npm_install/test/check.js‎

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,17 @@ function check(file, updateGolden = false) {
2929

3030
// Load the golden file for comparison
3131
const golden = path.posix.join(path.dirname(__filename), 'golden', file + '.golden');
32-
const goldenContents = fs.readFileSync(golden, {encoding: 'utf-8'}).replace(/\r\n/g, '\n');
3332

34-
// Check if actualContents matches golden file
35-
if (actualContents !== goldenContents) {
36-
if (updateGolden) {
37-
// Write to golden file
38-
fs.writeFileSync(golden, actualContents);
39-
console.error(`Replaced ${path.join(process.cwd(), golden)}`);
40-
} else {
33+
if (updateGolden) {
34+
// Write to golden file
35+
// TODO(kyliau): Consider calling mkdirp() here, otherwise write operation
36+
// would fail if directory does not already exist.
37+
fs.writeFileSync(golden, actualContents);
38+
console.error(`Replaced ${path.join(process.cwd(), golden)}`);
39+
} else {
40+
const goldenContents = fs.readFileSync(golden, {encoding: 'utf-8'}).replace(/\r\n/g, '\n');
41+
// Check if actualContents matches golden file
42+
if (actualContents !== goldenContents) {
4143
// Generated does not match golden
4244
const diff = unidiff.diffLines(goldenContents, actualContents);
4345
const prettyDiff = unidiff.formatLines(diff);
@@ -58,21 +60,27 @@ module.exports = {
5860
check,
5961
files: [
6062
'BUILD.bazel',
63+
'@angular/core/BUILD.bazel',
6164
'@gregmagolan/BUILD.bazel',
62-
'@gregmagolan/test-a/BUILD.bazel',
6365
'@gregmagolan/test-a/bin/BUILD.bazel',
64-
'@gregmagolan/test-b/BUILD.bazel',
66+
'@gregmagolan/test-a/BUILD.bazel',
6567
'@gregmagolan/test-b/bin/BUILD.bazel',
68+
'@gregmagolan/test-b/BUILD.bazel',
6669
'ajv/BUILD.bazel',
67-
'jasmine/BUILD.bazel',
6870
'jasmine/bin/BUILD.bazel',
69-
'unidiff/BUILD.bazel',
71+
'jasmine/BUILD.bazel',
72+
'rxjs/BUILD.bazel',
7073
'unidiff/bin/BUILD.bazel',
74+
'unidiff/BUILD.bazel',
75+
'zone.js/BUILD.bazel',
76+
'node_modules/@angular/core/BUILD.bazel',
7177
'node_modules/@gregmagolan/BUILD.bazel',
7278
'node_modules/@gregmagolan/test-a/BUILD.bazel',
7379
'node_modules/@gregmagolan/test-b/BUILD.bazel',
7480
'node_modules/ajv/BUILD.bazel',
7581
'node_modules/jasmine/BUILD.bazel',
82+
'node_modules/rxjs/BUILD.bazel',
7683
'node_modules/unidiff/BUILD.bazel',
84+
'node_modules/zone.js/BUILD.bazel',
7785
],
78-
};
86+
};

0 commit comments

Comments
 (0)