Project root directory

119 views
Skip to first unread message

Thomas Stephenson

unread,
Sep 16, 2014, 12:02:01 AM9/16/14
to mi...@dartlang.org
Is there any constant or final variable defined which gives access to
the project root directory?

For a while, I’ve been making the assumption that I could get to the
root of the project directory using

```
const _PUBSPEC_FILE = ‘pubspec.yaml’;

final PROJECT_ROOT = () {
var dir = new Directory(platform.script.path);
while (dir.listSync().every((f) => path.basename(f.path) != _PUBSPEC_FILE)) {
dir = dir.parent;
}
return dir.absolute.path;
}();
```

However, after looking at the source of `angular.dart` I find that
this assumption is not always valid, since the angular project
contains multiple pubspec files.

Bob Nystrom

unread,
Sep 16, 2014, 3:47:37 PM9/16/14
to General Dart Discussion
On Mon, Sep 15, 2014 at 9:01 PM, Thomas Stephenson <ova...@gmail.com> wrote:
Is there any constant or final variable defined which gives access to
the project root directory?

I guess the question is, which "project" do you want?

While we don't actively encourage it, there's nothing preventing nested packages like Angular has. (In fact, if I had the chance to do things over again in pub, I would possibly make nested packages a more central concept.)

Do you implicitly want the outermost one? Innermost?

You can implement either, it's just a question of what problem you're trying to solve.

Cheers!

- bob

Thomas Stephenson

unread,
Sep 17, 2014, 4:17:51 AM9/17/14
to mi...@dartlang.org
Well, in general the outermost one is the desirable one, because I
want to be able to run my server without having to worry about which
directory I’m executing it from. When specifying an asset to load,
paths relative to the script don’t always work, because there are
often multiple entry points into an application.

So the project root provides an anchor for paths within the project
which leaves no ambiguity regarding their location. That’s really the
entire point of keeping a reference to the project root. And as long
as you had a reference to the outermost one, it’s easy enough to use
that to get references to the inner ones anyway.

In my own projects I can construct it myself without worrying about
the most general case, but I maintain a couple of packages which are
intended to work against any application. At least one of these
packages would benefit from having an unambiguous reference to the
root of the project.

Thanks,
Thomas

On 17 September 2014 05:46, 'Bob Nystrom' via Dart Misc
> --
> For other discussions, see https://groups.google.com/a/dartlang.org/
>
> For HOWTO questions, visit http://stackoverflow.com/tags/dart
>
> To file a bug report or feature request, go to http://www.dartbug.com/new
>
> To unsubscribe from this group and stop receiving emails from it, send an
> email to misc+uns...@dartlang.org.

Sean Eagan

unread,
Sep 17, 2014, 9:31:04 AM9/17/14
to General Dart Discussion
I also had a need to access the root directory of the current package (innermost I guess), specifically I wanted to access the package version from the pubspec.yaml.  But as I understand it, there is no way to do this when using `pub serve`, `pub build`, or `pub run`, because the asset environment does not contain the root directory, and transformers cannot access anything outside of lib to copy it into the asset environment.  See:
www.dartbug.com/19434
This seems pretty unfortunate, since it can be done with `npm` quite easily:
http://stackoverflow.com/questions/9153571/is-there-a-way-to-get-version-from-package-json-in-nodejs-code

Cheers,
Sean Eagan

Bob Nystrom

unread,
Sep 17, 2014, 12:16:34 PM9/17/14
to General Dart Discussion

On Wed, Sep 17, 2014 at 6:30 AM, Sean Eagan <seane...@gmail.com> wrote:
I also had a need to access the root directory of the current package (innermost I guess), specifically I wanted to access the package version from the pubspec.yaml.

Can you give me some more details of your use case here?

Thanks!

- bob

Sean Eagan

unread,
Sep 17, 2014, 1:02:39 PM9/17/14
to General Dart Discussion

My use case is to implement a plugin in the `unscripted` package, which adds a `--version` flag which outputs the version from the `pubspec.yaml` of the package containing the script:


I want this plugin to work when the script is run via `pub run` and `pub global run`.

Thanks!
Sean Eagan

--

Sean Eagan

unread,
Sep 22, 2014, 8:27:14 AM9/22/14
to General Dart Discussion

This bug may be related as well:

http://www.dartbug.com/21020

Cheers,
Sean Eagan

Reply all
Reply to author
Forward
0 new messages