-
Notifications
You must be signed in to change notification settings - Fork 385
/
windows_icon_generator.dart
75 lines (64 loc) · 2.81 KB
/
windows_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
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;
/// A Implementation of [IconGenerator] for Windows
class WindowsIconGenerator extends IconGenerator {
/// Creates a instance of [WindowsIconGenerator]
WindowsIconGenerator(IconGeneratorContext context) : super(context, 'Windows');
@override
void createIcons() {
final imgFilePath = path.join(
context.prefixPath,
context.windowsConfig!.imagePath ?? context.config.imagePath,
);
context.logger.verbose('Decoding and loading image file from $imgFilePath...');
final imgFile = utils.decodeImageFile(imgFilePath);
// TODO(RatakondalaArun): remove null check
// #utils.decodeImageFile never returns null instead it throws Exception
if (imgFile == null) {
context.logger.error('Image File not found at given path $imgFilePath...');
throw FileNotFoundException(imgFilePath);
}
context.logger.verbose('Generating icon from $imgFilePath...');
_generateIcon(imgFile);
}
@override
bool validateRequirements() {
context.logger.verbose('Validating windows config...');
final windowsConfig = context.windowsConfig;
if (windowsConfig == null || !windowsConfig.generate) {
context.logger.error('Windows config is not provided or windows.generate is false. Skipped...');
return false;
}
if (windowsConfig.imagePath == null && context.config.imagePath == null) {
context.logger.error('Invalid config. Either provide windows.image_path or image_path');
return false;
}
// if icon_size is given it should be between 48<=icon_size<=256
// because .ico only supports this size
if (windowsConfig.iconSize != null && (windowsConfig.iconSize! < 48 || windowsConfig.iconSize! > 256)) {
context.logger.error(
'Invalid windows.icon_size=${windowsConfig.iconSize}. Icon size should be between 48<=icon_size<=256',
);
return false;
}
final entitesToCheck = [
path.join(context.prefixPath, constants.windowsDirPath),
path.join(context.prefixPath, windowsConfig.imagePath ?? context.config.imagePath),
];
final failedEntityPath = utils.areFSEntiesExist(entitesToCheck);
if (failedEntityPath != null) {
context.logger.error('$failedEntityPath this file or folder is required to generate web icons');
return false;
}
return true;
}
void _generateIcon(Image image) {
final favIcon = utils.createResizedImage(context.windowsConfig!.iconSize ?? constants.kWindowsIconSize, image);
final favIconFile = utils.createFileIfNotExist(path.join(context.prefixPath, constants.windowsIconFilePath));
favIconFile.writeAsBytesSync(encodeIco(favIcon));
}
}