Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Build artifacts for @angular-devkit/schematics

License

NotificationsYou must be signed in to change notification settings

angular/angular-devkit-schematics-builds

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

This repository is a snapshot of a commit on the original repository. The original code used togenerate this is located athttp://github.com/angular/angular-cli.

We do not accept PRs or Issues opened on this repository. You should not use this over a tested andreleased version of this package.

To test this snapshot in your own project, use

npm install git+https://github.com/angular/angular-devkit-schematics-builds.git

Schematics

A scaffolding library for the modern web.

Description

Schematics are generators that transform an existing filesystem. They can create files, refactor existing files, or move files around.

What distinguishes Schematics from other generators, such as Yeoman or Yarn Create, is that schematics are purely descriptive; no changes are applied to the actual filesystem until everything is ready to be committed. There is no side effect, by design, in Schematics.

Glossary

TermDescription
SchematicsA generator that executes descriptive code without side effects on an existing file system.
CollectionA list of schematics metadata. Schematics can be referred by name inside a collection.
ToolThe code using the Schematics library.
TreeA staging area for changes, containing the original file system, and a list of changes to apply to it.
RuleA function that applies actions to aTree. It returns a newTree that will contain all transformations to be applied.
SourceA function that creates an entirely newTree from an empty filesystem. For example, a file source could read files from disk and create a Create Action for each of those.
ActionAn atomic operation to be validated and committed to a filesystem or aTree. Actions are created by schematics.
SinkThe final destination of allActions.
TaskA Task is a way to execute an external command or script in a schematic. A Task can be used to perform actions such as installing dependencies, running tests, or building a project. A Task is created by using theSchematicContext object and can be scheduled to run before or after the schematicTree is applied.

Tooling

Schematics is a library, and does not work by itself. Areference CLI is available on this repository, and is published on NPM at@angular-devkit/schematics-cli. This document explains the library usage and the tooling API, but does not go into the tool implementation itself.

The tooling is responsible for the following tasks:

  1. Create the Schematic Engine, and pass in a Collection and Schematic loader.
  2. Understand and respect the Schematics metadata and dependencies between collections. Schematics can refer to dependencies, and it's the responsibility of the tool to honor those dependencies. The reference CLI uses NPM packages for its collections.
  3. Create the Options object. Options can be anything, but the schematics can specify a JSON Schema that should be respected. The reference CLI, for example, parses the arguments as a JSON object and validates it with the Schema specified by the collection.
  4. Schematics provides some JSON Schema formats for validation that tooling should add. These validate paths, html selectors and app names. Please check the reference CLI for how these can be added.
  5. Call the schematics with the original Tree. The tree should represent the initial state of the filesystem. The reference CLI uses the current directory for this.
  6. Create a Sink and commit the result of the schematics to the Sink. Many sinks are provided by the library; FileSystemSink and DryRunSink are examples.
  7. Output any logs propagated by the library, including debugging information.

The tooling API is composed of the following pieces:

Engine

TheSchematicEngine is responsible for loading and constructingCollections andSchematics. When creating an engine, the tooling provides anEngineHost interface that understands how to create aCollectionDescription from a name, and how to create aSchematicDescription.

Schematics (Generators)

Schematics are generators and part of aCollection.

Collection

A Collection is defined by acollection.json file (in the reference CLI). This JSON defines the following properties:

Prop NameTypeDescription
namestringThe name of the collection.
versionstringUnused field.

Schematic

Operators, Sources and Rules

ASource is a generator of aTree; it creates an entirely new root tree from nothing. ARule is a transformation from oneTree to another. ASchematic (at the root) is aRule that is normally applied on the filesystem.

Operators

FileOperators apply changes to a singleFileEntry and return a newFileEntry. The result follows these rules:

  1. If theFileEntry returned is null, aDeleteAction will be added to the action list.
  2. If the path changed, aRenameAction will be added to the action list.
  3. If the content changed, anOverwriteAction will be added to the action list.

It is impossible to create files using aFileOperator.

Provided Operators

The Schematics library provides multipleOperator factories by default that cover basic use cases:

FileOperatorDescription
contentTemplate<T>(options: T)Apply a content template (see theTemplating section)
pathTemplate<T>(options: T)Apply a path template (see theTemplating section)

Provided Sources

The Schematics library additionally provides multipleSource factories by default:

SourceDescription
empty()Creates a source that returns an emptyTree.
source(tree: Tree)Creates aSource that returns theTree passed in as argument.
url(url: string)Loads a list of files from the given URL and returns aTree with the files asCreateAction applied to an emptyTree.
apply(source: Source, rules: Rule[])Apply a list ofRules to a source, and return the resultingSource.

Provided Rules

The schematics library also providesRule factories by default:

RuleDescription
noop()Returns the inputTree as is.
chain(rules: Rule[])Returns aRule that's the concatenation of otherRules.
forEach(op: FileOperator)Returns aRule that applies an operator to every file of the inputTree.
move(root: string)Moves all the files from the input to a subdirectory.
merge(other: Tree)Merge the inputTree with the otherTree.
contentTemplate<T>(options: T)Apply a content template (see the Template section) to the entireTree.
pathTemplate<T>(options: T)Apply a path template (see the Template section) to the entireTree.
template<T>(options: T)Apply both path and content templates (see the Template section) to the entireTree.
filter(predicate: FilePredicate<boolean>)Returns the inputTree with files that do not pass theFilePredicate.

Templating

As referenced above, some functions are based upon a file templating system, which consists of path and content templating.

The system operates on placeholders defined inside files or their paths as loaded in theTree and fills these in as defined in the following, using values passed into theRule which applies the templating (i.e.template<T>(options: T)).

Path Templating

PlaceholderDescription
__variable__Replaced with the value ofvariable.
__variable@function__Replaced with the result of the callfunction(variable). Can be chained to the left (__variable@function1@function2__ etc).

Content Templating

PlaceholderDescription
<%= expression %>Replaced with the result of the call of the given expression. This only supports direct expressions, no structural (for/if/...) JavaScript.
<%- expression %>Same as above, but the value of the result will be escaped for HTML when inserted (i.e. replacing '<' with '&lt;')
<% inline code %>Inserts the given code into the template structure, allowing to insert structural JavaScript.
<%# text %>A comment, which gets entirely dropped.

Examples

Simple

An example of a simple Schematics which creates a "hello world" file, using an option to determine its path:

import{Tree}from'@angular-devkit/schematics';exportdefaultfunctionMySchematic(options:any){return(tree:Tree)=>{tree.create(options.path+'/hi','Hello world!');returntree;};}

A few things from this example:

  1. The function receives the list of options from the tooling.
  2. It returns aRule, which is a transformation from aTree to anotherTree.

Templating

A simplified example of a Schematics which creates a file containing a new Class, using an option to determine its name:

// files/__name@dasherize__.tsexportclass<%=classify(name)%>{}
// index.tsimport{strings}from'@angular-devkit/core';import{Rule,SchematicContext,SchematicsException,Tree,apply,branchAndMerge,mergeWith,template,url,}from'@angular-devkit/schematics';import{SchemaasClassOptions}from'./schema';exportdefaultfunction(options:ClassOptions):Rule{return(tree:Tree,context:SchematicContext)=>{if(!options.name){thrownewSchematicsException('Option (name) is required.');}consttemplateSource=apply(url('./files'),[template({        ...strings,        ...options,}),]);returnbranchAndMerge(mergeWith(templateSource));};}

Additional things from this example:

  1. strings provides the useddasherize andclassify functions, among others.
  2. The files are on-disk in the same root directory as theindex.ts and loaded into aTree.
  3. Then thetemplateRule fills in the specified templating placeholders. For this, it only knows about the variables and functions passed to it via the options-object.
  4. Finally, the resultingTree, containing the new file, is merged with the existing files of the project which the Schematic is run on.

About

Build artifacts for @angular-devkit/schematics

Resources

License

Code of conduct

Security policy

Stars

Watchers

Forks

Packages

No packages published

[8]ページ先頭

©2009-2025 Movatter.jp