Index: utils/pub/io.dart |
diff --git a/utils/pub/io.dart b/utils/pub/io.dart |
index 0c6903f19483ed0f3ecb276ca251f902be2cc5dc..803b8feed24f03323191f0ec268ad7243943a357 100644 |
--- a/utils/pub/io.dart |
+++ b/utils/pub/io.dart |
@@ -62,6 +62,8 @@ String join(part1, [part2, part3, part4]) { |
return Strings.join(parts, Platform.pathSeparator); |
} |
+List<String> splitPath(path) => _sanitizePath(path).split('/'); |
Bob Nystrom
2012/11/30 21:31:59
Doc comment.
nweiz
2012/11/30 21:37:45
Done.
|
+ |
/** |
* Gets the basename, the file name without any leading directory path, for |
* [file], which can either be a [String], [File], or [Directory]. |
@@ -245,8 +247,8 @@ Future<Directory> deleteDir(dir) { |
/** |
* Asynchronously lists the contents of [dir], which can be a [String] directory |
* path or a [Directory]. If [recursive] is `true`, lists subdirectory contents |
- * (defaults to `false`). If [includeHiddenFiles] is `true`, includes files |
- * beginning with `.` (defaults to `false`). |
+ * (defaults to `false`). If [includeHiddenFiles] is `true`, includes files and |
+ * directories beginning with `.` (defaults to `false`). |
*/ |
Future<List<String>> listDir(dir, |
{bool recursive: false, bool includeHiddenFiles: false}) { |
@@ -271,7 +273,10 @@ Future<List<String>> listDir(dir, |
} |
lister.onError = (error) => completer.completeException(error, stackTrace); |
- lister.onDir = (file) => contents.add(file); |
+ lister.onDir = (file) { |
+ if (!includeHiddenFiles && basename(file).startsWith('.')) return; |
+ contents.add(file); |
+ }; |
lister.onFile = (file) { |
if (!includeHiddenFiles && basename(file).startsWith('.')) return; |
contents.add(file); |
@@ -372,14 +377,7 @@ String getFullPath(entry) { |
var path = _getPath(entry); |
// Don't do anything if it's already absolute. |
- if (Platform.operatingSystem == 'windows') { |
- // An absolute path on Windows is either UNC (two leading backslashes), |
- // or a drive letter followed by a colon and a slash. |
- var ABSOLUTE = new RegExp(r'^(\\\\|[a-zA-Z]:[/\\])'); |
- if (ABSOLUTE.hasMatch(path)) return path; |
- } else { |
- if (path.startsWith('/')) return path; |
- } |
+ if (isAbsolute(path)) return path; |
// Using Path.join here instead of File().fullPathSync() because the former |
// does not require an actual file to exist at that path. |
@@ -387,6 +385,23 @@ String getFullPath(entry) { |
.toNativePath(); |
} |
+bool isAbsolute(entry) => _splitAbsolute(entry).first != null; |
+ |
+Pair<String, String> _splitAbsolute(entry) { |
+ var path = _getPath(entry); |
+ |
+ if (Platform.operatingSystem != 'windows') { |
+ return !path.startsWith('/') ? new Pair(null, path) |
+ : new Pair('/', path.substring(1)); |
+ } |
+ |
+ // An absolute path on Windows is either UNC (two leading backslashes), |
+ // or a drive letter followed by a colon and a slash. |
+ var match = new RegExp(r'^(\\\\|[a-zA-Z]:[/\\])').firstMatch(path); |
+ return match == null ? new Pair(null, path) |
+ : new Pair(match.group(0), path.substring(match.end)); |
+} |
+ |
/// Resolves [path] relative to the location of pub.dart. |
String relativeToPub(String path) { |
var scriptPath = new File(new Options().script).fullPathSync(); |
@@ -950,10 +965,16 @@ String _getPath(entry) { |
/// backslashes to forward slashes on Windows. |
String _sanitizePath(entry) { |
entry = _getPath(entry); |
- if (Platform.operatingSystem == 'windows') { |
- entry = entry.replaceAll('\\', '/'); |
- } |
- return entry; |
+ if (Platform.operatingSystem != 'windows') return entry; |
+ |
+ var split = _splitAbsolute(entry); |
+ if (split.first == null) return split.last.replaceAll('\\', '/'); |
+ |
+ // For absolute Windows paths, we don't want the prefix (either "\\" or e.g. |
+ // "C:\") to look like a normal path component, so we ensure that it only |
+ // contains backslashes. |
+ return '${split.first.replaceAll('/', '\\')}' |
+ '${split.last.replaceAll('\\', '/')}'; |
} |
/** |