Skip to content
This repository was archived by the owner on Apr 29, 2024. It is now read-only.

Commit e4b5858

Browse files
committed
Use package:config
1 parent e492c8f commit e4b5858

File tree

2 files changed

+46
-83
lines changed

2 files changed

+46
-83
lines changed

jnigen/lib/src/config/yaml_reader.dart

Lines changed: 39 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,17 @@ import 'dart:io';
66

77
import 'package:args/args.dart';
88
import 'package:yaml/yaml.dart';
9+
import 'package:config/config.dart' as pkg_config;
910

1011
import 'config_exception.dart';
1112

1213
/// YAML Reader which enables to override specific values from command line.
1314
class YamlReader {
14-
YamlReader.of(this.cli, this.yaml, this.yamlFile);
15-
Map<String, String> cli;
16-
Map<dynamic, dynamic> yaml;
17-
File? yamlFile;
15+
final pkg_config.Config _config;
16+
17+
final Uri? _configRoot;
18+
19+
YamlReader.of(this._config, this._configRoot);
1820

1921
/// Parses the provided command line arguments and returns a [YamlReader].
2022
///
@@ -42,19 +44,15 @@ class YamlReader {
4244
stderr.writeln(parser.usage);
4345
exit(1);
4446
}
45-
final configFile = results['config'] as String?;
46-
Map<dynamic, dynamic> yamlMap = {};
47-
if (configFile != null) {
47+
final configFilePath = results['config'] as String?;
48+
String? configFileContents;
49+
Uri? configFileUri;
50+
if (configFilePath != null) {
4851
try {
49-
final yamlInput = loadYaml(File(configFile).readAsStringSync(),
50-
sourceUrl: Uri.file(configFile));
51-
if (yamlInput is Map) {
52-
yamlMap = yamlInput;
53-
} else {
54-
throw ConfigException('YAML config must be set of key value pairs');
55-
}
52+
configFileContents = File(configFilePath).readAsStringSync();
53+
configFileUri = File(configFilePath).uri;
5654
} on Exception catch (e) {
57-
stderr.writeln('cannot read $configFile: $e');
55+
stderr.writeln('Cannot read $configFilePath: $e.');
5856
}
5957
}
6058
final regex = RegExp('([a-z-_.]+)=(.+)');
@@ -70,96 +68,54 @@ class YamlReader {
7068
}
7169
}
7270
return YamlReader.of(
73-
properties, yamlMap, configFile != null ? File(configFile) : null);
71+
pkg_config.Config(
72+
cliDefines: results['override'],
73+
environment: Platform.environment,
74+
fileContents: configFileContents,
75+
fileSourceUri: configFileUri),
76+
configFileUri?.resolve('.'),
77+
);
7478
}
7579

76-
bool? getBool(String property) {
77-
if (cli.containsKey(property)) {
78-
final v = cli[property]!;
79-
if (v == 'true') {
80-
return true;
81-
}
82-
if (v == 'false') {
83-
return false;
84-
}
85-
throw ConfigException('expected boolean value for $property, got $v');
86-
}
87-
return getYamlValue<bool>(property);
88-
}
80+
bool? getBool(String property) => _config.getBool(property);
8981

90-
String? getString(String property) {
91-
final configValue = cli[property] ?? getYamlValue<String>(property);
92-
return configValue;
93-
}
82+
String? getString(String property) => _config.getString(property);
9483

9584
/// Same as [getString] but path is resolved relative to YAML config if it's
9685
/// from YAML config.
97-
String? getPath(String property) {
98-
final cliOverride = cli[property];
99-
if (cliOverride != null) return cliOverride;
100-
final path = getYamlValue<String>(property);
101-
if (path == null) return null;
102-
// In (very unlikely) case YAML config didn't come from a file,
103-
// do not try to resolve anything.
104-
if (yamlFile == null) return path;
105-
final yamlDir = yamlFile!.parent;
106-
return yamlDir.uri.resolve(path).toFilePath();
107-
}
86+
String? getPath(String property) => _config.getPath(property)?.path;
10887

109-
List<String>? getStringList(String property) {
110-
final configValue = cli[property]?.split(';') ??
111-
getYamlValue<YamlList>(property)?.cast<String>();
112-
return configValue;
113-
}
88+
List<String>? getStringList(String property) => _config.getStringList(
89+
property,
90+
splitCliPattern: ';',
91+
combineAllConfigs: false,
92+
);
11493

11594
List<String>? getPathList(String property) {
116-
final cliOverride = cli[property]?.split(';');
117-
if (cliOverride != null) return cliOverride;
118-
final paths = getYamlValue<YamlList>(property)?.cast<String>();
119-
if (paths == null) return null;
120-
// In (very unlikely) case YAML config didn't come from a file.
121-
if (yamlFile == null) return paths;
122-
final yamlDir = yamlFile!.parent;
123-
return paths.map((path) => yamlDir.uri.resolve(path).toFilePath()).toList();
95+
final configResult = _config.getPathList(
96+
property,
97+
combineAllConfigs: false,
98+
splitCliPattern: ';',
99+
);
100+
return configResult?.map((e) => e.path).toList();
124101
}
125102

126103
String? getOneOf(String property, Set<String> values) {
127-
final value = cli[property] ?? getYamlValue<String>(property);
104+
final value = _config.getString(property);
128105
if (value == null || values.contains(value)) {
129106
return value;
130107
}
131108
throw ConfigException('expected one of $values for $property');
132109
}
133110

134111
Map<String, String>? getStringMap(String property) {
135-
final value = getYamlValue<YamlMap>(property);
112+
final value = _config.getFileValue<YamlMap>(property);
136113
return value?.cast<String, String>();
137114
}
138115

139-
bool hasValue(String property) => getYamlValue<dynamic>(property) != null;
140-
141-
T? getYamlValue<T>(String property) {
142-
final path = property.split('.');
143-
dynamic cursor = yaml;
144-
String current = '';
145-
for (var i in path) {
146-
if (cursor is YamlMap || cursor is Map) {
147-
cursor = cursor[i];
148-
} else {
149-
throw ConfigException('expected $current to be a YAML map');
150-
}
151-
current = [if (current != '') current, i].join('.');
152-
if (cursor == null) {
153-
return null;
154-
}
155-
}
156-
if (cursor is! T) {
157-
throw ConfigException(
158-
'expected $T for $property, got ${cursor.runtimeType}');
159-
}
160-
return cursor;
161-
}
116+
bool hasValue(String property) =>
117+
_config.getFileValue<dynamic>(property) != null;
162118

163119
/// Returns URI of the directory containing YAML config.
164-
Uri? getConfigRoot() => yamlFile?.parent.uri;
120+
Uri? getConfigRoot() => _configRoot;
165121
}

jnigen/pubspec.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
# for details. All rights reserved. Use of this source code is governed by a
33
# BSD-style license that can be found in the LICENSE file.
44

5+
# DO NOT MERGE: prototype.
6+
publish_to: none
7+
58
name: jnigen
69
version: 0.3.0
710
description: Experimental generator for FFI+JNI bindings.
@@ -17,6 +20,10 @@ dependencies:
1720
args: ^2.3.0
1821
yaml: ^3.1.0
1922
logging: ^1.0.2
23+
config:
24+
git:
25+
url: https://github.com/dcharkes/config.git
26+
ref: a0efac8eec57168f62102c1e5f045abf960f24ca
2027

2128
dev_dependencies:
2229
lints: ^2.0.0

0 commit comments

Comments
 (0)