Skip to content

Commit

Permalink
test(npm_install): Add angular deps and update golden files
Browse files Browse the repository at this point in the history
  • Loading branch information
kyliau authored and alexeagle committed Apr 3, 2019
1 parent a1c1f78 commit 7c09469
Show file tree
Hide file tree
Showing 22 changed files with 5,408 additions and 42 deletions.
3 changes: 3 additions & 0 deletions internal/common/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ bzl_library(
name = "bzl",
srcs = glob(["*.bzl"]),
visibility = ["//visibility:public"],
deps = [
"//internal/ng_apf_library:bzl",
],
)

# Exported to be consumed for generating skydoc.
Expand Down
14 changes: 9 additions & 5 deletions internal/common/dev_scripts_aspect.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,24 @@
"""Aspect to collect dev scripts from `deps` attribute.
"""

load("//internal/ng_apf_library:ng_apf_library.bzl", "ScriptsProvider")

DevScriptsProvider = provider(fields = ["dev_scripts"])

def _dev_scripts_aspect_impl(target, ctx):
result = depset()

# If target is a node module it'd provide a list of `scripts`.
if hasattr(target, "scripts"):
result = depset(transitive = [result, target.scripts])
if ScriptsProvider in target:
result = depset(transitive = [result, target[ScriptsProvider].scripts])

# Recursively collect transitive `dev_scripts` from the deps.
if hasattr(ctx.rule.attr, "deps"):
for dep in ctx.rule.attr.deps:
if hasattr(dep, "dev_scripts"):
result = depset(transitive = [result, dep.dev_scripts])
if DevScriptsProvider in dep:
result = depset(transitive = [result, dep[DevScriptsProvider].dev_scripts])

return struct(dev_scripts = result)
return [DevScriptsProvider(dev_scripts = result)]

dev_scripts_aspect = aspect(
_dev_scripts_aspect_impl,
Expand Down
6 changes: 5 additions & 1 deletion internal/ng_apf_library/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,12 @@ filegroup(
srcs = [
"BUILD.bazel",
"ng_apf_library.bzl",
"ng_apf_library.js",
],
visibility = ["//:__pkg__"],
)

exports_files(["ng_apf_library.bzl"])
exports_files([
"ng_apf_library.bzl",
"ng_apf_library.js",
])
21 changes: 13 additions & 8 deletions internal/ng_apf_library/ng_apf_library.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -12,34 +12,39 @@
# See the License for the specific language governing permissions and
# limitations under the License.

ScriptsProvider = provider(fields = ["scripts"])

def _ng_apf_library_impl(ctx):
umds = []
factories = []
summaries = []

for file in ctx.files.srcs:
if file.basename.endswith(".umd.js"):
umds.append(file)
elif file.basename.endswith(".ngfactory.js"):
factories.append(file)
elif file.basename.endswith(".ngsummary.js"):
summaries.append(file)

return struct(
files = depset(
return [
DefaultInfo(files = depset(
transitive = [src.files for src in ctx.attr.srcs] + [dep.files for dep in ctx.attr.deps],
)),
ScriptsProvider(
scripts = depset(umds + factories + summaries),
),
scripts = depset(umds + factories),
)
]

"""Provides a replacement for the default filegroup target, with the addition of
`scripts` provider.
"""
ng_apf_library = rule(
implementation = _ng_apf_library_impl,
attrs = {
"srcs": attr.label_list(
doc = "The list of files that comprise the package",
),
"deps": attr.label_list(
doc = "Flatten dependencies of the package",
doc = "Flattened dependencies of the package",
),
},
doc = "Provides a replacement for the default filegroup target, with the addition of `scripts` provider.",
)
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,11 @@ function isNgApfPackage(pkg) {
}

/**
* Prints a `ng_apf_library` rule that replaces the default filegroup target.
* Prints a `ng_apf_library` rule that replaces the generated filegroup target
* that the user will depend on, e.g. `@npm//@angular/core` will be an
* `ng_apf_library`.
*/
function printNgApfLibrary(pkg) {
const pkgDeps = pkg._dependencies.filter(dep => dep !== pkg && !dep._isNested);
function printNgApfLibrary(pkg, pkgDeps) {
return `
load("@build_bazel_rules_nodejs//internal/ng_apf_library:ng_apf_library.bzl", "ng_apf_library")
ng_apf_library(
Expand Down
2 changes: 1 addition & 1 deletion internal/npm_install/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ filegroup(
name = "generate_build_file",
srcs = [
"generate_build_file.js",
"ng_apf_library.js",
"//internal/ng_apf_library:ng_apf_library.js",
],
visibility = ["//internal:__subpackages__"],
)
Expand Down
11 changes: 7 additions & 4 deletions internal/npm_install/generate_build_file.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@

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

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

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

let result = `
# Generated targets for npm package "${pkg._dir}"
${printJson(pkg)}
${apf.isNgApfPackage(pkg) ? apf.printNgApfLibrary(pkg) : printPkgTarget(pkg)}
${apf.isNgApfPackage(pkg) ? apf.printNgApfLibrary(pkg, pkgDeps) : printPkgTarget(pkg, pkgDeps)}
filegroup(
name = "${pkg._name}__files",
Expand Down
2 changes: 1 addition & 1 deletion internal/npm_install/npm_install.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ def _add_scripts(repository_ctx):

repository_ctx.template(
"ng_apf_library.js",
repository_ctx.path(Label("//internal/npm_install:ng_apf_library.js")),
repository_ctx.path(Label("//internal/ng_apf_library:ng_apf_library.js")),
{},
)

Expand Down
4 changes: 4 additions & 0 deletions internal/npm_install/test/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ filegroup(
jasmine_node_test(
name = "test",
srcs = glob(["*.spec.js"]),
bootstrap = ["build_bazel_rules_nodejs/internal/npm_install/test/copy_ng_apf_library.js"],
data = [
":check.js",
":copy_ng_apf_library.js",
":goldens",
":package",
"//internal/npm_install:generate_build_file",
Expand All @@ -25,8 +27,10 @@ jasmine_node_test(

nodejs_binary(
name = "test.accept",
bootstrap = ["build_bazel_rules_nodejs/internal/npm_install/test/copy_ng_apf_library.js"],
data = [
":check.js",
":copy_ng_apf_library.js",
":goldens",
":package",
":update_golden.js",
Expand Down
34 changes: 21 additions & 13 deletions internal/npm_install/test/check.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,17 @@ function check(file, updateGolden = false) {

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

// Check if actualContents matches golden file
if (actualContents !== goldenContents) {
if (updateGolden) {
// Write to golden file
fs.writeFileSync(golden, actualContents);
console.error(`Replaced ${path.join(process.cwd(), golden)}`);
} else {
if (updateGolden) {
// Write to golden file
// TODO(kyliau): Consider calling mkdirp() here, otherwise write operation
// would fail if directory does not already exist.
fs.writeFileSync(golden, actualContents);
console.error(`Replaced ${path.join(process.cwd(), golden)}`);
} else {
const goldenContents = fs.readFileSync(golden, {encoding: 'utf-8'}).replace(/\r\n/g, '\n');
// Check if actualContents matches golden file
if (actualContents !== goldenContents) {
// Generated does not match golden
const diff = unidiff.diffLines(goldenContents, actualContents);
const prettyDiff = unidiff.formatLines(diff);
Expand All @@ -58,21 +60,27 @@ module.exports = {
check,
files: [
'BUILD.bazel',
'@angular/core/BUILD.bazel',
'@gregmagolan/BUILD.bazel',
'@gregmagolan/test-a/BUILD.bazel',
'@gregmagolan/test-a/bin/BUILD.bazel',
'@gregmagolan/test-b/BUILD.bazel',
'@gregmagolan/test-a/BUILD.bazel',
'@gregmagolan/test-b/bin/BUILD.bazel',
'@gregmagolan/test-b/BUILD.bazel',
'ajv/BUILD.bazel',
'jasmine/BUILD.bazel',
'jasmine/bin/BUILD.bazel',
'unidiff/BUILD.bazel',
'jasmine/BUILD.bazel',
'rxjs/BUILD.bazel',
'unidiff/bin/BUILD.bazel',
'unidiff/BUILD.bazel',
'zone.js/BUILD.bazel',
'node_modules/@angular/core/BUILD.bazel',
'node_modules/@gregmagolan/BUILD.bazel',
'node_modules/@gregmagolan/test-a/BUILD.bazel',
'node_modules/@gregmagolan/test-b/BUILD.bazel',
'node_modules/ajv/BUILD.bazel',
'node_modules/jasmine/BUILD.bazel',
'node_modules/rxjs/BUILD.bazel',
'node_modules/unidiff/BUILD.bazel',
'node_modules/zone.js/BUILD.bazel',
],
};
};
33 changes: 33 additions & 0 deletions internal/npm_install/test/copy_ng_apf_library.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* @fileoverview
* This script is run as part of the nodejs binary bootstrapping process.
* It is required because generate_build_file.js imports ng_apf_library.js
* from the same directory it is in. This script moves the dependency to the
* right location to simulate Bazel copy the template to npm/external.
*/

const path = require('path');
const fs = require('fs');

let source, dist;
const {RUNFILES_MANIFEST_FILE, TEST_TARGET} = process.env;

// Read the manifest only when running under `bazel test`.
if (TEST_TARGET && RUNFILES_MANIFEST_FILE) {
// This code path is executed on Windows where runfiles are not used.
const manifest = fs.readFileSync(RUNFILES_MANIFEST_FILE, 'utf-8');
const entry = manifest.split('\n').find(line => {
return line.startsWith("build_bazel_rules_nodejs/internal/ng_apf_library/ng_apf_library.js");
});
if (!entry) {
throw new Error(`Could not find entry for 'ng_apf_library.js' in MANIFEST`);
}
source = entry.split(' ')[1];
dest = path.resolve(source, '../../npm_install/ng_apf_library.js');
} else {
// For generating the golden files under `bazel run`.
source = 'internal/ng_apf_library/ng_apf_library.js';
dest = 'internal/npm_install/ng_apf_library.js';
}

fs.copyFileSync(source, dest);
14 changes: 14 additions & 0 deletions internal/npm_install/test/golden/@angular/core/BUILD.bazel.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

package(default_visibility = ["//visibility:public"])
alias(
name = "core",
actual = "//node_modules/@angular/core:core__pkg"
)
alias(
name = "core__files",
actual = "//node_modules/@angular/core:core__files"
)
alias(
name = "core__typings",
actual = "//node_modules/@angular/core:core__typings"
)
4 changes: 4 additions & 0 deletions internal/npm_install/test/golden/BUILD.bazel.golden
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,12 @@ filegroup(
"//node_modules/minimatch:minimatch__files",
"//node_modules/once:once__files",
"//node_modules/path-is-absolute:path-is-absolute__files",
"//node_modules/rxjs:rxjs__files",
"//node_modules/tslib:tslib__files",
"//node_modules/unidiff:unidiff__files",
"//node_modules/wrappy:wrappy__files",
"//node_modules/zone.js:zone.js__files",
"//node_modules/@angular/core:core__files",
"//node_modules/@gregmagolan/test-a:test-a__files",
"//node_modules/@gregmagolan/test-b:test-b__files",
],
Expand Down
Loading

0 comments on commit 7c09469

Please sign in to comment.