Skip to content

script that uses Platform.script on command line changes when run via pub run #1136

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
DartBot opened this issue Jun 5, 2015 · 4 comments
Closed
Labels
closed-as-intended Closed as the reported issue is expected behavior

Comments

@DartBot
Copy link

DartBot commented Jun 5, 2015

<img src="https://avatars.githubusercontent.com/u/5479?v=3" align="left" width="96" height="96"hspace="10"> Issue by sethladd
Originally opened as dart-lang/sdk#21008


(not sure if this is a bug or just my misunderstanding?)

I have a command-line utility that I'm writing. One of the things it does is read some of the files inside of itself. To do this, it does:

var path = Platform.script.toFilePath();

This works when I just run my script using dart. However, this fails when I run via pub run:

~/Code/stagehand[copy-dir-of-files*]$ pub run stagehand
Unhandled exception:
Unsupported operation: Cannot extract a file path from a http URI

­0 Uri.toFilePath (dart:core/uri.dart:1583)

­1 WebStarterKitGenerator._loadFiles (package:stagehand/generators/webstarterkit.dart:25:42)

­2 WebStarterKitGenerator.WebStarterKitGenerator (package:stagehand/generators/webstarterkit.dart:18:15)

­3 generators (package:stagehand/stagehand.dart:20:36)

­4 main (http://localhost:49605/stagehand.dart:20:27)

­5 _startIsolate.isolateStartHandler (dart:isolate-patch/isolate_patch.dart:212)

­6 _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:122)

Why am I doing Platform.script.toFilePath() ? I need to get a list of files inside a directory of my package. So I figured I'd first found out where I am and them go into lib/

basically, I need to be able to list files inside of my package. The next few lines go a little something like this:

    var script = new File(path);
    var dir = script.parent;
    var filesDir = new Directory([dir.path, '..', 'lib', 'generators',
        'webstarterkit'].join(Platform.pathSeparator));
    print(filesDir.existsSync());
    print(filesDir.path);

Should I be able to do Platform.script.toFilePath() and still let users run this script via pub run ?

Thanks!

@DartBot
Copy link
Author

DartBot commented Jun 5, 2015

<img src="https://avatars.githubusercontent.com/u/5479?v=3" align="left" width="48" height="48"hspace="10"> Comment by sethladd


If there's another way to find out where my script (or even just my package) is on the filesystem, that would be fine.

@DartBot DartBot added closed-as-intended Closed as the reported issue is expected behavior Pub-Run labels Jun 5, 2015
@DartBot
Copy link
Author

DartBot commented Jun 5, 2015

<img src="https://avatars.githubusercontent.com/u/5479?v=3" align="left" width="48" height="48"hspace="10"> Comment by sethladd


I think this means that I'll need to run all tests through pub. Thoughts?


cc @devoncarew.
Removed Type-Defect label.
Added Type-Meta, Area-Pub, Pub-Run, Triaged labels.

@DartBot
Copy link
Author

DartBot commented Jun 5, 2015

<img src="https://avatars.githubusercontent.com/u/188?v=3" align="left" width="48" height="48"hspace="10"> Comment by nex3


In general a library shouldn't make assumptions about its layout on disk. Pub moves different pieces around considerably, runs it via HTTP, and in some cases (e.g. running from a snapshotted executable) the virtual files being run will have no relationship at all with their source files.

We've been planning for a long time to add some means of inspecting and loading assets—this is why we originally created barback—but we haven't had the time to implement that and it doesn't look like we will in the near future. For now, I'm afraid there's no good way (other than transformers I guess) to refer to files in your package.


Added AsDesigned label.

@DartBot
Copy link
Author

DartBot commented Jun 5, 2015

This comment was originally written by [email protected]


Somewhat unfortunate, as I think a use case like the one below is pretty valid.
I found a solution, but only after a lot of trial and error, as I expected Platform.script.toFilePath() to work.

  • I have a package "awesome_package" where I keep -amongst other things- my generic zend framework
  • I start developing by cloning a lightweight 'boilerplate' project that runs in plain html
  • When finished with local development, I run "pub run awesome_package:zendify" that combines zend framework with my new code, using configuration from my poilerplate project.
  • Now I just have to upload

Solution:

  • have awesome_package as a dependency in my project (I'd prefer a pub global solution)
  • keep generic zend in awesome_package/lib
  • pub run awesome_package:zendify

inside zendify.dart i can access:
"lib/my_awesome_package/zend" = generic zend dir
"build/web/project.dart.js" = the stuff i developed
"lib/src/config.properties" = my config, f.ex. language strings i inject into my generic zend while copying it
"deploy" = my target dir

Thanks for listening.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-as-intended Closed as the reported issue is expected behavior
Projects
None yet
Development

No branches or pull requests

1 participant