Skip to content

Commit

Permalink
wip: .
Browse files Browse the repository at this point in the history
  • Loading branch information
hansl committed Jan 23, 2019
1 parent 811b596 commit 6d238c0
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 91 deletions.
8 changes: 5 additions & 3 deletions packages/angular_devkit/architect/builders/builders.json
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
{
"$schema": "../architect/src/builders-schema.json",
"version": 2,
"$schema": "../src/builders-schema.json",
"builders": {
"true": {
"version": 2,
"implementation": "./true",
"schema": "./noop-schema.json",
"description": "Always succeed."
},
"false": {
"version": 2,
"implementation": "./false",
"schema": "./noop-schema.json",
"description": "Always fails."
},
"and": {
"version": 2,
"implementation": "./and",
"schema": "./and-schema.json",
"schema": "./operator-schema.json",
"description": "A builder that executes many builders in parallel, and succeed if both succeeds."
}
}
Expand Down
104 changes: 41 additions & 63 deletions packages/angular_devkit/architect/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
import { experimental, isPromise, json, logging } from '@angular-devkit/core';
import { Observable, from, isObservable, of } from 'rxjs';
import { concatMap, first, map, shareReplay, switchMap, tap } from 'rxjs/operators';
import { JobInboundMessageKind, JobOutboundMessageKind } from '../../core/src/experimental/jobs';
import { Schema as RealBuilderInput, Target } from './input-schema';
import { JobInboundMessageKind } from '../../core/src/experimental/jobs';
import { Schema as RealBuilderInput, Target as RealTarget } from './input-schema';
import { Schema as RealBuilderOutput } from './output-schema';
import { Schema as RealBuilderProgress } from './progress-schema';

export type Target = Target;
export type Target = json.JsonObject & RealTarget;

// Type short hands.
export type BuilderJobHandler<
Expand Down Expand Up @@ -55,7 +55,7 @@ export const VersionSymbol = Symbol.for('@angular-devkit/architect:version');


export interface Builder<OptionT extends json.JsonObject> {
handler: experimental.jobs.JobHandler<boolean, BuilderInput, BuilderOutput>;
handler: experimental.jobs.JobHandler<json.JsonObject, BuilderInput, BuilderOutput>;
[BuilderSymbol]: true;
[VersionSymbol]: string;
}
Expand Down Expand Up @@ -154,11 +154,17 @@ async function _scheduleTarget(
reason: 'initial',
options: overrides,
target,
} as BuilderInput);
});
}
});
} else {

job.input.next({
currentDirectory: options.workspaceRoot,
workspaceRoot: options.currentDirectory,
reason: 'schedule',
options: overrides,
target,
});
}

job.outboundBus.subscribe(
Expand All @@ -181,70 +187,42 @@ async function _scheduleTarget(
export function createBuilder<OptT extends json.JsonObject>(
fn: BuilderHandlerFn<OptT>,
): Builder<OptT> {
function handler(
options: boolean,
context: experimental.jobs.JobHandlerContext<boolean, BuilderInput, BuilderOutput>,
) {
const description = context.description;
const cjh = experimental.jobs.createJobHandler;
const handler = cjh<json.JsonObject, BuilderInput, BuilderOutput>((options, context) => {
const scheduler = context.scheduler;

return new Observable<experimental.jobs.JobOutboundMessage<BuilderOutput>>(observer => {
const logger = new logging.Logger('job');
logger.subscribe(entry => {
observer.next({
kind: JobOutboundMessageKind.Log,
description,
entry,
});
});

context.inboundBus.subscribe(x => {
if (x.kind === experimental.jobs.JobInboundMessageKind.Input) {
const i = x.value;
const context: BuilderContext = {
workspaceRoot: i.workspaceRoot,
currentDirectory: i.currentDirectory,
target: i.target,
logger,
scheduleTarget(target: Target, overrides: json.JsonObject) {
return _scheduleTarget(target, overrides, scheduler, {
logger,
workspaceRoot: i.workspaceRoot,
currentDirectory: i.currentDirectory,
});
},
};

let result = fn(i.options as OptT, context);

if (isPromise(result)) {
result = from(result);
} else if (!isObservable(result)) {
result = of(result);
}

result.subscribe(
value => observer.next({
kind: experimental.jobs.JobOutboundMessageKind.Output,
description,
value,
}),
err => observer.error(err),
() => observer.complete(),
);
const logger = context.logger;

return new Observable<BuilderOutput>(observer => {
context.input.subscribe(i => {
const context: BuilderContext = {
workspaceRoot: i.workspaceRoot,
currentDirectory: i.currentDirectory,
target: i.target as Target,
logger: logger,
scheduleTarget(target: Target, overrides: json.JsonObject) {
return _scheduleTarget(target, overrides, scheduler, {
logger: logger.createChild(''),
workspaceRoot: i.workspaceRoot,
currentDirectory: i.currentDirectory,
});
},
};

let result = fn(i.options as OptT, context);

if (isPromise(result)) {
result = from(result);
} else if (!isObservable(result)) {
result = of(result);
}
});

// The job is ready to listen.
observer.next({
description,
kind: experimental.jobs.JobOutboundMessageKind.Start,
return result.subscribe(observer);
});
});
}
});

return {
handler: Object.assign(handler, { jobDescription: {} }),
handler,
[BuilderSymbol]: true,
[VersionSymbol]: require('../package.json').version,
};
Expand Down
90 changes: 68 additions & 22 deletions packages/angular_devkit/architect/src/progress-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,77 @@
"id": "BuilderProgressSchema",
"title": "Progress schema for builders.",
"type": "object",
"properties": {
"current": {
"type": "number",
"minimum": 0
},
"total": {
"type": "number",
"minimum": 0
"oneOf": [
{
"type": "object",
"properties": {
"state": {
"type": "string",
"enum": [
"stopped"
]
}
},
"required": [
"state"
]
},
"state": {
"type": "string",
"enum": [
"stopped",
"waiting",
"running",
"error"
{
"type": "object",
"properties": {
"state": {
"type": "string",
"enum": [
"waiting"
]
},
"status": {
"type": "string"
}
},
"required": [
"state"
]
},
"status": {
"type": "string"
{
"type": "object",
"properties": {
"state": {
"type": "string",
"enum": [
"running"
]
},
"current": {
"type": "number",
"minimum": 0
},
"total": {
"type": "number",
"minimum": 0
},
"status": {
"type": "string"
}
},
"required": [
"state"
]
},
"error": true
},
"required": [
"current",
"state"
{
"type": "object",
"properties": {
"state": {
"type": "string",
"enum": [
"error"
]
},
"error": true
},
"required": [
"state"
]
}
]
}
2 changes: 1 addition & 1 deletion packages/angular_devkit/architect_cli/bin/architect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ async function _executeTarget(
run.output.subscribe(
o => console.log('output: ', JSON.stringify(o)),
err => console.error('err', err),
() => console.log('complete\n\n'),
() => console.log('complete'),
);

// Wait for full completion of the builder.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,6 @@ export function createJobHandler<A extends JsonValue, I extends JsonValue, O ext
});

// Execute the function with the additional context.
subject.next({ kind: JobOutboundMessageKind.Start, description });

const channels = new Map<string, Subject<JsonValue>>();

const newContext: SimpleJobHandlerContext<A, I, O> = {
Expand Down Expand Up @@ -145,6 +143,7 @@ export function createJobHandler<A extends JsonValue, I extends JsonValue, O ext
},
};

subject.next({ kind: JobOutboundMessageKind.Start, description });
const result = fn(argument, newContext);
// If the result is a promise, simply wait for it to complete before reporting the result.
if (isPromise(result)) {
Expand Down

0 comments on commit 6d238c0

Please sign in to comment.