-
-
Notifications
You must be signed in to change notification settings - Fork 371
Feature/generator yaml support #772
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
Open
Lucas-Feichtinger
wants to merge
6
commits into
aissat:develop
Choose a base branch
from
Lucas-Feichtinger:feature/generator_yaml_support
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
854410e
Update generate.dart
Lucas-Feichtinger cfa45f1
Update pubspec.yaml
Lucas-Feichtinger 7505368
Update CHANGELOG.md
Lucas-Feichtinger a5645ac
Update generate.dart
Lucas-Feichtinger b82218d
Update generate.dart
Lucas-Feichtinger a107608
Update CHANGELOG.md
Lucas-Feichtinger File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,6 +4,7 @@ import 'dart:io'; | |
|
|
||
| import 'package:args/args.dart'; | ||
| import 'package:path/path.dart' as path; | ||
| import 'package:yaml/yaml.dart'; | ||
|
|
||
| const _preservedKeywords = [ | ||
| 'few', | ||
|
|
@@ -70,8 +71,8 @@ ArgParser _generateArgParser(GenerateOptions? generateOptions) { | |
| abbr: 'f', | ||
| defaultsTo: 'json', | ||
| callback: (String? x) => generateOptions!.format = x, | ||
| help: 'Support json or keys formats', | ||
| allowed: ['json', 'keys']); | ||
| help: 'Support json, yaml, or keys formats', | ||
| allowed: ['json', 'yaml', 'keys']); | ||
|
|
||
| parser.addFlag( | ||
| 'skip-unnecessary-keys', | ||
|
|
@@ -122,7 +123,7 @@ void handleLangFiles(GenerateOptions options) async { | |
| files = [sourceFile]; | ||
| } else { | ||
| //filtering format | ||
| files = files.where((f) => f.path.contains('.json')).toList(); | ||
| files = files.where((f) => f.path.contains(RegExp(r'\.(json|yaml|yml)$'))).toList(); | ||
| } | ||
|
|
||
| if (files.isNotEmpty) { | ||
|
|
@@ -154,6 +155,9 @@ void generateFile(List<FileSystemEntity> files, Directory outputPath, | |
| case 'json': | ||
| await _writeJson(classBuilder, files); | ||
| break; | ||
| case 'yaml': | ||
| await _writeYaml(classBuilder, files); | ||
| break; | ||
| case 'keys': | ||
| await _writeKeys(classBuilder, files, options.skipUnnecessaryKeys); | ||
| break; | ||
|
|
@@ -183,7 +187,7 @@ abstract class LocaleKeys { | |
| final fileData = File(files.first.path); | ||
|
|
||
| Map<String, dynamic> translations = | ||
| json.decode(await fileData.readAsString()); | ||
| json.decode(json.encode(loadYaml(await fileData.readAsString()))); | ||
|
|
||
|
Comment on lines
187
to
191
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Keys generation reads YAML only — add shared parser to support JSON and validate top-level. Without this, - Map<String, dynamic> translations =
- json.decode(json.encode(loadYaml(await fileData.readAsString())));
+ final Map<String, dynamic> translations = await _parseTranslations(fileData);Add this helper (place near // Parses .json, .yaml, .yml into Map<String, dynamic> and validates top-level map.
Future<Map<String, dynamic>> _parseTranslations(File file) async {
final ext = path.extension(file.path).toLowerCase();
final content = await file.readAsString();
if (ext == '.json') {
final decoded = json.decode(content);
if (decoded is! Map) {
throw const FormatException('Top-level JSON must be an object');
}
return Map<String, dynamic>.from(decoded as Map);
}
// YAML path
final yamlRoot = loadYaml(content);
// Normalize YamlMap/YamlList into JSON-friendly Map/List
final normalized = json.decode(json.encode(yamlRoot));
if (normalized is! Map) {
throw const FormatException('Top-level YAML must be a mapping');
}
return Map<String, dynamic>.from(normalized as Map);
}🤖 Prompt for AI Agents |
||
| file += _resolve(translations, skipUnnecessaryKeys); | ||
|
|
||
|
|
@@ -271,6 +275,47 @@ class CodegenLoader extends AssetLoader{ | |
| classBuilder.writeln(gFile); | ||
| } | ||
|
|
||
| Future _writeYaml( | ||
| StringBuffer classBuilder, List<FileSystemEntity> files) async { | ||
| var gFile = ''' | ||
| // DO NOT EDIT. This is code generated via package:easy_localization/generate.dart | ||
|
|
||
| // ignore_for_file: prefer_single_quotes, avoid_renaming_method_parameters, constant_identifier_names | ||
|
|
||
| import 'dart:ui'; | ||
|
|
||
| import 'package:easy_localization/easy_localization.dart' show AssetLoader; | ||
|
|
||
| class CodegenLoader extends AssetLoader{ | ||
| const CodegenLoader(); | ||
|
|
||
| @override | ||
| Future<Map<String, dynamic>?> load(String path, Locale locale) { | ||
| return Future.value(mapLocales[locale.toString()]); | ||
| } | ||
|
|
||
| '''; | ||
|
|
||
| final listLocales = []; | ||
|
|
||
| for (var file in files) { | ||
| final localeName = path | ||
| .basename(file.path) | ||
| .replaceFirst(RegExp(r'\.(yaml|yml)'), '') | ||
| .replaceAll('-', '_'); | ||
| listLocales.add('"$localeName": _$localeName'); | ||
| final fileData = File(file.path); | ||
|
|
||
| var data = loadYaml(await fileData.readAsString()); | ||
| final mapString = const JsonEncoder.withIndent(' ').convert(data); | ||
| gFile += 'static const Map<String,dynamic> _$localeName = $mapString;\n'; | ||
| } | ||
|
|
||
| gFile += | ||
| 'static const Map<String, Map<String,dynamic>> mapLocales = {${listLocales.join(', ')}};'; | ||
| classBuilder.writeln(gFile); | ||
| } | ||
|
|
||
| // _writeCsv(StringBuffer classBuilder, List<FileSystemEntity> files) async { | ||
| // List<String> listLocales = List(); | ||
| // final fileData = File(files.first.path); | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Filter by selected format and ensure only files are processed.
Currently mixes JSON with YAML when format='yaml' and may include directories. Make filtering deterministic and case-insensitive.
📝 Committable suggestion
🤖 Prompt for AI Agents