Hans Larsen 68220f13e4 feat(@angular-devkit/architect): New Architect API first draft
The new API has been described in this design doc:
https://docs.google.com/document/d/1SpN_2XEooI9_CPjqspAcNEBjVY874OWPZqOenjuF0qo/view

This first drafts add support for the API (given some deep imports). It is
still in draft mode but is committed to make it available to people to
start testing and moving their own builders.

This API rebuilds (not backward compatible) the Architect API package. To
use it people will need to import "@angular-devkit/architect/src/index2"
to start using it. A reference builder will be added in the next commit.

There are 2 pieces missing from this commit that will be added in the
same PR; 1) the architect-host and CLI to test, and 2) a reference
builder moved from the old API to the new one. These will be part of
the same PR.

Finally, there are missing tests in this package, but everything tested
manually and automatically works so far. Test coverage will be added
before the package is considered finished.

Due to a desire to keep architect, our tests and the scope of this PR
limited and keep the two APIs separated, every clashing files will
have a "2" suffix added to it. Once all builders have been moved and
we are sure everything works, all those files will be moved to their
final destination and the old API will be removed, in one PR.
2019-02-19 13:51:29 -08:00

109 lines
4.0 KiB
TypeScript

/**
* @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 { BuilderInfo, Target, targetStringFromTarget } from '../src/index2';
import { ArchitectHost, Builder } from '../src/internal';
export class TestingArchitectHost implements ArchitectHost {
private _builderImportMap = new Map<string, Builder>();
private _builderMap = new Map<string, BuilderInfo>();
private _targetMap = new Map<string, { builderName: string, options: json.JsonObject }>();
/**
* Can provide a backend host, in case of integration tests.
* @param workspaceRoot The workspace root to use.
* @param currentDirectory The current directory to use.
* @param _backendHost A host to defer calls that aren't resolved here.
*/
constructor(
public workspaceRoot = '',
public currentDirectory = workspaceRoot,
private _backendHost: ArchitectHost | null = null,
) {}
addBuilder(
builderName: string,
builder: Builder,
description = 'Testing only builder.',
optionSchema: json.schema.JsonSchema = { type: 'object' },
) {
this._builderImportMap.set(builderName, builder);
this._builderMap.set(builderName, { builderName, description, optionSchema });
}
async addBuilderFromPackage(packageName: string) {
const packageJson = await import(packageName + '/package.json');
if (!('builders' in packageJson)) {
throw new Error('Invalid package.json, builders key not found.');
}
const builderJsonPath = packageName + '/' + packageJson['builders'];
const builderJson = await import(builderJsonPath);
const builders = builderJson['builders'];
if (!builders) {
throw new Error('Invalid builders.json, builders key not found.');
}
for (const builderName of Object.keys(builders)) {
const b = builders[builderName];
// TODO: remove this check as v1 is not supported anymore.
if (!b.implementation) { continue; }
const handler = await import(builderJsonPath + '/../' + b.implementation);
const optionsSchema = await import(builderJsonPath + '/../' + b.schema);
this.addBuilder(builderName, handler, b.description, optionsSchema);
}
}
addTarget(target: Target, builderName: string, options: json.JsonObject = {}) {
this._targetMap.set(targetStringFromTarget(target), { builderName, options });
}
async getBuilderNameForTarget(target: Target): Promise<string | null> {
const name = targetStringFromTarget(target);
const maybeTarget = this._targetMap.get(name);
if (!maybeTarget) {
return this._backendHost && this._backendHost.getBuilderNameForTarget(target);
}
return maybeTarget.builderName;
}
/**
* Resolve a builder. This needs to return a string which will be used in a dynamic `import()`
* clause. This should throw if no builder can be found. The dynamic import will throw if
* it is unsupported.
* @param builderName The name of the builder to be used.
* @returns All the info needed for the builder itself.
*/
async resolveBuilder(builderName: string): Promise<BuilderInfo | null> {
return this._builderMap.get(builderName)
|| (this._backendHost && this._backendHost.resolveBuilder(builderName));
}
async getCurrentDirectory(): Promise<string> {
return this.currentDirectory;
}
async getWorkspaceRoot(): Promise<string> {
return this.workspaceRoot;
}
async getOptionsForTarget(target: Target): Promise<json.JsonObject | null> {
const name = targetStringFromTarget(target);
const maybeTarget = this._targetMap.get(name);
if (!maybeTarget) {
return this._backendHost && this._backendHost.getOptionsForTarget(target);
}
return maybeTarget.options;
}
async loadBuilder(info: BuilderInfo): Promise<Builder | null> {
return this._builderImportMap.get(info.builderName)
|| (this._backendHost && this._backendHost.loadBuilder(info));
}
}