angular-cn/aio/dist/generated/docs/guide/schematics-authoring.json

5 lines
25 KiB
JSON
Raw Permalink Normal View History

{
"id": "guide/schematics-authoring",
"title": "Authoring schematics",
"contents": "\n\n\n<div class=\"github-links\">\n <a href=\"https://github.com/angular/angular/edit/master/aio/content/guide/schematics-authoring.md?message=docs%3A%20describe%20your%20change...\" aria-label=\"Suggest Edits\" title=\"Suggest Edits\"><i class=\"material-icons\" aria-hidden=\"true\" role=\"img\">mode_edit</i></a>\n</div>\n\n\n<div class=\"content\">\n <h1 id=\"authoring-schematics\">Authoring schematics<a title=\"Link to this heading\" class=\"header-link\" aria-hidden=\"true\" href=\"guide/schematics-authoring#authoring-schematics\"><i class=\"material-icons\">link</i></a></h1>\n<p>You can create your own schematics to operate on Angular projects.\nLibrary developers typically package schematics with their libraries in order to integrate them with the Angular CLI.\nYou can also create stand-alone schematics to manipulate the files and constructs in Angular applications as a way of customizing them for your development environment and making them conform to your standards and constraints.\nSchematics can be chained, running other schematics to perform complex operations.</p>\n<p>Manipulating the code in an application has the potential to be both very powerful and correspondingly dangerous.\nFor example, creating a file that already exists would be an error, and if it was applied immediately, it would discard all the other changes applied so far.\nThe Angular Schematics tooling guards against side effects and errors by creating a virtual file system.\nA schematic describes a pipeline of transformations that can be applied to the virtual file system.\nWhen a schematic runs, the transformations are recorded in memory, and only applied in the real file system once they're confirmed to be valid.</p>\n<h2 id=\"schematics-concepts\">Schematics concepts<a title=\"Link to this heading\" class=\"header-link\" aria-hidden=\"true\" href=\"guide/schematics-authoring#schematics-concepts\"><i class=\"material-icons\">link</i></a></h2>\n<p>The public API for schematics defines classes that represent the basic concepts.</p>\n<ul>\n<li>\n<p>The virtual file system is represented by a <code>Tree</code>. The <code>Tree</code> data structure contains a <em>base</em> (a set of files that already exists) and a <em>staging area</em> (a list of changes to be applied to the base).\nWhen making modifications, you don't actually change the base, but add those modifications to the staging area.</p>\n</li>\n<li>\n<p>A <code>Rule</code> object defines a function that takes a <code>Tree</code>, applies transformations, and returns a new <code>Tree</code>. The main file for a schematic, <code>index.ts</code>, defines a set of rules that implement the schematic's logic.</p>\n</li>\n<li>\n<p>A transformation is represented by an <code>Action</code>. There are four action types: <code>Create</code>, <code>Rename</code>, <code>Overwrite</code>, and <code>Delete</code>.</p>\n</li>\n<li>\n<p>Each schematic runs in a context, represented by a <code>SchematicContext</code> object.</p>\n</li>\n</ul>\n<p>The context object passed into a rule provides access to utility functions and metadata that the schematic may need to work with, including a logging API to help with debugging.\nThe context also defines a <em>merge strategy</em> that determines how changes are merged from the staged tree into the base tree. A change can be accepted or ignored, or throw an exception.</p>\n<h3 id=\"defining-rules-and-actions\">Defining rules and actions<a title=\"Link to this heading\" class=\"header-link\" aria-hidden=\"true\" href=\"guide/schematics-authoring#defining-rules-and-actions\"><i class=\"material-icons\">link</i></a></h3>\n<p>When you create a new blank schematic with the <a href=\"guide/schematics-authoring#cli\">Schematics CLI</a>, the generated entry function is a <em>rule factory</em>.\nA <code>RuleFactory</code> object defines a higher-order function that creates a <code>Rule</code>.</p>\n<code-example language=\"TypeScript\" header=\"index.ts\">\nimport { Rule, SchematicContext, Tree } from '@angular-devkit/schematics';\n\n// You don't hav
}