|
| 1 | +# Schematics |
| 2 | +> A scaffolding library for the modern web. |
| 3 | +
|
| 4 | +## Description |
| 5 | +Schematics are generators that transform an existing filesystem. It can create files, refactor existing files, or move files around. |
| 6 | + |
| 7 | +What distinguish 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. |
| 8 | + |
| 9 | +# Glossary |
| 10 | + |
| 11 | +| Term | Description | |
| 12 | +|------|-------------| |
| 13 | +| **Schematics** | A generator that execute descriptive code without side effects on an existing file system. | |
| 14 | +| **Collection** | A list of schematics metadata. Schematics can be referred by name inside a collection. | |
| 15 | +| **Tool** | The code using the Schematics library. | |
| 16 | +| **Tree** | A staging area for changes, containing the original file system, and a list of changes to apply to it. | |
| 17 | +| **Rule** | A function that applies actions to a `Tree`. It returns a new Tree that will contain all transformations to be applied. | |
| 18 | +| **Source** | A function that creates an entirely new `Tree` from an empty filesystem. For example, a file source could read files from disk and create a Create Action for each of those. |
| 19 | +| **Action** | A atomic operation to be validated and committed to a filesystem or a `Tree`. Actions are created by schematics. | |
| 20 | +| **Sink** | The final destination of all `Action`s. | |
| 21 | + |
| 22 | +# Tooling |
| 23 | +Schematics is a library, and does not work by itself. A reference CLI is available in [`@angular/schematics-cli`](../schematics_cli/README.md). This document explain the library usage and the tooling API, but does not go into the tool implementation itself. |
| 24 | + |
| 25 | +The tooling is responsible for the following tasks: |
| 26 | + |
| 27 | +1. Create the Schematic Engine, and pass in a Collection and Schematic loader. |
| 28 | +1. 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. |
| 29 | +1. 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, parse the arguments as a JSON object and validate it with the Schema specified by the collection. |
| 30 | +1. 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. |
| 31 | +1. 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. |
| 32 | +1. Output any logs propagated by the library, including debugging information. |
| 33 | + |
| 34 | +# Examples |
| 35 | + |
| 36 | +## Simple |
| 37 | +An example of a simple Schematics which creates a "hello world" file, using an option to determine its path: |
| 38 | + |
| 39 | +```typescript |
| 40 | +import {Tree} from '@angular/schematics'; |
| 41 | + |
| 42 | +export default function MySchematic(options: any) { |
| 43 | + return (tree: Tree) => { |
| 44 | + tree.create(options.path + '/hi', 'Hello world!'); |
| 45 | + return tree; |
| 46 | + }; |
| 47 | +} |
| 48 | +``` |
| 49 | + |
| 50 | +A few things from this example: |
| 51 | + |
| 52 | +1. The function receives the list of options from the tooling. |
| 53 | +2. It returns a [`Rule`](src/engine/interface.ts#L73), which is a transformation from a `Tree` to another `Tree`. |
| 54 | + |
| 55 | + |
| 56 | + |
| 57 | +# Future Work |
| 58 | + |
| 59 | +Schematics is not done yet. Here's a list of things we are considering: |
| 60 | + |
| 61 | +* Smart defaults for Options. Having a JavaScript function for default values based on other default values. |
| 62 | +* Prompt for input options. This should only be prompted for the original schematics, dependencies to other schematics should not trigger another prompting. |
| 63 | +* Tasks for running tooling-specific jobs before and after a schematics has been scaffolded. Such tasks can involve initialize git, or npm install. A specific list of tasks should be provided by the tool, with unsupported tasks generating an error. |
| 64 | + |
0 commit comments