Support all Transform methods on ForeignTransform.

R=alanknight@google.com

Review URL: https://codereview.chromium.org//29343004

git-svn-id: https://dart.googlecode.com/svn/branches/bleeding_edge/dart@28895 260f80e4-7a28-3924-810f-c04153c831b5
This commit is contained in:
nweiz@google.com
2013-10-18 23:01:01 +00:00
parent 71813b4880
commit 9e9432510e
3 changed files with 134 additions and 2 deletions
@@ -19,8 +19,8 @@ import 'server.dart';
/// A Dart script to run in an isolate.
///
/// This script serializes one or more transformers defined in a Dart library and
/// marhsals calls to and from them with the host isolate.
/// This script serializes one or more transformers defined in a Dart library
/// and marshals calls to and from them with the host isolate.
const _TRANSFORMER_ISOLATE = """
import 'dart:async';
import 'dart:isolate';
@@ -116,6 +116,14 @@ class ForeignTransform implements Transform {
})).then(_deserializeAsset);
}
Future<String> readInputAsString(AssetId id, {Encoding encoding}) {
if (encoding == null) encoding = UTF8;
return getInput(id).then((input) => input.readAsString(encoding: encoding));
}
Stream<List<int>> readInput(AssetId id) =>
_futureStream(getInput(id).then((input) => input.read()));
void addOutput(Asset output) {
_port.send({
'type': 'addOutput',
@@ -335,6 +343,23 @@ String getErrorMessage(error) {
return error.toString();
}
}
/// Returns a buffered stream that will emit the same values as the stream
/// returned by [future] once [future] completes. If [future] completes to an
/// error, the return value will emit that error and then close.
Stream _futureStream(Future<Stream> future) {
var controller = new StreamController(sync: true);
future.then((stream) {
stream.listen(
controller.add,
onError: controller.addError,
onDone: controller.close);
}).catchError((e, stackTrace) {
controller.addError(e, stackTrace);
controller.close();
});
return controller.stream;
}
""";
/// Load and return all transformers and groups from the library identified by
@@ -0,0 +1,53 @@
// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS d.file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
library pub_tests;
import '../descriptor.dart' as d;
import '../test_pub.dart';
import '../serve/utils.dart';
const TRANSFORMER = """
import 'dart:async';
import 'package:barback/barback.dart';
class RewriteTransformer extends Transformer {
RewriteTransformer.asPlugin();
String get allowedExtensions => '.txt';
Future apply(Transform transform) {
return transform.readInputAsString(transform.primaryInput.id)
.then((contents) {
var id = transform.primaryInput.id.changeExtension(".out");
transform.addOutput(new Asset.fromString(id, "\$contents.out"));
});
}
}
""";
main() {
initConfig();
integration("a transform can use readInputAsString", () {
d.dir(appPath, [
d.pubspec({
"name": "myapp",
"transformers": ["myapp/src/transformer"]
}),
d.dir("lib", [d.dir("src", [
d.file("transformer.dart", TRANSFORMER)
])]),
d.dir("web", [
d.file("foo.txt", "foo")
])
]).create();
createLockFile('myapp', pkg: ['barback']);
startPubServe();
requestShouldSucceed("foo.out", "foo.out");
endPubServe();
});
}
@@ -0,0 +1,54 @@
// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS d.file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
library pub_tests;
import '../descriptor.dart' as d;
import '../test_pub.dart';
import '../serve/utils.dart';
const TRANSFORMER = """
import 'dart:async';
import 'package:barback/barback.dart';
class RewriteTransformer extends Transformer {
RewriteTransformer.asPlugin();
String get allowedExtensions => '.txt';
Future apply(Transform transform) {
return transform.readInput(transform.primaryInput.id).toList()
.then((contents) {
var id = transform.primaryInput.id.changeExtension(".out");
var asset = new Asset.fromString(id, "\$contents.out");
transform.addOutput(asset);
});
}
}
""";
main() {
initConfig();
integration("a transform can use readInputAsString", () {
d.dir(appPath, [
d.pubspec({
"name": "myapp",
"transformers": ["myapp/src/transformer"]
}),
d.dir("lib", [d.dir("src", [
d.file("transformer.dart", TRANSFORMER)
])]),
d.dir("web", [
d.file("foo.txt", "foo")
])
]).create();
createLockFile('myapp', pkg: ['barback']);
startPubServe();
requestShouldSucceed("foo.out", "[[102 111 111]].out");
endPubServe();
});
}