| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2012, 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 /// A comprehensive, cross-platform path manipulation library. | |
| 6 library path; | |
| 7 | |
| 8 import 'dart:io' as io; | |
| 9 | |
| 10 /// An internal builder for the current OS so we can provide a straight | |
| 11 /// functional interface and not require users to create one. | |
| 12 final _builder = new Builder(); | |
| 13 | |
| 14 /// Gets the path to the current working directory. | |
| 15 String get current => new io.Directory.current().path; | |
| 16 | |
| 17 /// Gets the path separator for the current platform. On Mac and Linux, this | |
| 18 /// is `/`. On Windows, it's `\`. | |
| 19 String get separator => _builder.separator; | |
| 20 | |
| 21 /// Converts [path] to an absolute path by resolving it relative to the current | |
| 22 /// working directory. If [path] is already an absolute path, just returns it. | |
| 23 /// | |
| 24 /// path.absolute('foo/bar.txt'); // -> /your/current/dir/foo/bar.txt | |
| 25 String absolute(String path) => join(current, path); | |
| 26 | |
| 27 /// Gets the part of [path] after the last separator. | |
| 28 /// | |
| 29 /// path.basename('path/to/foo.dart'); // -> 'foo.dart' | |
| 30 /// path.basename('path/to'); // -> 'to' | |
| 31 /// | |
| 32 /// Trailing separators are ignored. | |
| 33 /// | |
| 34 /// builder.dirname('path/to/'); // -> 'to' | |
| 35 String basename(String path) => _builder.basename(path); | |
| 36 | |
| 37 /// Gets the part of [path] after the last separator, and without any trailing | |
| 38 /// file extension. | |
| 39 /// | |
| 40 /// path.basenameWithoutExtension('path/to/foo.dart'); // -> 'foo' | |
| 41 /// | |
| 42 /// Trailing separators are ignored. | |
| 43 /// | |
| 44 /// builder.dirname('path/to/foo.dart/'); // -> 'foo' | |
| 45 String basenameWithoutExtension(String path) => | |
| 46 _builder.basenameWithoutExtension(path); | |
| 47 | |
| 48 /// Gets the part of [path] before the last separator. | |
| 49 /// | |
| 50 /// path.dirname('path/to/foo.dart'); // -> 'path/to' | |
| 51 /// path.dirname('path/to'); // -> 'to' | |
| 52 /// | |
| 53 /// Trailing separators are ignored. | |
| 54 /// | |
| 55 /// builder.dirname('path/to/'); // -> 'path' | |
| 56 String dirname(String path) => _builder.dirname(path); | |
| 57 | |
| 58 /// Gets the file extension of [path]: the portion of [basename] from the last | |
| 59 /// `.` to the end (including the `.` itself). | |
| 60 /// | |
| 61 /// path.extension('path/to/foo.dart'); // -> '.dart' | |
| 62 /// path.extension('path/to/foo'); // -> '' | |
| 63 /// path.extension('path.to/foo'); // -> '' | |
| 64 /// path.extension('path/to/foo.dart.js'); // -> '.js' | |
| 65 /// | |
| 66 /// If the file name starts with a `.`, then that is not considered the | |
| 67 /// extension: | |
| 68 /// | |
| 69 /// path.extension('~/.bashrc'); // -> '' | |
| 70 /// path.extension('~/.notes.txt'); // -> '.txt' | |
| 71 String extension(String path) => _builder.extension(path); | |
| 72 | |
| 73 // TODO(nweiz): add a UNC example for Windows once issue 7323 is fixed. | |
| 74 /// Returns the root of [path], if it's absolute, or the empty string if it's | |
| 75 /// relative. | |
| 76 /// | |
| 77 /// // Unix | |
| 78 /// path.rootPrefix('path/to/foo'); // -> '' | |
| 79 /// path.rootPrefix('/path/to/foo'); // -> '/' | |
| 80 /// | |
| 81 /// // Windows | |
| 82 /// path.rootPrefix(r'path\to\foo'); // -> '' | |
| 83 /// path.rootPrefix(r'C:\path\to\foo'); // -> r'C:\' | |
| 84 String rootPrefix(String path) => _builder.rootPrefix(path); | |
| 85 | |
| 86 /// Returns `true` if [path] is an absolute path and `false` if it is a | |
| 87 /// relative path. On POSIX systems, absolute paths start with a `/` (forward | |
| 88 /// slash). On Windows, an absolute path starts with `\\`, or a drive letter | |
| 89 /// followed by `:/` or `:\`. | |
| 90 bool isAbsolute(String path) => _builder.isAbsolute(path); | |
| 91 | |
| 92 /// Returns `true` if [path] is a relative path and `false` if it is absolute. | |
| 93 /// On POSIX systems, absolute paths start with a `/` (forward slash). On | |
| 94 /// Windows, an absolute path starts with `\\`, or a drive letter followed by | |
| 95 /// `:/` or `:\`. | |
| 96 bool isRelative(String path) => _builder.isRelative(path); | |
| 97 | |
| 98 /// Joins the given path parts into a single path using the current platform's | |
| 99 /// [separator]. Example: | |
| 100 /// | |
| 101 /// path.join('path', 'to', 'foo'); // -> 'path/to/foo' | |
| 102 /// | |
| 103 /// If any part ends in a path separator, then a redundant separator will not | |
| 104 /// be added: | |
| 105 /// | |
| 106 /// path.join('path/', 'to', 'foo'); // -> 'path/to/foo | |
| 107 /// | |
| 108 /// If a part is an absolute path, then anything before that will be ignored: | |
| 109 /// | |
| 110 /// path.join('path', '/to', 'foo'); // -> '/to/foo' | |
| 111 String join(String part1, [String part2, String part3, String part4, | |
| 112 String part5, String part6, String part7, String part8]) => | |
| 113 _builder.join(part1, part2, part3, part4, part5, part6, part7, part8); | |
| 114 | |
| 115 // TODO(nweiz): add a UNC example for Windows once issue 7323 is fixed. | |
| 116 /// Splits [path] into its components using the current platform's [separator]. | |
| 117 /// | |
| 118 /// path.split('path/to/foo'); // -> ['path', 'to', 'foo'] | |
| 119 /// | |
| 120 /// The path will *not* be normalized before splitting. | |
| 121 /// | |
| 122 /// path.split('path/../foo'); // -> ['path', '..', 'foo'] | |
| 123 /// | |
| 124 /// If [path] is absolute, the root directory will be the first element in the | |
| 125 /// array. Example: | |
| 126 /// | |
| 127 /// // Unix | |
| 128 /// path.split('/path/to/foo'); // -> ['/', 'path', 'to', 'foo'] | |
| 129 /// | |
| 130 /// // Windows | |
| 131 /// path.split(r'C:\path\to\foo'); // -> [r'C:\', 'path', 'to', 'foo'] | |
| 132 List<String> split(String path) => _builder.split(path); | |
| 133 | |
| 134 /// Normalizes [path], simplifying it by handling `..`, and `.`, and | |
| 135 /// removing redundant path separators whenever possible. | |
| 136 /// | |
| 137 /// path.normalize('path/./to/..//file.text'); // -> 'path/file.txt' | |
| 138 String normalize(String path) => _builder.normalize(path); | |
| 139 | |
| 140 /// Attempts to convert [path] to an equivalent relative path from the current | |
| 141 /// directory. | |
| 142 /// | |
| 143 /// // Given current directory is /root/path: | |
| 144 /// path.relative('/root/path/a/b.dart'); // -> 'a/b.dart' | |
| 145 /// path.relative('/root/other.dart'); // -> '../other.dart' | |
| 146 /// | |
| 147 /// If the [from] argument is passed, [path] is made relative to that instead. | |
| 148 /// | |
| 149 /// path.relative('/root/path/a/b.dart', | |
| 150 /// from: '/root/path'); // -> 'a/b.dart' | |
| 151 /// path.relative('/root/other.dart', | |
| 152 /// from: '/root/path'); // -> '../other.dart' | |
| 153 /// | |
| 154 /// Since there is no relative path from one drive letter to another on Windows, | |
| 155 /// this will return an absolute path in that case. | |
| 156 /// | |
| 157 /// path.relative(r'D:\other', from: r'C:\home'); // -> 'D:\other' | |
| 158 String relative(String path, {String from}) => | |
| 159 _builder.relative(path, from: from); | |
| 160 | |
| 161 /// Removes a trailing extension from the last part of [path]. | |
| 162 /// | |
| 163 /// withoutExtension('path/to/foo.dart'); // -> 'path/to/foo' | |
| 164 String withoutExtension(String path) => _builder.withoutExtension(path); | |
| 165 | |
| 166 /// An instantiable class for manipulating paths. Unlike the top-level | |
| 167 /// functions, this lets you explicitly select what platform the paths will use. | |
| 168 class Builder { | |
| 169 /// Creates a new path builder for the given style and root directory. | |
| 170 /// | |
| 171 /// If [style] is omitted, it uses the host operating system's path style. If | |
| 172 /// [root] is omitted, it defaults to the current working directory. If [root] | |
| 173 /// is relative, it is considered relative to the current working directory. | |
| 174 factory Builder({Style style, String root}) { | |
| 175 if (style == null) { | |
| 176 if (io.Platform.operatingSystem == 'windows') { | |
| 177 style = Style.windows; | |
| 178 } else { | |
| 179 style = Style.posix; | |
| 180 } | |
| 181 } | |
| 182 | |
| 183 if (root == null) root = current; | |
| 184 | |
| 185 return new Builder._(style, root); | |
| 186 } | |
| 187 | |
| 188 Builder._(this.style, this.root); | |
| 189 | |
| 190 /// The style of path that this builder works with. | |
| 191 final Style style; | |
| 192 | |
| 193 /// The root directory that relative paths will be relative to. | |
| 194 final String root; | |
| 195 | |
| 196 /// Gets the path separator for the builder's [style]. On Mac and Linux, | |
| 197 /// this is `/`. On Windows, it's `\`. | |
| 198 String get separator => style.separator; | |
| 199 | |
| 200 /// Gets the part of [path] after the last separator on the builder's | |
| 201 /// platform. | |
| 202 /// | |
| 203 /// builder.basename('path/to/foo.dart'); // -> 'foo.dart' | |
| 204 /// builder.basename('path/to'); // -> 'to' | |
| 205 /// | |
| 206 /// Trailing separators are ignored. | |
| 207 /// | |
| 208 /// builder.dirname('path/to/'); // -> 'to' | |
| 209 String basename(String path) => _parse(path).basename; | |
| 210 | |
| 211 /// Gets the part of [path] after the last separator on the builder's | |
| 212 /// platform, and without any trailing file extension. | |
| 213 /// | |
| 214 /// builder.basenameWithoutExtension('path/to/foo.dart'); // -> 'foo' | |
| 215 /// | |
| 216 /// Trailing separators are ignored. | |
| 217 /// | |
| 218 /// builder.dirname('path/to/foo.dart/'); // -> 'foo' | |
| 219 String basenameWithoutExtension(String path) => | |
| 220 _parse(path).basenameWithoutExtension; | |
| 221 | |
| 222 /// Gets the part of [path] before the last separator. | |
| 223 /// | |
| 224 /// builder.dirname('path/to/foo.dart'); // -> 'path/to' | |
| 225 /// builder.dirname('path/to'); // -> 'path' | |
| 226 /// | |
| 227 /// Trailing separators are ignored. | |
| 228 /// | |
| 229 /// builder.dirname('path/to/'); // -> 'path' | |
| 230 String dirname(String path) { | |
| 231 var parsed = _parse(path); | |
| 232 parsed.removeTrailingSeparators(); | |
| 233 if (parsed.parts.isEmpty) return parsed.root == null ? '.' : parsed.root; | |
| 234 if (parsed.parts.length == 1) { | |
| 235 return parsed.root == null ? '.' : parsed.root; | |
| 236 } | |
| 237 parsed.parts.removeLast(); | |
| 238 parsed.separators.removeLast(); | |
| 239 parsed.removeTrailingSeparators(); | |
| 240 return parsed.toString(); | |
| 241 } | |
| 242 | |
| 243 /// Gets the file extension of [path]: the portion of [basename] from the last | |
| 244 /// `.` to the end (including the `.` itself). | |
| 245 /// | |
| 246 /// builder.extension('path/to/foo.dart'); // -> '.dart' | |
| 247 /// builder.extension('path/to/foo'); // -> '' | |
| 248 /// builder.extension('path.to/foo'); // -> '' | |
| 249 /// builder.extension('path/to/foo.dart.js'); // -> '.js' | |
| 250 /// | |
| 251 /// If the file name starts with a `.`, then it is not considered an | |
| 252 /// extension: | |
| 253 /// | |
| 254 /// builder.extension('~/.bashrc'); // -> '' | |
| 255 /// builder.extension('~/.notes.txt'); // -> '.txt' | |
| 256 String extension(String path) => _parse(path).extension; | |
| 257 | |
| 258 // TODO(nweiz): add a UNC example for Windows once issue 7323 is fixed. | |
| 259 /// Returns the root of [path], if it's absolute, or an empty string if it's | |
| 260 /// relative. | |
| 261 /// | |
| 262 /// // Unix | |
| 263 /// builder.rootPrefix('path/to/foo'); // -> '' | |
| 264 /// builder.rootPrefix('/path/to/foo'); // -> '/' | |
| 265 /// | |
| 266 /// // Windows | |
| 267 /// builder.rootPrefix(r'path\to\foo'); // -> '' | |
| 268 /// builder.rootPrefix(r'C:\path\to\foo'); // -> r'C:\' | |
| 269 String rootPrefix(String path) { | |
| 270 var root = _parse(path).root; | |
| 271 return root == null ? '' : root; | |
| 272 } | |
| 273 | |
| 274 /// Returns `true` if [path] is an absolute path and `false` if it is a | |
| 275 /// relative path. On POSIX systems, absolute paths start with a `/` (forward | |
| 276 /// slash). On Windows, an absolute path starts with `\\`, or a drive letter | |
| 277 /// followed by `:/` or `:\`. | |
| 278 bool isAbsolute(String path) => _parse(path).isAbsolute; | |
| 279 | |
| 280 /// Returns `true` if [path] is a relative path and `false` if it is absolute. | |
| 281 /// On POSIX systems, absolute paths start with a `/` (forward slash). On | |
| 282 /// Windows, an absolute path starts with `\\`, or a drive letter followed by | |
| 283 /// `:/` or `:\`. | |
| 284 bool isRelative(String path) => !isAbsolute(path); | |
| 285 | |
| 286 /// Joins the given path parts into a single path. Example: | |
| 287 /// | |
| 288 /// builder.join('path', 'to', 'foo'); // -> 'path/to/foo' | |
| 289 /// | |
| 290 /// If any part ends in a path separator, then a redundant separator will not | |
| 291 /// be added: | |
| 292 /// | |
| 293 /// builder.join('path/', 'to', 'foo'); // -> 'path/to/foo | |
| 294 /// | |
| 295 /// If a part is an absolute path, then anything before that will be ignored: | |
| 296 /// | |
| 297 /// builder.join('path', '/to', 'foo'); // -> '/to/foo' | |
| 298 /// | |
| 299 String join(String part1, [String part2, String part3, String part4, | |
| 300 String part5, String part6, String part7, String part8]) { | |
| 301 var buffer = new StringBuffer(); | |
| 302 var needsSeparator = false; | |
| 303 | |
| 304 var parts = [part1, part2, part3, part4, part5, part6, part7, part8]; | |
| 305 for (var i = 1; i < parts.length; i++) { | |
| 306 if (parts[i] != null && parts[i - 1] == null) { | |
| 307 throw new ArgumentError("join(): part ${i - 1} was null, but part $i " | |
| 308 "was not."); | |
| 309 } | |
| 310 } | |
| 311 | |
| 312 for (var part in parts) { | |
| 313 if (part == null) continue; | |
| 314 | |
| 315 if (this.isAbsolute(part)) { | |
| 316 // An absolute path discards everything before it. | |
| 317 buffer.clear(); | |
| 318 buffer.add(part); | |
| 319 } else { | |
| 320 if (part.length > 0 && part[0].contains(style.separatorPattern)) { | |
| 321 // The part starts with a separator, so we don't need to add one. | |
| 322 } else if (needsSeparator) { | |
| 323 buffer.add(separator); | |
| 324 } | |
| 325 | |
| 326 buffer.add(part); | |
| 327 } | |
| 328 | |
| 329 // Unless this part ends with a separator, we'll need to add one before | |
| 330 // the next part. | |
| 331 needsSeparator = part.length > 0 && | |
| 332 !part[part.length - 1].contains(style.separatorPattern); | |
| 333 } | |
| 334 | |
| 335 return buffer.toString(); | |
| 336 } | |
| 337 | |
| 338 // TODO(nweiz): add a UNC example for Windows once issue 7323 is fixed. | |
| 339 /// Splits [path] into its components using the current platform's | |
| 340 /// [separator]. Example: | |
| 341 /// | |
| 342 /// builder.split('path/to/foo'); // -> ['path', 'to', 'foo'] | |
| 343 /// | |
| 344 /// The path will *not* be normalized before splitting. | |
| 345 /// | |
| 346 /// builder.split('path/../foo'); // -> ['path', '..', 'foo'] | |
| 347 /// | |
| 348 /// If [path] is absolute, the root directory will be the first element in the | |
| 349 /// array. Example: | |
| 350 /// | |
| 351 /// // Unix | |
| 352 /// builder.split('/path/to/foo'); // -> ['/', 'path', 'to', 'foo'] | |
| 353 /// | |
| 354 /// // Windows | |
| 355 /// builder.split(r'C:\path\to\foo'); // -> [r'C:\', 'path', 'to', 'foo'] | |
| 356 List<String> split(String path) { | |
| 357 var parsed = _parse(path); | |
| 358 // Filter out empty parts that exist due to multiple separators in a row. | |
| 359 parsed.parts = parsed.parts.where((part) => !part.isEmpty).toList(); | |
| 360 if (parsed.root != null) parsed.parts.insertRange(0, 1, parsed.root); | |
| 361 return parsed.parts; | |
| 362 } | |
| 363 | |
| 364 /// Normalizes [path], simplifying it by handling `..`, and `.`, and | |
| 365 /// removing redundant path separators whenever possible. | |
| 366 /// | |
| 367 /// builder.normalize('path/./to/..//file.text'); // -> 'path/file.txt' | |
| 368 String normalize(String path) { | |
| 369 if (path == '') return path; | |
| 370 | |
| 371 var parsed = _parse(path); | |
| 372 parsed.normalize(); | |
| 373 return parsed.toString(); | |
| 374 } | |
| 375 | |
| 376 /// Creates a new path by appending the given path parts to the [root]. | |
| 377 /// Equivalent to [join()] with [root] as the first argument. Example: | |
| 378 /// | |
| 379 /// var builder = new Builder(root: 'root'); | |
| 380 /// builder.resolve('path', 'to', 'foo'); // -> 'root/path/to/foo' | |
| 381 String resolve(String part1, [String part2, String part3, String part4, | |
| 382 String part5, String part6, String part7]) { | |
| 383 if (!?part2) return join(root, part1); | |
| 384 if (!?part3) return join(root, part1, part2); | |
| 385 if (!?part4) return join(root, part1, part2, part3); | |
| 386 if (!?part5) return join(root, part1, part2, part3, part4); | |
| 387 if (!?part6) return join(root, part1, part2, part3, part4, part5); | |
| 388 if (!?part7) return join(root, part1, part2, part3, part4, part5, part6); | |
| 389 return join(root, part1, part2, part3, part4, part5, part6, part7); | |
| 390 } | |
| 391 | |
| 392 /// Attempts to convert [path] to an equivalent relative path relative to | |
| 393 /// [root]. | |
| 394 /// | |
| 395 /// var builder = new Builder(root: '/root/path'); | |
| 396 /// builder.relative('/root/path/a/b.dart'); // -> 'a/b.dart' | |
| 397 /// builder.relative('/root/other.dart'); // -> '../other.dart' | |
| 398 /// | |
| 399 /// If the [from] argument is passed, [path] is made relative to that instead. | |
| 400 /// | |
| 401 /// builder.relative('/root/path/a/b.dart', | |
| 402 /// from: '/root/path'); // -> 'a/b.dart' | |
| 403 /// builder.relative('/root/other.dart', | |
| 404 /// from: '/root/path'); // -> '../other.dart' | |
| 405 /// | |
| 406 /// Since there is no relative path from one drive letter to another on | |
| 407 /// Windows, this will return an absolute path in that case. | |
| 408 /// | |
| 409 /// builder.relative(r'D:\other', from: r'C:\other'); // -> 'D:\other' | |
| 410 /// | |
| 411 /// This will also return an absolute path if an absolute [path] is passed to | |
| 412 /// a builder with a relative [root]. | |
| 413 /// | |
| 414 /// var builder = new Builder(r'some/relative/path'); | |
| 415 /// builder.relative(r'/absolute/path'); // -> '/absolute/path' | |
| 416 String relative(String path, {String from}) { | |
| 417 if (path == '') return '.'; | |
| 418 | |
| 419 from = from == null ? root : this.join(root, from); | |
| 420 | |
| 421 // We can't determine the path from a relative path to an absolute path. | |
| 422 if (this.isRelative(from) && this.isAbsolute(path)) { | |
| 423 return this.normalize(path); | |
| 424 } | |
| 425 | |
| 426 // If the given path is relative, resolve it relative to the root of the | |
| 427 // builder. | |
| 428 if (this.isRelative(path)) path = this.resolve(path); | |
| 429 | |
| 430 // If the path is still relative and `from` is absolute, we're unable to | |
| 431 // find a path from `from` to `path`. | |
| 432 if (this.isRelative(path) && this.isAbsolute(from)) { | |
| 433 throw new ArgumentError('Unable to find a path to "$path" from "$from".'); | |
| 434 } | |
| 435 | |
| 436 var fromParsed = _parse(from)..normalize(); | |
| 437 var pathParsed = _parse(path)..normalize(); | |
| 438 | |
| 439 // If the root prefixes don't match (for example, different drive letters | |
| 440 // on Windows), then there is no relative path, so just return the absolute | |
| 441 // one. In Windows, drive letters are case-insenstive and we allow | |
| 442 // calculation of relative paths, even if a path has not been normalized. | |
| 443 if (fromParsed.root != pathParsed.root && | |
| 444 ((fromParsed.root == null || pathParsed.root == null) || | |
| 445 fromParsed.root.toLowerCase().replaceAll('/', '\\') != | |
| 446 pathParsed.root.toLowerCase().replaceAll('/', '\\'))) { | |
| 447 return pathParsed.toString(); | |
| 448 } | |
| 449 | |
| 450 // Strip off their common prefix. | |
| 451 while (fromParsed.parts.length > 0 && pathParsed.parts.length > 0 && | |
| 452 fromParsed.parts[0] == pathParsed.parts[0]) { | |
| 453 fromParsed.parts.removeAt(0); | |
| 454 fromParsed.separators.removeAt(0); | |
| 455 pathParsed.parts.removeAt(0); | |
| 456 pathParsed.separators.removeAt(0); | |
| 457 } | |
| 458 | |
| 459 // If there are any directories left in the root path, we need to walk up | |
| 460 // out of them. | |
| 461 pathParsed.parts.insertRange(0, fromParsed.parts.length, '..'); | |
| 462 pathParsed.separators.insertRange(0, fromParsed.parts.length, | |
| 463 style.separator); | |
| 464 | |
| 465 // Corner case: the paths completely collapsed. | |
| 466 if (pathParsed.parts.length == 0) return '.'; | |
| 467 | |
| 468 // Make it relative. | |
| 469 pathParsed.root = ''; | |
| 470 pathParsed.removeTrailingSeparators(); | |
| 471 | |
| 472 return pathParsed.toString(); | |
| 473 } | |
| 474 | |
| 475 /// Removes a trailing extension from the last part of [path]. | |
| 476 /// | |
| 477 /// builder.withoutExtension('path/to/foo.dart'); // -> 'path/to/foo' | |
| 478 String withoutExtension(String path) { | |
| 479 var parsed = _parse(path); | |
| 480 | |
| 481 for (var i = parsed.parts.length - 1; i >= 0; i--) { | |
| 482 if (!parsed.parts[i].isEmpty) { | |
| 483 parsed.parts[i] = parsed.basenameWithoutExtension; | |
| 484 break; | |
| 485 } | |
| 486 } | |
| 487 | |
| 488 return parsed.toString(); | |
| 489 } | |
| 490 | |
| 491 _ParsedPath _parse(String path) { | |
| 492 var before = path; | |
| 493 | |
| 494 // Remove the root prefix, if any. | |
| 495 var root = style.getRoot(path); | |
| 496 if (root != null) path = path.substring(root.length); | |
| 497 | |
| 498 // Split the parts on path separators. | |
| 499 var parts = []; | |
| 500 var separators = []; | |
| 501 var start = 0; | |
| 502 for (var match in style.separatorPattern.allMatches(path)) { | |
| 503 parts.add(path.substring(start, match.start)); | |
| 504 separators.add(match[0]); | |
| 505 start = match.end; | |
| 506 } | |
| 507 | |
| 508 // Add the final part, if any. | |
| 509 if (start < path.length) { | |
| 510 parts.add(path.substring(start)); | |
| 511 separators.add(''); | |
| 512 } | |
| 513 | |
| 514 return new _ParsedPath(style, root, parts, separators); | |
| 515 } | |
| 516 } | |
| 517 | |
| 518 /// An enum type describing a "flavor" of path. | |
| 519 class Style { | |
| 520 /// POSIX-style paths use "/" (forward slash) as separators. Absolute paths | |
| 521 /// start with "/". Used by UNIX, Linux, Mac OS X, and others. | |
| 522 static final posix = new Style._('posix', '/', '/', '/'); | |
| 523 | |
| 524 /// Windows paths use "\" (backslash) as separators. Absolute paths start with | |
| 525 /// a drive letter followed by a colon (example, "C:") or two backslashes | |
| 526 /// ("\\") for UNC paths. | |
| 527 // TODO(rnystrom): The UNC root prefix should include the drive name too, not | |
| 528 // just the "\\". | |
| 529 static final windows = new Style._('windows', '\\', r'[/\\]', | |
| 530 r'\\\\|[a-zA-Z]:[/\\]'); | |
| 531 | |
| 532 Style._(this.name, this.separator, String separatorPattern, | |
| 533 String rootPattern) | |
| 534 : separatorPattern = new RegExp(separatorPattern), | |
| 535 _rootPattern = new RegExp('^$rootPattern'); | |
| 536 | |
| 537 /// The name of this path style. Will be "posix" or "windows". | |
| 538 final String name; | |
| 539 | |
| 540 /// The path separator for this style. On POSIX, this is `/`. On Windows, | |
| 541 /// it's `\`. | |
| 542 final String separator; | |
| 543 | |
| 544 /// The [Pattern] that can be used to match a separator for a path in this | |
| 545 /// style. Windows allows both "/" and "\" as path separators even though | |
| 546 /// "\" is the canonical one. | |
| 547 final Pattern separatorPattern; | |
| 548 | |
| 549 // TODO(nweiz): make this a Pattern when issue 7080 is fixed. | |
| 550 /// The [RegExp] that can be used to match the root prefix of an absolute | |
| 551 /// path in this style. | |
| 552 final RegExp _rootPattern; | |
| 553 | |
| 554 /// Gets the root prefix of [path] if path is absolute. If [path] is relative, | |
| 555 /// returns `null`. | |
| 556 String getRoot(String path) { | |
| 557 var match = _rootPattern.firstMatch(path); | |
| 558 if (match == null) return null; | |
| 559 return match[0]; | |
| 560 } | |
| 561 | |
| 562 String toString() => name; | |
| 563 } | |
| 564 | |
| 565 // TODO(rnystrom): Make this public? | |
| 566 class _ParsedPath { | |
| 567 /// The [Style] that was used to parse this path. | |
| 568 Style style; | |
| 569 | |
| 570 /// The absolute root portion of the path, or `null` if the path is relative. | |
| 571 /// On POSIX systems, this will be `null` or "/". On Windows, it can be | |
| 572 /// `null`, "//" for a UNC path, or something like "C:\" for paths with drive | |
| 573 /// letters. | |
| 574 String root; | |
| 575 | |
| 576 /// The path-separated parts of the path. All but the last will be | |
| 577 /// directories. | |
| 578 List<String> parts; | |
| 579 | |
| 580 /// The path separators following each part. The last one will be an empty | |
| 581 /// string unless the path ends with a trailing separator. | |
| 582 List<String> separators; | |
| 583 | |
| 584 /// The file extension of the last part, or "" if it doesn't have one. | |
| 585 String get extension => _splitExtension()[1]; | |
| 586 | |
| 587 /// `true` if this is an absolute path. | |
| 588 bool get isAbsolute => root != null; | |
| 589 | |
| 590 _ParsedPath(this.style, this.root, this.parts, this.separators); | |
| 591 | |
| 592 String get basename { | |
| 593 var copy = this.clone(); | |
| 594 copy.removeTrailingSeparators(); | |
| 595 if (copy.parts.isEmpty) return root == null ? '' : root; | |
| 596 return copy.parts.last; | |
| 597 } | |
| 598 | |
| 599 String get basenameWithoutExtension { | |
| 600 var copy = this.clone(); | |
| 601 copy.removeTrailingSeparators(); | |
| 602 if (copy.parts.isEmpty) return root == null ? '' : root; | |
| 603 return copy._splitExtension()[0]; | |
| 604 } | |
| 605 | |
| 606 void removeTrailingSeparators() { | |
| 607 while (!parts.isEmpty && parts.last == '') { | |
| 608 parts.removeLast(); | |
| 609 separators.removeLast(); | |
| 610 } | |
| 611 if (separators.length > 0) separators[separators.length - 1] = ''; | |
| 612 } | |
| 613 | |
| 614 void normalize() { | |
| 615 // Handle '.', '..', and empty parts. | |
| 616 var leadingDoubles = 0; | |
| 617 var newParts = []; | |
| 618 for (var part in parts) { | |
| 619 if (part == '.' || part == '') { | |
| 620 // Do nothing. Ignore it. | |
| 621 } else if (part == '..') { | |
| 622 // Pop the last part off. | |
| 623 if (newParts.length > 0) { | |
| 624 newParts.removeLast(); | |
| 625 } else { | |
| 626 // Backed out past the beginning, so preserve the "..". | |
| 627 leadingDoubles++; | |
| 628 } | |
| 629 } else { | |
| 630 newParts.add(part); | |
| 631 } | |
| 632 } | |
| 633 | |
| 634 // A relative path can back out from the start directory. | |
| 635 if (!isAbsolute) { | |
| 636 newParts.insertRange(0, leadingDoubles, '..'); | |
| 637 } | |
| 638 | |
| 639 // If we collapsed down to nothing, do ".". | |
| 640 if (newParts.length == 0 && !isAbsolute) { | |
| 641 newParts.add('.'); | |
| 642 } | |
| 643 | |
| 644 // Canonicalize separators. | |
| 645 var newSeparators = []; | |
| 646 newSeparators.insertRange(0, newParts.length, style.separator); | |
| 647 | |
| 648 parts = newParts; | |
| 649 separators = newSeparators; | |
| 650 | |
| 651 // Normalize the Windows root if needed. | |
| 652 if (root != null && style == Style.windows) { | |
| 653 root = root.replaceAll('/', '\\'); | |
| 654 } | |
| 655 removeTrailingSeparators(); | |
| 656 } | |
| 657 | |
| 658 String toString() { | |
| 659 var builder = new StringBuffer(); | |
| 660 if (root != null) builder.add(root); | |
| 661 for (var i = 0; i < parts.length; i++) { | |
| 662 builder.add(parts[i]); | |
| 663 builder.add(separators[i]); | |
| 664 } | |
| 665 | |
| 666 return builder.toString(); | |
| 667 } | |
| 668 | |
| 669 /// Splits the last part of the path into a two-element list. The first is | |
| 670 /// the name of the file without any extension. The second is the extension | |
| 671 /// or "" if it has none. | |
| 672 List<String> _splitExtension() { | |
| 673 if (parts.isEmpty) return ['', '']; | |
| 674 | |
| 675 var file = parts.last; | |
| 676 if (file == '..') return ['..', '']; | |
| 677 | |
| 678 var lastDot = file.lastIndexOf('.'); | |
| 679 | |
| 680 // If there is no dot, or it's the first character, like '.bashrc', it | |
| 681 // doesn't count. | |
| 682 if (lastDot <= 0) return [file, '']; | |
| 683 | |
| 684 return [file.substring(0, lastDot), file.substring(lastDot)]; | |
| 685 } | |
| 686 | |
| 687 _ParsedPath clone() => new _ParsedPath( | |
| 688 style, root, new List.from(parts), new List.from(separators)); | |
| 689 } | |
| OLD | NEW |