dart-lang / sdk

The Dart SDK, including the VM, JS and Wasm compilers, analysis, core libraries, and more.
https://dart.dev
BSD 3-Clause "New" or "Revised" License
10.08k stars 1.56k forks source link

Document 'copy package assets to the local file system'. #39578

Open bsutton opened 4 years ago

bsutton commented 4 years ago

I've google extensively and posted on stack overflow with no results so this appears to be an area that needs to be documented.

https://stackoverflow.com/questions/59039306/how-to-copy-dart-assets-not-flutter-to-local-system

To reiterate the stack overflow question. When building a flutter app you can specify assets in the pubspec under the 'flutter' key.

With a dart cli application no such key exists.

Further the pubspec specification doesn't appear to directly support the concept of assets: https://dart.dev/tools/pub/pubspec

I need to ship a number of templates with my dart cli application that are installed when the app is first run. https://pub.dev/packages/dshell

How do you package the equivalent of assets with a non-flutter dart cli application?

How do you access these assets at run time so they can be copied to the local file system?

This would appear to be a hole in the documentation or possible a missing feature, hence this issue.

If someone can point me in the right direction (on how this is done) I would be happy to push a PR with an update to the dart doco.

lrhn commented 4 years ago

The Dart platform does not have a general concept of assets that are available at run-time. Which files are available at run-time for an ahead-of-time compiled program depends on how it is deployed, which again depends on what it is deployed to.

There are way to specify assets for web-applications in the web/ directory, and it's a deployment issue to ensure that they are available at run-time. This link says something, but I believe it might be outdated. It only works for web applications.

Flutter, another platform with a different deployment format, has its own asset management.

For Pub-install/run native CLI applications, you can put files in the package's lib/ directory and use Isolate.resolvePackageUri to find them. This works because the "deployment" of pub packages to the pub cache retains the entire lib/ directory.

bsutton commented 4 years ago

That's exactly the info I needed, now to find out when to update the doco

bsutton commented 4 years ago

For others that might come this way. I had a little trouble working out the syntax to access a file in the package:

If the package in your pubspec is: dcli And you have a file in lib/src/myasset/fred.img

The use use: var resolvedUri = await Isolate.resolvePackageUri(Uri(scheme: 'package', path: 'dcli/src/myasset/fred.img'));