Skip to content

Commit

Permalink
Merging changes
Browse files Browse the repository at this point in the history
  • Loading branch information
mosuem committed Nov 14, 2024
1 parent 5bd9135 commit 8c8a7a5
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 88 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

This file was deleted.

55 changes: 16 additions & 39 deletions pkgs/messages_builder/lib/message_code_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -30,36 +30,31 @@ class MessageCodeBuilder {
});

Future<void> 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,
Expand Down Expand Up @@ -88,17 +83,7 @@ class MessageCodeBuilder {
}
}

List<MessageFileMetadata> collectMetadata(
List<LocatedMessageFile> 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<List<LocatedMessageFile>> parseMessageFiles() async =>
Future<List<LocatedMessageFile>> _parseMessageFiles() async =>
Future.wait(inputToOutputFiles.entries
.map((p) async => LocatedMessageFile(
path: path.relative(p.value, from: Directory.current.path),
Expand Down Expand Up @@ -132,7 +117,6 @@ The files $filesInContext have no metadata, so it is not clear which one is the
Future<void> includeFilesInPubspec(
String? context,
Iterable<String> fileList,
Directory generatedCodeFiles,
) async {
final contextMessage =
context != null ? 'For the messages in $context, the' : 'The';
Expand Down Expand Up @@ -174,13 +158,6 @@ Future<MessageFile> 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<String> path) =>
parseAt(path, orElse: () => wrapAsYamlNode(null)).value != null;
Expand Down
51 changes: 18 additions & 33 deletions pkgs/messages_builder/lib/message_data_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -22,38 +23,22 @@ class MessageDataBuilder {
});

Future<Map<String, String>> 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)
.map((file) => file.path)
.where((path) => p.extension(path) == '.arb')
.toList();

final mapping = <String, String>{};
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);
Expand All @@ -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(
Expand Down

0 comments on commit 8c8a7a5

Please sign in to comment.