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

Automating boring Tasks with the Angular CLI and Schematics

Automating boring Tasks with the Angular CLI and Schematics

Slides from my talk at frontendlove in Amsterdam, February 2018

Manfred Steyer

February 15, 2018
Tweet

More Decks by Manfred Steyer

Other Decks in Programming

Transcript

  1. ManfredSteyer Automating boring Tasks with the Angular CLI and Schematics

    Manfred Steyer SOFTWAREarchitekt.at ManfredSteyer
  2. ManfredSteyer About me… • Manfred Steyer • SOFTWAREarchitekt.at • Angular

    Trainings and Consultancy • Google Developer Expert (GDE) Page ▪ 2 Manfred Steyer
  3. 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. ManfredSteyer "All it takes is one bad project setup to

    reduce the sanest man alive to lunacy." -- based loosely on: Alan Moore, Batman: The Killing Joke, 1988
  5. ManfredSteyer Contents • What is Schematics? • How to create

    custom schematics? • How to use parameters? • How to use templates?
  6. ManfredSteyer Schematics Scaffolding Tool used by Angular CLI Schematics: The

    whole project Schematic: Code Generator Collection: Set of Schematics
  7. 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. 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. 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. 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. 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. 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
  13. 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
  14. ManfredSteyer Testing in Demo App npm install hello-world or npm

    link ..\hello-world schematics hello-world:hello-world
  15. ManfredSteyer Testing in Demo App npm install hello-world or npm

    link ..\hello-world ng generate hello-world:hello-world some-name
  16. 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
  17. ManfredSteyer Rule Factory gets Parameter Object export default function (options:

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

    MenuOptions): Rule { return (host: Tree, context: SchematicContext) => { […] } }
  19. ManfredSteyer Template export interface <%= classify(name) %>Item { title: string;

    iconClass: string; } passed property passed function
  20. ManfredSteyer Rule calling Templates export default function (options: MenuOptions): Rule

    { return (host: Tree, context: SchematicContext) => { […] } }
  21. 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
  22. 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); } }
  23. 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
  24. 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!