forked from jonbhanson/flutter_native_splash
/
cli_commands.dart
329 lines (297 loc) · 13.2 KB
/
cli_commands.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
/// ## Flutter Native Splash
///
/// This is the main entry point for the Flutter Native Splash package.
library flutter_native_splash_cli;
import 'package:image/image.dart';
import 'package:meta/meta.dart';
import 'package:path/path.dart' as p;
import 'package:universal_io/io.dart';
import 'package:xml/xml.dart';
import 'package:yaml/yaml.dart';
part 'android.dart';
part 'constants.dart';
part 'flavor_helper.dart';
part 'ios.dart';
part 'templates.dart';
part 'web.dart';
late _FlavorHelper _flavorHelper;
/// Create splash screens for Android and iOS
void createSplash({String? path, String? flavor}) {
if (flavor != null) {
print(
'''
╔════════════════════════════════════════════════════════════════════════════╗
║ Flavor detected! ║
╠════════════════════════════════════════════════════════════════════════════╣
║ Setting up the $flavor flavor.
╚════════════════════════════════════════════════════════════════════════════╝
''',
);
}
// It is important that the flavor setup occurs as soon as possible.
// So before we generate anything, we need to setup the flavor (even if it's the default one).
_flavorHelper = _FlavorHelper(flavor);
final config = getConfig(configFile: path);
createSplashByConfig(config);
}
/// Create splash screens for Android and iOS based on a config argument
void createSplashByConfig(Map<String, dynamic> config) {
// Preparing all the data for later usage
final String? image = _checkImageExists(config: config, parameter: 'image');
final String? imageAndroid =
_checkImageExists(config: config, parameter: 'image_android');
final String? imageIos =
_checkImageExists(config: config, parameter: 'image_ios');
final String? imageWeb =
_checkImageExists(config: config, parameter: 'image_web');
final String? darkImage =
_checkImageExists(config: config, parameter: 'image_dark');
final String? darkImageAndroid =
_checkImageExists(config: config, parameter: 'image_dark_android');
final String? darkImageIos =
_checkImageExists(config: config, parameter: 'image_dark_ios');
final String? darkImageWeb =
_checkImageExists(config: config, parameter: 'image_dark_web');
final String? brandingImage =
_checkImageExists(config: config, parameter: 'branding');
final String? brandingImageAndroid =
_checkImageExists(config: config, parameter: 'branding_android');
final String? brandingImageIos =
_checkImageExists(config: config, parameter: 'branding_ios');
final String? brandingDarkImage =
_checkImageExists(config: config, parameter: 'branding_dark');
final String? brandingDarkImageAndroid =
_checkImageExists(config: config, parameter: 'branding_dark_android');
final String? brandingDarkImageIos =
_checkImageExists(config: config, parameter: 'branding_dark_ios');
final String? color = parseColor(config['color']);
final String? darkColor = parseColor(config['color_dark']);
final String? backgroundImage =
_checkImageExists(config: config, parameter: 'background_image');
final String? backgroundImageAndroid =
_checkImageExists(config: config, parameter: 'background_android');
final String? backgroundImageIos =
_checkImageExists(config: config, parameter: 'background_ios');
final String? backgroundImageWeb =
_checkImageExists(config: config, parameter: 'background_web');
final String? darkBackgroundImage =
_checkImageExists(config: config, parameter: 'background_image_dark');
final String? darkBackgroundImageAndroid = _checkImageExists(
config: config, parameter: 'background_image_dark_android');
final String? darkBackgroundImageIos =
_checkImageExists(config: config, parameter: 'background_image_dark_ios');
final String? darkBackgroundImageWeb =
_checkImageExists(config: config, parameter: 'background_image_dark_web');
final plistFiles = config['info_plist_files'] as List<String>?;
String gravity = (config['fill'] as bool? ?? false) ? 'fill' : 'center';
if (config['android_gravity'] != null) {
gravity = config['android_gravity'] as String;
}
final String? androidScreenOrientation =
config['android_screen_orientation'] as String?;
final brandingGravity = config['branding_mode'] as String? ?? 'bottom';
final bool fullscreen = config['fullscreen'] as bool? ?? false;
final String iosContentMode =
config['ios_content_mode'] as String? ?? 'center';
final webImageMode = config['web_image_mode'] as String? ?? 'center';
String? android12Image;
String? android12DarkImage;
String? android12IconBackgroundColor;
String? darkAndroid12IconBackgroundColor;
String? android12Color;
String? android12DarkColor;
if (config['android_12'] != null) {
final android12Config = config['android_12'] as Map<String, dynamic>;
android12Image =
_checkImageExists(config: android12Config, parameter: 'image');
android12DarkImage =
_checkImageExists(config: android12Config, parameter: 'image_dark');
android12IconBackgroundColor =
parseColor(android12Config['icon_background_color']);
darkAndroid12IconBackgroundColor =
parseColor(android12Config['icon_background_color_dark']);
android12Color = parseColor(android12Config['color']) ?? color;
android12DarkColor = parseColor(android12Config['color_dark']) ?? darkColor;
}
if (!config.containsKey('android') || config['android'] as bool) {
if (Directory('android').existsSync()) {
_createAndroidSplash(
imagePath: imageAndroid ?? image,
darkImagePath: darkImageAndroid ?? darkImage,
brandingImagePath: brandingImageAndroid ?? brandingImage,
brandingDarkImagePath: brandingDarkImageAndroid ?? brandingDarkImage,
android12ImagePath: android12Image,
android12DarkImagePath: android12DarkImage,
android12IconBackgroundColor: android12IconBackgroundColor,
darkAndroid12IconBackgroundColor: darkAndroid12IconBackgroundColor,
backgroundImage: backgroundImageAndroid ?? backgroundImage,
darkBackgroundImage: darkBackgroundImageAndroid ?? darkBackgroundImage,
color: android12Color ?? color,
darkColor: android12DarkColor ?? darkColor,
gravity: gravity,
brandingGravity: brandingGravity,
fullscreen: fullscreen,
android12DarkBackgroundColor: android12DarkColor,
android12BackgroundColor: android12Color,
screenOrientation: androidScreenOrientation,
);
} else {
print('Android folder not found, skipping Android splash update...');
}
}
if (!config.containsKey('ios') || config['ios'] as bool) {
if (Directory('ios').existsSync()) {
_createiOSSplash(
imagePath: imageIos ?? image,
darkImagePath: darkImageIos ?? darkImage,
backgroundImage: backgroundImageIos ?? backgroundImage,
darkBackgroundImage: darkBackgroundImageIos ?? darkBackgroundImage,
brandingImagePath: brandingImageIos ?? brandingImage,
brandingDarkImagePath: brandingDarkImageIos ?? brandingDarkImage,
color: color,
darkColor: darkColor,
plistFiles: plistFiles,
iosContentMode: iosContentMode,
iosBrandingContentMode: brandingGravity,
fullscreen: fullscreen,
);
} else {
print('iOS folder not found, skipping iOS splash update...');
}
}
if (!config.containsKey('web') || config['web'] as bool) {
if (Directory('web').existsSync()) {
_createWebSplash(
imagePath: imageWeb ?? image,
darkImagePath: darkImageWeb ?? darkImage,
backgroundImage: backgroundImageWeb ?? backgroundImage,
darkBackgroundImage: darkBackgroundImageWeb ?? darkBackgroundImage,
color: color,
darkColor: darkColor,
imageMode: webImageMode,
);
} else {
print('Web folder not found, skipping web splash update...');
}
}
const String _greet = '''
✅ Native splash complete.
Now go finish building something awesome! 💪 You rock! 🤘🤩
Like the package? Please give it a 👍 here: https://pub.dev/packages/flutter_native_splash
''';
const String _whatsNew = '''
╔════════════════════════════════════════════════════════════════════════════╗
║ WHAT IS NEW: ║
╠════════════════════════════════════════════════════════════════════════════╣
║ You can now keep the splash screen up while your app initializes! ║
║ No need for a secondary splash screen anymore. Just use the remove() ║
║ method to remove the splash screen after your initialization is complete. ║
║ Check the docs for more info. ║
╚════════════════════════════════════════════════════════════════════════════╝
''';
print(_whatsNew + _greet);
}
/// Remove any splash screen by setting the default white splash
void removeSplash({String? path, String? flavor}) {
_flavorHelper = _FlavorHelper(flavor);
print("Restoring Flutter's default native splash screen...");
final config = getConfig(configFile: path);
final removeConfig = <String, dynamic>{
'color': '#ffffff',
'color_dark': '#000000'
};
if (config.containsKey('android')) {
removeConfig['android'] = config['android'];
}
if (config.containsKey('ios')) {
removeConfig['ios'] = config['ios'];
}
if (config.containsKey('web')) {
removeConfig['web'] = config['web'];
}
/// Checks if the image that was specified in the config file does exist.
/// If not the developer will receive an error message and the process will exit.
if (config.containsKey('info_plist_files')) {
removeConfig['info_plist_files'] = config['info_plist_files'];
}
createSplashByConfig(removeConfig);
}
String? _checkImageExists({
required Map<String, dynamic> config,
required String parameter,
}) {
final String? image = config[parameter]?.toString();
if (image != null) {
if (image.isNotEmpty && !File(image).existsSync()) {
print(
'The file "$image" set as the parameter "$parameter" was not found.',
);
exit(1);
}
if (p.extension(image).toLowerCase() != '.png') {
print(
'Unsupported file format: $image Your image must be a PNG file.',
);
exit(1);
}
}
return image == '' ? null : image;
}
/// Get config from `pubspec.yaml` or `flutter_native_splash.yaml`
Map<String, dynamic> getConfig({String? configFile}) {
// if `flutter_native_splash.yaml` exists use it as config file, otherwise use `pubspec.yaml`
String filePath;
if (configFile != null) {
if (File(configFile).existsSync()) {
filePath = configFile;
} else {
print('The config file `$configFile` was not found.');
exit(1);
}
} else if (_flavorHelper.flavor != null) {
filePath = 'flutter_native_splash-${_flavorHelper.flavor}.yaml';
} else if (File('flutter_native_splash.yaml').existsSync()) {
filePath = 'flutter_native_splash.yaml';
} else {
filePath = 'pubspec.yaml';
}
final Map yamlMap = loadYaml(File(filePath).readAsStringSync()) as Map;
if (yamlMap['flutter_native_splash'] is! Map) {
throw Exception(
'Your `$filePath` file does not contain a '
'`flutter_native_splash` section.',
);
}
// yamlMap has the type YamlMap, which has several unwanted side effects
return _yamlToMap(yamlMap['flutter_native_splash'] as YamlMap);
}
Map<String, dynamic> _yamlToMap(YamlMap yamlMap) {
final Map<String, dynamic> map = <String, dynamic>{};
for (final MapEntry<dynamic, dynamic> entry in yamlMap.entries) {
if (entry.value is YamlList) {
final list = <String>[];
for (final value in entry.value as YamlList) {
if (value is String) {
list.add(value);
}
}
map[entry.key as String] = list;
} else if (entry.value is YamlMap) {
map[entry.key as String] = _yamlToMap(entry.value as YamlMap);
} else {
map[entry.key as String] = entry.value;
}
}
return map;
}
@visibleForTesting
String? parseColor(dynamic color) {
dynamic colorValue = color;
if (colorValue is int) colorValue = colorValue.toString().padLeft(6, '0');
if (colorValue is String) {
colorValue = colorValue.replaceAll('#', '').replaceAll(' ', '');
if (colorValue.length == 6) return colorValue;
}
if (colorValue == null) return null;
throw Exception('Invalid color value');
}