使用酒吧全局激活激活飞镖应用程序



我创建了一个新的Dart应用程序,如下所示:

dart create hello

我可以像这样运行该应用程序:

dart run hello/bin/hello.dart

我尝试像这样激活该应用程序:

dart pub global activate --source path hello

但是我无法像预期的那样运行该文件:

hello

zsh:找不到命令:你好

.pub-cache/bin文件夹在我的缓存中,但 pub 全局激活没有把它放在那里。

这确实有效:

dart pub global run hello

世界您好!

但我希望能够在每次都无需键入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

世界您好!

您需要在文件的executables部分中列出要执行bin/下的文件pubspec.yaml。例如,要使bin/hello.dart可执行,请将以下内容添加到pubspec.yaml

....
executables:
hello:
....

然后当你运行时:dart pub global activate --source path hello

现在,您可以在bin/中调用hello而无需运行:pub global run hello

您必须在 CLI 中的executables键下列出您打算提供给软件包用户的每个可执行文件pubspec.yaml

所以看起来这个问题是因为从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

世界您好!

这只是一种临时解决方法。请关注此问题以获取更新。感谢Kelvin Omereshone为我指明了正确的方向。

最新更新