3

飞镖页面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_1foo_lib_2.dart有两个选项:

  • 导入“../foo_lib_1.dart”;
  • 导入“包:foo/foo_lib_1.dart”;

我的猜测是建议的方法是第一个驻留在lib下的任何此类导入。我认为这是pub update的原因似乎自动在bintestexample的任何文件夹中提供软链接到foo,例如foo -> ../lib。但是,它对顶级foo中的packages文件夹的作用不同。这意味着要使第二种类型的导入(即包导入)起作用,您需要添加:

foo:
  path: lib

pubspec.yaml中foo的依赖项。库使用包样式导入从自己的包中导入另一个库(不在testbinexample中)是否有任何优点或缺点?有明显的不一致的原因吗?


接受下面的答案后,我仍然没有看到它。这是我在 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
4

1 回答 1

3

实际上,您绝对可以使用package:foo/foo_lib_1.dart语法导入,而无需更改pubspec.yaml甚至pubspec.yaml首先创建一个!

您可以从语言级别看到这是正确的,在此测试中:https ://github.com/dart-lang/bleeding_edge/blob/master/dart/tests/standalone/package/packages/package1.dart

这在野外的一个例子是:https ://github.com/kevmoo/hop.dart/blob/master/lib/hop_tasks.dart#L17


我认为以一种或另一种方式编写没有任何好处,除了编写相对路径稍微短一些。

从项目结构的角度来看,当我钻入不会向用户公开的子目录时,我会使用相对路径导入。src通常被视为对外部用户不可见的特定于实现的细节,因此请使用指向您内心内容的相对路径。

但是,如果您在多个目录中工作,那么您应该使用package:导入来强化这些部分是独立且可互换的想法。在 lib 目录本身中,您想说这两个库虽然可能相互依赖,但可以独立存在并且不受其物理位置的约束。

我建议不要../在您的导入中使用,因为它很脆弱,并且如果/当您修改目录结构或部署时可能会以奇怪的方式中断。

于 2013-04-21T22:20:10.120 回答