Skip to content

Commit

Permalink
[cli_config] Introduce sync method (#246)
Browse files Browse the repository at this point in the history
Co-authored-by: Moritz <[email protected]>
  • Loading branch information
dcharkes and mosuem authored Mar 12, 2024
1 parent 35b25a7 commit 378790d
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 14 deletions.
5 changes: 5 additions & 0 deletions pkgs/cli_config/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 0.2.0

- **Breaking Change** Rename `Config.fromArgs` to `Config.fromArguments`.
- Add `Config.fromArgumentsSync`.

## 0.1.2

- Add usage docs to the readme.
Expand Down
6 changes: 3 additions & 3 deletions pkgs/cli_config/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,11 @@ In the API they are made available lower-cased and with underscores, and
## Example usage

This example creates a configuration which first looks for command-line defines
in the `args` list then looks in `Platform.environment`, then looks in any local
configuration file.
in the `arguments` list then looks in `Platform.environment`, then looks in any
local configuration file.

```dart
final config = await Config.fromArgs(args: args);
final config = await Config.fromArguments(arguments: arguments);
final pathValue =
config.optionalPath('my_path', resolveUri: true, mustExist: false);
print(pathValue?.toFilePath());
Expand Down
2 changes: 1 addition & 1 deletion pkgs/cli_config/example/bin/cli_config_example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import 'package:cli_config/cli_config.dart';

Future<void> main(List<String> args) async {
final config = await Config.fromArgs(args: args);
final config = await Config.fromArguments(arguments: args);
final myPath =
config.optionalPath('my_path', resolveUri: true, mustExist: false);
print(myPath?.toFilePath());
Expand Down
45 changes: 41 additions & 4 deletions pkgs/cli_config/lib/src/config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ class Config {

/// Constructs a config by parsing CLI arguments and loading the config file.
///
/// The [args] must be commandline arguments.
/// The [arguments] must be commandline arguments.
///
/// If provided, [environment] must be a map containing environment variables.
/// If not provided, [environment] defaults to [Platform.environment].
Expand All @@ -175,12 +175,12 @@ class Config {
/// If not provided, [workingDirectory] defaults to [Directory.current].
///
/// This async constructor is intended to be used directly in CLI files.
static Future<Config> fromArgs({
required List<String> args,
static Future<Config> fromArguments({
required List<String> arguments,
Map<String, String>? environment,
Uri? workingDirectory,
}) async {
final results = CliParser().parse(args);
final results = CliParser().parse(arguments);

// Load config file.
final configFile = results['config'] as String?;
Expand All @@ -200,6 +200,43 @@ class Config {
);
}

/// Constructs a config by parsing CLI arguments and loading the config file.
///
/// The [arguments] must be commandline arguments.
///
/// If provided, [environment] must be a map containing environment variables.
/// If not provided, [environment] defaults to [Platform.environment].
///
/// If provided, [workingDirectory] is used to resolves paths inside
/// [environment].
/// If not provided, [workingDirectory] defaults to [Directory.current].
///
/// This synchronous constructor is intended to be used directly in CLI files.
static Config fromArgumentsSync({
required List<String> arguments,
Map<String, String>? environment,
Uri? workingDirectory,
}) {
final results = CliParser().parse(arguments);

// Load config file.
final configFile = results['config'] as String?;
String? fileContents;
Uri? fileSourceUri;
if (configFile != null) {
fileContents = File(configFile).readAsStringSync();
fileSourceUri = Uri.file(configFile);
}

return Config.fromConfigFileContents(
commandLineDefines: results['define'] as List<String>,
workingDirectory: workingDirectory ?? Directory.current.uri,
environment: environment ?? Platform.environment,
fileContents: fileContents,
fileSourceUri: fileSourceUri,
);
}

/// Lookup a string value in this config.
///
/// First tries CLI argument defines, then environment variables, and
Expand Down
2 changes: 1 addition & 1 deletion pkgs/cli_config/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: cli_config
description: >-
A library to take config values from configuration files, CLI arguments, and
environment variables.
version: 0.1.2
version: 0.2.0
repository: https://github.com/dart-lang/tools/tree/main/pkgs/cli_config

environment:
Expand Down
19 changes: 14 additions & 5 deletions pkgs/cli_config/test/cli_config_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,8 @@ void main() {
}
},
));
final config = await Config.fromArgs(
args: [
final config = await Config.fromArguments(
arguments: [
'--config',
configFile.path,
'-Dbuild.out_dir=path/in/cli_arguments/',
Expand Down Expand Up @@ -194,15 +194,24 @@ void main() {
}
},
));
final config = await Config.fromArgs(
args: [

final config = await Config.fromArguments(
arguments: [
'--config',
configFile.path,
],
);

final result = config.optionalPath('build.out_dir');
expect(result!.path, resolvedPath.path);

final configSync = Config.fromArgumentsSync(
arguments: [
'--config',
configFile.path,
],
);
final resultSync = configSync.optionalPath('build.out_dir');
expect(resultSync!.path, resolvedPath.path);
});

test('provide pre-parsed config', () {
Expand Down

0 comments on commit 378790d

Please sign in to comment.