From 4e3e8de157602d4015576631f8ecad20ba5ce192 Mon Sep 17 00:00:00 2001 From: Tobe O Date: Thu, 12 Jul 2018 12:40:54 -0400 Subject: [PATCH] Verified tests on Dart 1 + 2 --- .gitignore | 5 +- .travis.yml | 5 +- CHANGELOG.md | 5 ++ README.md | 27 +--------- analysis_options.yaml | 3 +- example/main.dart | 9 ++++ lib/angel_configuration.dart | 33 +----------- lib/transformer.dart | 101 ----------------------------------- pubspec.yaml | 9 ++-- test/all_test.dart | 3 -- test/transformer.dart | 26 --------- 11 files changed, 30 insertions(+), 196 deletions(-) create mode 100644 example/main.dart delete mode 100644 lib/transformer.dart delete mode 100644 test/transformer.dart diff --git a/.gitignore b/.gitignore index ff2f8761..5e6bb7e7 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,7 @@ # Created by .ignore support plugin (hsz.mobi) ### JetBrains template .idea +*.iml # Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm # Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 @@ -73,4 +74,6 @@ doc/api/ # Don't commit pubspec lock file # (Library packages only! Remove pattern if developing an application package) -pubspec.lock \ No newline at end of file +pubspec.lock + +.dart_tool \ No newline at end of file diff --git a/.travis.yml b/.travis.yml index de2210c9..a9e2c109 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1 +1,4 @@ -language: dart \ No newline at end of file +language: dart +dart: + - dev + - stable \ No newline at end of file diff --git a/CHANGELOG.md b/CHANGELOG.md index 687b11ce..f32f9c31 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +# 2.0.0 +* Removed the `Configuration` class. +* Removed the `ConfigurationTransformer` class. +* Use `Map` casting to prevent runtime cast errors. + # 1.0.5 * Now using `package:merge_map` to merge configurations. Resolves [#5](https://github.com/angel-dart/configuration/issues/5). diff --git a/README.md b/README.md index 213338d9..16abcb27 100644 --- a/README.md +++ b/README.md @@ -98,29 +98,4 @@ foo: bar: baz quux: goodbye yellow: submarine -``` - -**In the Browser** - -You can easily load configuration values within your client-side app, -and they will be automatically replaced by a Barback transformer. - -In your `pubspec.yaml`: - -```yaml -transformers: -- angel_configuration -``` - -In your app: - -```dart -import 'package:angel_configuration/browser.dart'; - -main() async { - print(config("some_key.other.nested_key")); -} -``` - -You can also provide a `dir` or `env` argument, corresponding to -the ones on the server-side. +``` \ No newline at end of file diff --git a/analysis_options.yaml b/analysis_options.yaml index 518eb901..30c21882 100644 --- a/analysis_options.yaml +++ b/analysis_options.yaml @@ -1,2 +1,3 @@ analyzer: - strong-mode: true \ No newline at end of file + strong-mode: + implicit-casts: true \ No newline at end of file diff --git a/example/main.dart b/example/main.dart new file mode 100644 index 00000000..d2441630 --- /dev/null +++ b/example/main.dart @@ -0,0 +1,9 @@ +import 'package:angel_configuration/angel_configuration.dart'; +import 'package:angel_framework/angel_framework.dart'; +import 'package:file/local.dart'; + +main() async { + var app = new Angel(); + var fs = const LocalFileSystem(); + await app.configure(configuration(fs)); +} diff --git a/lib/angel_configuration.dart b/lib/angel_configuration.dart index 7ab73796..62262c11 100644 --- a/lib/angel_configuration.dart +++ b/lib/angel_configuration.dart @@ -6,35 +6,6 @@ import 'package:file/file.dart'; import 'package:merge_map/merge_map.dart'; import 'package:yaml/yaml.dart'; -final RegExp _equ = new RegExp(r'=$'); -final RegExp _sym = new RegExp(r'Symbol\("([^"]+)"\)'); - -/// A proxy object that encapsulates a server's configuration. -@proxy -class Configuration { - /// The [Angel] instance that loaded this configuration. - final Angel app; - Configuration(this.app); - - operator [](key) => app.configuration[key]; - operator []=(key, value) => app.configuration[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.configuration[name], - invocation.positionalArguments, invocation.namedArguments); - } else if (invocation.isGetter) { - return app.configuration[name]; - } - } - - super.noSuchMethod(invocation); - } -} - _loadYamlFile(Angel app, File yamlFile, Map env) async { if (await yamlFile.exists()) { var config = loadYaml(await yamlFile.readAsString()); @@ -89,8 +60,7 @@ _applyEnv(var v, Map env, Angel app) { /// load from a [overrideEnvironmentName]. /// /// You can also specify a custom [envPath] to load system configuration from. -AngelConfigurer configuration( - FileSystem fileSystem, +AngelConfigurer configuration(FileSystem fileSystem, {String directoryPath: "./config", String overrideEnvironmentName, String envPath}) { @@ -121,6 +91,5 @@ AngelConfigurer configuration( var configFile = sourceDirectory.childFile(configFilePath); await _loadYamlFile(app, configFile, env); - app.container.singleton(new Configuration(app)); }; } diff --git a/lib/transformer.dart b/lib/transformer.dart deleted file mode 100644 index 1527ebe1..00000000 --- a/lib/transformer.dart +++ /dev/null @@ -1,101 +0,0 @@ -import 'dart:async'; -import 'dart:convert'; -import 'package:barback/barback.dart'; -import 'package:analyzer/analyzer.dart'; -import 'package:angel_framework/angel_framework.dart'; -import 'package:file/local.dart'; -import 'angel_configuration.dart'; - -class ConfigurationTransformer extends Transformer { - final BarbackSettings _settings; - - @override - String get allowedExtensions => ".dart"; - - ConfigurationTransformer.asPlugin(this._settings) {} - - Future apply(Transform transform) async { - try { - var app = new Angel(); - - await app.configure(configuration( - const LocalFileSystem(), - directoryPath: _settings.configuration["dir"] ?? "./config", - overrideEnvironmentName: _settings.configuration["env"], - )); - - var text = await transform.primaryInput.readAsString(); - var compilationUnit = parseCompilationUnit(text); - var visitor = new ConfigAstVisitor(app.properties); - visitor.visitCompilationUnit(compilationUnit); - - await for (Map replaced in visitor.onReplaced) { - text = text.replaceAll(replaced["needle"], replaced["with"]); - } - - transform - .addOutput(new Asset.fromString(transform.primaryInput.id, text)); - } catch (e) { - // Fail silently... - } - } -} - -class ConfigAstVisitor extends GeneralizingAstVisitor { - Map _config; - var _onReplaced = new StreamController(); - String _prefix = ""; - Stream get onReplaced => _onReplaced.stream; - - ConfigAstVisitor(this._config); - - bool isConfigMethod(Expression function) => - function is SimpleIdentifier && function.name == "${_prefix}config"; - - resolveItem(String key) { - var split = key.split("."); - var parent = _config; - - for (int i = 0; i < split.length; i++) { - if (parent != null && parent is Map) parent = parent[split[i]]; - } - - return parent; - } - - @override - visitCompilationUnit(CompilationUnit ctx) { - var result = super.visitCompilationUnit(ctx); - _onReplaced.close(); - return result; - } - - @override - visitImportDirective(ImportDirective ctx) { - String uri = ctx.uri.stringValue; - - if (uri == "package:angel_configuration/browser.dart") { - _onReplaced.add({"needle": ctx.toString(), "with": ""}); - - if (ctx.asKeyword != null) { - _prefix = ctx.prefix.name; - } - } - - return super.visitImportDirective(ctx); - } - - @override - visitExpression(Expression ctx) { - if (ctx is MethodInvocation) { - if (isConfigMethod(ctx.function)) { - StringLiteral key = ctx.argumentList.arguments[0]; - var resolved = resolveItem(key.stringValue); - - _onReplaced - .add({"needle": ctx.toString(), "with": JSON.encode(resolved)}); - } - } - return super.visitExpression(ctx); - } -} diff --git a/pubspec.yaml b/pubspec.yaml index 3b792d63..35a9dc9b 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,15 +1,14 @@ name: angel_configuration description: Automatic YAML configuration loader for Angel. -version: 1.1.0 +version: 2.0.0-rc.0 author: Tobe O homepage: https://github.com/angel-dart/angel_configuration environment: - sdk: ">=1.19.0" + sdk: ">=1.8.0 <3.0.0" dependencies: - analyzer: ">=0.28.1 <1.0.0" angel_framework: ^1.1.0-alpha - barback: ^0.15.2 - dotenv: ^0.1.0 + dotenv: + git: https://github.com/thosakwe/dotenv.git file: ^2.0.0 merge_map: ^1.0.0 yaml: ^2.0.0 diff --git a/test/all_test.dart b/test/all_test.dart index af6fd698..62f70bc5 100644 --- a/test/all_test.dart +++ b/test/all_test.dart @@ -2,7 +2,6 @@ import 'package:angel_framework/angel_framework.dart'; import 'package:angel_configuration/angel_configuration.dart'; import 'package:file/local.dart'; import 'package:test/test.dart'; -import 'transformer.dart' as transformer; main() async { // Note: Set ANGEL_ENV to 'development' @@ -45,6 +44,4 @@ main() async { directoryPath: './test/config', overrideEnvironmentName: 'override')); expect(app.configuration['merge'], {'map': true, 'hello': 'goodbye'}); }); - - group("transformer", transformer.main); } diff --git a/test/transformer.dart b/test/transformer.dart deleted file mode 100644 index 950fef6e..00000000 --- a/test/transformer.dart +++ /dev/null @@ -1,26 +0,0 @@ -import 'package:analyzer/analyzer.dart'; -import 'package:angel_configuration/transformer.dart'; -import 'package:test/test.dart'; - -main() { - test("simple replacement", () async { - var visitor = new ConfigAstVisitor({"foo": "bar"}); - var source = ''' - import 'package:angel_configuration/browser.dart'; - - main() async { - var foo = config('foo'); - } - '''; - - var compilationUnit = parseCompilationUnit(source); - visitor.visitCompilationUnit(compilationUnit); - - var replaced = await visitor.onReplaced.take(2).last; - - expect(replaced["needle"], equals("config('foo')")); - expect(replaced["with"], equals('"bar"')); - - print(source.replaceAll(replaced["needle"], replaced["with"])); - }); -}