diff --git a/pkgs/messages_builder/lib/code_generation/classes_generation.dart b/pkgs/messages_builder/lib/code_generation/classes_generation.dart index fb4973b5..e1195d28 100644 --- a/pkgs/messages_builder/lib/code_generation/classes_generation.dart +++ b/pkgs/messages_builder/lib/code_generation/classes_generation.dart @@ -9,7 +9,6 @@ import '../located_message_file.dart'; import 'class_generation.dart'; import 'constructor_generation.dart'; import 'field_generation.dart'; -import 'message_file_metadata.dart'; import 'method_generation.dart'; class ClassesGeneration { diff --git a/pkgs/messages_builder/lib/code_generation/message_file_metadata.dart b/pkgs/messages_builder/lib/code_generation/message_file_metadata.dart deleted file mode 100644 index 02df752b..00000000 --- a/pkgs/messages_builder/lib/code_generation/message_file_metadata.dart +++ /dev/null @@ -1,15 +0,0 @@ -// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file -// for details. All rights reserved. Use of this source code is governed by a -// BSD-style license that can be found in the LICENSE file. - -class MessageFileMetadata { - final String hash; - final String path; - final String locale; - - MessageFileMetadata({ - required this.hash, - required this.path, - required this.locale, - }); -} diff --git a/pkgs/messages_builder/lib/message_code_builder.dart b/pkgs/messages_builder/lib/message_code_builder.dart index f224484f..6e8e0fc8 100644 --- a/pkgs/messages_builder/lib/message_code_builder.dart +++ b/pkgs/messages_builder/lib/message_code_builder.dart @@ -14,9 +14,9 @@ import 'package:yaml_edit/yaml_edit.dart'; import 'arb_parser.dart'; import 'code_generation/classes_generation.dart'; import 'code_generation/code_generation.dart'; -import 'code_generation/message_file_metadata.dart'; import 'generation_options.dart'; -import 'message_with_metadata.dart'; +import 'located_message_file.dart'; +import 'message_file.dart'; class MessageCodeBuilder { final GenerationOptions options; @@ -30,36 +30,31 @@ class MessageCodeBuilder { }); Future build() async { - final messageFiles = await parseMessageFiles(); + final messageFiles = await _parseMessageFiles(); - final families = messageFiles.groupListsBy( - (messageFile) => getParentFile(messageFiles, messageFile)); + final families = messageFiles + .groupListsBy((messageFile) => getParentFile(messageFiles, messageFile)) + .map((key, value) => + MapEntry(key, value.sortedBy((messageFile) => messageFile.locale))); var counter = 0; for (final MapEntry(key: parent, value: children) in families.entries) { final context = parent.file.context; - final childrensMetadata = collectMetadata(children); + await includeFilesInPubspec(context, children.map((f) => f.path)); - await includeFilesInPubspec( - context, - childrensMetadata.map((e) => e.path.split('/').skip(2).join('/')), - options.generatedCodeFiles, - ); - - final dummyFilePaths = Map.fromEntries(childrensMetadata + final dummyFilePaths = Map.fromEntries(children .map((e) => e.locale) .map((e) => MapEntry(e, [context, e, 'empty'].join('_')))); final library = ClassesGeneration( - options: options, - context: context, - initialLocale: parent.file.locale!, - messages: parent.file.messages, - messageFilesMetadata: childrensMetadata, - emptyFiles: dummyFilePaths) - .generate(); + options: options, + context: context, + parent: parent, + children: children, + emptyFiles: dummyFilePaths, + ).generate(); final code = CodeGenerator( options: options, @@ -88,17 +83,7 @@ class MessageCodeBuilder { } } - List collectMetadata( - List messageFiles) => - messageFiles - .map((messageFile) => MessageFileMetadata( - locale: messageFile.file.locale ?? 'en_US', - path: 'packages/${options.packageName}/${messageFile.path}', - hash: messageFile.file.hash, - )) - .sortedBy((resource) => resource.locale); - - Future> parseMessageFiles() async => + Future> _parseMessageFiles() async => Future.wait(inputToOutputFiles.entries .map((p) async => LocatedMessageFile( path: path.relative(p.value, from: Directory.current.path), @@ -132,7 +117,6 @@ The files $filesInContext have no metadata, so it is not clear which one is the Future includeFilesInPubspec( String? context, Iterable fileList, - Directory generatedCodeFiles, ) async { final contextMessage = context != null ? 'For the messages in $context, the' : 'The'; @@ -174,13 +158,6 @@ Future parseMessageFile( return ArbParser(options.findById).parseMessageFile(arb); } -class LocatedMessageFile { - final String path; - final MessageFile file; - - LocatedMessageFile({required this.path, required this.file}); -} - extension on YamlEditor { bool contains(List path) => parseAt(path, orElse: () => wrapAsYamlNode(null)).value != null; diff --git a/pkgs/messages_builder/lib/message_data_builder.dart b/pkgs/messages_builder/lib/message_data_builder.dart index 2f354c02..ebc5b355 100644 --- a/pkgs/messages_builder/lib/message_data_builder.dart +++ b/pkgs/messages_builder/lib/message_data_builder.dart @@ -8,6 +8,7 @@ import 'package:messages_serializer/messages_serializer.dart'; import 'package:path/path.dart' as p; import 'generation_options.dart'; +import 'message_code_builder.dart'; import 'message_file.dart'; class MessageDataBuilder { @@ -22,8 +23,7 @@ class MessageDataBuilder { }); Future> run() async { - print(''' -Searching "${p.relative(inputFolder.path, from: '.')}" for arb files.'''); + print('Starting to add arb files from $inputFolder to $outputFolder'); final arbFiles = await inputFolder .list() .where((file) => file is File) @@ -31,29 +31,14 @@ Searching "${p.relative(inputFolder.path, from: '.')}" for arb files.'''); .where((path) => p.extension(path) == '.arb') .toList(); + final mapping = {}; if (arbFiles.isEmpty) { - print('No arb files in ${p.relative(inputFolder.path, from: '.')}.'); - return {}; + print('No `.arb` files found in $inputFolder.'); + return mapping; } - final serializer = JsonSerializer(generationOptions.findById); - final inputOutputPairs = Map.fromEntries(arbFiles.map( - (inputPath) { - final assetName = p.setExtension( - p.basename(inputPath), - '.arb${serializer.extension}', - ); - final outputDataPath = p.join(outputFolder.path, assetName); - return MapEntry(inputPath, outputDataPath); - }, - )); - - if (arbFiles.isNotEmpty) { - print('Generating data files from arb sources.'); - } - for (final MapEntry(key: arbFilePath, value: outputDataPath) - in inputOutputPairs.entries) { - stdout.write('${p.relative(arbFilePath, from: '.')} --> '); + for (final arbFilePath in arbFiles) { + print('Generating $arbFilePath, bundle this in your assets.'); final arbFileUri = Uri.file(arbFilePath); final arbFileContents = await File.fromUri(arbFileUri).readAsString(); final messageBundle = await parseMessageFile(arbFileContents, options); @@ -62,18 +47,18 @@ Searching "${p.relative(inputFolder.path, from: '.')}" for arb files.'''); final data = _arbToData(messageBundle, arbFilePath, serializer); - stdout.writeln('${p.relative(outputDataPath, from: '.')}.'); - final dataFile = File(outputDataPath); - if (await dataFile.exists() && (await dataFile.readAsString() == data)) { - continue; - } else { - await dataFile.create(); - await dataFile.writeAsString(data); - print(''' -${p.relative(arbFilePath, from: '.')} --> ${p.relative(outputDataPath, from: '.')}'''); - } + final assetName = p.setExtension( + p.basename(arbFilePath), + '.arb${serializer.extension}', + ); + + final outputDataPath = outputFolder.uri.resolve(assetName); + final dataFile = File.fromUri(outputDataPath); + await dataFile.create(); + await dataFile.writeAsString(data); + mapping[arbFilePath] = outputDataPath.path; } - return inputOutputPairs; + return mapping; } String _arbToData(