Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(windows): add support for windows closes #380
- Loading branch information
1 parent
a69a4e9
commit c22a950
Showing
7 changed files
with
161 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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)); | ||
} | ||
} |