0

Goal

I'd like to launch a CLI program closely related to my flutter project, i.e., the program is saved somewhere near the flutter project folder.

My end goal here, is so that I could release a separate problem outside of the flutter app bundle at a fixed location relative to the bundle, e.g., same parent folder, while flutter-built exe can still find the program automatically. The solution targets Windows/macOS.

Expectation

I expect that I could retrieve a standard project path, such as the path to main.dart, and go from there using relative paths. I was so spoiled by Python's __file__ and wish to see something similar. This is also fairly easy to do with Windows/macOS native API, like this

For example, say I created a project under this folder

/path/to/my/flutter_project

I expect to call a Dart API to get the path of main.dart like this

/path/to/my/flutter_project/lib/main.dart

Observation

According to this answer The closest thing I got with flutter, is


import 'dart:io' as io;
Uri filePath = io.Platform.script.resolve('.');

however, puts me to a prescribed location:

// macOS
~/Library/Containers/com.example.flutterRelpath/Data/

This is the package data folder instead of the project folder.

If I query the script itself using

io.Platform.script.path

I get

~/Library/Containers/com.example.flutterRelpath/Data/main.dart

which is not the physical location of the script.

Question

Does it mean that I would need an installer to install the CLI program there or prepare a UI for the user to specify the location before I could use it? This seems a lot of trouble.

kakyo
  • 10,460
  • 14
  • 76
  • 140
  • Does this answer your question? [How do I get the directory of the current script, in Dart?](https://stackoverflow.com/questions/19623313/how-do-i-get-the-directory-of-the-current-script-in-dart) – Christopher Moore Jun 09 '21 at 02:18
  • If the above does not answer your question, your question is not clear. You're presenting your attempted solution rather than thoroughly describing your end goal. The parts of the question that seem to reference your end goal are also confusing. – Christopher Moore Jun 09 '21 at 02:25
  • @ChristopherMoore I updated my question's Goal and Expectation. Thanks for the link but the solution there does not give what I want. – kakyo Jun 09 '21 at 09:17
  • You've clarified why the linked question is not an exact duplicate. This is good, but as I said in my previous comment, your end goal is not clear. I see that you want a particular path, but I'm confused as to why you would want to do so in a released application. Additionally, include which flutter platforms you're targetting. – Christopher Moore Jun 09 '21 at 12:43
  • @ChristopherMoore I've added more info in my Goal section. Please take a look. Thanks! – kakyo Jun 10 '21 at 04:00

2 Answers2

1

There is no reason to obtain the path of the script, nor does that make sense to do in a compiled application as the source files are not directly used at runtime.

You can simply use a relative path to reference whatever file/executable you want.

final uri = Uri.file('relative/file/path');

This will give you a Uri to the path file in the file folder in the relative folder, which would be at the same level as your executable.

├── executable.exe
├── relative
│   └── file
│       └── path//The Uri will refer to HERE

In order for this to be a relative, the passed path must not start with a path separator. So it should not be:

final uri = Uri.file('/relative/file/path');
Christopher Moore
  • 15,626
  • 10
  • 42
  • 52
  • I more or less get the idea that we cannot obtain the executable location within the Dart world. This might be a platform-specific thing that must go through platform channels or FFI into the C world. – kakyo Jun 10 '21 at 04:26
  • @kakyo You can get the executable location in the Dart world just as your can in many other programming languages, but you're a bit confused between the difference in how compiled and fully-interpreted languages work. I'll explain more in another comment. – Christopher Moore Jun 10 '21 at 04:28
  • @kakyo Interpreted languages will appear to work a bit differently. Since you reference python in your question, I'll use that as an example. Python basically completely works with source code files. `.py` files. The python interpreter will interpret and execute instructions based on the contents of those files. There is no compilation, there is no executable, which means operations, such as these relative files, are done relative to the closest thing python has to an executable, the `.py` files. (1/2) – Christopher Moore Jun 10 '21 at 04:34
  • Thank you very much. Yeah, indeed I had always seen Dart as an interpreted environment, but now I realized that I was wrong. This is why it advertises itself as a solution that compiles itself to native. – kakyo Jun 10 '21 at 04:38
  • @kakyo Yes, it depends on how you treat your Dart code. If you "run"(don't compile) a Dart script with your `Platform.script.path` code, you'll get the result you expect. Using Platform channels or FFI and C would do nothing differently from Dart when it's compiled. – Christopher Moore Jun 10 '21 at 04:39
  • Exactly. I was expecting to get that script path under the interpreter environment, but I can see that you mean that's not the way end-user would use it, except that I really would love to enable my user to run `flutter run --release -d macos` as my alpha tester ... oh well, I know I could always ship binaries to them but this would be much easier if we are on the same SCM. – kakyo Jun 10 '21 at 04:41
0

Have a look at the dcli package and the DartScript and DartProject classes. they have methods designed to provide exactly this type of path information.

E.g.

DartProject.self.pathToProjectRoot
Brett Sutton
  • 3,900
  • 2
  • 28
  • 53