Skip to content
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

Feat/find dart files from directory #10

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Changelog

## 0.3.0
* add ability to find dart files in a directory

## 0.2.1
* bump deps

Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ results need to be incorporated.
To extract messages, run the `extract_to_arb.dart` program.

pub run intl_generator:extract_to_arb --output-dir=target/directory
my_program.dart more_of_my_program.dart
my_program.dart more_of_my_program.dart my/target/programs/directory

This will produce a file `intl_messages.arb` with the messages from all of these
programs. This is an [ARB][ARB] format file which can be used for input to
Expand All @@ -32,7 +32,7 @@ locale.

```
pub run intl_generator:generate_from_arb --generated-file-prefix=<prefix>
<my_dart_files> <translated_ARB_files>
<my_dart_files> <my_dart_files_directory> <translated_ARB_files>
```

This will generate Dart libraries, one per locale, which contain the
Expand Down
5 changes: 3 additions & 2 deletions bin/extract_to_arb.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import 'package:intl_generator/src/arb_generation.dart';
import 'package:intl_generator/src/directory_utils.dart';
import 'package:path/path.dart' as path;

main(List<String> args) {
Future<void> main(List<String> args) async {
var targetDir;
var outputFilename;
String? sourcesListFile;
Expand Down Expand Up @@ -92,7 +92,8 @@ main(List<String> args) {
allMessages["@@last_modified"] = new DateTime.now().toIso8601String();
}

var dartFiles = args.where((x) => x.endsWith(".dart")).toList();
final List<String> dartFiles = await findDartFilesInDirectories(args);
dartFiles.addAll(args.where((String file) => file.endsWith(".dart")));
dartFiles.addAll(linesFromFile(sourcesListFile));
for (var arg in dartFiles) {
var messages = extraction.parseFile(new File(arg), transformer);
Expand Down
5 changes: 3 additions & 2 deletions bin/generate_from_arb.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ late Map<String, List<MainMessage>> messages;

const jsonDecoder = const JsonCodec();

main(List<String> args) {
Future<void> main(List<String> args) async {
var targetDir;
var parser = new ArgParser();
var extraction = new MessageExtraction();
Expand Down Expand Up @@ -86,7 +86,8 @@ main(List<String> args) {
"don't need to be specified for messages.");

parser.parse(args);
var dartFiles = args.where((x) => x.endsWith("dart")).toList();
final List<String> dartFiles = await findDartFilesInDirectories(args);
dartFiles.addAll(args.where((String file) => file.endsWith(".dart")));
var jsonFiles = args.where((x) => x.endsWith(".arb")).toList();
dartFiles.addAll(linesFromFile(sourcesListFile));
jsonFiles.addAll(linesFromFile(translationsListFile));
Expand Down
29 changes: 29 additions & 0 deletions lib/src/directory_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,32 @@ String _relativeToBase(String base, String filename) {
return filename;
}
}

List<Directory> _findDirectories(List<String> paths) {
final List<Directory> directories = <Directory>[];
for (final String path in paths) {
if (FileSystemEntity.typeSync(path) == FileSystemEntityType.directory) {
directories.add(Directory(path));
}
}
return directories;
}

Future<List<String>> findDartFilesInDirectories(
List<String> paths,
) async {
final List<Directory> directories = _findDirectories(paths);
final List<String> dartFiles = <String>[];

for (final Directory directory in directories) {
final List<FileSystemEntity> entities =
await directory.list(recursive: true).toList();
final Iterable<FileSystemEntity> files = entities.where(_isDartFile);
dartFiles.addAll(files.map((FileSystemEntity file) => file.path));
}
return dartFiles;
}

bool _isDartFile(FileSystemEntity entity) {
return entity is File && entity.path.endsWith(".dart");
}
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: intl_generator
version: 0.2.1
version: 0.3.0
description: >-
Contains code to deal with internationalized/localized messages,
date and number formatting and parsing, bi-directional text, and
Expand Down