ITADN

feat: in-file generation tools

#1667OpenRodsevich 创建于 2026-03-30
R
Rodsevichcommented
**Description** I want mason to processes the files in bricks with custom prefixes automatically, I'm making a POC extension of it (masonex: mason extended) **Requirements** ## Advanced File Processing (Prefixes) `masonex` introduces special filename prefixes within the `__brick__` directory to provide fine-grained control over how files are generated or merged: | Prefix | Action | Description | | :--- | :--- | :--- | | `>>>` | **Merge** | Recursively merges iterable variables (Lists, Sets, Maps) in Dart, JSON, and YAML files. | | `>` | **Overwrite** | Always overwrites the target file, regardless of conflict resolution settings. | | `>>` | **Append** | Always appends the content to the end of the existing file. | | `<<` | **Prepend** | Always inserts the content at the beginning of the existing file. | | `!` | **Safe Check** | Only generates the file if it does *not* exist. Skips otherwise. | | `~` | **Temporary** | Generates the file but deletes it immediately after the generation process finishes. | | `?var?` | **Conditional** | Generates the file only if `var` evaluates to `true` or is a non-empty list. | | `*var*` | **Iterator** | Generates multiple files by iterating over the `var` list. Use `{{item}}` in the filename. | | `%id%` | **Snippet** | Marks the file as a fragment for in-file generation points defined by `@GenerateBefore(id)`, etc. | ### Dart Merging Logic When using the `>>>` prefix for `.dart` files: - **Lists**: New elements are appended. - **Sets**: Merged with existing elements, preserving uniqueness. - **Maps**: New keys are added, and existing keys are overwritten with new values. - **Imports**: Missing imports are automatically added to the top of the file. - **Top-level Declarations**: New classes, functions, or variables that don't exist in the target are appended. - **Scoped Merging**: Correctly identifies and merges fields inside classes based on qualified names. ## In-File Generations Use `package:masonex_annotations` to mark points in your source code where templates should be injected. ### Annotations - `@GenerateBefore('id')` - `@GenerateAfter('id')` - `@GenerationMerge('id')` ### Workflow 1. Annotate your Dart code: ```dart @GenerateAfter('plugins'): // {{new_plugin_config}} final plugins = []; ``` 2. Run `mustachex build` to scan the repo and update your `brick.yaml`. 3. When the brick is generated, `masonex` will inject snippets marked with `%id%` into the corresponding annotated points. **Additional Context** I'm working on an extension of mason that uses mustachex due to !57, but it think it would be best to merge both projects
2 条评论