Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add symlink support #27

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ $ pub global activate icon_font_generator
- `--out-font` * - Output icon font path (to file, for example: lib/font.ttf)
- `--out-flutter` * - Output flutter icon class (to file, for example: lib/icons.dart)
- `--class-name` * - The class name is also the font name used in pubspec.yaml (as font name)
- `--symlinks-map` - Source map of symlinked icons, have to be a json map with this pattern: symlink -> target
- `--height` - Fixed font height value, defaults: 512
- `--descent` - Offset applied to the baseline, defaults: 240
- `--package` - Name of package for generated icon data ([See more](https://api.flutter.dev/flutter/widgets/IconData/fontPackage.html))
Expand Down
15 changes: 15 additions & 0 deletions bin/icon_font_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ class GenerateCommand extends Command {
'class-name',
help: 'Flutter class name / family for generating file',
)
..addOption(
'symlinks-map',
help: 'Symlinks json map, with this pattern: symlink -> target',
)
..addOption(
'height',
help: 'Fixed font height value',
Expand Down Expand Up @@ -97,6 +101,12 @@ class GenerateCommand extends Command {
exit(1);
}

if (argResults!['symlinks-map'] != null &&
!argResults!['symlinks-map'].toString().endsWith('.json')) {
print('--symlinks-map have to be a .json file');
exit(1);
}

final genRootDir = Directory.fromUri(Platform.script.resolve('..'));

final npmPackage = File(path.join(genRootDir.path, 'package.json'));
Expand Down Expand Up @@ -139,6 +149,10 @@ class GenerateCommand extends Command {

final sourceIconsDirectory = Directory.fromUri(Directory.current.uri
.resolve(argResults!['from'].replaceAll('\\', '/')));
final symlinksMap = argResults!['symlinks-map'] == null
? null
: File.fromUri(Directory.current.uri
.resolve(argResults!['symlinks-map'].replaceAll('\\', '/')));
final outIconsFile = File.fromUri(Directory.current.uri
.resolve(argResults!['out-font'].replaceAll('\\', '/')));
final outFlutterClassFile = File.fromUri(Directory.current.uri
Expand Down Expand Up @@ -201,6 +215,7 @@ class GenerateCommand extends Command {

final generateClassResult = await generateFlutterClass(
iconMap: iconsMap,
symlinksMap: symlinksMap,
className: argResults!['class-name'],
packageName: argResults!['package'],
namingStrategy: argResults!['naming-strategy'],
Expand Down
41 changes: 38 additions & 3 deletions lib/generate_flutter_class.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,55 @@ class GenerateResult {

Future<GenerateResult> generateFlutterClass({
required File iconMap,
required File? symlinksMap,
required String className,
required String? packageName,
required String namingStrategy,
String indent = ' ',
}) async {
String reCase(String data) {
return namingStrategy == 'snake'
? ReCase(data).snakeCase
: ReCase(data).camelCase;
}

final Map<String, dynamic> icons = jsonDecode(await iconMap.readAsString());
final Map<String, dynamic> symlinks =
symlinksMap == null ? {} : jsonDecode(await symlinksMap.readAsString());

for (var symlinkEntry in symlinks.entries) {
// Symlinks values are already correctly cased
final symlink = symlinkEntry.key;
final target = symlinkEntry.value.toString();
final Iterable<String> reCasedIconKeys =
icons.keys.map((key) => reCase(key));

if (reCasedIconKeys.contains(symlink)) {
print(
'\x1B[33mWarning: symlink "$symlink" icon already exists - symlink creation skipped\x1B[0m');
continue;
} else if (symlinks.keys.contains(target)) {
print(
'\x1B[33mWarning: target "$target" icon is already a symlink - symlink creation skipped\x1B[0m');
continue;
} else if (!reCasedIconKeys.contains(target)) {
print(
'\x1B[33mWarning: target "$target" icon does not exist - symlink creation skipped\x1B[0m');
continue;
}

final targetEntry =
icons.entries.singleWhere((entry) => reCase(entry.key) == target);
icons.addAll({
symlink: targetEntry.value,
});
}

final dartIconsEntries = <String>{};
final dartIconsValues = <String>{};

for (final entry in icons.entries) {
final name = namingStrategy == 'snake'
? ReCase(entry.key).snakeCase
: ReCase(entry.key).camelCase;
final name = reCase(entry.key);

dartIconsEntries.add(someReplace(
template.icon
Expand Down