1 | // Copyright 2014 The Flutter Authors. All rights reserved. |
2 | // Use of this source code is governed by a BSD-style license that can be |
3 | // found in the LICENSE file. |
4 | |
5 | // ## Usage |
6 | // |
7 | // Run this program from the root of the git repository. |
8 | // |
9 | // ``` |
10 | // dart dev/tools/gen_defaults/bin/gen_defaults.dart [-v] |
11 | // ``` |
12 | |
13 | import 'dart:convert'; |
14 | import 'dart:io'; |
15 | |
16 | import 'package:args/args.dart' ; |
17 | import 'package:gen_defaults/action_chip_template.dart' ; |
18 | import 'package:gen_defaults/app_bar_template.dart' ; |
19 | import 'package:gen_defaults/badge_template.dart' ; |
20 | import 'package:gen_defaults/banner_template.dart' ; |
21 | import 'package:gen_defaults/bottom_app_bar_template.dart' ; |
22 | import 'package:gen_defaults/bottom_sheet_template.dart' ; |
23 | import 'package:gen_defaults/button_template.dart' ; |
24 | import 'package:gen_defaults/card_template.dart' ; |
25 | import 'package:gen_defaults/checkbox_template.dart' ; |
26 | import 'package:gen_defaults/chip_template.dart' ; |
27 | import 'package:gen_defaults/color_scheme_template.dart' ; |
28 | import 'package:gen_defaults/date_picker_template.dart' ; |
29 | import 'package:gen_defaults/dialog_template.dart' ; |
30 | import 'package:gen_defaults/divider_template.dart' ; |
31 | import 'package:gen_defaults/drawer_template.dart' ; |
32 | import 'package:gen_defaults/expansion_tile_template.dart' ; |
33 | import 'package:gen_defaults/fab_template.dart' ; |
34 | import 'package:gen_defaults/filter_chip_template.dart' ; |
35 | import 'package:gen_defaults/icon_button_template.dart' ; |
36 | import 'package:gen_defaults/input_chip_template.dart' ; |
37 | import 'package:gen_defaults/input_decorator_template.dart' ; |
38 | import 'package:gen_defaults/list_tile_template.dart' ; |
39 | import 'package:gen_defaults/menu_template.dart' ; |
40 | import 'package:gen_defaults/motion_template.dart' ; |
41 | import 'package:gen_defaults/navigation_bar_template.dart' ; |
42 | import 'package:gen_defaults/navigation_drawer_template.dart' ; |
43 | import 'package:gen_defaults/navigation_rail_template.dart' ; |
44 | import 'package:gen_defaults/popup_menu_template.dart' ; |
45 | import 'package:gen_defaults/progress_indicator_template.dart' ; |
46 | import 'package:gen_defaults/radio_template.dart' ; |
47 | import 'package:gen_defaults/search_bar_template.dart' ; |
48 | import 'package:gen_defaults/search_view_template.dart' ; |
49 | import 'package:gen_defaults/segmented_button_template.dart' ; |
50 | import 'package:gen_defaults/snackbar_template.dart' ; |
51 | import 'package:gen_defaults/surface_tint.dart' ; |
52 | import 'package:gen_defaults/switch_template.dart' ; |
53 | import 'package:gen_defaults/tabs_template.dart' ; |
54 | import 'package:gen_defaults/text_field_template.dart' ; |
55 | import 'package:gen_defaults/time_picker_template.dart' ; |
56 | import 'package:gen_defaults/token_logger.dart' ; |
57 | import 'package:gen_defaults/typography_template.dart' ; |
58 | |
59 | Map<String, dynamic> _readTokenFile(File file) { |
60 | return jsonDecode(file.readAsStringSync()) as Map<String, dynamic>; |
61 | } |
62 | |
63 | const String materialLib = 'packages/flutter/lib/src/material' ; |
64 | const String dataDir = 'dev/tools/gen_defaults/data' ; |
65 | |
66 | Future<void> main(List<String> args) async { |
67 | // Parse arguments |
68 | final ArgParser parser = ArgParser(); |
69 | parser.addFlag( |
70 | 'verbose' , |
71 | abbr: 'v' , |
72 | help: 'Enable verbose output' , |
73 | negatable: false, |
74 | ); |
75 | final ArgResults argResults = parser.parse(args); |
76 | final bool verbose = argResults['verbose' ] as bool; |
77 | |
78 | // Map of version number to list of data files that use that version. |
79 | final Map<String, List<String>> versionMap = <String, List<String>>{}; |
80 | // Map of all tokens to their values. |
81 | final Map<String, dynamic> tokens = <String, dynamic>{}; |
82 | |
83 | // Initialize. |
84 | for (final FileSystemEntity tokenFile in Directory(dataDir).listSync()) { |
85 | final Map<String, dynamic> tokenFileTokens = _readTokenFile(tokenFile as File); |
86 | final String version = tokenFileTokens['version' ] as String; |
87 | tokenFileTokens.remove('version' ); |
88 | versionMap[version] ??= <String>[]; |
89 | versionMap[version]!.add(tokenFile.uri.pathSegments.last); |
90 | |
91 | tokens.addAll(tokenFileTokens); |
92 | } |
93 | tokenLogger.init(allTokens: tokens, versionMap: versionMap); |
94 | // Handle light/dark color tokens separately because they share identical token names. |
95 | final Map<String, dynamic> colorLightTokens = _readTokenFile(File(' $dataDir/color_light.json' )); |
96 | final Map<String, dynamic> colorDarkTokens = _readTokenFile(File(' $dataDir/color_dark.json' )); |
97 | |
98 | // Generate tokens files. |
99 | ChipTemplate('Chip' , ' $materialLib/chip.dart' , tokens).updateFile(); |
100 | ActionChipTemplate('ActionChip' , ' $materialLib/action_chip.dart' , tokens).updateFile(); |
101 | AppBarTemplate('AppBar' , ' $materialLib/app_bar.dart' , tokens).updateFile(); |
102 | BottomAppBarTemplate('BottomAppBar' , ' $materialLib/bottom_app_bar.dart' , tokens).updateFile(); |
103 | BadgeTemplate('Badge' , ' $materialLib/badge.dart' , tokens).updateFile(); |
104 | BannerTemplate('Banner' , ' $materialLib/banner.dart' , tokens).updateFile(); |
105 | BottomAppBarTemplate('BottomAppBar' , ' $materialLib/bottom_app_bar.dart' , tokens).updateFile(); |
106 | BottomSheetTemplate('BottomSheet' , ' $materialLib/bottom_sheet.dart' , tokens).updateFile(); |
107 | ButtonTemplate('md.comp.elevated-button' , 'ElevatedButton' , ' $materialLib/elevated_button.dart' , tokens).updateFile(); |
108 | ButtonTemplate('md.comp.filled-button' , 'FilledButton' , ' $materialLib/filled_button.dart' , tokens).updateFile(); |
109 | ButtonTemplate('md.comp.filled-tonal-button' , 'FilledTonalButton' , ' $materialLib/filled_button.dart' , tokens).updateFile(); |
110 | ButtonTemplate('md.comp.outlined-button' , 'OutlinedButton' , ' $materialLib/outlined_button.dart' , tokens).updateFile(); |
111 | ButtonTemplate('md.comp.text-button' , 'TextButton' , ' $materialLib/text_button.dart' , tokens).updateFile(); |
112 | CardTemplate('md.comp.elevated-card' , 'Card' , ' $materialLib/card.dart' , tokens).updateFile(); |
113 | CardTemplate('md.comp.filled-card' , 'FilledCard' , ' $materialLib/card.dart' , tokens).updateFile(); |
114 | CardTemplate('md.comp.outlined-card' , 'OutlinedCard' , ' $materialLib/card.dart' , tokens).updateFile(); |
115 | CheckboxTemplate('Checkbox' , ' $materialLib/checkbox.dart' , tokens).updateFile(); |
116 | ColorSchemeTemplate(colorLightTokens, colorDarkTokens, 'ColorScheme' , ' $materialLib/theme_data.dart' , tokens).updateFile(); |
117 | DatePickerTemplate('DatePicker' , ' $materialLib/date_picker_theme.dart' , tokens).updateFile(); |
118 | DialogFullscreenTemplate('DialogFullscreen' , ' $materialLib/dialog.dart' , tokens).updateFile(); |
119 | DialogTemplate('Dialog' , ' $materialLib/dialog.dart' , tokens).updateFile(); |
120 | DividerTemplate('Divider' , ' $materialLib/divider.dart' , tokens).updateFile(); |
121 | DrawerTemplate('Drawer' , ' $materialLib/drawer.dart' , tokens).updateFile(); |
122 | ExpansionTileTemplate('ExpansionTile' , ' $materialLib/expansion_tile.dart' , tokens).updateFile(); |
123 | FABTemplate('FAB' , ' $materialLib/floating_action_button.dart' , tokens).updateFile(); |
124 | FilterChipTemplate('ChoiceChip' , ' $materialLib/choice_chip.dart' , tokens).updateFile(); |
125 | FilterChipTemplate('FilterChip' , ' $materialLib/filter_chip.dart' , tokens).updateFile(); |
126 | IconButtonTemplate('md.comp.icon-button' , 'IconButton' , ' $materialLib/icon_button.dart' , tokens).updateFile(); |
127 | IconButtonTemplate('md.comp.filled-icon-button' , 'FilledIconButton' , ' $materialLib/icon_button.dart' , tokens).updateFile(); |
128 | IconButtonTemplate('md.comp.filled-tonal-icon-button' , 'FilledTonalIconButton' , ' $materialLib/icon_button.dart' , tokens).updateFile(); |
129 | IconButtonTemplate('md.comp.outlined-icon-button' , 'OutlinedIconButton' , ' $materialLib/icon_button.dart' , tokens).updateFile(); |
130 | InputChipTemplate('InputChip' , ' $materialLib/input_chip.dart' , tokens).updateFile(); |
131 | ListTileTemplate('LisTile' , ' $materialLib/list_tile.dart' , tokens).updateFile(); |
132 | InputDecoratorTemplate('InputDecorator' , ' $materialLib/input_decorator.dart' , tokens).updateFile(); |
133 | MenuTemplate('Menu' , ' $materialLib/menu_anchor.dart' , tokens).updateFile(); |
134 | MotionTemplate('Motion' , ' $materialLib/motion.dart' , tokens, tokenLogger).updateFile(); |
135 | NavigationBarTemplate('NavigationBar' , ' $materialLib/navigation_bar.dart' , tokens).updateFile(); |
136 | NavigationDrawerTemplate('NavigationDrawer' , ' $materialLib/navigation_drawer.dart' , tokens).updateFile(); |
137 | NavigationRailTemplate('NavigationRail' , ' $materialLib/navigation_rail.dart' , tokens).updateFile(); |
138 | PopupMenuTemplate('PopupMenu' , ' $materialLib/popup_menu.dart' , tokens).updateFile(); |
139 | ProgressIndicatorTemplate('ProgressIndicator' , ' $materialLib/progress_indicator.dart' , tokens).updateFile(); |
140 | RadioTemplate('Radio<T>' , ' $materialLib/radio.dart' , tokens).updateFile(); |
141 | SearchBarTemplate('SearchBar' , ' $materialLib/search_anchor.dart' , tokens).updateFile(); |
142 | SearchViewTemplate('SearchView' , ' $materialLib/search_anchor.dart' , tokens).updateFile(); |
143 | SegmentedButtonTemplate('md.comp.outlined-segmented-button' , 'SegmentedButton' , ' $materialLib/segmented_button.dart' , tokens).updateFile(); |
144 | SnackbarTemplate('md.comp.snackbar' , 'Snackbar' , ' $materialLib/snack_bar.dart' , tokens).updateFile(); |
145 | // TODO(QuncCccccc): uncomment `SliderTemplate` once `Slider` widget is updated to match the latest M3 specs. |
146 | // SliderTemplate('md.comp.slider', 'Slider', '$materialLib/slider.dart', tokens).updateFile(); |
147 | SurfaceTintTemplate('SurfaceTint' , ' $materialLib/elevation_overlay.dart' , tokens).updateFile(); |
148 | SwitchTemplate('Switch' , ' $materialLib/switch.dart' , tokens).updateFile(); |
149 | TimePickerTemplate('TimePicker' , ' $materialLib/time_picker.dart' , tokens).updateFile(); |
150 | TextFieldTemplate('TextField' , ' $materialLib/text_field.dart' , tokens).updateFile(); |
151 | TabsTemplate('Tabs' , ' $materialLib/tabs.dart' , tokens).updateFile(); |
152 | TypographyTemplate('Typography' , ' $materialLib/typography.dart' , tokens).updateFile(); |
153 | |
154 | tokenLogger.printVersionUsage(verbose: verbose); |
155 | tokenLogger.printTokensUsage(verbose: verbose); |
156 | if (!verbose) { |
157 | print('\nTo see detailed version and token usage, run with --verbose (-v).' ); |
158 | } |
159 | |
160 | tokenLogger.dumpToFile('dev/tools/gen_defaults/generated/used_tokens.csv' ); |
161 | } |
162 | |