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

added support for flutter 'lib\' dir #700

Closed
Closed
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 pkgs/intl_translation/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## 0.19.1-dev
* Added support for Flutter `lib/` folder.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove references to Flutter


## 0.19.0-dev
* Always generate null safe code, remove `null-safe` flag.
* Add example for `he` locale.
Expand Down
9 changes: 9 additions & 0 deletions pkgs/intl_translation/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@ dart run intl_translation:extract_to_arb --output-dir=target/directory \
my_program.dart more_of_my_program.dart
```

This support passing a folder directory to it that contains `.dart` files. The code will automatically extract all `.dart` files
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
This support passing a folder directory to it that contains `.dart` files. The code will automatically extract all `.dart` files
This supports passing a folder directory that contains `.dart` files. The code will automatically extract all `.dart` files

in the folder AND its subfolders as well. This means it can be used for flutter `lib/` folder as well.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
in the folder AND its subfolders as well. This means it can be used for flutter `lib/` folder as well.
in the folder and its subfolders.

For example, to automatically extract all files in your `lib/` folder, you can use
`--lib-dir` to provide path to `lib` directory. E.g run in project root folder
```dart
dart run intl_translation:extract_to_arb --output-dir=target/directory --lib-dir=./lib
```


This supports wildcards. For example, to extract messages from a series of files in path `lib/**/*.dart`, you can run
```dart
dart run intl_translation:extract_to_arb --output-dir=target/directory
Expand Down
17 changes: 15 additions & 2 deletions pkgs/intl_translation/bin/extract_to_arb.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,13 @@ import 'package:intl_translation/src/arb_generation.dart';
import 'package:intl_translation/src/directory_utils.dart';
import 'package:path/path.dart' as path;

void main(List<String> args) {
void main(List<String> args) async {
var targetDir = '.';
var outputFilename = 'intl_messages.arb';
String? sourcesListFile;
String? libDir;
// ignore: omit_local_variable_types
List<String> filesInFlutterLib = [];
var transformer = false;
var parser = ArgParser();
var extract = MessageExtraction();
Expand Down Expand Up @@ -99,6 +102,11 @@ void main(List<String> args) {
help: "Fail for messages that don't have a description.",
callback: (val) => extract.descriptionRequired = val,
);
parser.addOption(
'lib-dir',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about a more general name, such as input or similar?

callback: (value) => libDir = value,
help: 'Directory to lib/* folder so that all files are parsed',
);

var argResults = parser.parse(args);
var showHelp = (argResults['help'] as bool?) ?? false;
Expand All @@ -119,9 +127,14 @@ void main(List<String> args) {
allMessages['@@last_modified'] = DateTime.now().toIso8601String();
}

if (libDir != null) {
filesInFlutterLib = await getDartFilesInFolder(libDir!);
}

var dartFiles = <String>[
...args.where((x) => x.endsWith('.dart')),
...linesFromFile(sourcesListFile)
...linesFromFile(sourcesListFile),
...filesInFlutterLib
];
dartFiles
.map((dartFile) => extract.parseFile(File(dartFile), transformer))
Expand Down
50 changes: 49 additions & 1 deletion pkgs/intl_translation/lib/src/directory_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
// BSD-style license that can be found in the LICENSE file.

import 'dart:io';

import 'package:path/path.dart' as path;
import 'ffile_system_entity.dart';

/// Takes a file with a list of file paths, one per line, and returns the names
/// as paths in terms of the directory containing [fileName].
Expand All @@ -30,3 +30,51 @@ String _relativeToBase(String base, String filename) {
return filename;
}
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about a simpler

Directory(pathGivenByUser)
      .listSync(recursive: true)
      .whereType<File>()
      .where((element) => element.path.endsWith('.dart'))
      .toList()

This would even by synchronous.

Future<List<String>> getDartFilesInFolder(String folderPath) async {
var dir = Directory(folderPath);
// ignore: omit_local_variable_types
List<String> results = [];
if (!dir.existsSync()) {
return results;
}
var filesAndFolders = await getFilesAndFoldersInDir(dir);
for (var i = 0; i < filesAndFolders.length; i++) {
var entity = filesAndFolders[i];
if (entity.isDartFile) {
results.add(entity.path);
continue;
}
if (entity.isDir) {
var results2 = await getDartFilesInFolder(entity.path);
results = [...results, ...results2];
continue;
}
}
return results;
}

/// Scan directory and return all the files and folders
Future<List<FFileSystemEntity>> getFilesAndFoldersInDir(Directory dir) async {
// ignore: omit_local_variable_types
List<FFileSystemEntity> results = [];
// ignore: omit_local_variable_types
Stream<FileSystemEntity> contents = dir.list(
recursive: false,
followLinks: false,
);
await for (FileSystemEntity entity in contents) {
var type = await FileSystemEntity.type(entity.path);
var entity2 = FFileSystemEntity(
entity: entity,
type: type,
);

if (!entity2.isDir && !entity2.isFile) {
continue;
}

results.add(entity2);
}
return results;
}
25 changes: 25 additions & 0 deletions pkgs/intl_translation/lib/src/ffile_system_entity.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright (c) 2023, 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';

///This class tries to improve code speed
///by adding cache functionality as well as helper
///methods to FileSystemEntity

class FFileSystemEntity {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be unnecessary with the change above.

final FileSystemEntity entity;
final FileSystemEntityType type;

FFileSystemEntity({
//cache the type
required this.type,
required this.entity,
});

bool get isDir => type == FileSystemEntityType.directory;
bool get isFile => type == FileSystemEntityType.file;
bool get isDartFile => entity.path.toLowerCase().endsWith('.dart');
String get path => entity.path;
}
2 changes: 1 addition & 1 deletion pkgs/intl_translation/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: intl_translation
version: 0.19.0-dev
version: 0.19.1-dev
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can just leave it at 0.19.0-dev while this package is not published.

description: >-
Contains code to deal with internationalized/localized messages,
date and number formatting and parsing, bi-directional text, and
Expand Down
Loading