refactor(bazel): Schematics should fetch angular from distro (#28851)
PR Close #28851
This commit is contained in:

committed by
Igor Minar

parent
96b597cfd0
commit
e5e4e63e71
@ -20,14 +20,6 @@ http_archive(
|
||||
url = "https://github.com/bazelbuild/rules_nodejs/releases/download/%s/rules_nodejs-%s.tar.gz" % (RULES_NODEJS_VERSION, RULES_NODEJS_VERSION),
|
||||
)
|
||||
|
||||
# The @angular repo contains rule for building Angular applications
|
||||
ANGULAR_VERSION = "<%= ANGULAR_VERSION %>"
|
||||
http_archive(
|
||||
name = "angular",
|
||||
url = "https://github.com/angular/angular/archive/%s.zip" % ANGULAR_VERSION,
|
||||
strip_prefix = "angular-%s" % ANGULAR_VERSION,
|
||||
)
|
||||
|
||||
<% if (sass) { %>
|
||||
# Rules for compiling sass
|
||||
RULES_SASS_VERSION = "<%= RULES_SASS_VERSION %>"
|
||||
@ -46,9 +38,7 @@ check_bazel_version("0.18.0")
|
||||
node_repositories()
|
||||
yarn_install(
|
||||
name = "npm",
|
||||
# Need a reference to @angular here so that Bazel sets up the
|
||||
# external repository before calling yarn_install
|
||||
data = ["@angular//:LICENSE"],
|
||||
data = ["//:angular-metadata.tsconfig.json"],
|
||||
package_json = "//:package.json",
|
||||
yarn_lock = "//:yarn.lock",
|
||||
)
|
||||
@ -72,5 +62,5 @@ ts_setup_workspace()
|
||||
load("@io_bazel_rules_sass//sass:sass_repositories.bzl", "sass_repositories")
|
||||
sass_repositories()
|
||||
<% } %>
|
||||
load("@angular//:index.bzl", "ng_setup_workspace")
|
||||
load("@npm_angular_bazel//:index.bzl", "ng_setup_workspace")
|
||||
ng_setup_workspace()
|
||||
|
@ -1,5 +1,5 @@
|
||||
load("@build_bazel_rules_typescript//:defs.bzl", "ts_library")
|
||||
load("@angular//:index.bzl", "protractor_web_test_suite")
|
||||
load("@npm_angular_bazel//:index.bzl", "protractor_web_test_suite")
|
||||
|
||||
ts_library(
|
||||
name = "e2e_lib",
|
||||
@ -22,7 +22,7 @@ ts_library(
|
||||
protractor_web_test_suite(
|
||||
name = "prodserver_test",
|
||||
data = [
|
||||
"@angular//" + "packages/bazel/src/protractor/utils",
|
||||
"@npm//@angular/bazel",
|
||||
"@npm//protractor",
|
||||
],
|
||||
on_prepare = ":protractor.on-prepare.js",
|
||||
@ -33,7 +33,7 @@ protractor_web_test_suite(
|
||||
protractor_web_test_suite(
|
||||
name = "devserver_test",
|
||||
data = [
|
||||
"@angular//" + "packages/bazel/src/protractor/utils",
|
||||
"@npm//@angular/bazel",
|
||||
"@npm//protractor",
|
||||
],
|
||||
on_prepare = ":protractor.on-prepare.js",
|
||||
|
@ -1,6 +1,6 @@
|
||||
package(default_visibility = ["//visibility:public"])
|
||||
|
||||
load("@angular//:index.bzl", "ng_module")
|
||||
load("@npm_angular_bazel//:index.bzl", "ng_module")
|
||||
load("@build_bazel_rules_karma//:defs.bzl", "ts_web_test_suite")
|
||||
load("@build_bazel_rules_nodejs//:defs.bzl", "rollup_bundle", "history_server")
|
||||
load("@build_bazel_rules_nodejs//internal/web_package:web_package.bzl", "web_package")
|
||||
@ -37,9 +37,9 @@ ng_module(
|
||||
"**/*.html",
|
||||
])<% if (sass) { %> + [":styles"]<% } %>,
|
||||
deps = [
|
||||
"@angular//packages/core",
|
||||
"@angular//packages/platform-browser",<% if (routing) { %>
|
||||
"@angular//packages/router",<% } %>
|
||||
"@npm//@angular/core",
|
||||
"@npm//@angular/platform-browser",<% if (routing) { %>
|
||||
"@npm//@angular/router",<% } %>
|
||||
"@npm//@types",
|
||||
"@npm//rxjs",
|
||||
],
|
||||
@ -79,6 +79,10 @@ ts_devserver(
|
||||
entry_module = "<%= utils.underscore(name) %>/src/main.dev",
|
||||
serving_path = "/bundle.min.js",
|
||||
scripts = [
|
||||
"@npm//node_modules/@angular/common:bundles/common.umd.js",
|
||||
"@npm//node_modules/@angular/common:bundles/common-http.umd.js",
|
||||
"@npm//node_modules/@angular/core:bundles/core.umd.js",
|
||||
"@npm//node_modules/@angular/platform-browser:bundles/platform-browser.umd.js",
|
||||
"@npm//node_modules/tslib:tslib.js",
|
||||
":rxjs_umd_modules",
|
||||
],
|
||||
@ -95,7 +99,7 @@ ts_library(
|
||||
srcs = glob(["**/*.spec.ts"]),
|
||||
deps = [
|
||||
":src",
|
||||
"@angular//packages/core/testing",
|
||||
"@npm//@angular/core",
|
||||
"@npm//@types",
|
||||
],
|
||||
)
|
||||
@ -107,15 +111,26 @@ ts_library(
|
||||
"initialize_testbed.ts",
|
||||
],
|
||||
deps = [
|
||||
"@angular//packages/core/testing",
|
||||
"@angular//packages/platform-browser-dynamic/testing",
|
||||
"@npm//@angular/core",
|
||||
"@npm//@angular/platform-browser-dynamic",
|
||||
"@npm//@types",
|
||||
],
|
||||
)
|
||||
|
||||
ts_web_test_suite(
|
||||
name = "test",
|
||||
srcs = ["@npm//node_modules/tslib:tslib.js"],
|
||||
srcs = [
|
||||
"@npm//node_modules/@angular/common:bundles/common.umd.js",
|
||||
"@npm//node_modules/@angular/compiler:bundles/compiler.umd.js",
|
||||
"@npm//node_modules/@angular/compiler:bundles/compiler-testing.umd.js",
|
||||
"@npm//node_modules/@angular/core:bundles/core.umd.js",
|
||||
"@npm//node_modules/@angular/core:bundles/core-testing.umd.js",
|
||||
"@npm//node_modules/@angular/platform-browser:bundles/platform-browser.umd.js",
|
||||
"@npm//node_modules/@angular/platform-browser:bundles/platform-browser-testing.umd.js",
|
||||
"@npm//node_modules/@angular/platform-browser-dynamic:bundles/platform-browser-dynamic.umd.js",
|
||||
"@npm//node_modules/@angular/platform-browser-dynamic:bundles/platform-browser-dynamic-testing.umd.js",
|
||||
"@npm//node_modules/tslib:tslib.js",
|
||||
],
|
||||
runtime_deps = [
|
||||
":initialize_testbed",
|
||||
],
|
||||
|
@ -9,32 +9,12 @@
|
||||
*/
|
||||
|
||||
import {strings} from '@angular-devkit/core';
|
||||
import {Rule, SchematicContext, SchematicsException, Tree, apply, applyTemplates, mergeWith, move, url} from '@angular-devkit/schematics';
|
||||
import {Rule, SchematicContext, Tree, apply, applyTemplates, mergeWith, url} from '@angular-devkit/schematics';
|
||||
import {getWorkspace} from '@schematics/angular/utility/config';
|
||||
import {latestVersions} from '@schematics/angular/utility/latest-versions';
|
||||
import {validateProjectName} from '@schematics/angular/utility/validation';
|
||||
|
||||
import {Schema as BazelWorkspaceOptions} from './schema';
|
||||
|
||||
|
||||
/**
|
||||
* Look for package.json file for package with `packageName` in node_modules and
|
||||
* extract its version.
|
||||
*/
|
||||
function findVersion(packageName: string, host: Tree): string|null {
|
||||
const candidate = `node_modules/${packageName}/package.json`;
|
||||
if (host.exists(candidate)) {
|
||||
try {
|
||||
const packageJson = JSON.parse(host.read(candidate).toString());
|
||||
if (packageJson.name === packageName && packageJson.version) {
|
||||
return packageJson.version;
|
||||
}
|
||||
} catch {
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Clean the version string and return version in the form "1.2.3". Return
|
||||
* null if version string is invalid. This is similar to semver.clean() but
|
||||
@ -72,20 +52,6 @@ export default function(options: BazelWorkspaceOptions): Rule {
|
||||
}
|
||||
validateProjectName(name);
|
||||
|
||||
// If the project already has some deps installed, Bazel should use existing
|
||||
// versions.
|
||||
const existingVersions = {
|
||||
Angular: findVersion('@angular/core', host),
|
||||
RxJs: findVersion('rxjs', host),
|
||||
};
|
||||
|
||||
Object.keys(existingVersions).forEach((name: 'Angular' | 'RxJs') => {
|
||||
const version = existingVersions[name] as string;
|
||||
if (version) {
|
||||
context.logger.info(`Bazel will reuse existing version for ${name}: ${version}`);
|
||||
}
|
||||
});
|
||||
|
||||
if (!host.exists('yarn.lock')) {
|
||||
host.create('yarn.lock', '');
|
||||
}
|
||||
@ -93,8 +59,6 @@ export default function(options: BazelWorkspaceOptions): Rule {
|
||||
const workspaceVersions = {
|
||||
'RULES_NODEJS_VERSION': '0.18.6',
|
||||
'RULES_NODEJS_SHA256': '1416d03823fed624b49a0abbd9979f7c63bbedfd37890ddecedd2fe25cccebc6',
|
||||
'ANGULAR_VERSION': existingVersions.Angular || clean(latestVersions.Angular),
|
||||
// TODO(kyliau): Consider moving this to latest-versions.ts
|
||||
'RULES_SASS_VERSION': '1.17.0',
|
||||
};
|
||||
|
||||
|
@ -61,7 +61,7 @@ describe('Bazel-workspace Schematic', () => {
|
||||
host = schematicRunner.runSchematic('bazel-workspace', options, host);
|
||||
expect(host.files).toContain('/src/BUILD.bazel');
|
||||
const content = host.readContent('/src/BUILD.bazel');
|
||||
expect(content).toContain('@angular//packages/router');
|
||||
expect(content).toContain('@npm//@angular/router');
|
||||
});
|
||||
|
||||
describe('WORKSPACE', () => {
|
||||
|
@ -44,7 +44,6 @@ function addDevDependenciesToPackageJson(options: Schema) {
|
||||
const devDependencies: {[k: string]: string} = {
|
||||
'@angular/bazel': angularCoreVersion,
|
||||
'@angular/upgrade': angularCoreVersion,
|
||||
// TODO(kyliau): Consider moving this to latest-versions.ts
|
||||
'@bazel/bazel': '^0.22.1',
|
||||
'@bazel/ibazel': '^0.9.0',
|
||||
'@bazel/karma': '^0.23.2',
|
||||
|
Reference in New Issue
Block a user