Skip to content

Commit

Permalink
Changes
Browse files Browse the repository at this point in the history
  • Loading branch information
mosuem committed May 28, 2024
1 parent dc27228 commit d951e14
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 53 deletions.
28 changes: 9 additions & 19 deletions pkgs/messages/example_json/hook/build.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,21 @@
// 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.

import 'dart:io';

import 'package:messages_builder/generation_options.dart';
import 'package:messages_builder/hook.dart';
import 'package:native_assets_cli/native_assets_cli.dart';

void main(List<String> args) {
build(args, (config, output) async {
final builder = MessagesDataBuilder(
arbFiles: [
'assets/l10n/testarb.arb',
'assets/l10n/testarb_de.arb',
'assets/l10n/testarbctx2.arb',
'assets/l10n/testarbctx2_fr.arb',
],
generationOptions: await generationOptions(config),
);
// final builder = MessagesDataBuilder.fromFiles(
// [
// 'assets/l10n/testarb.arb',
// 'assets/l10n/testarb_de.arb',
// 'assets/l10n/testarbctx2.arb',
// 'assets/l10n/testarbctx2_fr.arb',
// ],
// );
final builder = MessagesDataBuilder.fromFolder('assets/l10n/');

await builder.run(config: config, output: output, logger: null);
});
}

Future<GenerationOptions> generationOptions(BuildConfig config) async {
final packageRoot = config.packageRoot;
final pubspecUri = packageRoot.resolve('pubspec.yaml');
final file = File.fromUri(pubspecUri);
return GenerationOptions.fromPubspec(await file.readAsString());
}
104 changes: 70 additions & 34 deletions pkgs/messages_builder/lib/hook.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,45 @@ import 'builder.dart';
import 'generation_options.dart';
import 'message_with_metadata.dart';

Future<GenerationOptions> _generationOptions(BuildConfig config) async {
final packageRoot = config.packageRoot;
final pubspecUri = packageRoot.resolve('pubspec.yaml');
final file = File.fromUri(pubspecUri);
return GenerationOptions.fromPubspec(await file.readAsString());
}

class MessagesDataBuilder {
final GenerationOptions generationOptions;
final List<String> arbFiles;
final Future<List<String>> Function(BuildConfig config) arbFiles;

MessagesDataBuilder(
{required this.generationOptions, required this.arbFiles});
//TODO allow arbs from other locations than package root subfolders
MessagesDataBuilder.fromFiles(List<String> relativePaths)
: arbFiles = ((config) async => relativePaths);

Serializer<String> get serializer =>
JsonSerializer(generationOptions.findById);
MessagesDataBuilder.fromFolder(String relativePath)
: arbFiles = ((config) =>
Directory(config.packageRoot.resolve(relativePath).path)
.list()
.where((file) => file is File)
.map((file) => file.path)
.where((path) => p.extension(path) == '.arb')
.map((path) => p.relative(path, from: config.packageRoot.path))
.toList());

Future<void> run({
required BuildConfig config,
required BuildOutput output,
required Logger? logger,
}) async {
for (final arbFilePath in arbFiles) {
final files = await arbFiles(config);
for (final arbFilePath in files) {
if (p.isAbsolute(arbFilePath)) {
throw ArgumentError('Paths for .arb files must be relative to the'
' package root ${config.packageRoot}, but $arbFilePath is'
' absolute.');
}
final arbFileUri = config.packageRoot.resolve(arbFilePath);
final arbFile = File.fromUri(arbFileUri);
final arbFileContents = await arbFile.readAsString();
final arbFileContents = await File.fromUri(arbFileUri).readAsString();
final generationOptions = await _generationOptions(config);
final messageBundle = await parseMessageFile(
arbFileContents,
generationOptions,
Expand All @@ -41,51 +61,67 @@ class MessagesDataBuilder {
/// unpacked at runtime so that the messages can be read from it.
///
/// Returns the list of indices of the messages which are visible to the user.
final serialization = _serializeArb(messageBundle, arbFilePath);
final serializer = _buildSerializer(generationOptions);

final data = _arbToDataFile(
messageBundle,
arbFilePath,
serializer,
);

final (fileName, file) = await _createDataFile(arbFilePath, config);
final assetName = _assetName(
p.relative(arbFileUri.path, from: config.packageRoot.path),
serializer.extension,
);
final file = await _createAssetFile(assetName, config);

await _writeDataToFile(serialization, file);
await _writeDataToFile(data, file);

output.addAsset(DataAsset(
package: config.packageName,
name: fileName,
name: assetName,
file: file.uri,
));

output.addDependency(arbFileUri);
}
output.addDependency(config.packageRoot.resolve('hook/build.dart'));
}

Serialization<String> _serializeArb(
MessagesWithMetadata messageBundle, String arbFilePath) =>
serializer.serialize(
messageBundle.hash,
messageBundle.locale ?? inferLocale(arbFilePath) ?? 'en_US',
messageBundle.messages.map((e) => e.message).toList(),
);
String _arbToDataFile(MessagesWithMetadata messageBundle, String arbFilePath,
Serializer<String> serializer) =>
serializer
.serialize(
messageBundle.hash,
messageBundle.locale ?? inferLocale(arbFilePath) ?? 'en_US',
messageBundle.messages.map((e) => e.message).toList(),
)
.data;

String _assetName(String relativePath, String extension) {
final dataFile = p.setExtension(relativePath, extension);
return dataFile;
}

Serializer<String> _buildSerializer(GenerationOptions generationOptions) =>
JsonSerializer(generationOptions.findById);

Future<File> _createAssetFile(String dataFile, BuildConfig config) async {
final outputDirectory =
Directory.fromUri(config.outputDirectory.resolve(config.packageName));
final file = File.fromUri(outputDirectory.uri.resolve(dataFile));
await file.create(recursive: true);
return file;
}

Future<void> _writeDataToFile<T>(
Serialization<T> serialization,
T data,
File file,
) async {
final data = serialization.data;
if (data is Uint8List) {
await file.writeAsBytes(data);
} else if (data is String) {
await file.writeAsString(data);
}
}

Future<(String, File)> _createDataFile(
String arbFilePath,
BuildConfig config,
) async {
final dataFile = p.setExtension(arbFilePath, serializer.extension);
final directory =
Directory.fromUri(config.outputDirectory.resolve(config.packageName));
final file = File.fromUri(directory.uri.resolve(dataFile));
await file.create(recursive: true);
return (dataFile, file);
}
}

0 comments on commit d951e14

Please sign in to comment.