-
Notifications
You must be signed in to change notification settings - Fork 39
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -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 | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
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 | ||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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(); | ||
|
@@ -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', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How about a more general name, such as |
||
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; | ||
|
@@ -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)) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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]. | ||
|
@@ -30,3 +30,51 @@ String _relativeToBase(String base, String filename) { | |
return filename; | ||
} | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How about a simpler
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; | ||
} |
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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
} |
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can just leave it at |
||
description: >- | ||
Contains code to deal with internationalized/localized messages, | ||
date and number formatting and parsing, bi-directional text, and | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove references to Flutter