2016-09-19 20:24:26 +00:00
|
|
|
import 'dart:async';
|
|
|
|
import 'dart:convert';
|
|
|
|
import 'package:barback/barback.dart';
|
|
|
|
import 'package:analyzer/analyzer.dart';
|
|
|
|
import 'package:angel_framework/angel_framework.dart';
|
2017-09-25 13:50:56 +00:00
|
|
|
import 'package:file/local.dart';
|
2016-09-19 20:24:26 +00:00
|
|
|
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 {
|
2017-03-08 22:47:58 +00:00
|
|
|
try {
|
|
|
|
var app = new Angel();
|
2016-09-19 20:24:26 +00:00
|
|
|
|
2017-09-25 13:50:56 +00:00
|
|
|
await app.configure(configuration(
|
|
|
|
const LocalFileSystem(),
|
|
|
|
directoryPath: _settings.configuration["dir"] ?? "./config",
|
|
|
|
overrideEnvironmentName: _settings.configuration["env"],
|
|
|
|
));
|
2016-09-19 20:24:26 +00:00
|
|
|
|
2017-03-08 22:47:58 +00:00
|
|
|
var text = await transform.primaryInput.readAsString();
|
|
|
|
var compilationUnit = parseCompilationUnit(text);
|
|
|
|
var visitor = new ConfigAstVisitor(app.properties);
|
|
|
|
visitor.visitCompilationUnit(compilationUnit);
|
2016-09-19 20:24:26 +00:00
|
|
|
|
2017-03-08 22:47:58 +00:00
|
|
|
await for (Map replaced in visitor.onReplaced) {
|
2016-09-19 20:24:26 +00:00
|
|
|
text = text.replaceAll(replaced["needle"], replaced["with"]);
|
2017-03-08 22:47:58 +00:00
|
|
|
}
|
2016-09-19 20:24:26 +00:00
|
|
|
|
2017-03-08 22:47:58 +00:00
|
|
|
transform
|
|
|
|
.addOutput(new Asset.fromString(transform.primaryInput.id, text));
|
|
|
|
} catch (e) {
|
|
|
|
// Fail silently...
|
|
|
|
}
|
2016-09-19 20:24:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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]];
|
|
|
|
}
|
2017-03-08 22:47:58 +00:00
|
|
|
|
2016-09-19 20:24:26 +00:00
|
|
|
return parent;
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
visitCompilationUnit(CompilationUnit ctx) {
|
2017-03-08 22:47:58 +00:00
|
|
|
var result = super.visitCompilationUnit(ctx);
|
|
|
|
_onReplaced.close();
|
|
|
|
return result;
|
2016-09-19 20:24:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@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) {
|
2017-03-08 22:47:58 +00:00
|
|
|
_prefix = ctx.prefix.name;
|
2016-09-19 20:24:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|