build method
Generate
IconFont
from fileMap.
Implementation
@override
@mustCallSuper
FutureOr<void> build([final BuildStep? buildStep]) async {
FutureOr<T> runAction<T extends Object?>(
final String label,
final FutureOr<T> Function() action,
) =>
buildStep != null
? buildStep.trackStage(label, action, isExternal: true)
: action();
ProcessResult result;
// ProcessResult result = await runAction(
// 'Check Node.JS version.',
// () => Process.run('node', <String>['--version'], runInShell: true),
// );
// if (result.exitCode != 0) {
// throw Exception('Please install Node.JS v10+');
// }
await super.build(buildStep);
final String fantasticonExecutable = join(
Directory.current.path,
'.dart_tool',
'node_modules',
'.bin',
'fantasticon${Platform.isWindows ? '.cmd' : ''}',
);
if (force || !File(fantasticonExecutable).existsSync()) {
final File packageFile =
File(join(Directory.current.path, '.dart_tool', 'package.json'));
if (!packageFile.existsSync()) {
await packageFile.writeAsString(json.encode(package));
}
result = await runAction(
'Run ${yarn ? 'yarn' : 'npm'} install.',
() => Process.run(
yarn ? 'yarn' : 'npm',
<String>['install', '--no-fund'],
workingDirectory: join(Directory.current.path, '.dart_tool'),
runInShell: true,
),
);
if (result.exitCode != 0) {
final Object? stderr = result.stderr;
throw Exception(
stderr is String && stderr.isNotEmpty ? stderr : result.stdout,
);
}
}
result = await runAction(
'Run fantasticon.',
() => Process.run(
fantasticonExecutable,
<String>[
canonicalize(joinAll(split(importPath))),
if (height != null) ...<String>['--font-height', height!.toString()],
if (descent != null) ...<String>['--descent', descent!.toString()],
if (normalize != null) ...<String>[
'--normalize',
normalize!.toString()
],
'--name',
basenameWithoutExtension(fontExportPath),
'--output',
canonicalize(joinAll(split(dirname(fontExportPath)))),
...<String>['--asset-types', 'json'],
...<String>['--font-types', extension(fontExportPath).substring(1)]
],
workingDirectory: Directory.current.path,
runInShell: true,
),
);
if (result.exitCode != 0) {
final Object? stderr = result.stderr;
throw Exception(
stderr is String && stderr.isNotEmpty ? stderr : result.stdout,
);
}
iconsMap.clear();
iconsMap[''] = importPath;
Iterable<String> baseSegments = split(canonicalize(importPath));
final List<String> sameSegments = fileMap.keys.sameSegments.toList();
if (!const IterableEquality<String>().equals(
baseSegments,
sameSegments.length > baseSegments.length
? sameSegments.sublist(0, baseSegments.length)
: sameSegments,
)) {
baseSegments = const <String>[];
}
int globalIndex = 0;
for (final String inputPath in fileMap.keys.sorted(
(final String $1, final String $2) =>
basenameWithoutExtension($2).startsWith(basenameWithoutExtension($1))
? 1
: $1.compareTo($2),
)) {
Map<String, Object?> nestedIconsMap = iconsMap;
final List<String> segments =
split(inputPath).sublist(baseSegments.length);
for (int index = 0; index < segments.length; index++) {
final String segment = segments.elementAt(index);
if (index == segments.length - 1) {
final String fullPath =
posix.join(importPath, posix.joinAll(segments));
nestedIconsMap[fullPath] = baseCodePoint + globalIndex++;
} else {
nestedIconsMap.putIfAbsent(segment, () => <String, Object?>{});
nestedIconsMap = nestedIconsMap[segment]! as Map<String, Object?>;
if (!nestedIconsMap.containsKey('')) {
nestedIconsMap[''] = posix.join(
importPath,
posix.joinAll(segments.sublist(0, segments.length - index - 1)),
);
}
}
}
}
}