Flutter: How to get app path from within the app after it's built - flutter-linux

I have a project named foo.
I built the bundle by flutter build linux and the binary is available in /build/linux/x64/release/bundle/foo
Lets say I moved this bundle folder to ~/Downloads and then execute foo
I should be able to display the path of the binary /home/user/Downloads/bundle/foo.
If I rename foo to bar manually and execute bar, I should be able to display /home/user/Downloads/bundle/bar
Directory.current will give the parent directory (/home/user/Downloads/bundle/), but I need the name of the binary too (foo). Is this possible?

Related

Correct way to infer relative path from project

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.
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');
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

Unable to create desktop application support from existing project in flutter

I am creating a desktop application from an existing app.
The command given in documentation is :
flutter create .
Running this command in the root of the project directory I am getting this error
"MyApp" is not a valid Dart package name.
Note: MyApp is name of my project
Flutter only allows project names that are lower case letters and may contain an underscore or a number. This is actually a convention from dart packages. Check here for the prescribed convention. It is likely that your folder name is used by flutter as its project name. You can try to overcome this by changing the folder name. But please do keep a backup before doing flutter create on an existing project. For more details on possible options check this post as well.
write app name in lowercase
flutter create myapp

How to bundle native macos executable with a flutter app?

I want to bundle a native console program for macos written in Rust, into a Flutter app.
The Flutter app needs to call this program from Dart with something like this:
Process.runSync("./myconsoleapp", ["argument1", "argument2"]);
The Flutter app can't seem to find myconsoleapp.
Is there a way to embed myconsoleapp into the Flutter app, and make it executable? I tried manually adding and it didn't work.
Steve
Flutter for windows simply bundle project.exe along with a bunch of other files into a directory in (build\windows\runner\debug\). No one can stops you from adding your files to that directory.
In your dart code, simply start a new process from the newly added file:
var exePath =
p.join(p.dirname(Platform.resolvedExecutable), 'myFile.exe');
var result = await Process.run(exePath);
It can't find your executable because there is no relationship between ./--which is the current directory in the environment your app was launched from--and your application's location. If you want to access something bundled in your application you should be using NSBundle's pathForResource:ofType: to get the path to it (either via a method channel, or FFI).
Is there a way to embed myconsoleapp into the Flutter app,
You would add it as a bundled resource via Xcode. (There's nothing Flutter-specific about this step.)
and make it executable?
Presumably the product of your Rust build process is already executable.

Swift save files to project folder

I'm writing a swift terminal program (let's say it's called "myProject") that requires me to save some files. I would like the files to be saved within the project folder programmatically.
I've seen in this post there are some environment variables like $(PROJECT_DIR) that give the path to the project folder. I've already checked that XCode does have the $(PROJECT_DIR) variable using:
$ xcodebuild -project myProject.xcodeproj -target "myProject" -showBuildSettings
But I'm not exactly sure how I can use this variable in my actual code.
Like if I tried
print($(PROJECT_DIR))
I would just get the errors:
use of unresolved identifier '$'
use of unresolved identifier 'PROJECT_DIR'
How do I get the path to my project folder programmatically?
Those aren't environment variables that are available to your program at runtime; they're used internally by Xcode for the build process.
Your program is located at Bundle.main.executableURL (NSBundle.mainBundle().executableURL in Swift 2) at runtime (executablePath is also available if you prefer).
If you have additional files that your program needs to access, they must be in a known location. You can use a standard directory in the filesystem such as /usr/local/bin, or you can create a bundle and copy those resources into the bundle as part of the build process.

ClickOnce and mageUI adding custom file folder problems

What I do: Publishing .Net 3.5 application via VS, or msbuild (whatever), and now I need to insert to the published application separate directory (Modules - not added as a reference to the project - our project architecture does not allow this). So I'm copying the folder to published location then runs mageUI.exe select the application manifest and then the problems starts. As I press the populate button it's adding also hidden directory? So I remove the directory, after I want to save the manifest file I get the
"Warning MSB3110: Assembly 'NAME.Shell.application.deploy' has mismatched identity 'NAME.Shell.application', expected file name: 'NAME.Shell.application.application'".
Did you have the same problems, according to http://smartclient.codeplex.com/Project/Download/FileDownload.aspx?DownloadId=5060 it should be easy as 1-2-3 but it's not.
On the irony side, I am currently updating the SCSF documentation for ClickOnce deployment for .NET 4.0, and now I know exactly what the problem it.
First, you want to publish to a disk location, not directly to the webserver. So do that, and then copy the dll's you want to add to the deployment into the version folder with the application manifest (.manifest). Rename them yourself -- just add .deploy on the end.
Bring up MageUI and open the application manifest in that folder, then uncheck the box that says "add .deploy to the file names". Click "populate".
Then remove the deployment manifest from the list; it's called something like appname.application. You don't want it to be included in the manifest's list of files.
Now you can save and sign the application manifest. Then without exiting mageUI, open the deployment manifest that is in the root folder (NOT the one in the version folder). Click on "Application Reference", then click Select Manifest. Dig down to the application manifest in the version folder that you just signed and select it. Then just save and sign the deployment manifest. It puts a relative path in there, so if you modify the .application file in the version folder, it won't work right when deployed.
Now take that .application file from the root folder and copy it into the version folder (replacing the one that's there) so you have the right copy for that version in case you need it later.
Now copy the whole shebang out to the webserver. it should work.
I think you are saying you need to add a folder of stuff to your ClickOnce deployment? What exactly are you adding? You say modules -- are they going to be compiled in, or is it something else?
If you want to add files to the deployment you have already created, you need to use MageUI to edit the application manifest, which is called something like appname.exe.manifest. (The appname.application is actually called the deployment manifest, which is confusing but true.) After adding the files, you will need to re-sign the deployment manifest as well (assuming you signed it in the first place).
It would be easier just to add what you want included to your project and re-publish. Depending on what it is, you can set the build action to 'content' (jpegs, html, etc) or 'none' (dll's) and then set "copy to output directory" to true, and it will include it in your deployment.
I'm not sure exactly what you are including. Give me more info, and I can provide more specific help.
RobinDotNet