Barback transformer
This commit is contained in:
parent
055a91e7b3
commit
3317624c49
5 changed files with 128 additions and 3 deletions
1
lib/browser.dart
Normal file
1
lib/browser.dart
Normal file
|
@ -0,0 +1 @@
|
|||
external dynamic config(String key);
|
93
lib/transformer.dart
Normal file
93
lib/transformer.dart
Normal file
|
@ -0,0 +1,93 @@
|
|||
import 'dart:async';
|
||||
import 'dart:convert';
|
||||
import 'package:barback/barback.dart';
|
||||
import 'package:analyzer/analyzer.dart';
|
||||
import 'package:angel_framework/angel_framework.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 {
|
||||
var app = new Angel();
|
||||
|
||||
await app.configure(loadConfigurationFile(
|
||||
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));
|
||||
}
|
||||
}
|
||||
|
||||
class ConfigAstVisitor extends GeneralizingAstVisitor {
|
||||
Map _config;
|
||||
var _onReplaced = new StreamController<Map>();
|
||||
String _prefix = "";
|
||||
Stream<Map> 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);
|
||||
}
|
||||
}
|
|
@ -1,10 +1,12 @@
|
|||
name: angel_configuration
|
||||
description: YAML configuration loader for Angel.
|
||||
version: 1.0.1
|
||||
version: 1.0.1+1
|
||||
author: thosakwe <thosakwe@gmail.com>
|
||||
homepage: https://github.com/angel-dart/angel_configuration
|
||||
dependencies:
|
||||
analyzer: ">=0.28.1 <0.29.0"
|
||||
angel_framework: ">=1.0.0-dev < 2.0.0"
|
||||
barback: ">=0.15.2 < 0.16.0"
|
||||
yaml: ">= 2.1.8 < 2.2.0"
|
||||
dev_dependencies:
|
||||
test: ">= 0.12.13 < 0.13.0"
|
||||
test: ">= 0.12.13 < 0.13.0"
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
import 'package:angel_framework/angel_framework.dart';
|
||||
import 'package:angel_configuration/angel_configuration.dart';
|
||||
import 'package:test/test.dart';
|
||||
import 'transformer.dart' as transformer;
|
||||
|
||||
main() async {
|
||||
// Note: Set ANGEL_ENV to 'development'
|
||||
|
@ -25,4 +26,6 @@ main() async {
|
|||
expect(angel.properties['hello'], equals('goodbye'));
|
||||
expect(angel.properties['foo']['version'], equals('baz'));
|
||||
});
|
||||
}
|
||||
|
||||
group("transformer", transformer.main);
|
||||
}
|
||||
|
|
26
test/transformer.dart
Normal file
26
test/transformer.dart
Normal file
|
@ -0,0 +1,26 @@
|
|||
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"]));
|
||||
});
|
||||
}
|
Loading…
Reference in a new issue