Skip to content

Commit

Permalink
add bazel schematics
Browse files Browse the repository at this point in the history
  • Loading branch information
vsavkin committed Aug 16, 2017
1 parent 287ccb6 commit 2371cf1
Show file tree
Hide file tree
Showing 47 changed files with 1,053 additions and 547 deletions.
14 changes: 7 additions & 7 deletions e2e/addngrx.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ describe('addNgRxToModule', () => {

it('should add root configuration', () => {
newApp('new proj --skipInstall');
runSchematic('@nrwl/nx:addNgRxToModule --module=src/app/app.module.ts --root', {cwd: 'proj'});
runSchematic('@nrwl/nx:addNgRxToModule --module=src/app/app.module.ts --root', {projectName: 'proj'});

checkFilesExists(
`proj/src/app/+state/app.actions.ts`,
Expand All @@ -26,27 +26,27 @@ describe('addNgRxToModule', () => {

addNgRx('proj');

runCLI('build', {cwd: 'proj'});
runCLI('test --single-run', {cwd: 'proj'});
runCLI('build', {projectName: 'proj'});
runCLI('test --single-run', {projectName: 'proj'});

}, 50000);

it('should add empty root configuration', () => {
newApp('new proj2 --skipInstall');
runSchematic('@nrwl/nx:addNgRxToModule --module=src/app/app.module.ts --emptyRoot', {cwd: 'proj2'});
runSchematic('@nrwl/nx:addNgRxToModule --module=src/app/app.module.ts --emptyRoot', {projectName: 'proj2'});

const contents = readFile('proj2/src/app/app.module.ts');
expect(contents).toContain('StoreModule.forRoot');
expect(contents).not.toContain('EffectsModule.forRoot');

addNgRx('proj2');

runCLI('build', {cwd: 'proj2'});
runCLI('build', {projectName: 'proj2'});
}, 50000);

it('should add feature configuration', () => {
newApp('new proj3 --skipInstall');
runSchematic('@nrwl/nx:addNgRxToModule --module=src/app/app.module.ts', {cwd: 'proj3'});
runSchematic('@nrwl/nx:addNgRxToModule --module=src/app/app.module.ts', {projectName: 'proj3'});

checkFilesExists(
`proj3/src/app/+state/app.actions.ts`,
Expand All @@ -65,7 +65,7 @@ describe('addNgRxToModule', () => {

it('should generate files without importing them', () => {
newApp('new proj4 --skipInstall');
runSchematic('@nrwl/nx:addNgRxToModule --module=src/app/app.module.ts --skipImport', {cwd: 'proj4'});
runSchematic('@nrwl/nx:addNgRxToModule --module=src/app/app.module.ts --skipImport', {projectName: 'proj4'});

checkFilesExists(
`proj4/src/app/+state/app.actions.ts`,
Expand Down
28 changes: 28 additions & 0 deletions e2e/app.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import {
addNgRx, checkFilesExists, cleanup, newApp, readFile, runCLI, runCommand, runSchematic,
updateFile
} from './utils';

describe('application', () => {
beforeEach(cleanup);

it('creates a new application in a workspace', () => {
runSchematic('@nrwl/nx:application --name=proj');
runSchematic('@nrwl/nx:app --name=myapp', {projectName: 'proj'});

checkFilesExists(
`proj/tsconfig.json`,
`proj/WORKSPACE`,
`proj/BUILD.bazel`,
`proj/apps/myapp/BUILD.bazel`,
`proj/apps/myapp/src/index.html`,
`proj/apps/myapp/src/app/app.module.ts`,
`proj/apps/myapp/src/app/app.component.ts`
);

expect(readFile('proj/apps/myapp/src/app/app.module.ts')).toContain('bootstrap: [AppComponent]');

const cliConfig = JSON.parse(readFile('proj/.angular-cli.json'));
expect(cliConfig.apps.length).toEqual(1);
});
});
9 changes: 0 additions & 9 deletions e2e/package.json__tmpl__

This file was deleted.

21 changes: 11 additions & 10 deletions e2e/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,18 @@ import {readFileSync, statSync, writeFileSync} from 'fs';
export function newApp(command: string): string {
return execSync(`../node_modules/.bin/ng ${command}`, {cwd: `./tmp`}).toString();
}
export function runCLI(command: string, {cwd}: {cwd: string}): string {
cwd = cwd === undefined ? '' : cwd;
return execSync(`../../node_modules/.bin/ng ${command}`, {cwd: `./tmp/${cwd}`}).toString();
export function runCLI(command: string, {projectName: projectName}: {projectName: string}): string {
projectName = projectName === undefined ? '' : projectName;
return execSync(`../../node_modules/.bin/ng ${command}`, {cwd: `./tmp/${projectName}`}).toString();
}
export function runSchematic(command: string, {cwd}: {cwd: string}): string {
cwd = cwd === undefined ? '' : cwd;
return execSync(`../../node_modules/.bin/schematics ${command}`, {cwd: `./tmp/${cwd}`}).toString();
export function runSchematic(command: string, {projectName}: {projectName?: string} = {}): string {
const up = projectName ? '../' : '';
projectName = projectName === undefined ? '' : projectName;
return execSync(`../${up}node_modules/.bin/schematics ${command}`, {cwd: `./tmp/${projectName}`}).toString();
}
export function runCommand(command: string, {cwd}: {cwd: string}): string {
cwd = cwd === undefined ? '' : cwd;
return execSync(command, {cwd: `./tmp/${cwd}`}).toString();
export function runCommand(command: string, {projectName}: {projectName: string}): string {
projectName = projectName === undefined ? '' : projectName;
return execSync(command, {cwd: `./tmp/${projectName}`}).toString();
}

export function updateFile(f: string, content: string): void {
Expand Down Expand Up @@ -70,5 +71,5 @@ export function addNgRx(path: string): string {
p['dependencies']['@ngrx/effects'] = '4.0.2';
p['dependencies']['jasmine-marbles'] = '0.1.0';
updateFile(`${path}/package.json`, JSON.stringify(p, null, 2));
return runCommand('npm install', {cwd: path});
return runCommand('npm install', {projectName: path});
}
18 changes: 18 additions & 0 deletions e2e/workspace.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import {
addNgRx, checkFilesExists, cleanup, newApp, readFile, runCLI, runCommand, runSchematic,
updateFile
} from './utils';

describe('workspace', () => {
beforeEach(cleanup);

it('creates a new workspace for developing angular applications', () => {
runSchematic('@nrwl/nx:application --name=proj --version=0.1');

checkFilesExists(
`proj/tsconfig.json`,
`proj/WORKSPACE`,
`proj/BUILD.bazel`
);
});
});
8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@
"scripts": {
"build": "./scripts/build.sh",
"e2e": "yarn build && ./scripts/e2e.sh",
"release": "yarn build && ./scripts/release.sh"
"package": "./scripts/package.sh",
"release": "./scripts/release.sh"
},
"dependencies" :{
"@angular-devkit/schematics": "0.0.16",
"@angular-devkit/schematics": "0.0.17",
"jasmine-marbles": "0.1.0"
},
"peerDependencies" :{
Expand All @@ -20,7 +21,8 @@
"typescript": "2.4.2",
"@types/node": "8.0.7",
"@types/jasmine": "2.5.53",
"jest": "20.0.4"
"jest": "20.0.4",
"@schematics/angular": "git+https://github.com/Brocco/zzz-ng-schematics.git"
},
"author": "Victor Savkin",
"license": "MIT",
Expand Down
2 changes: 1 addition & 1 deletion scripts/e2e.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ cp -r build/src node_modules/@nrwl/nx
cp package.json node_modules/@nrwl/nx/package.json

rm -rf tmp
jest --maxWorkers=1 ./build/e2e
jest --maxWorkers=1 ./build/e2e/app.test.js
6 changes: 6 additions & 0 deletions scripts/package.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/usr/bin/env bash

./scripts/build.sh
cp package.json build/src/package.json
cp README.md build/src/README.md
cp LICENSE build/src/LICENSE
4 changes: 1 addition & 3 deletions scripts/release.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#!/usr/bin/env bash

cp package.json build/src/package.json
cp README.md build/src/README.md
cp LICENSE build/src/LICENSE
./scripts/package.sh
cd build/src
git init
git add .
Expand Down
8 changes: 8 additions & 0 deletions src/bazel/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
load("@build_bazel_rules_typescript//:defs.bzl", "nodejs_binary")
exports_files(["webpack.config.js", "test.js"])

nodejs_binary(
name = "webpack",
entry_point = "webpack/bin/webpack",
visibility = ["//visibility:public"],
)
1 change: 1 addition & 0 deletions src/bazel/WORKSPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
tree artifact
62 changes: 62 additions & 0 deletions src/bazel/webpack.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
def _collect_es5_sources_impl(target, ctx):
result = set()
if hasattr(ctx.rule.attr, "srcs"):
for dep in ctx.rule.attr.srcs:
if hasattr(dep, "es5_sources"):
result += dep.es5_sources
if hasattr(target, "typescript"):
result += target.typescript.es5_sources
return struct(es5_sources = result)

_collect_es5_sources = aspect(
_collect_es5_sources_impl,
attr_aspects = ["deps", "srcs"],
)

def _webpack_bundle_impl(ctx):
inputs = set()
for s in ctx.attr.srcs:
if hasattr(s, "es5_sources"):
inputs += s.es5_sources

config = ctx.attr.config.files.to_list()[0]

if ctx.attr.mode == 'prod':
main = ctx.new_file('bundles/main.bundle.prod.js')
polyfills = ctx.new_file('bundles/polyfills.bundle.prod.js')
vendor = ctx.new_file('bundles/vendor.bundle.prod.js')
styles = ctx.new_file('bundles/styles.bundle.prod.js')
else:
main = ctx.new_file('bundles/main.bundle.js')
polyfills = ctx.new_file('bundles/polyfills.bundle.js')
vendor = ctx.new_file('bundles/vendor.bundle.js')
styles = ctx.new_file('bundles/styles.bundle.js')

inputs += [config]
args = []

if ctx.attr.mode == 'prod':
args += ['-p']

args += ['--config', config.path]
args += ['--env.bin', ctx.configuration.bin_dir.path]
args += ['--env.package', ctx.label.package]
args += ['--env.mode', ctx.attr.mode]

ctx.action(
progress_message = "Webpack bundling %s" % ctx.label,
inputs = inputs.to_list(),
outputs = [main, polyfills, vendor, styles],
executable = ctx.executable._webpack,
arguments = args,
)
return DefaultInfo(files=depset([main, polyfills, vendor, styles]))

webpack_bundle = rule(implementation = _webpack_bundle_impl,
attrs = {
"srcs": attr.label_list(allow_files=True, aspects=[_collect_es5_sources]),
"config": attr.label(allow_single_file=True),
"mode": attr.string(default="dev"),
"_webpack": attr.label(default=Label("@build_bazel_rules_nrwl//:webpack"), executable=True, cfg="host")
}
)
Loading

0 comments on commit 2371cf1

Please sign in to comment.