1
votes

i started with the simple_transformer example on how to write a simple Dart Pub Transformer simple_transformer. This example defines the content to insert into files by specifying it in code

String copyright = "Copyright (c) 2014, the Example project authors.\n";

instead i wanted to use the new (Dart 1.12) Resource class from the core package in order to load this copyright message from a local file (lib/copyright.txt):

static Future<String> loadCopyright() {
  var copyrightRessource = new Resource("package:simple_resource_loading_transformer/copyright.txt");
  return copyrightRessource.readAsString();
}

While invoking this method from the main function works

main() {
  print('load copyright.txt');
  //this loads the resource as expected
  InsertCopyright.loadCopyright().then(
      (String code)=>print(code)
  );
}

, invoking it in the Transformer's apply-method fails when trying to transform another package (which is what Transformers are for). You'll get a

Build error: Transform InsertCopyright on {your project} threw error: Load Error for "package:simple_resource_loading_transformer/copyright.txt": SocketException: OS Error: Connection refused

How do i make Resource work in a Pub Transformer? Or is this missing functionality that still should be added to Dart?

Update

so here is the working solution based on the proposed usage of the Transform API

static Future<String> loadCopyright(Transform transform) {
  var copyrightAssetId = new AssetId('simple_resource_loading_transformer', 'lib/copyright.txt');
  return transform.readInputAsString(copyrightAssetId);
}

The Transform instance comes from the parameter of your Transformer.apply method.

1
Resource is experimental and might not work properly yet. Especially it trying to create a socket connection IMHO doesn't look to good.Günter Zöchbauer
"Resource is experimental and might not work properly yet." so should i put this as a bug somewhere?StephanS
I guess so. github.com/dart-lang/sdk/issues and please cross-link with this question.Günter Zöchbauer

1 Answers

1
votes

See https://github.com/dart-lang/barback/issues/65#issuecomment-142455056

You should really be using the Barback Transform APIs to load assets anyway. That's what it's for.