self property

Returns the instance of the currently running DartProject.

If you call this method from a non-compiled script then we start the search from the scripts directory and search up the directory tree.

If you call this method from a compiled script then we will return the current working directory as there is no 'project root' for a compiled script.

If you are looking to load the project from a directory then use DartProject.fromPath()

Implementation

// ignore: prefer_constructors_over_static_methods
static DartProject get self {
  if (Platform.packageConfig != null) {
    /// When running as a unit test we can't use DartScript.self
    /// as it returns the the test runner.
    /// The packageConfig is available if passed (which unit tests do)
    /// and when passed is probably the most relable means of
    /// determining the project directory.
    return _current ??=
        DartProject.fromPath(dirname(Platform.packageConfig!));
  }
  final script = DartScript.self;
  var startFrom = '.';
  if (!script.isCompiled) {
    startFrom = script.pathToScript;
  }
  return _current ??= DartProject.fromPath(startFrom);
}