Skip to content

Commit

Permalink
wip: architect API
Browse files Browse the repository at this point in the history
  • Loading branch information
hansl committed Feb 8, 2019
1 parent 063525f commit 05ae15d
Show file tree
Hide file tree
Showing 35 changed files with 2,315 additions and 252 deletions.
10 changes: 0 additions & 10 deletions packages/_/builders/builders.json

This file was deleted.

12 changes: 0 additions & 12 deletions packages/_/builders/package.json

This file was deleted.

20 changes: 0 additions & 20 deletions packages/_/builders/src/true.ts

This file was deleted.

43 changes: 43 additions & 0 deletions packages/angular_devkit/architect/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,50 @@ licenses(["notice"]) # MIT

load("@build_bazel_rules_typescript//:defs.bzl", "ts_library")
load("@build_bazel_rules_nodejs//:defs.bzl", "npm_package")
load("//tools:ts_json_schema.bzl", "ts_json_schema")

package(default_visibility = ["//visibility:public"])


ts_json_schema(
name = "builder_input_schema",
src = "src/input-schema.json",
)
ts_json_schema(
name = "builder_output_schema",
src = "src/output-schema.json",
)
ts_json_schema(
name = "builder_builders_schema",
src = "src/builders-schema.json",
)


ts_library(
name = "node",
srcs = glob(
include = ["node/**/*.ts"],
exclude = [
"**/*_spec.ts",
"**/*_spec_large.ts",
],
),
module_name = "@angular-devkit/architect/node",
module_root = "node/index.d.ts",
# strict_checks = False,
deps = [
"//packages/angular_devkit/core",
"//packages/angular_devkit/core:node",
"@rxjs",
"@rxjs//operators",
"@npm//@types/node",
":architect",
":builder_builders_schema",
":builder_input_schema",
":builder_output_schema",
],
)

ts_library(
name = "architect",
srcs = glob(
Expand All @@ -29,6 +70,8 @@ ts_library(
"@rxjs",
"@rxjs//operators",
"@npm//@types/node",
":builder_input_schema",
":builder_output_schema",
],
)

Expand Down
59 changes: 59 additions & 0 deletions packages/angular_devkit/architect/builders/all-of.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import { json } from '@angular-devkit/core';
import { EMPTY, from, of } from 'rxjs';
import { map, mergeMap, tap } from 'rxjs/operators';
import { BuilderOutput, BuilderRun, createBuilder } from '../src/index2';
import { Schema as OperatorSchema } from './operator-schema';

export default createBuilder<json.JsonObject & OperatorSchema>((options, context) => {
const allRuns: Promise<[number, BuilderRun]>[] = [];

context.progress(0,
(options.targets ? options.targets.length : 0)
+ (options.builders ? options.builders.length : 0),
);

if (options.targets) {
allRuns.push(...options.targets.map(({ target: targetStr, overrides }, i) => {
const [project, target, configuration] = targetStr.split(/:/g, 3);

return context.scheduleTarget({ project, target, configuration }, overrides || {})
.then(run => [i, run] as [number, BuilderRun]);
}));
}

if (options.builders) {
allRuns.push(...options.builders.map(({ builder, options }, i) => {
return context.scheduleBuilder(builder, options || {})
.then(run => [i, run] as [number, BuilderRun]);
}));
}

const allResults: (BuilderOutput | null)[] = allRuns.map(() => null);
let n = 0;

return from(allRuns).pipe(
tap(() => context.progress(0, allRuns.length)),
mergeMap(runPromise => from(runPromise)),
mergeMap(([i, run]: [number, BuilderRun]) => run.output.pipe(map(output => [i, output]))),
mergeMap<[number, BuilderOutput], BuilderOutput>(([i, output]) => {
allResults[i] = output;
context.progress(++n, allRuns.length);

if (allResults.some(x => x === null)) {
// Some builders aren't done running yet.
return EMPTY;
} else {
return of({
success: allResults.every(x => x ? x.success : false),
});
}
}),
);
});
25 changes: 25 additions & 0 deletions packages/angular_devkit/architect/builders/builders.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"$schema": "../src/builders-schema.json",
"builders": {
"true": {
"implementation": "./true",
"schema": "./noop-schema.json",
"description": "Always succeed."
},
"false": {
"implementation": "./false",
"schema": "./noop-schema.json",
"description": "Always fails."
},
"allOf": {
"implementation": "./all-of",
"schema": "./operator-schema.json",
"description": "A builder that executes many builders in parallel, and succeed if both succeeds."
},
"concat": {
"implementation": "./concat",
"schema": "./operator-schema.json",
"description": "A builder that executes many builders one after the other, and stops when one fail. It will succeed if all builders succeeds (and return the last output)"
}
}
}
57 changes: 57 additions & 0 deletions packages/angular_devkit/architect/builders/concat.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import { json } from '@angular-devkit/core';
import { from, of } from 'rxjs';
import { concatMap, first, last, map, switchMap } from 'rxjs/operators';
import { BuilderOutput, BuilderRun, createBuilder } from '../src/index2';
import { Schema as OperatorSchema } from './operator-schema';

export default createBuilder<json.JsonObject & OperatorSchema>((options, context) => {
const allRuns: (() => Promise<BuilderRun>)[] = [];

context.progress(0,
(options.targets ? options.targets.length : 0)
+ (options.builders ? options.builders.length : 0),
);


if (options.targets) {
allRuns.push(...options.targets.map(({ target: targetStr, overrides }) => {
const [project, target, configuration] = targetStr.split(/:/g, 3);

return () => context.scheduleTarget({ project, target, configuration }, overrides || {});
}));
}

if (options.builders) {
allRuns.push(...options.builders.map(({ builder, options }) => {
return () => context.scheduleBuilder(builder, options || {});
}));
}

let stop: BuilderOutput | null = null;
let i = 0;
context.progress(i++, allRuns.length);

return from(allRuns).pipe(
concatMap(fn => stop ? of(null) : from(fn()).pipe(
switchMap(run => run === null ? of(null) : run.output.pipe(first())),
)),
map(output => {
context.progress(i++, allRuns.length);
if (output === null || stop !== null) {
return stop || { success: false };
} else if (output.success === false) {
return stop = output;
} else {
return output;
}
}),
last(),
);
});
14 changes: 14 additions & 0 deletions packages/angular_devkit/architect/builders/false.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import { of } from 'rxjs';
import { createBuilder } from '../src/index2';

export default createBuilder(() => of({
success: false,
error: 'False builder always errors.',
}));
File renamed without changes.
45 changes: 45 additions & 0 deletions packages/angular_devkit/architect/builders/operator-schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
{
"$schema": "http://json-schema.org/schema",
"description": "All input types of builders that perform operations on one or multiple sub-builders.",
"type": "object",
"properties": {
"builders": {
"type": "array",
"items": {
"type": "object",
"properties": {
"builder": {
"type": "string",
"pattern": ".*:.*"
},
"options": {
"type": "object"
}
},
"required": [
"builder"
]
},
"minItems": 1
},
"targets": {
"type": "array",
"items": {
"type": "object",
"properties": {
"target": {
"type": "string",
"pattern": ".*:.*"
},
"overrides": {
"type": "object"
}
},
"required": [
"target"
]
},
"minItems": 1
}
}
}
11 changes: 11 additions & 0 deletions packages/angular_devkit/architect/builders/true.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import { of } from 'rxjs';
import { createBuilder } from '../src/index2';

export default createBuilder(() => of({ success: true }));
8 changes: 8 additions & 0 deletions packages/angular_devkit/architect/node/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
export * from './node-modules-architect-host';
Loading

0 comments on commit 05ae15d

Please sign in to comment.