|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google Inc. All Rights Reserved. |
| 4 | + * |
| 5 | + * Use of this source code is governed by an MIT-style license that can be |
| 6 | + * found in the LICENSE file at https://angular.io/license |
| 7 | + */ |
| 8 | + |
| 9 | +import { |
| 10 | + BaseException, |
| 11 | + JsonObject, |
| 12 | + JsonParseMode, |
| 13 | + Path, |
| 14 | + dirname, |
| 15 | + getSystemPath, |
| 16 | + join, |
| 17 | + logging, |
| 18 | + normalize, |
| 19 | + parseJson, |
| 20 | + resolve, |
| 21 | + schema, |
| 22 | + virtualFs, |
| 23 | +} from '@angular-devkit/core'; |
| 24 | +import { resolve as nodeResolve } from '@angular-devkit/core/node'; |
| 25 | +import { Observable } from 'rxjs/Observable'; |
| 26 | +import { of } from 'rxjs/observable/of'; |
| 27 | +import { _throw } from 'rxjs/observable/throw'; |
| 28 | +import { concatMap } from 'rxjs/operators'; |
| 29 | +import { |
| 30 | + BuildEvent, |
| 31 | + Builder, |
| 32 | + BuilderConstructor, |
| 33 | + BuilderContext, |
| 34 | + BuilderDescription, |
| 35 | + BuilderMap, |
| 36 | +} from './builder'; |
| 37 | +import { Workspace } from './workspace'; |
| 38 | + |
| 39 | + |
| 40 | +export class ProjectNotFoundException extends BaseException { |
| 41 | + constructor(name?: string) { |
| 42 | + const nameOrDefault = name ? `Project '${name}'` : `Default project`; |
| 43 | + super(`${nameOrDefault} could not be found in workspace.`); |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +export class TargetNotFoundException extends BaseException { |
| 48 | + constructor(name?: string) { |
| 49 | + const nameOrDefault = name ? `Target '${name}'` : `Default target`; |
| 50 | + super(`${nameOrDefault} could not be found in workspace.`); |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +export class ConfigurationNotFoundException extends BaseException { |
| 55 | + constructor(name: string) { |
| 56 | + super(`Configuration '${name}' could not be found in project.`); |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +export class SchemaValidationException extends BaseException { |
| 61 | + constructor(errors: string[]) { |
| 62 | + super(`Schema validation failed with the following errors:\n ${errors.join('\n ')}`); |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +// TODO: break this exception apart into more granular ones. |
| 67 | +export class BuilderCannotBeResolvedException extends BaseException { |
| 68 | + constructor(builder: string) { |
| 69 | + super(`Builder '${builder}' cannot be resolved.`); |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +export class WorkspaceNotYetLoadedException extends BaseException { |
| 74 | + constructor() { super(`Workspace needs to be loaded before Architect is used.`); } |
| 75 | +} |
| 76 | + |
| 77 | +export interface Target<OptionsT = {}> { |
| 78 | + root: Path; |
| 79 | + projectType: string; |
| 80 | + builder: string; |
| 81 | + options: OptionsT; |
| 82 | +} |
| 83 | + |
| 84 | +export interface TargetOptions<OptionsT = {}> { |
| 85 | + project?: string; |
| 86 | + target?: string; |
| 87 | + configuration?: string; |
| 88 | + overrides?: Partial<OptionsT>; |
| 89 | +} |
| 90 | + |
| 91 | +export class Architect { |
| 92 | + private readonly _workspaceSchema = join(normalize(__dirname), 'workspace-schema.json'); |
| 93 | + private readonly _buildersSchema = join(normalize(__dirname), 'builders-schema.json'); |
| 94 | + private _workspace: Workspace; |
| 95 | + |
| 96 | + constructor(private _root: Path, private _host: virtualFs.Host<{}>) { } |
| 97 | + |
| 98 | + loadWorkspaceFromHost(workspacePath: Path) { |
| 99 | + return this._host.read(join(this._root, workspacePath)).pipe( |
| 100 | + concatMap((buffer) => { |
| 101 | + const json = JSON.parse(virtualFs.fileBufferToString(buffer)); |
| 102 | + |
| 103 | + return this.loadWorkspaceFromJson(json); |
| 104 | + }), |
| 105 | + ); |
| 106 | + } |
| 107 | + |
| 108 | + loadWorkspaceFromJson(json: Workspace) { |
| 109 | + return this._validateAgainstSchema(json, this._workspaceSchema).pipe( |
| 110 | + concatMap((validatedWorkspace: Workspace) => { |
| 111 | + this._workspace = validatedWorkspace; |
| 112 | + |
| 113 | + return of(this); |
| 114 | + }), |
| 115 | + ); |
| 116 | + } |
| 117 | + |
| 118 | + getTarget<OptionsT>(options: TargetOptions = {}): Target<OptionsT> { |
| 119 | + let { project, target: targetName } = options; |
| 120 | + const { configuration, overrides } = options; |
| 121 | + |
| 122 | + if (!this._workspace) { |
| 123 | + throw new WorkspaceNotYetLoadedException(); |
| 124 | + } |
| 125 | + |
| 126 | + project = project || this._workspace.defaultProject as string; |
| 127 | + const workspaceProject = this._workspace.projects[project]; |
| 128 | + |
| 129 | + if (!workspaceProject) { |
| 130 | + throw new ProjectNotFoundException(project); |
| 131 | + } |
| 132 | + |
| 133 | + targetName = targetName || workspaceProject.defaultTarget as string; |
| 134 | + const workspaceTarget = workspaceProject.targets[targetName]; |
| 135 | + |
| 136 | + if (!workspaceTarget) { |
| 137 | + throw new TargetNotFoundException(targetName); |
| 138 | + } |
| 139 | + |
| 140 | + const workspaceTargetOptions = workspaceTarget.options; |
| 141 | + let workspaceConfiguration; |
| 142 | + |
| 143 | + if (configuration) { |
| 144 | + workspaceConfiguration = workspaceTarget.configurations |
| 145 | + && workspaceTarget.configurations[configuration]; |
| 146 | + |
| 147 | + if (!workspaceConfiguration) { |
| 148 | + throw new ConfigurationNotFoundException(configuration); |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + // Resolve root for the target. |
| 153 | + // TODO: add Path format to JSON schemas |
| 154 | + const target: Target<OptionsT> = { |
| 155 | + root: resolve(this._root, normalize(workspaceProject.root)), |
| 156 | + projectType: workspaceProject.projectType, |
| 157 | + builder: workspaceTarget.builder, |
| 158 | + options: { |
| 159 | + ...workspaceTargetOptions, |
| 160 | + ...workspaceConfiguration, |
| 161 | + ...overrides as {}, |
| 162 | + } as OptionsT, |
| 163 | + }; |
| 164 | + |
| 165 | + return target; |
| 166 | + } |
| 167 | + |
| 168 | + // Will run the target using the target. |
| 169 | + run<OptionsT>( |
| 170 | + target: Target<OptionsT>, |
| 171 | + partialContext: Partial<BuilderContext> = {}, |
| 172 | + ): Observable<BuildEvent> { |
| 173 | + const context: BuilderContext = { |
| 174 | + logger: new logging.NullLogger(), |
| 175 | + architect: this, |
| 176 | + host: this._host, |
| 177 | + ...partialContext, |
| 178 | + }; |
| 179 | + |
| 180 | + let builderDescription: BuilderDescription; |
| 181 | + |
| 182 | + return this.getBuilderDescription(target).pipe( |
| 183 | + concatMap(description => { |
| 184 | + builderDescription = description; |
| 185 | + |
| 186 | + return this.validateBuilderOptions(target, builderDescription); |
| 187 | + }), |
| 188 | + concatMap(() => of(this.getBuilder(builderDescription, context))), |
| 189 | + concatMap(builder => builder.run(target)), |
| 190 | + ); |
| 191 | + } |
| 192 | + |
| 193 | + getBuilderDescription<OptionsT>(target: Target<OptionsT>): Observable<BuilderDescription> { |
| 194 | + return new Observable((obs) => { |
| 195 | + // TODO: this probably needs to be more like NodeModulesEngineHost. |
| 196 | + const basedir = getSystemPath(this._root); |
| 197 | + const [pkg, builderName] = target.builder.split(':'); |
| 198 | + const pkgJsonPath = nodeResolve(pkg, { basedir, resolvePackageJson: true }); |
| 199 | + let buildersJsonPath: Path; |
| 200 | + |
| 201 | + // Read the `builders` entry of package.json. |
| 202 | + return this._host.read(normalize(pkgJsonPath)).pipe( |
| 203 | + concatMap(buffer => |
| 204 | + of(parseJson(virtualFs.fileBufferToString(buffer), JsonParseMode.Loose))), |
| 205 | + concatMap((pkgJson: JsonObject) => { |
| 206 | + const pkgJsonBuildersentry = pkgJson['builders'] as string; |
| 207 | + if (!pkgJsonBuildersentry) { |
| 208 | + throw new BuilderCannotBeResolvedException(target.builder); |
| 209 | + } |
| 210 | + |
| 211 | + buildersJsonPath = join(dirname(normalize(pkgJsonPath)), pkgJsonBuildersentry); |
| 212 | + |
| 213 | + return this._host.read(buildersJsonPath); |
| 214 | + }), |
| 215 | + concatMap((buffer) => of(JSON.parse(virtualFs.fileBufferToString(buffer)))), |
| 216 | + // Validate builders json. |
| 217 | + concatMap((builderMap) => |
| 218 | + this._validateAgainstSchema<BuilderMap>(builderMap, this._buildersSchema)), |
| 219 | + |
| 220 | + |
| 221 | + concatMap((builderMap) => { |
| 222 | + const builderDescription = builderMap.builders[builderName]; |
| 223 | + |
| 224 | + if (!builderDescription) { |
| 225 | + throw new BuilderCannotBeResolvedException(target.builder); |
| 226 | + } |
| 227 | + |
| 228 | + // Resolve paths in the builder description. |
| 229 | + const builderJsonDir = dirname(buildersJsonPath); |
| 230 | + builderDescription.schema = join(builderJsonDir, builderDescription.schema); |
| 231 | + builderDescription.class = join(builderJsonDir, builderDescription.class); |
| 232 | + |
| 233 | + // Validate options again builder schema. |
| 234 | + return of(builderDescription); |
| 235 | + }), |
| 236 | + ).subscribe(obs); |
| 237 | + }); |
| 238 | + } |
| 239 | + |
| 240 | + validateBuilderOptions<OptionsT>( |
| 241 | + target: Target<OptionsT>, builderDescription: BuilderDescription, |
| 242 | + ): Observable<OptionsT> { |
| 243 | + return this._validateAgainstSchema<OptionsT>(target.options, |
| 244 | + normalize(builderDescription.schema)); |
| 245 | + } |
| 246 | + |
| 247 | + getBuilder<OptionsT>( |
| 248 | + builderDescription: BuilderDescription, context: BuilderContext, |
| 249 | + ): Builder<OptionsT> { |
| 250 | + // TODO: support more than the default export, maybe via builder#import-name. |
| 251 | + const builderModule = require(getSystemPath(builderDescription.class)); |
| 252 | + const builderClass = builderModule['default'] as BuilderConstructor<OptionsT>; |
| 253 | + |
| 254 | + return new builderClass(context); |
| 255 | + } |
| 256 | + |
| 257 | + // Warning: this method changes contentJson in place. |
| 258 | + // TODO: add transforms to resolve paths. |
| 259 | + private _validateAgainstSchema<T = {}>(contentJson: {}, schemaPath: Path): Observable<T> { |
| 260 | + const registry = new schema.CoreSchemaRegistry(); |
| 261 | + |
| 262 | + return this._host.read(schemaPath).pipe( |
| 263 | + concatMap((buffer) => of(JSON.parse(virtualFs.fileBufferToString(buffer)))), |
| 264 | + concatMap((schemaContent) => registry.compile(schemaContent)), |
| 265 | + concatMap(validator => validator(contentJson)), |
| 266 | + concatMap(validatorResult => { |
| 267 | + if (validatorResult.success) { |
| 268 | + return of(contentJson as T); |
| 269 | + } else { |
| 270 | + return _throw(new SchemaValidationException(validatorResult.errors as string[])); |
| 271 | + } |
| 272 | + }), |
| 273 | + ); |
| 274 | + } |
| 275 | +} |
0 commit comments