-
Notifications
You must be signed in to change notification settings - Fork 387
/
web_icon_generator.dart
148 lines (123 loc) · 5.27 KB
/
web_icon_generator.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
import 'dart:convert';
import 'package:image/image.dart';
import 'package:path/path.dart' as path;
import '../abs/icon_generator.dart';
import '../constants.dart' as constants;
import '../custom_exceptions.dart';
import '../utils.dart' as utils;
import 'web_template.dart';
final metaTagsTemplate = (
String appleMobileWebAppTitle,
String appleMobileWebAppStatusBarStyle, {
bool shouldInsertFLIString = false,
}) =>
'''
<!--Generated by Flutter Launcher Icons-->
<!--FLI-->
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="$appleMobileWebAppStatusBarStyle">
<meta name="apple-mobile-web-app-title" content="$appleMobileWebAppTitle">
<link rel="apple-touch-icon" href="icons/Icon-192.png">
<!-- Favicon -->
<link rel="icon" type="image/png" href="favicon.png"/>
<link rel="manifest" href="manifest.json">
<!--FLIEND-->
<!--Generated by Flutter Launcher Icons--END-->
''';
/// Generates Web icons for flutter
class WebIconGenerator extends IconGenerator {
static const _webIconSizeTemplates = <WebIconTemplate>[
WebIconTemplate(size: 192),
WebIconTemplate(size: 512),
WebIconTemplate(size: 192, maskable: true),
WebIconTemplate(size: 512, maskable: true),
];
WebIconGenerator(IconGeneratorContext context) : super(context, 'Web');
@override
void createIcons() {
final imgFilePath = path.join(context.prefixPath, context.webConfig!.imagePath ?? context.config.imagePath!);
context.logger.verbose('Decoding and loading image file at $imgFilePath...');
final imgFile = utils.decodeImageFile(imgFilePath);
if (imgFile == null) {
context.logger.error('Image File not found at give path $imgFilePath...');
throw FileNotFoundException(imgFilePath);
}
// generate favicon in web/favicon.png
context.logger.verbose('Generating favicon from $imgFilePath...');
_generateFavicon(imgFile);
// generate icons in web/icons/
context.logger.verbose('Generating icons from $imgFilePath...');
_generateIcons(imgFile);
// update manifest.json in web/mainfest.json
context.logger.verbose('Updating ${path.join(context.prefixPath, constants.webManifestFilePath)}...');
_updateManifestFile();
// todo: update index.html in web/index.html
// as we are using flutter default config we no need
// to update index.html for now
// _updateIndexFile();
}
@override
bool validateRequirments() {
// check if web config exists
context.logger.verbose('Checking webconfig...');
final webConfig = context.webConfig;
if (webConfig == null || !webConfig.generate) {
context.logger.verbose('Web config is not provided or generate is false. Skipped...');
return false;
}
if (webConfig.imagePath == null && context.config.imagePath == null) {
context.logger.verbose('Invalid config. Either provide web.imagePath or imagePath');
return false;
}
// verify web platform related files and directories exists
final entitesToCheck = [
path.join(context.prefixPath, constants.webDirPath),
path.join(context.prefixPath, constants.webManifestFilePath),
path.join(context.prefixPath, constants.webIndexFilePath),
];
// web platform related files must exist to continue
final failedEntityPath = utils.areFSEntiesExist(entitesToCheck);
if (failedEntityPath != null) {
context.logger.error('$failedEntityPath this file or folder is required to generate web icons');
}
return true;
}
void _generateFavicon(Image image) {
final favIcon = utils.createResizedImage(constants.kFaviconSize, image);
final favIconFile = utils.createFileIfNotExist(path.join(context.prefixPath, constants.webFaviconFilePath));
favIconFile.writeAsBytesSync(encodePng(favIcon));
}
void _generateIcons(Image image) {
final iconsDir = utils.createDirIfNotExist(path.join(context.prefixPath, constants.webIconsDirPath));
// generate icons
for (final template in _webIconSizeTemplates) {
final resizedImg = utils.createResizedImage(template.size, image);
final iconFile = utils.createFileIfNotExist(path.join(context.prefixPath, iconsDir.path, template.iconFile));
iconFile.writeAsBytesSync(encodePng(resizedImg));
}
}
// void _updateIndexFile() {
// todo
// final indexFile = File(constants.webIndexFilePath);
// if (!indexFile.existsSync()) {
// throw FileNotFoundException(constants.webFaviconFilePath);
// }
// }
void _updateManifestFile() {
final manifestFile = utils.createFileIfNotExist(path.join(context.prefixPath, constants.webManifestFilePath));
final manifestConfig = jsonDecode(manifestFile.readAsStringSync()) as Map<String, dynamic>;
// update background_color
if (context.webConfig?.backgroundColor != null) {
manifestConfig['background_color'] = context.webConfig?.backgroundColor;
}
// update theme_color
if (context.webConfig?.themeColor != null) {
manifestConfig['theme_color'] = context.webConfig?.themeColor;
}
// replace existing icons to eliminate conflicts
manifestConfig
..remove('icons')
..['icons'] = _webIconSizeTemplates.map<Map<String, dynamic>>((e) => e.iconManifest).toList();
manifestFile.writeAsStringSync(utils.prettifyJsonEncode(manifestConfig));
}
}