This repository was archived by the owner on Apr 4, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 155
feat(@angular-devkit/schematics): support collection extension #398
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,6 +40,13 @@ export class UnknownUrlSourceProtocol extends BaseException { | |
export class UnknownCollectionException extends BaseException { | ||
constructor(name: string) { super(`Unknown collection "${name}".`); } | ||
} | ||
|
||
export class CircularCollectionException extends BaseException { | ||
constructor(name: string) { | ||
super(`Circular collection reference "${name}".`); | ||
} | ||
} | ||
|
||
export class UnknownSchematicException extends BaseException { | ||
constructor(name: string, collection: CollectionDescription<{}>) { | ||
super(`Schematic "${name}" not found in collection "${collection.name}".`); | ||
|
@@ -76,16 +83,38 @@ export class SchematicEngine<CollectionT extends object, SchematicT extends obje | |
return collection; | ||
} | ||
|
||
const [description, bases] = this._createCollectionDescription(name); | ||
|
||
collection = new CollectionImpl<CollectionT, SchematicT>(description, this, bases); | ||
this._collectionCache.set(name, collection); | ||
this._schematicCache.set(name, new Map()); | ||
|
||
return collection; | ||
} | ||
|
||
private _createCollectionDescription( | ||
name: string, | ||
parentNames?: Set<string>, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just one nit but not stopping the approval; |
||
): [CollectionDescription<CollectionT>, Array<CollectionDescription<CollectionT>>] { | ||
const description = this._host.createCollectionDescription(name); | ||
if (!description) { | ||
throw new UnknownCollectionException(name); | ||
} | ||
if (parentNames && parentNames.has(description.name)) { | ||
throw new CircularCollectionException(name); | ||
} | ||
|
||
collection = new CollectionImpl<CollectionT, SchematicT>(description, this); | ||
this._collectionCache.set(name, collection); | ||
this._schematicCache.set(name, new Map()); | ||
const bases = new Array<CollectionDescription<CollectionT>>(); | ||
if (description.extends) { | ||
parentNames = (parentNames || new Set<string>()).add(description.name); | ||
for (const baseName of description.extends) { | ||
const [base, baseBases] = this._createCollectionDescription(baseName, new Set(parentNames)); | ||
|
||
return collection; | ||
bases.unshift(base, ...baseBases); | ||
} | ||
} | ||
|
||
return [description, bases]; | ||
} | ||
|
||
createContext( | ||
|
@@ -148,21 +177,43 @@ export class SchematicEngine<CollectionT extends object, SchematicT extends obje | |
return schematic; | ||
} | ||
|
||
const description = this._host.createSchematicDescription(name, collection.description); | ||
let collectionDescription = collection.description; | ||
let description = this._host.createSchematicDescription(name, collection.description); | ||
if (!description) { | ||
throw new UnknownSchematicException(name, collection.description); | ||
if (collection.baseDescriptions) { | ||
for (const base of collection.baseDescriptions) { | ||
description = this._host.createSchematicDescription(name, base); | ||
if (description) { | ||
collectionDescription = base; | ||
break; | ||
} | ||
} | ||
} | ||
if (!description) { | ||
// Report the error for the top level schematic collection | ||
throw new UnknownSchematicException(name, collection.description); | ||
} | ||
} | ||
|
||
const factory = this._host.getSchematicRuleFactory(description, collection.description); | ||
const factory = this._host.getSchematicRuleFactory(description, collectionDescription); | ||
schematic = new SchematicImpl<CollectionT, SchematicT>(description, factory, collection, this); | ||
|
||
schematicMap.set(name, schematic); | ||
|
||
return schematic; | ||
} | ||
|
||
listSchematicNames(collection: Collection<CollectionT, SchematicT>) { | ||
return this._host.listSchematicNames(collection.description); | ||
listSchematicNames(collection: Collection<CollectionT, SchematicT>): string[] { | ||
const names = this._host.listSchematicNames(collection.description); | ||
|
||
if (collection.baseDescriptions) { | ||
for (const base of collection.baseDescriptions) { | ||
names.push(...this._host.listSchematicNames(base)); | ||
} | ||
} | ||
|
||
// remove duplicates | ||
return [...new Set(names)]; | ||
} | ||
|
||
transformOptions<OptionT extends object, ResultT extends object>( | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
...ular_devkit/schematics/tools/file-system-engine-host/extends-basic-string/collection.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"name": "extends-basic-string", | ||
"extends": "works", | ||
"schematics": { | ||
"schematic2": { | ||
"description": "2", | ||
"factory": "../null-factory" | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How hard to add
type: [ "array", "string" ]
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added support for both.