Skip to content
Permalink
Browse files

feat(bazel): Bazel workspace schematics

This commit creates a schematics for Bazel workspace.
  • Loading branch information...
mrmeku authored and kyliau committed Sep 14, 2018
1 parent 297c54e commit 525ba62384b0429fa0783c2878dbfc5a85e4c7bf
@@ -1,7 +1,9 @@
.DS_STORE

/dist/
bazel-*
bazel-bin
bazel-genfiles
bazel-testlogs
e2e_test.*
node_modules
bower_components
@@ -32,9 +32,10 @@
},
"// 1": "dependencies are used locally and by bazel",
"dependencies": {
"@angular-devkit/schematics": "^0.5.5",
"@angular-devkit/core": "^7.0.4",
"@angular-devkit/schematics": "^7.0.4",
"@bazel/typescript": "0.20.3",
"@schematics/angular": "^0.5.4",
"@schematics/angular": "^7.0.4",
"@types/chokidar": "1.7.3",
"@types/convert-source-map": "^1.5.1",
"@types/diff": "^3.2.2",
@@ -142,4 +143,4 @@
"vrsource-tslint-rules": "5.1.1",
"webpack": "1.12.9"
}
}
}
@@ -16,5 +16,6 @@ npm_package(
"//packages/bazel/src/ng_package:lib",
"//packages/bazel/src/ngc-wrapped:ngc_lib",
"//packages/bazel/src/protractor/utils",
"//packages/bazel/src/schematics",
],
)
@@ -12,7 +12,10 @@
},
"typings": "./src/ngc-wrapped/index.d.ts",
"dependencies": {
"@angular-devkit/core": "^7.0.4",
"@angular-devkit/schematics": "^7.0.4",
"@bazel/typescript": "^0.20.3",
"@schematics/angular": "^7.0.4",
"@types/node": "6.0.84",
"shelljs": "0.8.2",
"tsickle": "0.32.1"
@@ -25,6 +28,7 @@
"type": "git",
"url": "https://github.com/angular/angular.git"
},
"schematics": "./src/schematics/collection.json",
"ng-update": {
"packageGroup": "NG_UPDATE_PACKAGE_GROUP"
},
@@ -0,0 +1,29 @@
package(default_visibility = ["//visibility:public"])

filegroup(
name = "package_assets",
srcs = glob(["*"]),
visibility = ["//packages/bazel:__subpackages__"],
)

load("//tools:defaults.bzl", "ts_library", "jasmine_node_test")

ts_library(
name = "schematics",
srcs = [],
data = [
"collection.json",
],
deps = [
"//packages/bazel/src/schematics/bazel-workspace",
],
)

jasmine_node_test(
name = "test",
bootstrap = ["angular/tools/testing/init_node_spec.js"],
deps = [
"//packages/bazel/src/schematics/bazel-workspace:test",
"//tools/testing:node",
],
)
@@ -0,0 +1,12 @@
## Generate .d.ts file from JSON schema

The script to generate .d.ts file is located in the angular-cli repo. Make sure
the CLI repository is checked out on your local machine.

Then, in the CLI repository, run the following command

```shell
bazel run //tools:quicktype_runner -- \
~/Documents/GitHub/angular/packages/bazel/src/schematics/ng-new/schema.json \
~/Documents/GitHub/angular/packages/bazel/src/schematics/ng-new/schema.d.ts
```
@@ -0,0 +1,34 @@
package(default_visibility = ["//visibility:public"])

load("//tools:defaults.bzl", "ts_library")

ts_library(
name = "bazel-workspace",
srcs = [
"index.ts",
"schema.d.ts",
],
data = glob(["files/**/*"]) + [
"schema.json",
],
deps = [
"@ngdeps//@angular-devkit/schematics",
"@ngdeps//@angular-devkit/core",
"@ngdeps//@schematics/angular",
],
)

ts_library(
name = "test",
testonly = True,
srcs = [
"index_spec.ts",
],
data = [
"//packages/bazel/src/schematics:package_assets",
],
deps = [
":bazel-workspace",
"@ngdeps//@angular-devkit/schematics",
],
)
@@ -0,0 +1,12 @@
package(default_visibility = ["//visibility:public"])

alias(
name = "install",
actual = "@nodejs//:yarn",
)

# This export allows targets in other packages to reference files that live
# in this package.
exports_files([
"tsconfig.json",
])
@@ -0,0 +1,88 @@
# The WORKSPACE file tells Bazel that this directory is a "workspace", which is like a project root.
# The content of this file specifies all the external dependencies Bazel needs to perform a build.

####################################
# ESModule imports (and TypeScript imports) can be absolute starting with the workspace name.
# The name of the workspace should match the npm package where we publish, so that these
# imports also make sense when referencing the published package.
workspace(name = "<%= name %>")

# The Bazel buildtools repo contains tools like the BUILD file formatter, buildifier
# This commit matches the version of buildifier in angular/ngcontainer
# If you change this, also check if it matches the version in the angular/ngcontainer
# version in /.circleci/config.yml
BAZEL_BUILDTOOLS_VERSION = "<%= BAZEL_BUILDTOOLS_VERSION %>"
http_archive(
name = "com_github_bazelbuild_buildtools",
url = "https://github.com/bazelbuild/buildtools/archive/%s.zip" % BAZEL_BUILDTOOLS_VERSION,
strip_prefix = "buildtools-%s" % BAZEL_BUILDTOOLS_VERSION,
)

# The @angular repo contains rule for building Angular applications
# Provides "build_bazel_rules_typescript"
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,
)

# RxJS
RXJS_VERSION = "<%= RXJS_VERSION %>"
http_archive(
name = "rxjs",
url = "https://registry.yarnpkg.com/rxjs/-/rxjs-%s.tgz" % RXJS_VERSION,
strip_prefix = "package/src",
)

# Rules for compiling sass
RULES_SASS_VERSION = "<%= RULES_SASS_VERSION %>"
http_archive(
name = "io_bazel_rules_sass",
url = "https://github.com/bazelbuild/rules_sass/archive/%s.zip" % RULES_SASS_VERSION,
strip_prefix = "rules_sass-%s" % RULES_SASS_VERSION,
)

####################################
# Load and install our dependencies downloaded above.

load("@angular//packages/bazel:package.bzl", "rules_angular_dependencies")
rules_angular_dependencies()

load("@build_bazel_rules_typescript//:package.bzl", "rules_typescript_dependencies")
rules_typescript_dependencies()
# build_bazel_rules_nodejs is loaded transitively through rules_typescript_dependencies.

load("@build_bazel_rules_nodejs//:defs.bzl", "check_bazel_version", "node_repositories", "yarn_install")
# 0.18.0 is needed for .bazelignore
check_bazel_version("0.18.0")
node_repositories(
preserve_symlinks = True,
node_version = "<%= NODE_VERSION %>",
yarn_version = "<%= YARN_VERSION %>",
)
yarn_install(
name = "npm",
package_json = "//:package.json",
yarn_lock = "//:yarn.lock",
)

load("@io_bazel_rules_go//go:def.bzl", "go_rules_dependencies", "go_register_toolchains")
go_rules_dependencies()
go_register_toolchains()

load("@io_bazel_rules_webtesting//web:repositories.bzl", "browser_repositories", "web_test_repositories")
web_test_repositories()
browser_repositories(
chromium = True,
firefox = True,
)

load("@build_bazel_rules_typescript//:defs.bzl", "ts_setup_workspace", "check_rules_typescript_version")
ts_setup_workspace()

load("@io_bazel_rules_sass//sass:sass_repositories.bzl", "sass_repositories")
sass_repositories()

load("@angular//:index.bzl", "ng_setup_workspace")
ng_setup_workspace()
@@ -0,0 +1,17 @@
# Make TypeScript and Angular compilation fast, by keeping a few copies of the
# compiler running as daemons, and cache SourceFile AST's to reduce parse time.
build --strategy=TypeScriptCompile=worker
build --strategy=AngularTemplateCompile=worker

# Don't create bazel-* symlinks in the WORKSPACE directory.
# These require .gitignore and may scare users.
# Also, it's a workaround for https://github.com/bazelbuild/rules_typescript/issues/12
# which affects the common case of having `tsconfig.json` in the WORKSPACE directory.
#
# Instead, you should run `bazel info bazel-bin` to find out where the outputs went.
build --symlink_prefix=dist/

test --test_output=errors

# Use the Angular 6 compiler
build --define=compile=legacy
@@ -0,0 +1,90 @@
package(default_visibility = ["//visibility:public"])

load("@angular//:index.bzl", "ng_module")
load("@build_bazel_rules_typescript//:defs.bzl", "ts_library", "ts_web_test_suite")
load("@build_bazel_rules_nodejs//:defs.bzl", "rollup_bundle", "history_server")
load("@build_bazel_rules_typescript//:defs.bzl", "ts_devserver")

ng_module(
name = "src",
srcs = glob(["**/*.ts"], exclude = ["**/*.spec.ts", "test.ts"]),
assets = glob([
"**/*.css",
"**/*.html",
]),
deps = [
"@angular//packages/core",
"@angular//packages/platform-browser",
"@npm//@types",
],
)

rollup_bundle(
name = "bundle",
entry_point = "src/main",
deps = ["//src"],
)

# Needed because the prodserver only loads static files that appear under this
# package.
genrule(
name = "zonejs",
srcs = ["@npm//node_modules/zone.js:dist/zone.min.js"],
outs = ["zone.min.js"],
cmd = "cp $< $@",
)

history_server(
name = "prodserver",
data = [
"index.html",
":bundle",
":zonejs",
],
)

ts_devserver(
name = "devserver",
port = 4200,
additional_root_paths = [
"npm/node_modules/zone.js/dist",
"npm/node_modules/tslib",
],
entry_module = "<%= name %>/src/main",
serving_path = "/bundle.min.js",
static_files = [
"@npm//node_modules/zone.js:dist/zone.min.js",
"@npm//node_modules/tslib:tslib.js",
"index.html",
],
deps = [":src"],
)

ts_library(
name = "test_lib",
testonly = 1,
srcs = glob(["**/*.spec.ts"]),
deps = [
":src",
"@angular//packages/core/testing",
"@angular//packages/platform-browser-dynamic/testing",
"@npm//@types",
],
)

ts_web_test_suite(
name = "test",
srcs = ["@npm//node_modules/tslib:tslib.js"],
# do not sort
bootstrap = [
"@npm//node_modules/zone.js:dist/zone-testing-bundle.js",
"@npm//node_modules/reflect-metadata:Reflect.js",
],
browsers = [
"@io_bazel_rules_webtesting//browsers:chromium-local",
],
deps = [
":test_lib",
"@npm//karma-jasmine",
],
)
@@ -0,0 +1,41 @@
import { Tree, mergeWith, apply, url, move, SchematicContext, SchematicsException, Rule, applyTemplates } from "@angular-devkit/schematics";
import { getWorkspace } from "@schematics/angular/utility/config";
import { validateProjectName } from "@schematics/angular/utility/validation";
import { Schema as BazelWorkspaceOptions } from './schema';
import { strings } from '@angular-devkit/core';

export default function (options: BazelWorkspaceOptions): Rule {
return (host: Tree, context: SchematicContext) => {
if (!options.name) {
throw new SchematicsException(`Invalid options, "name" is required.`);
}
validateProjectName(options.name);
let newProjectRoot = '';
try {
const workspace = getWorkspace(host);
newProjectRoot = workspace.newProjectRoot || '';
} catch { }
const appDir = `${newProjectRoot}/${options.name}`;

const workspaceVersions = {
'ANGULAR_VERSION': '7.0.2',
'BAZEL_BUILDTOOLS_VERSION': '0.17.1',
'RULES_SASS_VERSION': '1.14.1',
'RXJS_VERSION': '6.3.3',
'NODE_VERSION': '10.9.0',
'YARN_VERSION': '1.9.2',
};

return mergeWith(
apply(url('./files'), [
applyTemplates({
utils: strings,
...options,
'dot': '.',
...workspaceVersions,
}),
move(appDir),
])
);
};
}
Oops, something went wrong.

0 comments on commit 525ba62

Please sign in to comment.
You can’t perform that action at this time.