2016-04-22 01:17:31 +00:00
|
|
|
library angel_configuration;
|
|
|
|
|
|
|
|
import 'dart:io';
|
|
|
|
import 'package:angel_framework/angel_framework.dart';
|
2016-11-23 20:51:20 +00:00
|
|
|
import 'package:angel_route/src/extensible.dart';
|
2016-04-22 01:17:31 +00:00
|
|
|
import 'package:yaml/yaml.dart';
|
|
|
|
|
2016-11-23 20:51:20 +00:00
|
|
|
final RegExp _equ = new RegExp(r'=$');
|
|
|
|
final RegExp _sym = new RegExp(r'Symbol\("([^"]+)"\)');
|
|
|
|
|
2016-11-23 23:40:42 +00:00
|
|
|
@proxy
|
2016-11-23 20:51:20 +00:00
|
|
|
class Configuration {
|
|
|
|
final Angel app;
|
|
|
|
Configuration(this.app);
|
|
|
|
|
|
|
|
operator [](key) => app.properties[key];
|
|
|
|
operator []=(key, value) => app.properties[key] = value;
|
|
|
|
|
|
|
|
noSuchMethod(Invocation invocation) {
|
|
|
|
if (invocation.memberName != null) {
|
|
|
|
String name = _sym.firstMatch(invocation.memberName.toString()).group(1);
|
|
|
|
|
|
|
|
if (invocation.isMethod) {
|
|
|
|
return Function.apply(app.properties[name], invocation.positionalArguments,
|
|
|
|
invocation.namedArguments);
|
|
|
|
} else if (invocation.isGetter) {
|
|
|
|
return app.properties[name];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
super.noSuchMethod(invocation);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-02 23:35:21 +00:00
|
|
|
_loadYamlFile(Angel app, File yamlFile) async {
|
|
|
|
if (await yamlFile.exists()) {
|
|
|
|
Map config = loadYaml(await yamlFile.readAsString());
|
2016-04-22 01:17:31 +00:00
|
|
|
for (String key in config.keys) {
|
|
|
|
app.properties[key] = config[key];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
loadConfigurationFile(
|
|
|
|
{String directoryPath: "./config", String overrideEnvironmentName}) {
|
2016-05-02 23:35:21 +00:00
|
|
|
return (Angel app) async {
|
2016-04-22 01:17:31 +00:00
|
|
|
Directory sourceDirectory = new Directory(directoryPath);
|
|
|
|
String environmentName = Platform.environment['ANGEL_ENV'] ?? 'development';
|
|
|
|
|
|
|
|
if (overrideEnvironmentName != null) {
|
|
|
|
environmentName = overrideEnvironmentName;
|
|
|
|
}
|
|
|
|
|
|
|
|
File defaultYaml = new File.fromUri(
|
|
|
|
sourceDirectory.absolute.uri.resolve("default.yaml"));
|
2016-05-02 23:35:21 +00:00
|
|
|
await _loadYamlFile(app, defaultYaml);
|
2016-04-22 01:17:31 +00:00
|
|
|
|
|
|
|
String configFilePath = "$environmentName.yaml";
|
|
|
|
File configFile = new File.fromUri(
|
|
|
|
sourceDirectory.absolute.uri.resolve(configFilePath));
|
|
|
|
|
2016-05-02 23:35:21 +00:00
|
|
|
await _loadYamlFile(app, configFile);
|
2016-11-28 02:03:05 +00:00
|
|
|
app.container.singleton(new Configuration(app));
|
2016-04-22 01:17:31 +00:00
|
|
|
};
|
|
|
|
}
|