使用 pub global activate 激活 dart 应用程序
Activating a dart app with pub global activate
我像这样创建了一个新的 Dart 应用程序:
dart create hello
我可以这样 运行 应用程序:
dart run hello/bin/hello.dart
我尝试这样激活应用程序:
dart pub global activate --source path hello
但我不能 运行 我期望的文件:
hello
zsh: command not found: hello
.pub-cache/bin
文件夹在我的缓存中,但 pub global activate 没有把它放在那里。
这确实有效:
dart pub global run hello
Hello world!
但我希望能够 运行 脚本而无需每次都输入 dart pub global run
。
如果我从 pub.dev 做一个包,它工作正常:
dart pub global activate webdev
它将一个 webdev 可执行文件放入 .pub-cache/bin
文件夹,我可以 运行 它。
webdev --version
2.7.4
那么我还需要执行其他步骤才能让我的 hello 应用程序进入可执行文件夹吗?
我也试过编译它:
dart compile exe hello/bin/hello.dart
并再次激活它:
dart pub global activate --source path hello
但是.pub-cache/bin
文件夹中仍然没有二进制文件。有什么建议吗?
更新
得到凯文下面的回答后,我将以下内容添加到pubspec.yaml:
executables:
hello:
然后我运行下面的命令:
dart pub global activate --source path hello
给出了以下结果(修改了用户名):
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".
但是如果我运行这个:
hello
我收到以下错误:
/Users/suragch/.pub-cache/bin/hello: line 7: pub: command not found
运行 不过这仍然有效:
dart pub global run hello
Hello world!
您需要在 pubspec.yaml
文件的 executables
部分列出您希望可执行的 bin/
下的文件。例如,要使 bin/hello.dart
可执行,请将以下内容添加到 pubspec.yaml
....
executables:
hello:
....
那么当你 运行: dart pub global activate --source path hello
您现在可以在 bin/
中调用 hello
而无需 运行ning:pub global run hello
再次
您必须在 pubspec.yaml
中的 executables
键下列出您打算在 CLI 中提供给软件包用户的每个可执行文件
所以看起来这个问题是由于从 pub
切换到 dart pub
导致的错误引起的。如果您使用编辑器打开 .pub-cache/bin/hello
文件,您将看到以下内容:
#!/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 "$@"
关于第 7 行的错误是最后一行,您可以看到它引用的是纯文本 pub
。将该行更改为以下内容:
dart pub global run hello:hello "$@"
现在您可以从任何地方运行您的应用程序:
hello
Hello world!
这只是临时解决方法。关注 this issue 获取更新。感谢 Kelvin Omereshone 为我指明了正确的方向。
我像这样创建了一个新的 Dart 应用程序:
dart create hello
我可以这样 运行 应用程序:
dart run hello/bin/hello.dart
我尝试这样激活应用程序:
dart pub global activate --source path hello
但我不能 运行 我期望的文件:
hello
zsh: command not found: hello
.pub-cache/bin
文件夹在我的缓存中,但 pub global activate 没有把它放在那里。
这确实有效:
dart pub global run hello
Hello world!
但我希望能够 运行 脚本而无需每次都输入 dart pub global run
。
如果我从 pub.dev 做一个包,它工作正常:
dart pub global activate webdev
它将一个 webdev 可执行文件放入 .pub-cache/bin
文件夹,我可以 运行 它。
webdev --version
2.7.4
那么我还需要执行其他步骤才能让我的 hello 应用程序进入可执行文件夹吗?
我也试过编译它:
dart compile exe hello/bin/hello.dart
并再次激活它:
dart pub global activate --source path hello
但是.pub-cache/bin
文件夹中仍然没有二进制文件。有什么建议吗?
更新
得到凯文下面的回答后,我将以下内容添加到pubspec.yaml:
executables:
hello:
然后我运行下面的命令:
dart pub global activate --source path hello
给出了以下结果(修改了用户名):
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".
但是如果我运行这个:
hello
我收到以下错误:
/Users/suragch/.pub-cache/bin/hello: line 7: pub: command not found
运行 不过这仍然有效:
dart pub global run hello
Hello world!
您需要在 pubspec.yaml
文件的 executables
部分列出您希望可执行的 bin/
下的文件。例如,要使 bin/hello.dart
可执行,请将以下内容添加到 pubspec.yaml
....
executables:
hello:
....
那么当你 运行: dart pub global activate --source path hello
您现在可以在 bin/
中调用 hello
而无需 运行ning:pub global run hello
再次
您必须在 pubspec.yaml
executables
键下列出您打算在 CLI 中提供给软件包用户的每个可执行文件
所以看起来这个问题是由于从 pub
切换到 dart pub
导致的错误引起的。如果您使用编辑器打开 .pub-cache/bin/hello
文件,您将看到以下内容:
#!/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 "$@"
关于第 7 行的错误是最后一行,您可以看到它引用的是纯文本 pub
。将该行更改为以下内容:
dart pub global run hello:hello "$@"
现在您可以从任何地方运行您的应用程序:
hello
Hello world!
这只是临时解决方法。关注 this issue 获取更新。感谢 Kelvin Omereshone 为我指明了正确的方向。