This commit is contained in:
regiostech 2016-04-21 21:17:31 -04:00
parent d0b3e20a5f
commit c71dd265de
13 changed files with 241 additions and 0 deletions

76
.gitignore vendored Normal file
View file

@ -0,0 +1,76 @@
# Created by .ignore support plugin (hsz.mobi)
### JetBrains template
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
# User-specific stuff:
.idea/workspace.xml
.idea/tasks.xml
.idea/dictionaries
.idea/vcs.xml
.idea/jsLibraryMappings.xml
# Sensitive or high-churn files:
.idea/dataSources.ids
.idea/dataSources.xml
.idea/dataSources.local.xml
.idea/sqlDataSources.xml
.idea/dynamic.xml
.idea/uiDesigner.xml
# Gradle:
.idea/gradle.xml
.idea/libraries
# Mongo Explorer plugin:
.idea/mongoSettings.xml
## File-based project format:
*.iws
## Plugin-specific files:
# IntelliJ
/out/
# mpeltonen/sbt-idea plugin
.idea_modules/
# JIRA plugin
atlassian-ide-plugin.xml
# Crashlytics plugin (for Android Studio and IntelliJ)
com_crashlytics_export_strings.xml
crashlytics.properties
crashlytics-build.properties
fabric.properties
### Dart template
# See https://www.dartlang.org/tools/private-files.html
# Files and directories created by pub
.buildlog
.packages
.project
.pub/
build/
**/packages/
# Files created by dart2js
# (Most Dart developers will use pub build to compile Dart, use/modify these
# rules if you intend to use dart2js directly
# Convention is to use extension '.dart.js' for Dart compiled to Javascript to
# differentiate from explicit Javascript files)
*.dart.js
*.part.js
*.js.deps
*.js.map
*.info.json
# Directory created by dartdoc
doc/api/
# Don't commit pubspec lock file
# (Library packages only! Remove pattern if developing an application package)
pubspec.lock
.idea/

View file

@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="WEB_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$">
<excludeFolder url="file://$MODULE_DIR$/.pub" />
<excludeFolder url="file://$MODULE_DIR$/packages" />
<excludeFolder url="file://$MODULE_DIR$/test/config/packages" />
<excludeFolder url="file://$MODULE_DIR$/test/packages" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="Dart SDK" level="application" />
<orderEntry type="library" name="Dart Packages" level="project" />
</component>
</module>

6
.idea/encodings.xml Normal file
View file

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding">
<file url="PROJECT" charset="UTF-8" />
</component>
</project>

14
.idea/misc.xml Normal file
View file

@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="PhpWorkspaceProjectConfiguration" backward_compatibility_performed="true" />
<component name="ProjectLevelVcsManager" settingsEditedManually="false">
<OptionsSetting value="true" id="Add" />
<OptionsSetting value="true" id="Remove" />
<OptionsSetting value="true" id="Checkout" />
<OptionsSetting value="true" id="Update" />
<OptionsSetting value="true" id="Status" />
<OptionsSetting value="true" id="Edit" />
<ConfirmationsSetting value="0" id="Add" />
<ConfirmationsSetting value="0" id="Remove" />
</component>
</project>

8
.idea/modules.xml Normal file
View file

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/angel_configuration.iml" filepath="$PROJECT_DIR$/.idea/angel_configuration.iml" />
</modules>
</component>
</project>

View file

@ -0,0 +1,9 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="All Tests" type="DartTestRunConfigurationType" factoryName="Dart Test" singleton="true">
<option name="envs">
<entry key="ANGEL_ENV" value="development" />
</option>
<option name="filePath" value="$PROJECT_DIR$/test/all_tests.dart" />
<method />
</configuration>
</component>

33
README.md Normal file
View file

@ -0,0 +1,33 @@
# Angel Configuration
YAML configuration loader for Angel.
# About
Any web app needs different configuration for development and production. This plugin will search
for a `config/default.yaml` file. If it is found, configuratiom from it is loaded into `angel.properties`.
Then, it will look for a `config/$ANGEL_ENV` file. (i.e. config/development.yaml). If this found, all of its
configuration be loaded, and will override anything loaded from the `default.yaml` file. This allows for your
app to work under different conditions without you re-coding anything. :)
# Installation
In `pubspec.yaml`:
dependencies:
angel_framework: ^0.0.0-dev
angel_static: ^1.0.0-beta
# Usage
```dart
import 'dart:io';
import 'package:angel_framework/angel_framework.dart';
import 'package:angel_configuration/angel_configuration.dart';
main() async {
Angel angel = new Angel();
angel.configure(loadConfigurationFile()); // It's that easy
}
```
`loadConfigurationFile` also accepts a `sourceDirectory` or `overrideEnvironmentName` parameter.
The former will allow you to search in a directory other than `config`, and the latter lets you
override `$ANGEL_ENV` by specifying a specific configuration name to look for (i.e. 'production').

View file

@ -0,0 +1,36 @@
library angel_configuration;
import 'dart:io';
import 'package:angel_framework/angel_framework.dart';
import 'package:yaml/yaml.dart';
_loadYamlFile(Angel app, File yamlFile) {
if (yamlFile.existsSync()) {
Map config = loadYaml(yamlFile.readAsStringSync());
for (String key in config.keys) {
app.properties[key] = config[key];
}
}
}
loadConfigurationFile(
{String directoryPath: "./config", String overrideEnvironmentName}) {
return (Angel app) {
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"));
_loadYamlFile(app, defaultYaml);
String configFilePath = "$environmentName.yaml";
File configFile = new File.fromUri(
sourceDirectory.absolute.uri.resolve(configFilePath));
_loadYamlFile(app, configFile);
};
}

10
pubspec.yaml Normal file
View file

@ -0,0 +1,10 @@
name: angel_configuration
description: YAML configuration loader for Angel.
version: 1.0.0-dev
author: thosakwe <thosakwe@gmail.com>
homepage: https://github.com/angel-dart/angel_configuration
dependencies:
angel_framework: ">=0.0.0-dev < 0.1.0"
yaml: ">= 2.1.8 < 2.2.0"
dev_dependencies:
test: ">= 0.12.13 < 0.13.0"

27
test/all_tests.dart Normal file
View file

@ -0,0 +1,27 @@
import 'package:angel_framework/angel_framework.dart';
import 'package:angel_configuration/angel_configuration.dart';
import 'package:test/test.dart';
main() {
// Note: Set ANGEL_ENV to 'development'
Angel angel = new Angel();
test('can load based on ANGEL_ENV', () {
angel.configure(loadConfigurationFile(directoryPath: './test/config'));
expect(angel.properties['hello'], equals('world'));
expect(angel.properties['foo']['version'], equals('bar'));
});
test('will load default.yaml if exists', () {
expect(angel.properties["set_via"], equals("default"));
});
test('can override ANGEL_ENV', () {
angel.configure(loadConfigurationFile(
directoryPath: './test/config', overrideEnvironmentName: 'override'));
expect(angel.properties['hello'], equals('goodbye'));
expect(angel.properties['foo']['version'], equals('baz'));
});
}

1
test/config/default.yaml Normal file
View file

@ -0,0 +1 @@
set_via: default

View file

@ -0,0 +1,3 @@
hello: world
foo:
version: bar

View file

@ -0,0 +1,3 @@
hello: goodbye
foo:
version: baz