Activating a dart app with pub global activate - dart

I created a new Dart app like this:
dart create hello
I can run the app like this:
dart run hello/bin/hello.dart
I try to activate the app like this:
dart pub global activate --source path hello
But I can't run the file like I would expect:
hello
zsh: command not found: hello
The .pub-cache/bin folder is in my cache but pub global activate didn't put it there.
This does work:
dart pub global run hello
Hello world!
But I'd like to be able to run the script without typing dart pub global run every time.
If I do a package from pub.dev it works ok:
dart pub global activate webdev
It puts a webdev executable inside the .pub-cache/bin folder and I can run it.
webdev --version
2.7.4
So is there another step I need to do to make my hello app get into the executable folder?
I also tried compiling it:
dart compile exe hello/bin/hello.dart
And activating it again:
dart pub global activate --source path hello
But there is still no binary in the .pub-cache/bin folder. Any suggestions?
Update
After getting Kevin's answer below, I added the following to pubspec.yaml:
executables:
hello:
Then I ran the following command:
dart pub global activate --source path hello
which gave the following result (username modified):
Resolving dependencies...
Got dependencies!
Package hello is currently active at path "/Users/suragch/Dev/DartProjects/hello".
Installed executable hello.
Activated hello 1.0.0 at path "/Users/suragch/Dev/DartProjects/hello".
But if I run this:
hello
I get the following error:
/Users/suragch/.pub-cache/bin/hello: line 7: pub: command not found
Running this still works, though:
dart pub global run hello
Hello world!

You need to list the files under bin/ that you want to be executable in the executables section in pubspec.yaml file. For example for bin/hello.dart to be executable add the following to pubspec.yaml
....
executables:
hello:
....
Then when you run: dart pub global activate --source path hello
You can now call hello in bin/ without running: pub global run hello
Again
You have to list every executables you intend to be made available to the user of your package in the CLI under the executables key in pubspec.yaml

So it looks like this problem was caused because because of a bug as a result of switching from pub to dart pub. If you open the .pub-cache/bin/hello file with an editor, you'll see the following:
#!/usr/bin/env sh
# This file was created by pub v2.13.1.
# Package: hello
# Version: 1.0.0
# Executable: hello
# Script: hello
pub global run hello:hello "$#"
The error about line 7 is the last line where you can see it is referencing plain pub. Change that line to the following:
dart pub global run hello:hello "$#"
Now you can run your app from anywhere:
hello
Hello world!
This is only a temporary workaround. Follow this issue for updates. Thank you to Kelvin Omereshone for pointing me in the right direction.

Related

Permission denied on running globally activated dart package

I'm trying to develop a command line package so I followed these steps:
dart create --template=package-simple package_name
then activating the package globally with:
dart pub global activate --source path $(pwd)
note that I didn't find anything in the documentation about --source path actual_path but whenever I use the command only with --source actual_path I get the following:
"/Users/user/Desktop/Workspace/package_name" is not an allowed value for option "source".
So after successfully activating the package I tried to run it using:
package_name
But I got this:
zsh: command not found: package_name
Found out that the documentation here is outdated and after searching the local directory I found the actual directory is global_packages not bin, and so I've changed it.
Now when I try package_name I get:
zsh: permission denied: package_name
After investigation I found that package_name is a directory in the global_packages directory and it only contains pubspec.lock so I've tried chmod a+x on the file and the directory with no avail.
Further investigation the global_packages contained another package intl_utils inside I found:
.packages incremental
.dart_tool bin pubspec.lock
But running intl_utils on the terminal too get's me the same error:
zsh: permission denied: intl_utils
I didn't install the package manually but the VS Code extension Flutter Intl did and it seems to work perfectly fine.
Now I have two questions:
permission denied on what exactly since the Flutter Intl extension works perfectly???
I have a file in my package bin/my_package.dart but it didn't go there, am I doing something wrong?
I'm running dart through flutter:
Flutter (Channel master, 2.13.0-0.0.pre.90, on macOS 12.3 21E230 darwin-x64, locale en-US)
dart --version
Dart SDK version: 2.17.0-202.0.dev (dev) (Thu Mar 10 20:55:41 2022 -0800) on "macos_x64"
Edit #1
Tried dart pub global run package_name and it worked but it seems like it's building the package again, but still I need to use it without the long line the correct way.
Edit #2
To create the app I've tried
dart create -t console-full package_name but still with the same results.
So after messing around and changing the branch (a lot) turns out the problem is in the pubspec.yaml where there should be :
executables:
package_name: # you can add a custom path here if you want though
And I guess the docs were right about "bin" directory but it left me wondering about global_packages and intl_utils

Testing solution from Exercism

I just installed Exercism and would like to test if I managed to complete the hello-world assignment.
I changed the hello_world.dart file in the lib folder to print the solution, but now I am struggling with running the test file.
In the guide it says to simply run
$ pub run test
Is the $-sign meant to mean something to me? Cause all I am getting is: "'$' is not recognized as an internal or external command,operable program or batch file." I have the same issue with running "pub get" or "pub run test". (What does pub mean?)
To summarize, I ran:
exercism configure "my token"
exercism download --exercise=hello-world --track=dart
cd C:\Users\Martin\exercism\dart\hello-world
I changed the file in C:\Users\Martin\Exercism\dart\hello-world\lib
And then I tried
$ pub run test
pub run test
pub get
And none of these seem to do anything. So, I don't know how to test my file.
I do have flutter installed and run is fine with Android Studio, if that is relevant. It seems to me that I can choose to write the solution in both notepad++ or Android Studio.
Thanks for any help
pub is package manager for dart programming language, just like npm for javascript. Since you've installed it on WINDOWS i'm guessing you used choco - package manager to install the dart-sdk. https://dart.dev/get-dart.
Once installed make sure you've set dart-sdk the path in Environment variable.

Create and Run Dart Console Application Using VSCode?

I can create Flutter new application using VSCode. Now I want to learn Dart programming language.
How can I create and run a Dart console application Using VS Code?
There is an easy way to create and run a Dart console application:
Open VSCode
Press cmd + shift + p on Mac or ctrl + shift + p on windows.
Choose Dart: New project
4.Then you should select Console Application
5.After locating console application everything is ready for you 😊
Well, how to run it? So easy!!!🙂
Just go to terminal and type: "dart main.dart"
or copy the main.dart path then type: "dart (paste main file path)"
or press ctrl + F5
That's it. I hope you enjoy. 😁
To run a Dart console application:
Install the Code Runner Extension
Open the Dart code file in Text Editor, then use shortcut Ctrl+Alt+N, or right click the Text Editor and then click Run Code in context menu, the Dart console application will run and the output will be shown in the Output Window.
Steps to run in VS Code:
Open Visual Studio Code.
Close whatever folder you have open.
Make sure you have Dart Code Extension installed, or install it if you don't have it.
Press Ctrl + Shift + P on Windows or Cmd + Shift + P on Mac
Select Dart: New Project
Select Console Application
Type in a name, example: hello_world
Hit Enter
Select a folder that already exists.
Wait until it finishes
main.dart file will show up.
Hit Ctrl + F5 to run or Run, Run without debugging.
In the Debug Console window, you will see the result.
Dart SDK installing with Flutter.
For MacOS Dart directory /Users/burak/flutter/bin/cache/dart-sdk/bin
Add to PATH this bin directory and use dart with below.
dart --version
Run .dart file.
dart main.dart
I found a way to do it, you can install packages etc as well. instructions below..
- Installing
pub global activate stagehand
- Add to .bash_profile
export PATH="$PATH":"$HOME/.pub-cache/bin"
- Create and download packages
mkdir fancy_project
cd fancy_project
stagehand console-full
pub get
If you are not only flutter mobile developer, but web-developer too, you are probably using npm or yarn. If so you can use nodemon to run your dart code automatically after any change
install brew and npm if you don't have it
install $ npm install -g nodemon (or yarn: $ yarn global add nodemon )
Create dart project.
Create nodemon.json file
{
"watch": [
"lib/" // or any other place where you hold the dart files
],
"execMap": {
"dart": "dart"
},
"ext": "dart"
}
Run you code: $ nodemon ./lib/main.dart
p.s. Code in the .dart file should have main function:
void main() {
print('hello world');
}
If you are using VsCode, just save code by CMD + S and nodemon demon will run updated code,
If you already have flutter (windows) installed , then find where you installed flutter. Once you are in flutter folder then go through
flutter\bin\cache\dart-sdk\bin .
I have installed flutter on C:\src folder so my path is
C:\src\flutter\bin\cache\dart-sdk\bin .
Now copy this full path and set it as environment variable.
If you can't set environment variable then open search bar and type "env" . Click the result click environment variable and add that path in( Path variable).
In VScode Ctrl+Shift+` this will open a new terminal . Right click on your dart code and copy path.
In terminal just type this line without bracket and press enter
[dart "paste path"]
If you have already run flutter application on VSCode, then you should be able to run dart console applications. However, please check the following:
Dart SDK is already installed on your system.
Check if the Dart 2.20 Extension (Current Version) is already installed on the VSCode. If not, please install the same on your VSCode, and restart VSCode.
Create Dart console application and save with .dart extension (say Hello.dart), in a suitable location.
From the top menu bar of VSCode, open a terminal. From the
terminal inside of the VSCode, change to directory where you have saved
the dart program.
Run the dart console program with the command:
dart [hello.dart]
Console program results are shown in the terminal. I hope this is what you are looking for.
You can insert this JSON code in your setting.json in the .vscode folder .
"dart.sdkPath": "[DART_SDK_PATH]"
For example in my Windows OS :
"dart.sdkPath": "F:\\dev\\sdk\\dart-sdk"
And My All Setting in setting.json :
{
"version": "0.2.0",
"configurations": [
{
"name": "Dart",
"program": "bin/main.dart",
"request": "launch",
"type": "dart"
}
] ,
"dart.sdkPath": "F:\\dev\\sdk\\dart-sdk"
}
Notice the This Image :
boom that's it. it will print value in console.
Straight Forward Answer
Install Flutter SDK ( this contains dart SDK by default )
Add dart to you environment path
Now create a dart file and run it with the dart command, for example create sunday.dart file and put the following content inside.
void main(){
print("HAVE A NICE DAY")
}
run the dart code with the following code
dart sunday.dart the output will be
HAVE A NICE DAY
Have a look at the dcli package. It is an sdk for building console apps in dart.
To create a sample dart console app that demonstrates a number of best practices run
dcli create --template=full myproject

Dart - getting start - missing step - how to install pub

I'm trying to get started and setup to develop Dart.
I'm following the instructions on https://webdev.dartlang.org/guides/get-started to install on Linux. However there seems to be an essential step missing.
On the above link, step 2 is about installing the SDK.
Step 3 is titled:
Get CLI tools or WebStorm (or both)
However step gives examples of using the pub command, and the links it references also use this command, however there are no instructions on where to find the pub utility or how to install it.
I would have assumed that pub was provided as part of the Dart SDK. I can run dart in my terminal, and see that it is installed. For example dart --version returns Dart VM version: 2.0.0 (Unknown timestamp) on "linux_x64". But pub returns zsh: command not found: pub
My question, therefore, is where do I find, and how do I install pub?
You need to add thedart-sdk/bin directory to the system PATH variable.

stagehand command not found when trying to create new application

I just installed stagehand on my mac and the install went off without a hitch, but when I try using it to spawn a new dart application I get this error -bash: stagehand: command not found. Any ideas?
I've looked at my machine, and stagehand is in ~/.pub-cache/bin which I think I had to add to my .profile manually:
export PATH=$PATH:~/.pub-cache/bin
(edit: I've just found the confirming information tucked away in the pub pages: https://www.dartlang.org/tools/pub/cmd/pub-global#running-a-script-from-your-path)
I'm on mac too. I did this:
brew tap dart-lang/dart
brew install dart
stagehand
zsh: command not found: stagehand
And then this command fixed it:
pub global activate stagehand
Now it works:
stagehand
Welcome to Stagehand!
I came across same issue on my windows pc. I had to check flutter installation directory and add following .pub-cache\bin directory to Windows path environment variable.
D:\flutter\.pub-cache\bin
I think this might be helpful to any Windows user.
On MacOS run this command: export PATH="$PATH":"$HOME/.pub-cache/bin"
I installed it, but still gave me error not found, but I noticed warning in following message after running the command:
Package stagehand is currently active at version 3.3.11.
Resolving dependencies...
The package stagehand is already activated at newest available version.
To recompile executables, first run `global decativate stagehand`.
Installed executable stagehand.
Warning: Pub installs executables into C:\Users\Nijat\AppData\Local\Pub\Cache\bin, which is not on your path.
You can fix that by adding that directory to your system's "Path" environment variable.
A web search for "configure windows path" will show you how.
Activated stagehand 3.3.11.
The problem is this line:
Warning: Pub installs executables into C:\Users\Nijat\AppData\Local\Pub\Cache\bin, which is not on your path.
Thus, you also need to add that to your environment variables. This solved my problem and now I can run stagehand.

Resources