Skip to content

Commit bc99b77

Browse files
jbogarthydematsko
authored andcommitted
docs: add schematics guide (angular#28343)
PR Close angular#28343
1 parent a3ec058 commit bc99b77

37 files changed

+1295
-18
lines changed

aio/content/examples/.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,3 +92,6 @@ upgrade-phonecat-3-final/tsconfig-aot.json
9292
upgrade-phonecat-3-final/rollup-config.js
9393
!upgrade-phonecat-*/**/karma.conf.js
9494
!upgrade-phonecat-*/**/karma-test-shim.js
95+
96+
# schematics
97+
!schematics-for-libraries/projects/my-lib/package.json
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"projectType": "schematics"
3+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"$schema": "../../node_modules/ng-packagr/ng-package.schema.json",
3+
"dest": "../../dist/my-lib",
4+
"lib": {
5+
"entryFile": "src/public_api.ts"
6+
}
7+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// #docplaster
2+
// #docregion collection
3+
{
4+
"name": "my-lib",
5+
"version": "0.0.1",
6+
// #enddocregion collection
7+
"scripts": {
8+
"build": "../../node_modules/.bin/tsc -p tsconfig.schematics.json",
9+
"copy:schemas": "cp --parents schematics/*/schema.json ../../dist/my-lib/",
10+
"copy:files": "cp --parents -p schematics/*/files/** ../../dist/my-lib/",
11+
"copy:collection": "cp schematics/collection.json ../../dist/my-lib/schematics/collection.json",
12+
"postbuild": "npm run copy:schemas && npm run copy:files && npm run copy:collection"
13+
},
14+
"peerDependencies": {
15+
"@angular/common": "^7.2.0",
16+
"@angular/core": "^7.2.0"
17+
},
18+
// #docregion collection
19+
"schematics": "./schematics/collection.json"
20+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"$schema": "../../../node_modules/@angular-devkit/schematics/collection-schema.json",
3+
"schematics": {
4+
"ng-add": {
5+
"description": "Add my library to the project.",
6+
"factory": "./ng-add/index#ngAdd"
7+
}
8+
}
9+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"$schema": "../../../node_modules/@angular-devkit/schematics/collection-schema.json",
3+
"schematics": {
4+
"ng-add": {
5+
"description": "Add my library to the project.",
6+
"factory": "./ng-add/index#ngAdd"
7+
},
8+
"my-service": {
9+
"description": "Generate a service in the project.",
10+
"factory": "./my-service/index#myService",
11+
"schema": "./my-service/schema.json"
12+
}
13+
}
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// #docregion template
2+
import { Injectable } from '@angular/core';
3+
import { HttpClient } from '@angular/common/http';
4+
5+
@Injectable({
6+
providedIn: 'root'
7+
})
8+
export class <%= classify(name) %>Service {
9+
constructor(private http: HttpClient) { }
10+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { Rule, Tree } from '@angular-devkit/schematics';
2+
3+
import { Schema as MyServiceSchema } from './schema';
4+
5+
// #docregion factory
6+
export function myService(options: MyServiceSchema): Rule {
7+
return (tree: Tree) => {
8+
return tree;
9+
};
10+
}
11+
// #enddocregion factory
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// #docplaster
2+
// #docregion schematics-imports, schema-imports, workspace
3+
import {
4+
Rule, Tree, SchematicsException,
5+
apply, url, applyTemplates, move,
6+
chain, mergeWith
7+
} from '@angular-devkit/schematics';
8+
9+
import { strings, normalize, experimental } from '@angular-devkit/core';
10+
// #enddocregion schematics-imports
11+
12+
import { Schema as MyServiceSchema } from './schema';
13+
// #enddocregion schema-imports
14+
15+
export function myService(options: MyServiceSchema): Rule {
16+
return (tree: Tree) => {
17+
const workspaceConfig = tree.read('/angular.json');
18+
if (!workspaceConfig) {
19+
throw new SchematicsException('Could not find Angular workspace configuration');
20+
}
21+
22+
// convert workspace to string
23+
const workspaceContent = workspaceConfig.toString();
24+
25+
// parse workspace string into JSON object
26+
const workspace: experimental.workspace.WorkspaceSchema = JSON.parse(workspaceContent);
27+
// #enddocregion workspace
28+
// #docregion project-fallback
29+
if (!options.project) {
30+
options.project = workspace.defaultProject;
31+
}
32+
// #enddocregion project-fallback
33+
34+
// #docregion project-info
35+
const projectName = options.project as string;
36+
37+
const project = workspace.projects[projectName];
38+
39+
const projectType = project.projectType === 'application' ? 'app' : 'lib';
40+
// #enddocregion project-info
41+
42+
// #docregion path
43+
if (options.path === undefined) {
44+
options.path = `${project.sourceRoot}/${projectType}`;
45+
}
46+
// #enddocregion path
47+
48+
// #docregion template
49+
const templateSource = apply(url('./files'), [
50+
applyTemplates({
51+
classify: strings.classify,
52+
dasherize: strings.dasherize,
53+
name: options.name
54+
}),
55+
move(normalize(options.path as string))
56+
]);
57+
// #enddocregion template
58+
59+
// #docregion chain
60+
return chain([
61+
mergeWith(templateSource)
62+
]);
63+
// #enddocregion chain
64+
// #docregion workspace
65+
};
66+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"$schema": "http://json-schema.org/schema",
3+
"id": "SchematicsMyService",
4+
"title": "My Service Schema",
5+
"type": "object",
6+
"properties": {
7+
"name": {
8+
"description": "The name of the service.",
9+
"type": "string"
10+
},
11+
"path": {
12+
"type": "string",
13+
"format": "path",
14+
"description": "The path to create the service.",
15+
"visible": false
16+
},
17+
"project": {
18+
"type": "string",
19+
"description": "The name of the project.",
20+
"$default": {
21+
"$source": "projectName"
22+
}
23+
}
24+
},
25+
"required": [
26+
"name"
27+
]
28+
}

0 commit comments

Comments
 (0)