Skip to content

Commit 9abffdc

Browse files
committed
chore: enhance generate script with Dart formatting support
1 parent 2cdca77 commit 9abffdc

File tree

4 files changed

+42
-20
lines changed

4 files changed

+42
-20
lines changed
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
1-
include: ../analysis_options.yaml
1+
include: ../analysis_options.yaml
2+
analyzer:
3+
errors:
4+
unintended_html_in_doc_comment: ignore

maplibre_gl_platform_interface/lib/src/source_properties.dart

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// @dart=3.9
2+
13
// This file is generated by
24
// ./scripts/lib/generate.dart
35

@@ -63,8 +65,8 @@ class VectorSourceProperties implements SourceProperties {
6365
final String? attribution;
6466

6567
/// A property to use as a feature id (for feature state). Either a
66-
/// property name, or an object of the form `{sourceLayer:
67-
/// propertyName}`. If specified as a string for a vector tile source,
68+
/// property name, or an object of the form `{<sourceLayer>:
69+
/// <propertyName>}`. If specified as a string for a vector tile source,
6870
/// the same property is used across all its source layers.
6971
///
7072
/// Type: promoteId
@@ -607,15 +609,9 @@ class VideoSourceProperties implements SourceProperties {
607609
/// Type: array
608610
final List<List>? coordinates;
609611

610-
const VideoSourceProperties({
611-
this.urls,
612-
this.coordinates,
613-
});
612+
const VideoSourceProperties({this.urls, this.coordinates});
614613

615-
VideoSourceProperties copyWith(
616-
List<String>? urls,
617-
List<List>? coordinates,
618-
) {
614+
VideoSourceProperties copyWith(List<String>? urls, List<List>? coordinates) {
619615
return VideoSourceProperties(
620616
urls: urls ?? this.urls,
621617
coordinates: coordinates ?? this.coordinates,
@@ -657,15 +653,9 @@ class ImageSourceProperties implements SourceProperties {
657653
/// Type: array
658654
final List<List>? coordinates;
659655

660-
const ImageSourceProperties({
661-
this.url,
662-
this.coordinates,
663-
});
656+
const ImageSourceProperties({this.url, this.coordinates});
664657

665-
ImageSourceProperties copyWith(
666-
String? url,
667-
List<List>? coordinates,
668-
) {
658+
ImageSourceProperties copyWith(String? url, List<List>? coordinates) {
669659
return ImageSourceProperties(
670660
url: url ?? this.url,
671661
coordinates: coordinates ?? this.coordinates,

scripts/lib/generate.dart

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import 'dart:convert';
33

44
import 'package:mustache_template/mustache_template.dart';
55
import 'package:recase/recase.dart';
6+
import 'package:dart_style/dart_style.dart';
7+
import 'package:pub_semver/pub_semver.dart';
68

79
import 'conversions.dart';
810

@@ -91,7 +93,33 @@ Future<void> render(
9193
final template = Template(templateFile);
9294
final outputFile = File('$outputPath/$filename');
9395

94-
outputFile.writeAsString(template.renderString(renderContext));
96+
var rendered = template.renderString(renderContext);
97+
98+
// Format Dart files so that subsequent `dart format --set-exit-if-changed` doesn't fail (in CI).
99+
if (filename.endsWith('.dart')) {
100+
try {
101+
// Determine language version from current Dart SDK (major.minor)
102+
final sdkVersion = Platform.version.split(' ').first; // e.g. 3.5.2
103+
final versionParts = sdkVersion.split('.');
104+
final languageVersion =
105+
'${versionParts.elementAt(0)}.${versionParts.elementAt(1)}';
106+
107+
// Prepend language version pragma if not already present
108+
if (!rendered.startsWith('// @dart=')) {
109+
rendered = '// @dart=$languageVersion\n\n$rendered';
110+
}
111+
112+
// Pass the version to the formatter so it can format accordingly
113+
final versionObj = Version.parse('$languageVersion.0');
114+
final formatter = DartFormatter(languageVersion: versionObj);
115+
rendered = formatter.format(rendered);
116+
} catch (e) {
117+
// If formatting fails, keep the unformatted content but log a warning.
118+
stderr.writeln('Warning: dart_style failed for $filename: $e');
119+
}
120+
}
121+
122+
await outputFile.writeAsString(rendered);
95123
}
96124

97125
List<Map<String, dynamic>> buildStyleProperties(

scripts/pubspec.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ environment:
99
sdk: ">=3.5.0 <4.0.0"
1010

1111
dependencies:
12+
dart_style: ^3.1.2
1213
mustache_template: ^2.0.1
1314
recase: ^4.1.0
1415

0 commit comments

Comments
 (0)