Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Angular Apps at the Push of a Button? The Angular CLI and its Code Generator Schematics

Angular Apps at the Push of a Button? The Angular CLI and its Code Generator Schematics

Talk from BASTA! Spring 2018 in Frankfurt, Germany.

Source Code:
https://github.com/manfredsteyer/basta-spring-2018-schematics

Blog:
https://www.softwarearchitekt.at

Manfred Steyer

February 21, 2018
Tweet

More Decks by Manfred Steyer

Other Decks in Programming

Transcript

  1. @BASTAcon & @ManfredSteyer Angular Apps at the Push of a

    Button? The Angular CLI and its Code Generator Schematics Manfred Steyer SOFTWAREarchitekt.at ManfredSteyer
  2. @BASTAcon & @ManfredSteyer About me… • Manfred Steyer • SOFTWAREarchitekt.at

    • Angular Trainings and Consultancy • Google Developer Expert (GDE) Page ▪ 3 Manfred Steyer
  3. @BASTAcon & @ManfredSteyer Have you ever tried to … …

    setup a modern JavaScript project manually? • Compiler/ Transpiler • Package manager and packages • Build chain optimized for bundle size, performance, etc. • Testing tools including code coverage • Linting tools
  4. @BASTAcon & @ManfredSteyer Using the CLI ng new my-project cd

    my-project ng generate component my-component
  5. @BASTAcon & @ManfredSteyer Contents • What is Schematics? • How

    to create custom schematics? • How to use parameters? • How to use templates? • How to modify existing code?
  6. @BASTAcon & @ManfredSteyer Schematics Scaffolding Tool used by Angular CLI

    Schematics: The whole project Schematic: Code Generator Collection: Set of Schematics
  7. @BASTAcon & @ManfredSteyer Schematics are atomic (like db transactions) A

    B D E C F A B D E C F Your Project Staging Area Tree Object
  8. @BASTAcon & @ManfredSteyer Schematics are atomic (like db transactions) A

    B D E C F A B D' E C F Your Project Staging Area Tree Object
  9. @BASTAcon & @ManfredSteyer Schematics are atomic (like db transactions) A

    B D E C F A B D' E C F G Your Project Staging Area Tree Object
  10. @BASTAcon & @ManfredSteyer Schematics are atomic (like db transactions) A

    B D E C F A B D' E C F G Your Project Staging Area Tree Object
  11. @BASTAcon & @ManfredSteyer Schematics are atomic (like db transactions) A

    B D' E C F G A B D' E C F G Your Project Staging Area Tree Object
  12. @BASTAcon & @ManfredSteyer Scaffold Collection npm install -g @angular-devkit/schematics-cli schematics

    blank --name=hello-world schematics schematic --name=hello-world Contains sample schematics
  13. @BASTAcon & @ManfredSteyer Surviving Schematics: Rule 1 ;-) "devDependencies": {

    "@angular-devkit/core": "0.0.29", "@angular-devkit/schematics": "0.0.52", "@schematics/angular": "0.1.17", […] } Use the same versions as your CLI-generated project! + npm install Versions used in CLI 1.6.8
  14. @BASTAcon & @ManfredSteyer Rule Factory export function helloWorld(options: any): Rule

    { return (tree: Tree, _context: SchematicContext) => { _context.logger.debug('Manfred was here!'); tree.create('hello.txt', 'Hello World!'); return tree; }; } Rule
  15. @BASTAcon & @ManfredSteyer Testing in Demo App npm install hello-world

    or npm link ..\hello-world schematics hello-world:hello-world
  16. @BASTAcon & @ManfredSteyer Testing in Demo App npm install hello-world

    or npm link ..\hello-world ng generate hello-world:hello-world some-name
  17. @BASTAcon & @ManfredSteyer Option Class in schema.ts export interface MenuOptions

    { name: string; appRoot: string; path: string; sourceDir: string; menuService: boolean; […] } provided by the CLI custom stuff
  18. @BASTAcon & @ManfredSteyer Rule Factory gets Parameter Object export default

    function (options: MenuOptions): Rule { return (host: Tree, context: SchematicContext) => { […] } }
  19. @BASTAcon & @ManfredSteyer Rule Factory gets Parameter Object export default

    function (options: MenuOptions): Rule { return (host: Tree, context: SchematicContext) => { […] } }
  20. @BASTAcon & @ManfredSteyer Template export interface <%= classify(name) %>Item {

    title: string; iconClass: string; } passed property passed function
  21. @BASTAcon & @ManfredSteyer Rule calling Templates export default function (options:

    MenuOptions): Rule { return (host: Tree, context: SchematicContext) => { […] } }
  22. @BASTAcon & @ManfredSteyer Rule calling Templates export default function (options:

    MenuOptions): Rule { return (host: Tree, context: SchematicContext) => { const templateSource = apply(url('./files'), [ filterTemplates(options), template({...stringUtils, ...options}), move(options.sourceDir) ]); […] } } passed properties passed functions
  23. @BASTAcon & @ManfredSteyer Rule calling Templates export default function (options:

    MenuOptions): Rule { return (host: Tree, context: SchematicContext) => { const templateSource = apply(url('./files'), [ filterTemplates(options), template({...stringUtils, ...options}), move(options.sourceDir) ]); const rule = chain([ branchAndMerge(chain([ mergeWith(templateSource), […] ])) ]); return rule(host, context); } }
  24. @BASTAcon & @ManfredSteyer Declaring generated Components import { NgModule }

    from '@angular/core'; import { CommonModule } from '@angular/common'; import { SideMenuComponent } from './side-menu/side-menu.component'; @NgModule({ imports: [ CommonModule ], declarations: [ SideMenuComponent ], exports: [ SideMenuComponent ] }) export class CoreModule { }
  25. @BASTAcon & @ManfredSteyer Custom Rule for modifying existing Code •

    String Operations • RegExp • TypeScript Compiler API
  26. @BASTAcon & @ManfredSteyer Custom Rule for modifying existing Code •

    String Operations • RegExp • TypeScript Compiler API
  27. @BASTAcon & @ManfredSteyer Code File as Syntax Tree (simplified) ClassDeclaration

    ClassKeyword (class) Identifier (Demo) … SyntaxList class Demo { constructor(otherDemo: Demo) {} } Subtree w/ Constructor …
  28. @BASTAcon & @ManfredSteyer Injecting Service into Component […] import {

    SideMenuService } from './core/side-menu/side-menu.service'; @Component({ […] }) export class AppComponent { constructor(private sideMenuService: SideMenuService) { // sideMenuService.show = true; } }
  29. @BASTAcon & @ManfredSteyer Further information • Great introduction in Angular

    Blog • https://blog.angular.io/schematics-an-introduction-dc1dfbc2a2b2 • My Tutorial/ Case Study (3 articles so far) • https://goo.gl/kGS6L5 • Angular-Crud (Generator) • https://www.npmjs.com/package/angular-crud
  30. @BASTAcon & @ManfredSteyer Conclusion Automate boring tasks! Currently: Labs Project

    Official Solution in ng world CLI, Nx, Ngrx, … Staging Area, Templates, Rules Create own or tweak existing ones Try it out!