mason icon indicating copy to clipboard operation
mason copied to clipboard

feat: In-file code generation + diff summary proposal

Open Rodsevich opened this issue 2 years ago • 2 comments

Description

It's a common use case the inclusion of your generated code to well known existing files. This is a proposal for that:

Suppose this tree:

├── classes
│   ├── index.dart
│   ├── classes
│   │   ├── class1.dart
│   │   └── class2.dart

index.dart contents:

//classes/index.dart

@masonAppend('generatedClassesVar', '{{name.pascalCase()}}()')
var classesGenerated = {Class1(), Class2()};

//List of generated classes:
// - Class1
// - Class2
// @masonBefore('generatedClassesList', '// - {{name.pascalCase()}})

@masonAfter('anythingElse', 'var {{name}}{{/hasAssign}}{{assign}}{{#hasAssign}};')
var aThing;
var anythingElse = {};
var anotherThing = false;

and the brick that generated those files:

//bricks/classes/__brick__/classes/classes/{{name.camelCase()}}.dart
class {{name.pascalCase()}}() {
    //...
}
#bricks/classes/brick.yaml
name: classes
description: Create classes
version: 0.1.0+1
vars:
  name:
    type: string
    description: The class name.
    prompt: Enter your class name.
files:
  classes/index.dart: generatedClassesVar
  classes/index.dart: generatedClassesList

The CLI usage should be something like this:

image

observations

  • The annotations could be replaced with markers, i.e. some recognizable string that could be included in a comment like @masonBefore in this example. It could be replaced to // #masonBefore or anything better.
  • The files:on the YAML is intended to be discussed, together with its syntax.
  • A cheap way of having the in-file generation feature done would be adding hooks, but I'm way sure this feature is a must-have for a code-generation solution like mason that should work out of the box with that few configuration lines of the YAML.
  • A cheap way of showing the summary would be using git diff, it could start as a by default showable if the package has one and the working dir is clean (creating the whole commit, wether by default or as a configuration or flag)

Requirements

  • [ ] Discuss proposal
  • [ ] Approve working draft
  • [ ] Make annotations
  • [ ] Make editing logic
  • [ ] Add diff summary to command outputs

Rodsevich avatar Jun 07 '22 08:06 Rodsevich

Hi @Rodsevich 👋 Thanks so much for opening an issue!

Are you able to provide a snippet of the raw template and a snippet of the generated output side-by-side? I just want to make sure I fully understand the proposal, thanks! 🙏

felangel avatar Jun 10 '22 15:06 felangel

Sure!

before generation after generation
index.dart index.dart
image image
structure structure
image image
class file template class file generation
image image

Rodsevich avatar Jun 11 '22 08:06 Rodsevich