dart - 如何使用“dart pub global run”命令运行脚本?
问题描述
我的包目录foo
:
pubspec.yaml 文件有:
name: foo
executables:
foo: bar
foo
使用激活包dart pub global activate ...
,如果我将目录添加到PATH
环境,则以下命令有效:
foo bar
但是如果我删除它并尝试运行该dart pub global run
命令,它就不起作用。
me@mac foo % dart pub global run foo bar
Could not find bin/foo.dart.
那么,如何在不将其添加到PATH
using的情况下运行脚本,如此处dart pub global run
建议的那样。
您可以从命令行直接从激活的包中运行脚本。如果无法直接运行脚本,也可以使用 dart pub global run。
注意:dart pub global run foo:bar
有效,但这与文件中定义的可执行文件无关pubspec.yaml
。
解决方案
指定:
executables:
foo: bar
意味着当包被激活时,该命令foo
将映射到文件bar.dart
. bar
调用时无需指定。
例如,我有这个包:
name: dictionary
...
executables:
scrabble:
它定义了一个scrabble
调用scrabble.dart
. 我这样激活它:
dart pub global activate --source path dictionary
这报告:
...
Installed executable scrabble.
Activated dictionary 1.0.0 at path "[...]\Dart\dictionary".
我可以调用scrabble
调用scrabble.dart
. 我也可以这样调用它:
dart pub global run dictionary:scrabble
如果我pubspec
有:
executables:
dictionary: scrabble
然后该命令dictionary
调用文件scrabble.dart
。
(不要忘记pub
在您的PATH
.C:\Users\pohara\AppData\Roaming\Pub\Cache\bin
补充:继续这个dictionary: scrabble
例子,当我激活字典时,我得到:
...
Installed executable dictionary.
Activated dictionary 1.0.0 at path "[...]\Dart\dictionary".
我pub
在 Windows 上的缓存有这个文件来运行dictionary
命令:
> cat C:\Users\pohara\AppData\Roaming\Pub\Cache\bin\dictionary.bat
@echo off
rem This file was created by pub v2.12.2.
rem Package: dictionary
rem Version: 1.0.0
rem Executable: dictionary
rem Script: scrabble
pub global run dictionary:scrabble %*
因此该命令dictionary
显式运行包scrabble
中的文件dictionary
。
这两个命令都失败了:
> dart pub global run dictionary scrabble
Could not find bin\dictionary.dart.
> dart pub global run dictionary
Could not find bin\dictionary.dart.
当然,这个成功了:
> dart pub global run dictionary:scrabble
推荐阅读
- visual-studio - Windows 窗体和 Visual Studio 上的 Program.cs 之间的链接
- c# - 运行我的随机数程序时出现空白屏幕
- rust - Rust“类型的可变性不同”
- django - Django REST API POST 熊猫数据框
- shell - 如何从linux中的句子中剪切部分字符串?
- sql - Sql:如何获得前一行/天的收盘报价?
- javascript - Node.js Discord.js UnhandledPromiseRejectionWarning:TypeError:无法读取未定义的属性“calculatedPosition”
- python - 在python上合并数据框
- php - 自动检测用户何时验证电子邮件
- r - R中的特殊字符奇怪的行为