loreanvictor / tmplr

Automate Code Scaffolding
MIT License
26 stars 0 forks source link

Add filtering to commands operating on multiple files. #24

Open loreanvictor opened 1 year ago

loreanvictor commented 1 year ago

A stronger filtering system for commands accepting glob patterns as input (copy, update, remove, write) can be particularly useful, specifically combined with from file. For example the following would allow adding license headers to all files lacking a license header:

steps:
  - read: license
    from file: license-header

  - write: '{{ license }}\n{{ __content__ }}'
    to: '**/*.(jsx?|tsx?|cpp|java)'
    where:
      steps:
        - read: __content__
          from file: __file__
      eval: '{{ __content__ | not matches: /^\s*\/\*\*\s*\*\s*\@license/ }}'

This would require setting a loop variable __file__, which would be the address (absolute?) of the file. I think for cleanliness, it should be unset after the loop is executed. A cleaner solution would be to create a loop-specific scope (the parser would need to do this, but it would be relatively straightforward).