OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | |
2 // for details. All rights reserved. Use of this source code is governed by a | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 library path_source; | |
6 | |
7 import 'dart:async'; | |
8 import 'dart:io'; | |
9 | |
10 import '../../pkg/path/lib/path.dart' as path; | |
11 | |
12 import 'io.dart'; | |
13 import 'package.dart'; | |
14 import 'pubspec.dart'; | |
15 import 'version.dart'; | |
16 import 'source.dart'; | |
17 import 'utils.dart'; | |
18 | |
19 /// A package [Source] that installs packages from a given local file path. | |
nweiz
2013/02/13 00:13:56
Add a TODO here about supporting relative paths.
Bob Nystrom
2013/02/13 18:28:36
Done.
| |
20 class PathSource extends Source { | |
21 final name = 'path'; | |
22 final shouldCache = false; | |
23 | |
24 Future<Pubspec> describe(PackageId id) { | |
25 return defer(() { | |
26 var error = _validatePath(id.name, id.description); | |
27 if (error != null) throw error; | |
28 | |
29 return new Pubspec.load(id.name, id.description, systemCache.sources); | |
30 }); | |
31 } | |
32 | |
33 Future<bool> install(PackageId id, String path) { | |
34 return defer(() { | |
35 if (_validatePath(id.name, id.description) != null) return false; | |
36 return createPackageSymlink(id.name, id.description, path); | |
37 }).then((_) => true); | |
38 } | |
39 | |
40 void validateDescription(description, {bool fromLockFile: false}) { | |
41 if (description is! String) { | |
42 throw new FormatException("The description must be a path string."); | |
43 } | |
44 } | |
45 | |
46 /// Ensures that [dir] is a valid path. It must be an absolute path that | |
47 /// points to an existing directory. Returns `null` if the path is OK, or an | |
48 /// error message if the path is invalid. | |
49 String _validatePath(String name, String dir) { | |
nweiz
2013/02/13 00:13:56
It seems like this should throw an error, which ca
Bob Nystrom
2013/02/13 18:28:36
Done.
| |
50 // Relative paths are not (currently) allowed because the user would expect | |
51 // them to be relative to the pubspec where the dependency appears. That in | |
52 // turn means that two pubspecs in different locations with the same | |
53 // relative path dependency could refer to two different packages. That | |
54 // violates pub's rule that a description should uniquely identify a | |
55 // package. | |
56 // | |
57 // At some point, we may want to loosen this, but it will mean tracking | |
58 // where a given PackageId appeared. | |
59 if (!path.isAbsolute(dir)) { | |
60 return "Path dependency for package '$name' must be an absolute path. " | |
61 "Was '$dir'."; | |
nweiz
2013/02/13 00:13:56
Don't we usually indent continued strings four spa
Bob Nystrom
2013/02/13 18:28:36
Fixed. We do line up continued strings inside func
| |
62 } | |
63 | |
64 if (fileExists(dir)) { | |
65 return "Path dependency for package '$name' must refer to a " | |
66 "directory, not a file. Was '$dir'."; | |
67 } | |
68 | |
69 if (!dirExists(dir)) { | |
70 return "Could not find package '$name' at '$dir'."; | |
71 } | |
72 | |
73 return null; | |
74 } | |
75 } | |
OLD | NEW |