@@ -8,65 +8,107 @@ import 'package:meta/meta.dart';
88
99enum _MaterialVersion { material3, material3Expressive }
1010
11- abstract class M3TokenTemplate extends _TokenTemplate {
11+ /// A template for generating Material 3 component defaults.
12+ abstract class M3TokenTemplate extends TokenTemplate {
1213 const M3TokenTemplate ();
1314
1415 @override
1516 _MaterialVersion get _version => _MaterialVersion .material3;
1617}
1718
18- abstract class M3ETokenTemplate extends _TokenTemplate {
19+ /// A template for generating Material 3 Expressive component defaults.
20+ abstract class M3ETokenTemplate extends TokenTemplate {
1921 const M3ETokenTemplate ();
2022
2123 @override
2224 _MaterialVersion get _version => _MaterialVersion .material3Expressive;
2325}
2426
25- abstract class _TokenTemplate {
26- const _TokenTemplate ();
27+ @visibleForTesting
28+ abstract class TokenTemplate {
29+ const TokenTemplate ();
2730
28- static const String copyrightHeader = '''
31+ /// The copyright header prepended to all generated defaults files.
32+ static const String _copyrightHeader = '''
2933// Copyright 2013 The Flutter Authors
3034// Use of this source code is governed by a BSD-style license that can be
3135// found in the LICENSE file.
3236''' ;
3337
34- static const String headerComment = '''
38+ /// The warning comment prepended to all generated defaults files informing
39+ /// readers not to edit them by hand.
40+ static const String _headerComment = '''
3541
3642// Do not edit by hand. The code is generated from data in the Material
3743// Design token database by the script:
3844// packages/material_ui/tool/gen_defaults/bin/gen_defaults.dart.
3945''' ;
4046
41- /// The Material version this template is for.
42- _MaterialVersion get _version;
47+ /// The regular expression used to verify that a template name is written with
48+ /// spaces and capitalized words (e.g., "Icon Button").
49+ static final RegExp _nameRegExp = RegExp (r'^[A-Z][a-zA-Z0-9]*( [A-Z][a-zA-Z0-9]*)*$' );
4350
4451 /// The name of the template, which corresponds to the target file name.
45- /// E.g., 'typography ' for generating 'typography_defaults .g.dart'.
52+ /// E.g., 'Icon Button ' for generating 'icon_button_m3_defaults .g.dart'.
4653 String get name;
4754
55+ /// The path of the parent file relative to `lib/src` .
56+ /// E.g., 'icon_button.dart' or 'buttons/icon_button.dart'.
57+ String get parentFilePath;
58+
59+ /// The path to the library's directory where generated files are placed.
4860 @visibleForTesting
4961 String get materialLib {
5062 const packagePath = 'packages/material_ui' ;
5163 const generatedDirectory = 'lib/src/generated' ;
52- final String relativeOutputPath = switch (_version) {
53- _MaterialVersion .material3 => generatedDirectory,
54- _MaterialVersion .material3Expressive => '$generatedDirectory /material_3_expressive' ,
55- };
5664 if (Directory (packagePath).existsSync ()) {
57- return '$packagePath /$relativeOutputPath ' ;
65+ return '$packagePath /$generatedDirectory ' ;
5866 }
59- return relativeOutputPath ;
67+ return generatedDirectory ;
6068 }
6169
62- String generateContents ();
70+ /// The Material version this template is for.
71+ _MaterialVersion get _version;
6372
73+ /// The name of the class that will be generated (e.g. `_M3IconButtonDefaults`
74+ /// or `_M3EIconButtonDefaults` ).
75+ String get _className {
76+ assert (
77+ _nameRegExp.hasMatch (name),
78+ 'The template name "$name " must use spaces and capitalized words (e.g., "Typography" or "Icon Button").' ,
79+ );
80+ final String camelName = name.replaceAll (' ' , '' );
81+ return switch (_version) {
82+ _MaterialVersion .material3 => '_M3${camelName }Defaults' ,
83+ _MaterialVersion .material3Expressive => '_M3E${camelName }Defaults' ,
84+ };
85+ }
86+
87+ /// The regular expression used to verify that the generated contents declare
88+ /// the class.
89+ RegExp get _classRegExp => RegExp ('class\\ s+$_className \\ b' );
90+
91+ /// Returns the body of the generated file as a string.
92+ ///
93+ /// The [className] parameter must be used to declare the class.
94+ String generateContents (String className);
95+
96+ /// Generates the file under the target path [materialLib] and formats it.
6497 void generateFile ({bool verbose = false }) {
65- final fileName = '$materialLib /${name }_defaults.g.dart' ;
98+ final String snakeName = name.toLowerCase ().replaceAll (' ' , '_' );
99+ final String outputFileName = switch (_version) {
100+ _MaterialVersion .material3 => '${snakeName }_m3_defaults.g.dart' ,
101+ _MaterialVersion .material3Expressive => '${snakeName }_m3e_defaults.g.dart' ,
102+ };
103+ final fileName = '$materialLib /$outputFileName ' ;
66104 if (verbose) {
67105 stdout.writeln ('Generating file: $fileName ' );
68- stdout.writeln ('Target parent file name: $ name .dart ' );
106+ stdout.writeln ('Target parent file path: lib/src/$ parentFilePath ' );
69107 }
108+ assert (
109+ ! parentFilePath.startsWith ('/' ) && ! parentFilePath.startsWith ('lib/' ),
110+ 'parentFilePath must be relative to lib/src/ (e.g. "icon_button.dart").' ,
111+ );
70112 final file = File (fileName);
71113 if (! file.existsSync ()) {
72114 if (verbose) {
@@ -75,20 +117,21 @@ abstract class _TokenTemplate {
75117 file.createSync (recursive: true );
76118 }
77119
78- final parentName = '$name .dart' ;
79-
80120 if (verbose) {
81121 stdout.writeln ('Generating contents...' );
82122 }
123+ final String contents = generateContents (_className);
124+ assert (
125+ contents.contains (_classRegExp),
126+ 'The generated contents for "$name " must define the class "$_className ". '
127+ 'Make sure you are utilizing the passed `className` parameter.' ,
128+ );
129+
83130 final buffer = StringBuffer ();
84- buffer.write (copyrightHeader);
85- buffer.write (headerComment);
86- final String partOfPath = switch (_version) {
87- _MaterialVersion .material3 => '../$parentName ' ,
88- _MaterialVersion .material3Expressive => '../../material_3_expressive/$parentName ' ,
89- };
90- buffer.write ("part of '$partOfPath ';\n\n " );
91- buffer.write (generateContents ());
131+ buffer.write (_copyrightHeader);
132+ buffer.write (_headerComment);
133+ buffer.write ("part of '../$parentFilePath ';\n\n " );
134+ buffer.write (contents);
92135
93136 if (verbose) {
94137 stdout.writeln ('Writing generated contents to $fileName ...' );
0 commit comments