From 9e9432510ea13e22f9c1aba634bb568ff3b07aff Mon Sep 17 00:00:00 2001 From: "nweiz@google.com" Date: Fri, 18 Oct 2013 23:01:01 +0000 Subject: [PATCH] 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 --- .../lib/src/barback/load_transformers.dart | 29 +++++++++- .../can_use_read_input_as_string_test.dart | 53 ++++++++++++++++++ .../transformer/can_use_read_input_test.dart | 54 +++++++++++++++++++ 3 files changed, 134 insertions(+), 2 deletions(-) create mode 100644 sdk/lib/_internal/pub/test/transformer/can_use_read_input_as_string_test.dart create mode 100644 sdk/lib/_internal/pub/test/transformer/can_use_read_input_test.dart diff --git a/sdk/lib/_internal/pub/lib/src/barback/load_transformers.dart b/sdk/lib/_internal/pub/lib/src/barback/load_transformers.dart index cbd444d24f1..141d83b4c81 100644 --- a/sdk/lib/_internal/pub/lib/src/barback/load_transformers.dart +++ b/sdk/lib/_internal/pub/lib/src/barback/load_transformers.dart @@ -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 readInputAsString(AssetId id, {Encoding encoding}) { + if (encoding == null) encoding = UTF8; + return getInput(id).then((input) => input.readAsString(encoding: encoding)); + } + + Stream> 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 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 diff --git a/sdk/lib/_internal/pub/test/transformer/can_use_read_input_as_string_test.dart b/sdk/lib/_internal/pub/test/transformer/can_use_read_input_as_string_test.dart new file mode 100644 index 00000000000..0084d5c8105 --- /dev/null +++ b/sdk/lib/_internal/pub/test/transformer/can_use_read_input_as_string_test.dart @@ -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(); + }); +} diff --git a/sdk/lib/_internal/pub/test/transformer/can_use_read_input_test.dart b/sdk/lib/_internal/pub/test/transformer/can_use_read_input_test.dart new file mode 100644 index 00000000000..7fde01a76c0 --- /dev/null +++ b/sdk/lib/_internal/pub/test/transformer/can_use_read_input_test.dart @@ -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(); + }); +}