Skip to content

Commit

Permalink
fix(@angular-devkit/schematics): fully scope merge actions in ScopedTree
Browse files Browse the repository at this point in the history
  • Loading branch information
clydin authored and mgechev committed Jan 18, 2019
1 parent 0563e96 commit 757bca4
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 4 deletions.
60 changes: 56 additions & 4 deletions packages/angular_devkit/schematics/src/tree/scoped.ts
Expand Up @@ -14,6 +14,7 @@ import {
relative,
} from '@angular-devkit/core';
import { Action } from './action';
import { DelegateTree } from './delegate';
import {
DirEntry,
FileEntry,
Expand Down Expand Up @@ -89,7 +90,16 @@ export class ScopedTree implements Tree {
get root(): DirEntry { return this._root; }

branch(): Tree { return new ScopedTree(this._base.branch(), this._root.scope); }
merge(other: Tree, strategy?: MergeStrategy): void { this._base.merge(other, strategy); }
merge(other: Tree, strategy?: MergeStrategy): void {
const self = this;
const delegate = new class extends DelegateTree {
get actions(): Action[] {
return other.actions.map(action => self._fullPathAction(action));
}
}(other);

this._base.merge(delegate, strategy);
}

// Readonly.
read(path: string): Buffer | null { return this._base.read(this._fullPath(path)); }
Expand Down Expand Up @@ -125,15 +135,57 @@ export class ScopedTree implements Tree {
}

apply(action: Action, strategy?: MergeStrategy): void {
return this._base.apply(action, strategy);
return this._base.apply(this._fullPathAction(action), strategy);
}

get actions(): Action[] {
const scopedActions = [];

for (const action of this._base.actions) {
if (!action.path.startsWith(this._root.scope + '/')) {
continue;
}

if (action.kind !== 'r') {
scopedActions.push({
...action,
path: join(NormalizedRoot, relative(this._root.scope, action.path)),
});
} else if (action.to.startsWith(this._root.scope + '/')) {
scopedActions.push({
...action,
path: join(NormalizedRoot, relative(this._root.scope, action.path)),
to: join(NormalizedRoot, relative(this._root.scope, action.to)),
});
}
}

return scopedActions;
}
get actions(): Action[] { return this._base.actions; }

[TreeSymbol]() {
return this;
}

private _fullPath(path: string) {
private _fullPath(path: string): Path {
return join(this._root.scope, normalize('/' + path));
}

private _fullPathAction(action: Action) {
let fullPathAction: Action;
if (action.kind === 'r') {
fullPathAction = {
...action,
path: this._fullPath(action.path),
to: this._fullPath(action.to),
};
} else {
fullPathAction = {
...action,
path: this._fullPath(action.path),
};
}

return fullPathAction;
}
}
25 changes: 25 additions & 0 deletions packages/angular_devkit/schematics/src/tree/scoped_spec.ts
Expand Up @@ -133,6 +133,31 @@ describe('ScopedTree', () => {
]);
});

it('supports merge into a scoped tree', () => {
const other = new HostTree();
other.create('other-file', 'other');

scoped.merge(other);

expect(base.exists('/level-1/other-file')).toBeTruthy();
expect(base.exists('/other-file')).toBeFalsy();
});

it('supports merge from a scoped tree', () => {
const other = new HostTree();

other.merge(scoped);

expect(other.exists('/file-1-1')).toBeTruthy();
expect(other.exists('file-1-1')).toBeTruthy();
expect(other.exists('/file-1-2')).toBeTruthy();

expect(other.exists('/file-0-1')).toBeFalsy();
expect(other.exists('file-0-1')).toBeFalsy();
expect(other.exists('/level-1/file-1-1')).toBeFalsy();
expect(other.exists('level-1/file-1-1')).toBeFalsy();
});

it('supports root', () => {
expect(scoped.root).not.toBeNull();
expect(scoped.root.path as string).toBe('/');
Expand Down

0 comments on commit 757bca4

Please sign in to comment.