Dart Transformer无法覆盖资源
我编写了一个AggregateTransformer,它应该用更新的版本覆盖main.dart文件(更改导入) (在添加新版本之前先删除资产不会更改任何内容:) 但更新后的版本消失得无影无踪。尝试按id检索会产生原始内容:Dart Transformer无法覆盖资源,dart,dart-pub,Dart,Dart Pub,我编写了一个AggregateTransformer,它应该用更新的版本覆盖main.dart文件(更改导入) (在添加新版本之前先删除资产不会更改任何内容:) 但更新后的版本消失得无影无踪。尝试按id检索会产生原始内容: Asset updatedMainAssetRetrieved = await transform.getInput(updatedMainAsset.id); 转换器输出mainAsset和updatedMainAsset的内容,以便您可以检查updatedMainAss
Asset updatedMainAssetRetrieved = await transform.getInput(updatedMainAsset.id);
转换器输出mainAsset和updatedMainAsset的内容,以便您可以检查updatedMainAsset的内容是否确实已更新。通过调用pub run main.dart来调用转换器
完整代码/伪代码如下所示:
class ReplacePackageTransformer extends AggregateTransformer {
ReplacePackageTransformer.asPlugin();
@override
String classifyPrimary(AssetId id) => id.toString().endsWith(".dart") ? "dart-files" : null;
@override
apply(AggregateTransform transform) {
//capture the whole execution to allow better stacktraces if an error occurs
Chain.capture(() async {
//create a file lib/replacement.dart that defines the same method as lib/default.dart
final newLibFileName = "replacement.dart";
final newLibAsset = _createReplacementAsset(...);
//add this new asset
transform.addOutput(newLibAsset);
//rewrite main.dart to import replacement.dart instead of default.dart. To that end:
//1) retrieve the asset for main.dart
Asset mainAsset = await _getFileAsset(transform, "main.dart");
//2) create a new asset with the same id as mainAsset but with updated content
Asset updatedMainAsset = await _replaceDefaultImport(mainAsset, ...);
//3) adding this asset should overwrite/replace the original main.dart-asset because they use the same id
transform.addOutput(updatedMainAsset);
});
}
//helper methods ...
}
您可以找到整个transformer(以及项目的其余部分)
更新/解决方案
丹尼斯·卡塞洛是对的!我的变压器的应用方法必须返回未来(以便后续变压器可以等待它完成)!在调用Chain.capture之前添加一个return就足够了(因为回调i capture有一个异步主体,因此返回一个capture将转发/返回的未来)
如此变化
apply(AggregateTransform transform) {
Chain.capture(() async {...});
//no return statement so void is returned
}
到
解决我的问题 解决方案非常简单:只需在
链前面添加一个返回
apply
的dartdoc说明:
如果这样做是异步的,那么它应该返回一个[未来],一旦完成就完成
如果在转换后访问输入,则输入仍将是原始输入。您也不需要调用transform.consumeInput
。这似乎是相关的。不知道这是否有用。我建议你发布这个问题的链接,不要确定我的问题与你链接的问题有关,因为那是关于一个零件文件的,他们建议内联。虽然我的资产是一个单文件库(没有部件),但它已经内联。我在dart misc中添加了一篇文章,由于第一篇文章已被审核,因此该文章尚未在线。
class ReplacePackageTransformer extends AggregateTransformer {
ReplacePackageTransformer.asPlugin();
@override
String classifyPrimary(AssetId id) => id.toString().endsWith(".dart") ? "dart-files" : null;
@override
apply(AggregateTransform transform) {
//capture the whole execution to allow better stacktraces if an error occurs
Chain.capture(() async {
//create a file lib/replacement.dart that defines the same method as lib/default.dart
final newLibFileName = "replacement.dart";
final newLibAsset = _createReplacementAsset(...);
//add this new asset
transform.addOutput(newLibAsset);
//rewrite main.dart to import replacement.dart instead of default.dart. To that end:
//1) retrieve the asset for main.dart
Asset mainAsset = await _getFileAsset(transform, "main.dart");
//2) create a new asset with the same id as mainAsset but with updated content
Asset updatedMainAsset = await _replaceDefaultImport(mainAsset, ...);
//3) adding this asset should overwrite/replace the original main.dart-asset because they use the same id
transform.addOutput(updatedMainAsset);
});
}
//helper methods ...
}
apply(AggregateTransform transform) {
Chain.capture(() async {...});
//no return statement so void is returned
}
Future apply(AggregateTransform transform) {
return Chain.capture(() async {...});
//() async {...} returns a Future that Chain.capture and apply forward/return
}