Index: utils/pub/path_source.dart |
diff --git a/utils/pub/path_source.dart b/utils/pub/path_source.dart |
new file mode 100644 |
index 0000000000000000000000000000000000000000..a50b17188ae91d36aae30fd718d005cbfdb5e887 |
--- /dev/null |
+++ b/utils/pub/path_source.dart |
@@ -0,0 +1,75 @@ |
+// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
+// for details. All rights reserved. Use of this source code is governed by a |
+// BSD-style license that can be found in the LICENSE file. |
+ |
+library path_source; |
+ |
+import 'dart:async'; |
+import 'dart:io'; |
+ |
+import '../../pkg/path/lib/path.dart' as path; |
+ |
+import 'io.dart'; |
+import 'package.dart'; |
+import 'pubspec.dart'; |
+import 'version.dart'; |
+import 'source.dart'; |
+import 'utils.dart'; |
+ |
+/// 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.
|
+class PathSource extends Source { |
+ final name = 'path'; |
+ final shouldCache = false; |
+ |
+ Future<Pubspec> describe(PackageId id) { |
+ return defer(() { |
+ var error = _validatePath(id.name, id.description); |
+ if (error != null) throw error; |
+ |
+ return new Pubspec.load(id.name, id.description, systemCache.sources); |
+ }); |
+ } |
+ |
+ Future<bool> install(PackageId id, String path) { |
+ return defer(() { |
+ if (_validatePath(id.name, id.description) != null) return false; |
+ return createPackageSymlink(id.name, id.description, path); |
+ }).then((_) => true); |
+ } |
+ |
+ void validateDescription(description, {bool fromLockFile: false}) { |
+ if (description is! String) { |
+ throw new FormatException("The description must be a path string."); |
+ } |
+ } |
+ |
+ /// Ensures that [dir] is a valid path. It must be an absolute path that |
+ /// points to an existing directory. Returns `null` if the path is OK, or an |
+ /// error message if the path is invalid. |
+ 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.
|
+ // Relative paths are not (currently) allowed because the user would expect |
+ // them to be relative to the pubspec where the dependency appears. That in |
+ // turn means that two pubspecs in different locations with the same |
+ // relative path dependency could refer to two different packages. That |
+ // violates pub's rule that a description should uniquely identify a |
+ // package. |
+ // |
+ // At some point, we may want to loosen this, but it will mean tracking |
+ // where a given PackageId appeared. |
+ if (!path.isAbsolute(dir)) { |
+ return "Path dependency for package '$name' must be an absolute path. " |
+ "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
|
+ } |
+ |
+ if (fileExists(dir)) { |
+ return "Path dependency for package '$name' must refer to a " |
+ "directory, not a file. Was '$dir'."; |
+ } |
+ |
+ if (!dirExists(dir)) { |
+ return "Could not find package '$name' at '$dir'."; |
+ } |
+ |
+ return null; |
+ } |
+} |