Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/material_ui/tool/gen_defaults/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ This updates generated component theming files under
## Templates

There is a template file for every component that needs defaults from the token
database. These templates are implemented as subclasses of either `M3TokenTemplate` or `M3ETokenTemplate`.
database. These templates are implemented as subclasses of either `TokenTemplateM3` or `TokenTemplateM3E`.

Templates need to override the `generateContents` method to provide the
generated code block as a string.
Expand Down
14 changes: 7 additions & 7 deletions packages/material_ui/tool/gen_defaults/templates/template.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@ import '../data/shape_struct.dart';
enum _MaterialVersion { material3, material3Expressive }

/// A template for generating Material 3 component defaults.
abstract class M3TokenTemplate extends TokenTemplate {
const M3TokenTemplate();
abstract class TokenTemplateM3 extends TokenTemplate {
const TokenTemplateM3();

@override
_MaterialVersion get _version => _MaterialVersion.material3;
}

/// A template for generating Material 3 Expressive component defaults.
abstract class M3ETokenTemplate extends TokenTemplate {
const M3ETokenTemplate();
abstract class TokenTemplateM3E extends TokenTemplate {
const TokenTemplateM3E();

@override
_MaterialVersion get _version => _MaterialVersion.material3Expressive;
Expand Down Expand Up @@ -52,7 +52,7 @@ abstract class TokenTemplate {
static final RegExp _nameRegExp = RegExp(r'^[A-Z][a-zA-Z0-9]*( [A-Z][a-zA-Z0-9]*)*$');

/// The name of the template, which corresponds to the target file name.
/// E.g., 'Icon Button' for generating 'icon_button_m3_defaults.g.dart'.
/// E.g., 'Icon Button' for generating 'icon_button_defaults_m3.g.dart'.
String get name;

/// The path of the parent file relative to `lib/src`.
Expand Down Expand Up @@ -154,8 +154,8 @@ abstract class TokenTemplate {
void generateFile({bool verbose = false}) {
final String snakeName = name.toLowerCase().replaceAll(' ', '_');
final String outputFileName = switch (_version) {
_MaterialVersion.material3 => '${snakeName}_m3_defaults.g.dart',
_MaterialVersion.material3Expressive => '${snakeName}_m3e_defaults.g.dart',
_MaterialVersion.material3 => '${snakeName}_defaults_m3.g.dart',
_MaterialVersion.material3Expressive => '${snakeName}_defaults_m3e.g.dart',
};
final fileName = '$materialLib/$outputFileName';
if (verbose) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,16 @@ void main() {

for (final isM3E in <bool>[true, false]) {
TokenTemplate buttonTemplate() =>
isM3E ? M3EIconButtonTemplate(testPath()) : M3IconButtonTemplate(testPath());
isM3E ? IconButtonTemplateM3E(testPath()) : IconButtonTemplateM3(testPath());

String filePath() {
final fileName = 'icon_button_m3${isM3E ? 'e' : ''}_defaults.g.dart';
final fileName = 'icon_button_defaults_m3${isM3E ? 'e' : ''}.g.dart';
return '${testPath()}/$fileName';
}

group(isM3E ? 'M3E Template' : 'M3 Template', () {
test(
'will generate a part file ending in icon_button_m3${isM3E ? 'e' : ''}_defaults.g.dart',
'will generate a part file ending in icon_button_defaults_m3${isM3E ? 'e' : ''}.g.dart',
() {
buttonTemplate().generateFile(verbose: true);
expect(File(filePath()).existsSync(), isTrue);
Expand Down Expand Up @@ -69,12 +69,12 @@ void main() {
}

test('color generates color expression', () {
final template = M3IconButtonTemplate(testPath());
final template = IconButtonTemplateM3(testPath());
expect(template.color(TokenColorRole.onSurface, '_colors'), '_colors.onSurface');
});

test('colorWithOpacity generates color expression with opacity', () {
final template = M3IconButtonTemplate(testPath());
final template = IconButtonTemplateM3(testPath());
expect(
template.colorWithOpacity(TokenColorRole.onSurface, 0.12, '_colors'),
'_colors.onSurface.withOpacity(0.12)',
Expand All @@ -86,7 +86,7 @@ void main() {
});

test('shape generates shape expressions', () {
final template = M3IconButtonTemplate(testPath());
final template = IconButtonTemplateM3(testPath());
expect(
template.shape(
const ShapeStruct(
Expand Down Expand Up @@ -126,7 +126,7 @@ void main() {
});

test('shape throws UnsupportedError for unsupported shape family', () {
final template = M3IconButtonTemplate(testPath());
final template = IconButtonTemplateM3(testPath());
expect(
() => template.shape(
const ShapeStruct(
Expand All @@ -146,12 +146,11 @@ void main() {
),
);
});

test('will run dart format over the generated file', () {
Comment on lines 148 to 149

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

For better readability and consistency with the rest of the test suite, please maintain a blank line between the test blocks.

Suggested change
});
test('will run dart format over the generated file', () {
});
test('will run dart format over the generated file', () {

final template = UnformattedTemplate(testPath());
template.generateFile();

final file = File('${testPath()}/unformatted_m3_defaults.g.dart');
final file = File('${testPath()}/unformatted_defaults_m3.g.dart');
expect(file.readAsStringSync(), contains(formattedClass));
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
import '../../templates/template.dart';
import 'icon_button_token_data.dart';

class M3EIconButtonTemplate extends M3ETokenTemplate {
M3EIconButtonTemplate(this.customMaterialLib);
class IconButtonTemplateM3E extends TokenTemplateM3E {
IconButtonTemplateM3E(this.customMaterialLib);

final String customMaterialLib;

Expand All @@ -30,8 +30,8 @@ class $className {
}
}

class M3IconButtonTemplate extends M3TokenTemplate {
M3IconButtonTemplate(this.customMaterialLib);
class IconButtonTemplateM3 extends TokenTemplateM3 {
IconButtonTemplateM3(this.customMaterialLib);

final String customMaterialLib;

Expand Down Expand Up @@ -66,7 +66,7 @@ class $className {
}
}

class UnformattedTemplate extends M3TokenTemplate {
class UnformattedTemplate extends TokenTemplateM3 {
UnformattedTemplate(this.customMaterialLib);

final String customMaterialLib;
Expand All @@ -91,7 +91,7 @@ final int x = 1 ;
}
}

class InvalidTemplate extends M3TokenTemplate {
class InvalidTemplate extends TokenTemplateM3 {
InvalidTemplate(this.customMaterialLib);

final String customMaterialLib;
Expand All @@ -115,7 +115,7 @@ class _SomeOtherClassNameDefaults {
}
}

class SnakeCaseNameTemplate extends M3TokenTemplate {
class SnakeCaseNameTemplate extends TokenTemplateM3 {
SnakeCaseNameTemplate(this.customMaterialLib);

final String customMaterialLib;
Expand Down
Loading