Skip to content

Commit

Permalink
Add builder for messages
Browse files Browse the repository at this point in the history
  • Loading branch information
mosuem committed May 27, 2024
1 parent 6a9ed79 commit c170bd1
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 31 deletions.
38 changes: 7 additions & 31 deletions pkgs/messages/example_json/hook/build.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,16 @@
// 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/messages_build.dart';
import 'package:native_assets_cli/native_assets_cli.dart';

const package = 'example_json';
void main(List<String> args) {
final dir = 'lib/';
build(args, (config, output) async {
getArbs(config, output, dir, [
'testarb',
'testarbctx2',
'testarbctx2_fr',
]);
});
}
final builder = MessageBuilder(
arbFiles: ['lib/testarb.arb', 'lib/testarbctx2.arb'],
locales: ['en', 'de', 'fr'],
);

void getArbs(
BuildConfig config,
BuildOutput output,
String dir,
List<String> assets,
) {
output.addAssets(assets.map((asset) => getArb(config, dir, asset)));
output.addDependencies([
config.packageRoot.resolve('hook/build.dart'),
]);
}

DataAsset getArb(BuildConfig config, String dir, String name) {
final directory = Directory(dir);
final resolved = directory.uri.resolve('$name.json');
return DataAsset(
package: package,
name: resolved.path,
file: config.packageRoot.resolveUri(resolved),
);
await builder.run(config: config, output: output, logger: null);
});
}
51 changes: 51 additions & 0 deletions pkgs/messages/lib/messages_build.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// 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.

import 'dart:io';

import 'package:logging/logging.dart';
import 'package:native_assets_cli/native_assets_cli.dart';
import 'package:path/path.dart' as p;

// implements Builder
class MessageBuilder {
final List<String> arbFiles;
final List<String> locales;

MessageBuilder({
required this.arbFiles,
required this.locales,
});

Future<void> run({
required BuildConfig config,
required BuildOutput output,
required Logger? logger,
}) async {
DataAsset fileToAsset(String relativePath) => DataAsset(
package: config.packageName,
name: relativePath,
file: config.packageRoot.resolve(relativePath),
);

for (final arb in arbFiles) {
final arbNoExtension = p.withoutExtension(arb);
for (final locale in locales) {
final data = p.setExtension(arbNoExtension, '_$locale.json');
final asset = fileToAsset(data);
if (await File.fromUri(asset.file!).exists()) {
output.addAsset(asset);
} else {
logger?.warning('Could not find messages for locale "$locale" and'
' arb "$arb" at ${asset.file}');
}
}
}

output.addDependencies([
...arbFiles,
'hook/build.dart',
].map(config.packageRoot.resolve));
}
}
3 changes: 3 additions & 0 deletions pkgs/messages/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ environment:
dependencies:
collection: ^1.17.1
intl: ^0.18.0
logging: ^1.2.0
native_assets_cli: ^0.6.0
path: ^1.9.0

dev_dependencies:
dart_flutter_team_lints: ^2.1.1
Expand Down

0 comments on commit c170bd1

Please sign in to comment.