Skip to content

Partial update of Angular schematics with new workspace rules #14126

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
Apr 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion etc/api/angular_devkit/schematics/src/_golden-api.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ export interface RequiredWorkflowExecutionContext {
schematic: string;
}

export declare type Rule = (tree: Tree, context: SchematicContext) => Tree | Observable<Tree> | Rule | Promise<void> | void;
export declare type Rule = (tree: Tree, context: SchematicContext) => Tree | Observable<Tree> | Rule | Promise<void> | Promise<Rule> | void;

export declare type RuleFactory<T extends object> = (options: T) => Rule;

Expand Down
4 changes: 2 additions & 2 deletions packages/angular_devkit/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import * as experimental from './experimental';
import * as json from './json/index';
import * as logging from './logger/index';
import * as terminal from './terminal/index';
import * as workspace from './workspace';
import * as workspaces from './workspace';

export * from './exception/exception';
export * from './json/index';
Expand All @@ -23,5 +23,5 @@ export {
json,
logging,
terminal,
workspace,
workspaces,
};
4 changes: 2 additions & 2 deletions packages/angular_devkit/core/src/workspace/definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,8 +201,8 @@ export class ProjectDefinitionCollection extends DefinitionCollection<ProjectDef
}

private _validateName(name: string): void {
if (typeof name !== 'string' || !/^[a-zA-Z][.0-9a-zA-Z]*(-[.0-9a-zA-Z]*)*$/.test(name)) {
throw new Error('Project name must be a valid npm package name without a scope.');
if (typeof name !== 'string' || !/^(?:@\w[\w\.-]*\/)?\w[\w\.-]*$/.test(name)) {
throw new Error('Project name must be a valid npm package name.');
}
}

Expand Down
14 changes: 10 additions & 4 deletions packages/angular_devkit/core/src/workspace/json/reader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import {
} from '../definitions';
import { WorkspaceHost } from '../host';
import { JsonWorkspaceMetadata, JsonWorkspaceSymbol } from './metadata';
import { createVirtualAstObject } from './utilities';
import { createVirtualAstObject, escapeKey } from './utilities';

interface ParserContext {
readonly host: WorkspaceHost;
Expand Down Expand Up @@ -78,7 +78,7 @@ export async function readJsonWorkspace(

const specialWorkspaceExtensions = ['cli', 'defaultProject', 'newProjectRoot', 'schematics'];

const specialProjectExtensions = ['cli', 'schematics'];
const specialProjectExtensions = ['cli', 'schematics', 'projectType'];

function parseWorkspace(workspaceNode: JsonAstObject, context: ParserContext): WorkspaceDefinition {
const jsonMetadata = context.metadata;
Expand Down Expand Up @@ -116,7 +116,13 @@ function parseWorkspace(workspaceNode: JsonAstObject, context: ParserContext): W
if (context.trackChanges && projectsNode) {
const parentNode = projectsNode;
collectionListener = (name, action, newValue) => {
jsonMetadata.addChange(action, `/projects/${name}`, parentNode, newValue, 'project');
jsonMetadata.addChange(
action,
`/projects/${escapeKey(name)}`,
parentNode,
newValue,
'project',
);
};
}

Expand Down Expand Up @@ -212,7 +218,7 @@ function parseProject(
collectionListener = (name, action, newValue) => {
jsonMetadata.addChange(
action,
`/projects/${projectName}/targets/${name}`,
`/projects/${projectName}/targets/${escapeKey(name)}`,
parentNode,
newValue,
'target',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ describe('JSON WorkspaceDefinition Tracks Workspace Changes', () => {
expect(metadata.hasChanges).toBeTruthy();
expect(metadata.changeCount).toBe(2);

change = metadata.findChangesForPath('/schematics/@angular/schematics:component')[0];
change = metadata.findChangesForPath('/schematics/@angular~1schematics:component')[0];
expect(change).not.toBeUndefined();
if (change) {
expect(change.op).toBe('replace');
Expand Down Expand Up @@ -350,7 +350,7 @@ describe('JSON WorkspaceDefinition Tracks Workspace Changes', () => {
expect(metadata.hasChanges).toBeTruthy();
expect(metadata.changeCount).toBe(2);

change = metadata.findChangesForPath('/schematics/@angular/schematics:component')[0];
change = metadata.findChangesForPath('/schematics/@angular~1schematics:component')[0];
expect(change).not.toBeUndefined();
if (change) {
expect(change.op).toBe('replace');
Expand Down
28 changes: 22 additions & 6 deletions packages/angular_devkit/core/src/workspace/json/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,22 @@ function createPropertyDescriptor(value: JsonValue | undefined): PropertyDescrip
};
}

export function escapeKey(key: string | number): string | number {
if (typeof key === 'number') {
return key;
}

return key.replace('~', '~0').replace('/', '~1');
}

export function unescapeKey(key: string | number): string | number {
if (typeof key === 'number') {
return key;
}

return key.replace('~1', '/').replace('~0', '~');
}

export function createVirtualAstObject<T extends object = JsonObject>(
root: JsonAstObject,
options: {
Expand Down Expand Up @@ -129,7 +145,7 @@ function create(
return undefined;
}

const propertyPath = path + '/' + p;
const propertyPath = path + '/' + escapeKey(p);
const cacheEntry = cache.get(propertyPath);
if (cacheEntry) {
if (cacheEntry.value) {
Expand All @@ -153,7 +169,7 @@ function create(
return false;
}

return cache.has(path + '/' + p) || findNode(ast, p) !== undefined;
return cache.has(path + '/' + escapeKey(p)) || findNode(ast, p) !== undefined;
},
get(target: {}, p: PropertyKey): unknown {
if (typeof p === 'symbol' || Reflect.has(target, p)) {
Expand All @@ -162,7 +178,7 @@ function create(
return undefined;
}

const propertyPath = path + '/' + p;
const propertyPath = path + '/' + escapeKey(p);
const cacheEntry = cache.get(propertyPath);
if (cacheEntry) {
return cacheEntry.value;
Expand Down Expand Up @@ -200,7 +216,7 @@ function create(
// TODO: Check if is JSON value
const jsonValue = value as JsonValue;

const propertyPath = path + '/' + p;
const propertyPath = path + '/' + escapeKey(p);
const cacheEntry = cache.get(propertyPath);
if (cacheEntry) {
const oldValue = cacheEntry.value;
Expand All @@ -227,7 +243,7 @@ function create(
return false;
}

const propertyPath = path + '/' + p;
const propertyPath = path + '/' + escapeKey(p);
const cacheEntry = cache.get(propertyPath);
if (cacheEntry) {
const oldValue = cacheEntry.value;
Expand Down Expand Up @@ -267,7 +283,7 @@ function create(
for (const key of cache.keys()) {
const relativeKey = key.substr(path.length + 1);
if (relativeKey.length > 0 && !relativeKey.includes('/')) {
keys.push(relativeKey);
keys.push(unescapeKey(relativeKey));
}
}

Expand Down
3 changes: 2 additions & 1 deletion packages/angular_devkit/core/src/workspace/json/writer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
JsonWorkspaceMetadata,
JsonWorkspaceSymbol,
} from './metadata';
import { unescapeKey } from './utilities';

export async function writeJsonWorkspace(
workspace: WorkspaceDefinition,
Expand Down Expand Up @@ -211,7 +212,7 @@ function updateJsonWorkspace(metadata: JsonWorkspaceMetadata): string {
const multiline = node.start.line !== node.end.line;
const pathSegments = path.split('/');
const depth = pathSegments.length - 1; // TODO: more complete analysis
const propertyOrIndex = pathSegments[depth];
const propertyOrIndex = unescapeKey(pathSegments[depth]);
const jsonValue = normalizeValue(value, type);
if (op === 'add' && jsonValue === undefined) {
continue;
Expand Down
2 changes: 1 addition & 1 deletion packages/angular_devkit/schematics/src/engine/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,4 +231,4 @@ export type AsyncFileOperator = (tree: FileEntry) => Observable<FileEntry | null
*/
export type Source = (context: SchematicContext) => Tree | Observable<Tree>;
export type Rule = (tree: Tree, context: SchematicContext) =>
Tree | Observable<Tree> | Rule | Promise<void> | void;
Tree | Observable<Tree> | Rule | Promise<void> | Promise<Rule> | void;
11 changes: 10 additions & 1 deletion packages/angular_devkit/schematics/src/rules/call.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,16 @@ export function callRule(
}),
);
} else if (isPromise(result)) {
return from(result).pipe(map(() => inputTree));
return from(result).pipe(
mergeMap(inner => {
if (typeof inner === 'function') {
// This is considered a Rule, chain the rule and return its output.
return callRule(inner, observableOf(inputTree), context);
} else {
return observableOf(inputTree);
}
}),
);
} else if (TreeSymbol in result) {
return observableOf(result);
} else {
Expand Down
Loading