飞镖页面http://pub.dartlang.org/doc/#adding-a-dependency描述了如何让飞镖文件(parser_test.dart)使用“导入”包从自己的包中导入文件:... “导入风格。这似乎暗示这是一件好事 - 比使用相对路径更好。显示的此示例适用于test中似乎很特殊的文件。但是,那么为什么从包中的 lib 导入相同的包lib 文件没有意义。也许它确实有意义,但如果是这样,pub 更新并不方便。
foo/
/lib/
foo_lib_1.dart
foo_lib_2.dart
src/
foo_lib_1/
foo_lib_1_impl.dart
foo_lib_2/
foo_lib_2_impl.dart
假设foo_lib_2使用foo_lib_1。foo_lib_2.dart有两个选项:
- 导入“../foo_lib_1.dart”;
- 导入“包:foo/foo_lib_1.dart”;
我的猜测是建议的方法是第一个驻留在lib下的任何此类导入。我认为这是pub update的原因似乎自动在bin、test或example的任何包文件夹中提供软链接到foo,例如foo -> ../lib。但是,它对顶级foo中的packages文件夹的作用不同。这意味着要使第二种类型的导入(即包导入)起作用,您需要添加:
foo:
path: lib
到pubspec.yaml中foo的依赖项。库使用包样式导入从自己的包中导入另一个库(不在test、bin或example中)是否有任何优点或缺点?有明显的不一致的原因吗?
接受下面的答案后,我仍然没有看到它。这是我在 shell 会话中看到的内容,我想将此行为与答案相协调。任何解释表示赞赏。我使用的是 emacs 而不是 DartEditor,因此这里使用的是老式的命令行方法。
### Show all files, one dart library file and one yaml, plus empty
### lib and test folders
user@user-thinkpad:/tmp/uml_codegen_sample$ ls -R
.:
lib pubspec.yaml test
./lib:
plusauri.dart
./test:
### Show contents of pubspec
user@user-thinkpad:/tmp/uml_codegen_sample$ cat pubspec.yaml
name: domain_model
version: 0.0.1
description: >
Auto-generated support from /home/user/plusauri/modeling/plusauri.xmi.json
dependencies:
ebisu:
path: /home/user/open_source/codegen/dart/ebisu
### Run pub install and show the changes. Note there is a soft
### link to packages from test, but not lib.
user@user-thinkpad:/tmp/uml_codegen_sample$ pub install
Resolving dependencies...
Dependencies installed!
Some packages that were installed are not compatible with your SDK version 0.4.7+5.r21658 and may not work:
- 'pathos' requires >=0.5.0+1
You may be able to resolve this by upgrading to the latest Dart SDK
or adding a version constraint to use an older version of a package.
user@user-thinkpad:/tmp/uml_codegen_sample$ ls -R
.:
lib packages pubspec.lock pubspec.yaml test
./lib:
plusauri.dart
./packages:
domain_model ebisu pathos
./test:
packages
### Note here the program does not work, and suspiciously pub
### install put no packages link under lib like it did test
user@user-thinkpad:/tmp/uml_codegen_sample$ dart lib/plusauri.dart
Unable to open file: /tmp/uml_codegen_sample/lib/packages/ebisu/ebisu_utils.dart'file:///tmp/uml_codegen_sample/lib/plusauri.dart': Error: line 5 pos 1: library handler failed
import "package:ebisu/ebisu_utils.dart" as EBISU_UTILS;
^
### Copy the same dart file to test to show that it can run there
### just fine
user@user-thinkpad:/tmp/uml_codegen_sample$ cp lib/plusauri.dart test/
user@user-thinkpad:/tmp/uml_codegen_sample$ dart test/plusauri.dart
Main for library plusauri
user@user-thinkpad:/tmp/uml_codegen_sample$
### Finally, manually create the soft link in lib, to show it will
### then run
user@user-thinkpad:/tmp/uml_codegen_sample$ ln -s ../packages lib/packages
user@user-thinkpad:/tmp/uml_codegen_sample$ dart lib/plusauri.dart
Main for library plusauri