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

Feature: Custom code gen class name #694

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
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
31 changes: 24 additions & 7 deletions bin/generate.dart
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,14 @@ ArgParser _generateArgParser(GenerateOptions? generateOptions) {
help: 'If true - Skip unnecessary keys of nested objects.',
);

parser.addOption(
'class-name',
abbr: 'c',
defaultsTo: 'LocaleKeys',
callback: (String? x) => generateOptions!.className = x!,
help: 'Custom class name for generated file',
);

return parser;
}

Expand All @@ -91,6 +99,7 @@ class GenerateOptions {
String? outputDir;
String? outputFile;
String? format;
String? className;
bool? skipUnnecessaryKeys;

@override
Expand All @@ -106,7 +115,6 @@ void handleLangFiles(GenerateOptions options) async {
final sourcePath = Directory(path.join(current.path, source.path));
final outputPath =
Directory(path.join(current.path, output.path, options.outputFile));

if (!await sourcePath.exists()) {
stderr.writeln('Source path does not exist');
return;
Expand Down Expand Up @@ -141,8 +149,12 @@ Future<List<FileSystemEntity>> dirContents(Directory dir) {
return completer.future;
}

void generateFile(List<FileSystemEntity> files, Directory outputPath,
GenerateOptions options) async {
void generateFile(
List<FileSystemEntity> files,
Directory outputPath,
GenerateOptions options,
) async {
final className = options.className ?? 'LocaleKeys';
var generatedFile = File(outputPath.path);
if (!generatedFile.existsSync()) {
generatedFile.createSync(recursive: true);
Expand All @@ -155,7 +167,8 @@ void generateFile(List<FileSystemEntity> files, Directory outputPath,
await _writeJson(classBuilder, files);
break;
case 'keys':
await _writeKeys(classBuilder, files, options.skipUnnecessaryKeys);
await _writeKeys(
classBuilder, files, options.skipUnnecessaryKeys, className);
break;
// case 'csv':
// await _writeCsv(classBuilder, files);
Expand All @@ -170,12 +183,16 @@ void generateFile(List<FileSystemEntity> files, Directory outputPath,
stdout.writeln('All done! File generated in ${outputPath.path}');
}

Future _writeKeys(StringBuffer classBuilder, List<FileSystemEntity> files,
bool? skipUnnecessaryKeys) async {
Future _writeKeys(
StringBuffer classBuilder,
List<FileSystemEntity> files,
bool? skipUnnecessaryKeys,
String className,
) async {
var file = '''
// DO NOT EDIT. This is code generated via package:easy_localization/generate.dart

abstract class LocaleKeys {
abstract class $className {
''';

final fileData = File(files.first.path);
Expand Down