-
Notifications
You must be signed in to change notification settings - Fork 385
/
utils.dart
79 lines (68 loc) · 2.14 KB
/
utils.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
import 'dart:convert';
import 'dart:io';
import 'package:image/image.dart';
import 'package:path/path.dart' as path;
import 'custom_exceptions.dart';
Image createResizedImage(int iconSize, Image image) {
if (image.width >= iconSize) {
return copyResize(
image,
width: iconSize,
height: iconSize,
interpolation: Interpolation.average,
);
} else {
return copyResize(
image,
width: iconSize,
height: iconSize,
interpolation: Interpolation.linear,
);
}
}
void printStatus(String message) {
print('• $message');
}
String generateError(Exception e, String? error) {
final errorOutput = error == null ? '' : ' \n$error';
return '\n✗ ERROR: ${(e).runtimeType.toString()}$errorOutput';
}
// TODO(RatakondalaArun): Remove nullable return type
// this can never return null value since it already throws exception
Image? decodeImageFile(String filePath) {
final image = decodeImage(File(filePath).readAsBytesSync());
if (image == null) {
throw NoDecoderForImageFormatException(filePath);
}
return image;
}
/// Creates [File] in the given [filePath] if not exists
File createFileIfNotExist(String filePath) {
final file = File(path.joinAll(path.split(filePath)));
if (!file.existsSync()) {
file.createSync(recursive: true);
}
return file;
}
/// Creates [Directory] in the given [dirPath] if not exists
Directory createDirIfNotExist(String dirPath) {
final dir = Directory(path.joinAll(path.split(dirPath)));
if (!dir.existsSync()) {
dir.createSync(recursive: true);
}
return dir;
}
/// Returns a prettified json string
String prettifyJsonEncode(Object? map) => JsonEncoder.withIndent(' ' * 2).convert(map);
/// Check if give [File] or [Directory] exists at the give [paths],
/// if not returns the failed [FileSystemEntity] path
String? areFSEntiesExist(List<String> paths) {
for (final path in paths) {
final fsType = FileSystemEntity.typeSync(path);
if (![FileSystemEntityType.directory, FileSystemEntityType.file].contains(fsType)) {
return path;
}
}
return null;
}
String flavorConfigFile(String flavor) => 'flutter_launcher_icons-$flavor.yaml';