Skip to content

Commit aa99736

Browse files
committed
docs: update CONTRIBUTING.md and README.md to clarify code generation workflow and emphasize not editing generated files directly. Enhance generate.dart to auto-format generated Dart files, ensuring CI compatibility and reducing diffs
1 parent 8a35357 commit aa99736

File tree

3 files changed

+60
-32
lines changed

3 files changed

+60
-32
lines changed

CONTRIBUTING.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,36 @@ interested in making contributions:
1717
4. If there are any changes that developers should be aware of, please update
1818
the [changelog](https://github.com/maplibre/flutter-maplibre-gl/blob/master/CHANGELOG.md)
1919
once your pull request has been merged to the `main` branch.
20+
21+
## Code Generation & Formatting
22+
23+
Some parts of the public API (layer & source property helpers, expression utilities, etc.) are **generated**.
24+
25+
Do not edit generated Dart / Java / Swift files manually. Instead:
26+
27+
1. Make or adjust templates / generator logic under `scripts/` (main entry: `scripts/lib/generate.dart`).
28+
29+
2. Activate Melos and run a clean bootstrap:
30+
```bash
31+
dart pub global activate melos && melos clean && melos bootstrap
32+
```
33+
3. Run the generator:
34+
```bash
35+
melos run generate
36+
```
37+
4. (Optional) Run the workspace formatter (Dart files generated are already batch‑formatted automatically):
38+
```bash
39+
melos format-all
40+
```
41+
5. Review changes:
42+
```bash
43+
git diff
44+
```
45+
6. Stage & commit if everything looks correct.
46+
47+
Notes:
48+
- The generator itself batch‑formats newly created Dart files using `dart format` so CI should not introduce extra diffs.
49+
- Running `melos format-all` afterward is still fine (idempotent) and catches accidental manual edits elsewhere.
50+
- Never hand‑edit generated files: your edits will be overwritten the next time the generator runs.
51+
52+
If you add new style specification fields, extend the mapping logic in `scripts/lib/conversions.dart` and/or templates under `scripts/templates/`.

maplibre_gl/README.md

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -319,18 +319,14 @@ but it seems like the expression still works well.
319319
## Contributing
320320

321321
Setup [melos](https://melos.invertase.dev/~melos-latest/getting-started) and run
322-
the
323-
324-
```bash
325-
melos bootstrap
326-
```
327-
328-
command in the plugin root directory. Run the example app and familiarize
329-
yourself with the plugin directory structure.
322+
the `melos bootstrap` command in the plugin root directory.\
323+
Run the example app and familiarize yourself with the plugin directory structure.
330324

331325
[Feedback](https://github.com/maplibre/flutter-maplibre-gl/issues), contributing
332326
pull requests
333327
and [bug reports](https://github.com/maplibre/flutter-maplibre-gl/issues) are
334328
very welcome - check
335329
the [CONTRIBUTING.md](https://github.com/maplibre/flutter-maplibre-gl/blob/main/CONTRIBUTING.md)
336330
guidelines.
331+
332+
**Generated code**: Some API surface (layer/source property helpers, expression utilities) is produced via a generator under `scripts/`. Do not modify generated files directly—see the Code Generation & Formatting section in `CONTRIBUTING.md` for the workflow (`melos run generate` then `melos format-all`).

scripts/lib/generate.dart

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
// Key goals:
88
// - Keep hand‑written logic small; most surface area is generated.
99
// - Ensure deterministic output (stable ordering helps minimal diffs).
10-
// - Apply formatting immediately so CI "format-all" never fails after generation.
10+
// - Generated Dart files are batch‑formatted at the end using `dart format`
11+
// so that a subsequent CI `melos format-all` step produces no diffs.
1112
//
1213
// Notes:
1314
// - Do NOT edit the generated files manually; instead adjust templates or
@@ -101,15 +102,33 @@ Future<void> main() async {
101102
"maplibre_gl_platform_interface/lib/src/source_properties.dart",
102103
];
103104

105+
final generatedDartFiles = <String>[];
104106
for (final template in templates) {
105-
await render(renderContext, template);
107+
final path = await render(renderContext, template);
108+
if (path.endsWith('.dart')) {
109+
generatedDartFiles.add(path);
110+
}
111+
}
112+
113+
// Auto-format only the Dart files we just generated so that a subsequent
114+
// CI `melos format-all` step does not introduce extra diffs.
115+
if (generatedDartFiles.isNotEmpty) {
116+
final result = await Process.run(
117+
'dart',
118+
['format', ...generatedDartFiles],
119+
runInShell: true,
120+
);
121+
if (result.exitCode != 0) {
122+
stderr.writeln('Warning: dart format failed: ${result.stderr}');
123+
}
106124
}
107125
}
108126

109127
/// Render a single template file.
110128
/// [path] is the relative workspace path to the output (and indirectly the
111129
/// template at scripts/templates/$filename.template).
112-
Future<void> render(
130+
/// Returns the absolute path of the written file.
131+
Future<String> render(
113132
Map<String, List> renderContext,
114133
String path,
115134
) async {
@@ -128,28 +147,8 @@ Future<void> render(
128147
final outputFile = File('$outputPath/$filename');
129148

130149
final rendered = template.renderString(renderContext);
131-
132150
await outputFile.writeAsString(rendered);
133-
134-
// For Dart targets, run the official formatter to ensure perfect parity
135-
// with `dart format` (used by melos format-all in CI). Doing this instead
136-
// of using the dart_style API avoids subtle version / option drift.
137-
if (filename.endsWith('.dart')) {
138-
try {
139-
final result = await Process.run(
140-
'dart',
141-
['format', outputFile.path],
142-
runInShell: true,
143-
);
144-
if (result.exitCode != 0) {
145-
stderr.writeln(
146-
'Warning: dart format failed for ${outputFile.path}: ${result.stderr}',
147-
);
148-
}
149-
} catch (e) {
150-
stderr.writeln('Warning: spawning dart format failed for $filename: $e');
151-
}
152-
}
151+
return outputFile.path;
153152
}
154153

155154
/// Build the (paint/layout) style properties list for a given style.json key.

0 commit comments

Comments
 (0)