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 camel and snake case option for Flutter class variable names #19

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
6 changes: 6 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

105 changes: 105 additions & 0 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 14 additions & 18 deletions bin/fontify.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ import 'dart:io';
import 'package:args/args.dart';
import 'package:fontify/src/cli/arguments.dart';
import 'package:fontify/src/cli/options.dart';
import 'package:fontify/src/common/api.dart';
import 'package:fontify/src/common.dart';
import 'package:fontify/src/otf/io.dart';
import 'package:fontify/src/utils/flutter_class_gen.dart';
import 'package:fontify/src/utils/logger.dart';
import 'package:path/path.dart' as p;
import 'package:yaml/yaml.dart';
Expand Down Expand Up @@ -46,14 +47,12 @@ void _run(CliArguments parsedArgs) {
}

if (parsedArgs.classFile?.existsSync() ?? false) {
logger.v(
'Output file for a Flutter class already exists (${parsedArgs.classFile!.path}) - '
logger.v('Output file for a Flutter class already exists (${parsedArgs.classFile!.path}) - '
'overwriting it');
}

if (!parsedArgs.fontFile.existsSync()) {
logger.v(
'Output file for a font file already exists (${parsedArgs.fontFile.path}) - '
logger.v('Output file for a font file already exists (${parsedArgs.fontFile.path}) - '
'overwriting it');
}

Expand All @@ -63,13 +62,11 @@ void _run(CliArguments parsedArgs) {
.toList();

if (svgFileList.isEmpty) {
logger.w(
"The input directory doesn't contain any SVG file (${parsedArgs.svgDir.path}).");
logger.w("The input directory doesn't contain any SVG file (${parsedArgs.svgDir.path}).");
}

final svgMap = {
for (final f in svgFileList)
p.basenameWithoutExtension(f.path): File(f.path).readAsStringSync(),
for (final f in svgFileList) p.basenameWithoutExtension(f.path): File(f.path).readAsStringSync(),
};

final otfResult = svgToOtf(
Expand All @@ -88,13 +85,13 @@ void _run(CliArguments parsedArgs) {
final fontFileName = p.basename(parsedArgs.fontFile.path);

final classString = generateFlutterClass(
glyphList: otfResult.glyphList,
className: parsedArgs.className,
indent: parsedArgs.indent,
fontFileName: fontFileName,
familyName: otfResult.font.familyName,
package: parsedArgs.fontPackage,
);
glyphList: otfResult.glyphList,
className: parsedArgs.className,
indent: parsedArgs.indent,
fontFileName: fontFileName,
familyName: otfResult.font.familyName,
package: parsedArgs.fontPackage,
variableNameCase: VariableNameCase.values.firstWhere((e) => e.option == parsedArgs.variableNameCase));

parsedArgs.classFile!.writeAsStringSync(classString);
}
Expand Down Expand Up @@ -124,8 +121,7 @@ ${_argParser.usage}
exit(64);
}

const _kAbout =
'Converts .svg icons to an OpenType font and generates Flutter-compatible class.';
const _kAbout = 'Converts .svg icons to an OpenType font and generates Flutter-compatible class.';

const _kUsage = '''
Usage: fontify <input-svg-dir> <output-font-file> [options]
Expand Down
1 change: 1 addition & 0 deletions example/example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ void main() {
familyName: svgToOtfResult.font.familyName,
className: 'MyIcons',
fontFileName: fontFileName,
variableNameCase: VariableNameCase.camel,
);

// Writing class content to a file
Expand Down
31 changes: 14 additions & 17 deletions lib/src/cli/arguments.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const _kArgAllowedTypes = <CliArgument, List<Type>>{
CliArgument.verbose: [bool],
CliArgument.help: [bool],
CliArgument.configFile: [String],
CliArgument.variableNameCase: [String]
};

const kDefaultVerbose = false;
Expand All @@ -44,6 +45,7 @@ const kOptionNames = EnumClass<CliArgument, String>({

CliArgument.recursive: 'recursive',
CliArgument.verbose: 'verbose',
CliArgument.variableNameCase: 'variable-name-case',

CliArgument.help: 'help',
CliArgument.configFile: 'config-file',
Expand All @@ -61,6 +63,7 @@ const kConfigKeys = EnumClass<CliArgument, String>({
CliArgument.fontName: 'font_name',
CliArgument.normalize: 'normalize',
CliArgument.ignoreShapes: 'ignore_shapes',
CliArgument.variableNameCase: 'variable_name_case',

CliArgument.recursive: 'recursive',
CliArgument.verbose: 'verbose',
Expand Down Expand Up @@ -92,6 +95,7 @@ enum CliArgument {
// Others
recursive,
verbose,
variableNameCase,

// Only in CLI
help,
Expand All @@ -113,6 +117,7 @@ class CliArguments {
this.normalize,
this.verbose,
this.configFile,
this.variableNameCase,
);

/// Creates [CliArguments] for a map of raw values.
Expand All @@ -135,6 +140,7 @@ class CliArguments {
map[CliArgument.normalize] as bool?,
map[CliArgument.verbose] as bool?,
map[CliArgument.configFile] as File?,
map[CliArgument.variableNameCase] as String?,
);
}

Expand All @@ -150,15 +156,15 @@ class CliArguments {
final bool? normalize;
final bool? verbose;
final File? configFile;
final String? variableNameCase;
}

/// Parses argument list.
///
/// Throws [CliHelpException], if 'help' option is present.
///
/// Returns an instance of [CliArguments] containing all parsed data.
Map<CliArgument, dynamic> parseArguments(
ArgParser argParser, List<String> args) {
Map<CliArgument, dynamic> parseArguments(ArgParser argParser, List<String> args) {
late final ArgResults argResults;
try {
argResults = argParser.parse(args);
Expand All @@ -170,13 +176,11 @@ Map<CliArgument, dynamic> parseArguments(
throw CliHelpException();
}

final posArgsLength =
math.min(_kPositionalArguments.length, argResults.rest.length);
final posArgsLength = math.min(_kPositionalArguments.length, argResults.rest.length);

final rawArgMap = <CliArgument, dynamic>{
for (final e in kOptionNames.entries) e.key: argResults[e.value],
for (var i = 0; i < posArgsLength; i++)
_kPositionalArguments[i]: argResults.rest[i],
for (var i = 0; i < posArgsLength; i++) _kPositionalArguments[i]: argResults.rest[i],
};

return rawArgMap;
Expand Down Expand Up @@ -219,9 +223,7 @@ Map<CliArgument, dynamic>? parseConfig(String config) {
return null;
}

final entries = fontifyYamlMap.entries
.map(_mapConfigKeyEntry)
.whereType<MapEntry<CliArgument, dynamic>>();
final entries = fontifyYamlMap.entries.map(_mapConfigKeyEntry).whereType<MapEntry<CliArgument, dynamic>>();

return Map<CliArgument, dynamic>.fromEntries(entries);
}
Expand All @@ -235,10 +237,7 @@ CliArguments parseArgsAndConfig(ArgParser argParser, List<String> args) {
var parsedArgs = parseArguments(argParser, args);
final dynamic configFile = parsedArgs[CliArgument.configFile];

final configList = <String>[
if (configFile is String) configFile,
..._kDefaultConfigPathList
].map((e) => File(e));
final configList = <String>[if (configFile is String) configFile, ..._kDefaultConfigPathList].map((e) => File(e));

for (final configFile in configList) {
if (configFile.existsSync()) {
Expand Down Expand Up @@ -304,13 +303,11 @@ extension CliArgumentMapExtension on Map<CliArgument, dynamic> {
}

if (svgDir.statSync().type != FileSystemEntityType.directory) {
throw CliArgumentException(
"The input directory is not a directory or it doesn't exist.");
throw CliArgumentException("The input directory is not a directory or it doesn't exist.");
}

if (indent != null && indent < 0) {
throw CliArgumentException(
'indent must be a non-negative integer, was $indent.');
throw CliArgumentException('indent must be a non-negative integer, was $indent.');
}
}

Expand Down
21 changes: 13 additions & 8 deletions lib/src/cli/options.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:args/args.dart';

import '../../fontify.dart';
import 'arguments.dart';

void defineOptions(ArgParser argParser) {
Expand All @@ -8,8 +9,7 @@ void defineOptions(ArgParser argParser) {
..addOption(
kOptionNames[CliArgument.classFile]!,
abbr: 'o',
help:
'Output path for Flutter-compatible class that contains identifiers for the icons.',
help: 'Output path for Flutter-compatible class that contains identifiers for the icons.',
valueHelp: 'path',
)
..addOption(
Expand All @@ -28,10 +28,17 @@ void defineOptions(ArgParser argParser) {
..addOption(
kOptionNames[CliArgument.fontPackage]!,
abbr: 'p',
help:
'Name of a package that provides a font. Used to provide a font through package dependency.',
help: 'Name of a package that provides a font. Used to provide a font through package dependency.',
valueHelp: 'name',
)
..addOption(
kOptionNames[CliArgument.variableNameCase]!,
abbr: 'n',
help: 'The case to use when generating variable names.',
valueHelp: 'name',
allowed: VariableNameCase.values.map((e) => e.option),
defaultsTo: VariableNameCase.camel.option,
)
..addSeparator('Font options:')
..addOption(
kOptionNames[CliArgument.fontName]!,
Expand All @@ -41,8 +48,7 @@ void defineOptions(ArgParser argParser) {
)
..addFlag(
kOptionNames[CliArgument.normalize]!,
help:
'Enables glyph normalization for the font. Disable this if every icon has the same size and positioning.',
help: 'Enables glyph normalization for the font. Disable this if every icon has the same size and positioning.',
defaultsTo: true,
)
..addFlag(
Expand All @@ -54,8 +60,7 @@ void defineOptions(ArgParser argParser) {
..addOption(
kOptionNames[CliArgument.configFile]!,
abbr: 'z',
help:
'Path to Fontify yaml configuration file. pubspec.yaml and fontify.yaml files are used by default.',
help: 'Path to Fontify yaml configuration file. pubspec.yaml and fontify.yaml files are used by default.',
valueHelp: 'path',
)
..addFlag(
Expand Down
Loading