-
Notifications
You must be signed in to change notification settings - Fork 387
/
flutter_launcher_icons_config.dart
153 lines (127 loc) · 4.4 KB
/
flutter_launcher_icons_config.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
import 'dart:io';
import 'package:checked_yaml/checked_yaml.dart' as yaml;
import 'package:json_annotation/json_annotation.dart';
import 'package:path/path.dart' as path;
import 'constants.dart' as constants;
import 'custom_exceptions.dart';
import 'utils.dart' as utils;
part 'flutter_launcher_icons_config.g.dart';
@JsonSerializable(
anyMap: true,
checked: true,
)
class FlutterLauncherIconsConfig {
/// Generic imagepath
@JsonKey(name: 'image_path')
final String? imagePath;
/// Returns true or path if android config is enabled
final dynamic android; // path or bool
/// Returns true or path if ios config is enabled
final dynamic ios; // path or bool
/// Image path specific to android
@JsonKey(name: 'image_path_android')
final String? imagePathAndroid;
/// Image path specific to ios
@JsonKey(name: 'image_path_ios')
final String? imagePathIOS;
/// android adaptive icon foreground image
@JsonKey(name: 'adaptive_icon_foreground')
final String? adaptiveIconForeground;
/// android adaptive_icon_background image
@JsonKey(name: 'adaptive_icon_background')
final String? adaptiveIconBackground;
/// Web platform config
@JsonKey(name: 'web')
final WebConfig? webConfig;
const FlutterLauncherIconsConfig({
this.imagePath,
this.android = false,
this.ios = false,
this.imagePathAndroid,
this.imagePathIOS,
this.adaptiveIconForeground,
this.adaptiveIconBackground,
this.webConfig,
});
factory FlutterLauncherIconsConfig.fromJson(Map json) => _$FlutterLauncherIconsConfigFromJson(json);
/// Loads flutter launcher icons configs from given [filePath]
static FlutterLauncherIconsConfig? loadConfigFromPath(String filePath, String prefixPath) {
final configFile = File(path.join(prefixPath, filePath));
if (!configFile.existsSync()) {
return null;
}
final configContent = configFile.readAsStringSync();
try {
return yaml.checkedYamlDecode<FlutterLauncherIconsConfig?>(
configContent,
(json) {
// todo: add support for new scheme https://github.com/fluttercommunity/flutter_launcher_icons/issues/373
return json == null || json['flutter_icons'] == null
? null
: FlutterLauncherIconsConfig.fromJson(json['flutter_icons']);
},
allowNull: true,
);
} on yaml.ParsedYamlException catch (e) {
throw InvalidConfigException(e.formattedMessage);
} catch (e) {
rethrow;
}
}
/// Loads flutter launcher icons config from `pubspec.yaml` file
static FlutterLauncherIconsConfig? loadConfigFromPubSpec(String prefix) {
try {
final pubspecFile = File(path.join(prefix, constants.pubspecFilePath));
if (!pubspecFile.existsSync()) {
return null;
}
final pubspecContent = pubspecFile.readAsStringSync();
return yaml.checkedYamlDecode<FlutterLauncherIconsConfig?>(
pubspecContent,
(json) {
// todo: add support for new scheme https://github.com/fluttercommunity/flutter_launcher_icons/issues/373
return json == null || json['flutter_icons'] == null
? null
: FlutterLauncherIconsConfig.fromJson(json['flutter_icons']);
},
allowNull: true,
);
} on yaml.ParsedYamlException catch (e) {
throw InvalidConfigException(e.formattedMessage);
} catch (e) {
rethrow;
}
}
static FlutterLauncherIconsConfig? loadConfigFromFlavor(String flavor, String prefixPath) {
return FlutterLauncherIconsConfig.loadConfigFromPath(utils.flavorConfigFile(flavor), prefixPath);
}
Map<String, dynamic> toJson() => _$FlutterLauncherIconsConfigToJson(this);
@override
String toString() => 'FlutterLauncherIconsConfig: ${toJson()}';
}
@JsonSerializable(
anyMap: true,
checked: true,
)
class WebConfig {
final bool generate;
/// Image path for web
@JsonKey(name: 'image_path')
final String? imagePath;
/// manifest.json's background_color
@JsonKey(name: 'background_color')
final String? backgroundColor;
/// manifest.json's theme_color
@JsonKey(name: 'theme_color')
final String? themeColor;
const WebConfig({
this.generate = false,
this.imagePath,
this.backgroundColor,
this.themeColor,
});
factory WebConfig.fromJson(Map json) => _$WebConfigFromJson(json);
Map<String, dynamic> toJson() => _$WebConfigToJson(this);
@override
String toString() => 'WebConfig: ${toJson()}';
}