1

The aim is to create a config file for server-side Dart application, which can be imported as needed into scripts like so:

import 'Config/config.dart';

What is crucial for this config script however, is that it knows it's own location when being accessed with an import (not the location of the file accessing it). Currently it uses this line below to find the path:

final String ROOT_DIR = dirname(Platform.script.toFilePath());

However, this returns the file path of the file importing it and not the file that is being imported. This needs to be known purely for working out a relative path to the root folder, which will allow other absolute paths to be known in the config file (such as routes, controllers, and things), like so:

final String PUBLIC_DIR = join(ROOT_DIR, 'Public');
final String VIEWS_DIR = join(ROOT_DIR, 'Views');

What would be the best way of approaching this? I have seen this post: Get script path in Dart (analog __DIR__ constant in PHP) which is the same sort of situation, however I can't see a clean way of using relative paths to find the route folder.

Probably missing something really obvious, but can't see it at the moment. Any help would be much appreciated, thank you for reading.

Community
  • 1
  • 1
Will Squire
  • 6,127
  • 7
  • 45
  • 57

1 Answers1

1

This is not supported in Dart. Maybe Mezoni found some kind of trick to get this information which he packed in his caller_info package https://stackoverflow.com/a/24880092/217408.

You can't rely on the path where the files are stored during development.
Currently it is only experimental but when you run dart2dart on your code, all or many parts of the code are inlined.

Community
  • 1
  • 1
Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
  • Ah ok, so it won't even know where the minified and tree shaked directory/file will be? I'll mark this as the answer, but can't dart access bash files and get this information? Or are you saying that dart apps will be compiled into one file? (Which is what I expect from reading this: https://code.google.com/p/dart/issues/detail?id=20090). Thank you for your answer. – Will Squire Oct 03 '14 at 19:05
  • When you run pub build(dart2js/dart2dart) the code can end in one file. Transformers can remove code and insert newly generated code. Script files can be loaded from temporary URIs, ... Dart provides only a path to the initial script file (containing `main()`). – Günter Zöchbauer Oct 03 '14 at 20:22