0001 // PathKit - Effortless path operations 0002 0003 #if os(Linux) 0004 import Glibc 0005 0006 let system_glob = Glibc.glob 0007 #else 0008 import Darwin 0009 0010 let system_glob= Darwin.glob 0011 #endif 0012 0013 import Foundation 0014 0015 0016 /// Represents a filesystem path. 0017 public struct Path
PathKit.swift:550 if system_glob(cPattern, flags, nil, >) == 0 {{ 0018 /// The character used by the OS to separate two path elements 0019 public static let separator
PathKit.swift:41 } else if components.first == Path.separator && components.count > 1 {PathKit.swift:42 let p = components.joinWithSeparator(Path.separator)PathKit.swift:51 path = components.joinWithSeparator(Path.separator)PathKit.swift:59 extension Path : StringLiteralConvertible {PathKit.swift:79 extension Path : CustomStringConvertible {PathKit.swift:88 extension Path : Hashable {PathKit.swift:97 extension Path {PathKit.swift:103 return path.hasPrefix(Path.separator)PathKit.swift:118 public func absolute() -> Path {PathKit.swift:123 return (Path.current + self).normalize()PathKit.swift:132 public func normalize() -> Path {PathKit.swift:133 return Path(NSString(string: self.path).stringByStandardizingPath)PathKit.swift:141 public func abbreviate() -> Path {PathKit.swift:146 return Path(NSString(string: self.path).stringByAbbreviatingWithTildeInPath)PathKit.swift:154 public func symlinkDestination() throws -> Path {PathKit.swift:155 let symlinkDestination = try Path.fileManager.destinationOfSymbolicLinkAtPath(path)PathKit.swift:156 let symlinkPath = Path(symlinkDestination)PathKit.swift:168 extension Path {PathKit.swift:213 extension Path {PathKit.swift:220 return Path.fileManager.fileExistsAtPath(self.path)PathKit.swift:231 guard Path.fileManager.fileExistsAtPath(normalize().path, isDirectory: &directory) else {PathKit.swift:246 guard Path.fileManager.fileExistsAtPath(normalize().path, isDirectory: &directory) else {PathKit.swift:259 let _ = try Path.fileManager.destinationOfSymbolicLinkAtPath(path)PathKit.swift:273 return Path.fileManager.isReadableFileAtPath(self.path)PathKit.swift:283 return Path.fileManager.isWritableFileAtPath(self.path)PathKit.swift:293 return Path.fileManager.isExecutableFileAtPath(self.path)PathKit.swift:303 return Path.fileManager.isDeletableFileAtPath(self.path)PathKit.swift:310 extension Path {PathKit.swift:318 try Path.fileManager.createDirectoryAtPath(self.path, withIntermediateDirectories: false, attributes: nil)PathKit.swift:327 try Path.fileManager.createDirectoryAtPath(self.path, withIntermediateDirectories: true, attributes: nil)PathKit.swift:336 try Path.fileManager.removeItemAtPath(self.path)PathKit.swift:344 public func move(destination: Path) throws -> () {PathKit.swift:345 try Path.fileManager.moveItemAtPath(self.path, toPath: destination.path)PathKit.swift:353 public func copy(destination: Path) throws -> () {PathKit.swift:354 try Path.fileManager.copyItemAtPath(self.path, toPath: destination.path)PathKit.swift:361 public func link(destination: Path) throws -> () {PathKit.swift:362 try Path.fileManager.linkItemAtPath(self.path, toPath: destination.path)PathKit.swift:369 public func symlink(destination: Path) throws -> () {PathKit.swift:370 try Path.fileManager.createSymbolicLinkAtPath(self.path, withDestinationPath: destination.path)PathKit.swift:377 extension Path {PathKit.swift:384 return self.init(Path.fileManager.currentDirectoryPath)PathKit.swift:387 Path.fileManager.changeCurrentDirectoryPath(newValue.description)PathKit.swift:382 public static var current: Path {PathKit.swift:399 let previous = Path.currentPathKit.swift:400 Path.current = selfPathKit.swift:401 defer { Path.current = previous }PathKit.swift:409 extension Path {PathKit.swift:417 return Path(NSHomeDirectory())PathKit.swift:413 public static var home: Path {PathKit.swift:427 return Path(NSTemporaryDirectory())PathKit.swift:423 public static var temporary: Path {PathKit.swift:434 public static func processUniqueTemporary() throws -> Path {PathKit.swift:445 public static func uniqueTemporary() throws -> Path {PathKit.swift:455 extension Path {PathKit.swift:506 extension Path {PathKit.swift:511 public func parent() -> Path {PathKit.swift:519 public func children() throws -> [Path] {PathKit.swift:520 return try Path.fileManager.contentsOfDirectoryAtPath(path).map {PathKit.swift:521 self + Path($0)PathKit.swift:530 public func recursiveChildren() throws -> [Path] {PathKit.swift:531 return try Path.fileManager.subpathsOfDirectoryAtPath(path).map {PathKit.swift:532 self + Path($0)PathKit.swift:540 extension Path {PathKit.swift:541 public static func glob(pattern: String) -> [Path] {PathKit.swift:558 return Path(path)PathKit.swift:569 public func glob(pattern: String) -> [Path] {PathKit.swift:570 return Path.glob((self + pattern).description)PathKit.swift:577 extension Path : SequenceType {PathKit.swift:581 public typealias Element = PathPathKit.swift:583 let path: PathPathKit.swift:586 init(path: Path) {PathKit.swift:588 self.directoryEnumerator = Path.fileManager.enumeratorAtPath(path.path)!PathKit.swift:591 public func next() -> Path? {PathKit.swift:617 extension Path : Equatable {}PathKit.swift:624 public func ==(lhs: Path, rhs: Path) -> Bool {PathKit.swift:624 public func ==(lhs: Path, rhs: Path) -> Bool {PathKit.swift:637 public func ~=(lhs: Path, rhs: Path) -> Bool {PathKit.swift:637 public func ~=(lhs: Path, rhs: Path) -> Bool {PathKit.swift:645 extension Path : Comparable {}PathKit.swift:648 public func <(lhs: Path, rhs: Path) -> Bool {PathKit.swift:648 public func <(lhs: Path, rhs: Path) -> Bool {PathKit.swift:656 public func +(lhs: Path, rhs: Path) -> Path {PathKit.swift:656 public func +(lhs: Path, rhs: Path) -> Path {PathKit.swift:656 public func +(lhs: Path, rhs: Path) -> Path {PathKit.swift:661 public func +(lhs: Path, rhs: String) -> Path {PathKit.swift:661 public func +(lhs: Path, rhs: String) -> Path {PathKit.swift:666 internal func +(lhs: String, rhs: String) -> Path {PathKit.swift:667 if rhs.hasPrefix(Path.separator) {PathKit.swift:669 return Path(rhs)PathKit.swift:675 if lSlice.count > 1 && lSlice.last == Path.separator {PathKit.swift:685 if (lSlice.count > 1 || lSlice.first != Path.separator) && !lSlice.isEmpty {PathKit.swift:703 return Path(components: lSlice + rSlice)= "/" 0020 0021 /// The underlying string representation 0022 internal var path
PathKit.swift:41 } else if components.first == Path.separator && components.count > 1 {PathKit.swift:42 let p = components.joinWithSeparator(Path.separator)PathKit.swift:51 path = components.joinWithSeparator(Path.separator)PathKit.swift:103 return path.hasPrefix(Path.separator)PathKit.swift:667 if rhs.hasPrefix(Path.separator) {PathKit.swift:675 if lSlice.count > 1 && lSlice.last == Path.separator {PathKit.swift:685 if (lSlice.count > 1 || lSlice.first != Path.separator) && !lSlice.isEmpty {: String 0023 0024 internal static var fileManager
PathKit.swift:29 self.path = ""PathKit.swift:34 self.path = pathPathKit.swift:40 path = "."PathKit.swift:47 path = p.substringFromIndex(p.startIndex.successor())PathKit.swift:51 path = components.joinWithSeparator(Path.separator)PathKit.swift:72 self.path = valuePathKit.swift:81 return self.pathPathKit.swift:90 return path.hashValuePathKit.swift:103 return path.hasPrefix(Path.separator)PathKit.swift:133 return Path(NSString(string: self.path).stringByStandardizingPath)PathKit.swift:146 return Path(NSString(string: self.path).stringByAbbreviatingWithTildeInPath)PathKit.swift:155 let symlinkDestination = try Path.fileManager.destinationOfSymbolicLinkAtPath(path)PathKit.swift:174 return NSString(string: path).lastPathComponentPathKit.swift:193 return NSString(string: path).pathComponentsPathKit.swift:201 let pathExtension = NSString(string: path).pathExtensionPathKit.swift:220 return Path.fileManager.fileExistsAtPath(self.path)PathKit.swift:231 guard Path.fileManager.fileExistsAtPath(normalize().path, isDirectory: &directory) else {PathKit.swift:246 guard Path.fileManager.fileExistsAtPath(normalize().path, isDirectory: &directory) else {PathKit.swift:259 let _ = try Path.fileManager.destinationOfSymbolicLinkAtPath(path)PathKit.swift:273 return Path.fileManager.isReadableFileAtPath(self.path)PathKit.swift:283 return Path.fileManager.isWritableFileAtPath(self.path)PathKit.swift:293 return Path.fileManager.isExecutableFileAtPath(self.path)PathKit.swift:303 return Path.fileManager.isDeletableFileAtPath(self.path)PathKit.swift:318 try Path.fileManager.createDirectoryAtPath(self.path, withIntermediateDirectories: false, attributes: nil)PathKit.swift:327 try Path.fileManager.createDirectoryAtPath(self.path, withIntermediateDirectories: true, attributes: nil)PathKit.swift:336 try Path.fileManager.removeItemAtPath(self.path)PathKit.swift:345 try Path.fileManager.moveItemAtPath(self.path, toPath: destination.path)PathKit.swift:345 try Path.fileManager.moveItemAtPath(self.path, toPath: destination.path)PathKit.swift:354 try Path.fileManager.copyItemAtPath(self.path, toPath: destination.path)PathKit.swift:354 try Path.fileManager.copyItemAtPath(self.path, toPath: destination.path)PathKit.swift:362 try Path.fileManager.linkItemAtPath(self.path, toPath: destination.path)PathKit.swift:362 try Path.fileManager.linkItemAtPath(self.path, toPath: destination.path)PathKit.swift:370 try Path.fileManager.createSymbolicLinkAtPath(self.path, withDestinationPath: destination.path)PathKit.swift:370 try Path.fileManager.createSymbolicLinkAtPath(self.path, withDestinationPath: destination.path)PathKit.swift:461 return try NSData(contentsOfFile: path, options: NSDataReadingOptions(rawValue: 0))PathKit.swift:472 return try NSString(contentsOfFile: path, encoding: encoding).substringFromIndex(0) as StringPathKit.swift:483 try data.writeToFile(normalize().path, options: .DataWritingAtomic)PathKit.swift:499 try NSString(string: string).writeToFile(normalize().path, atomically: true, encoding: encoding)PathKit.swift:520 return try Path.fileManager.contentsOfDirectoryAtPath(path).map {PathKit.swift:531 return try Path.fileManager.subpathsOfDirectoryAtPath(path).map {PathKit.swift:588 self.directoryEnumerator = Path.fileManager.enumeratorAtPath(path.path)!PathKit.swift:625 return lhs.path == rhs.pathPathKit.swift:625 return lhs.path == rhs.pathPathKit.swift:649 return lhs.path < rhs.pathPathKit.swift:649 return lhs.path < rhs.pathPathKit.swift:657 return lhs.path + rhs.pathPathKit.swift:657 return lhs.path + rhs.pathPathKit.swift:662 return lhs.path + rhs= NSFileManager.defaultManager() 0025 0026 // MARK: Init 0027 0028 public init() { 0029 self.path = "" 0030 } 0031 0032 /// Create a Path from a given String 0033 public init
PathKit.swift:155 let symlinkDestination = try Path.fileManager.destinationOfSymbolicLinkAtPath(path)PathKit.swift:220 return Path.fileManager.fileExistsAtPath(self.path)PathKit.swift:231 guard Path.fileManager.fileExistsAtPath(normalize().path, isDirectory: &directory) else {PathKit.swift:246 guard Path.fileManager.fileExistsAtPath(normalize().path, isDirectory: &directory) else {PathKit.swift:259 let _ = try Path.fileManager.destinationOfSymbolicLinkAtPath(path)PathKit.swift:273 return Path.fileManager.isReadableFileAtPath(self.path)PathKit.swift:283 return Path.fileManager.isWritableFileAtPath(self.path)PathKit.swift:293 return Path.fileManager.isExecutableFileAtPath(self.path)PathKit.swift:303 return Path.fileManager.isDeletableFileAtPath(self.path)PathKit.swift:318 try Path.fileManager.createDirectoryAtPath(self.path, withIntermediateDirectories: false, attributes: nil)PathKit.swift:327 try Path.fileManager.createDirectoryAtPath(self.path, withIntermediateDirectories: true, attributes: nil)PathKit.swift:336 try Path.fileManager.removeItemAtPath(self.path)PathKit.swift:345 try Path.fileManager.moveItemAtPath(self.path, toPath: destination.path)PathKit.swift:354 try Path.fileManager.copyItemAtPath(self.path, toPath: destination.path)PathKit.swift:362 try Path.fileManager.linkItemAtPath(self.path, toPath: destination.path)PathKit.swift:370 try Path.fileManager.createSymbolicLinkAtPath(self.path, withDestinationPath: destination.path)PathKit.swift:384 return self.init(Path.fileManager.currentDirectoryPath)PathKit.swift:387 Path.fileManager.changeCurrentDirectoryPath(newValue.description)PathKit.swift:520 return try Path.fileManager.contentsOfDirectoryAtPath(path).map {PathKit.swift:531 return try Path.fileManager.subpathsOfDirectoryAtPath(path).map {PathKit.swift:588 self.directoryEnumerator = Path.fileManager.enumeratorAtPath(path.path)!(_ path: String) { 0034 self.path = path 0035 } 0036 0037 /// Create a Path by joining multiple path components together 0038 public init
PathKit.swift:133 return Path(NSString(string: self.path).stringByStandardizingPath)PathKit.swift:146 return Path(NSString(string: self.path).stringByAbbreviatingWithTildeInPath)PathKit.swift:156 let symlinkPath = Path(symlinkDestination)PathKit.swift:384 return self.init(Path.fileManager.currentDirectoryPath)PathKit.swift:417 return Path(NSHomeDirectory())PathKit.swift:427 return Path(NSTemporaryDirectory())PathKit.swift:521 self + Path($0)PathKit.swift:532 self + Path($0)PathKit.swift:558 return Path(path)PathKit.swift:669 return Path(rhs)<S : CollectionType where S.Generator.Element == String>(components: S) { 0039 if components.isEmpty { 0040 path = "." 0041 } else if components.first == Path.separator && components.count > 1 { 0042 let p = components.joinWithSeparator(Path.separator) 0043 #if os(Linux) 0044 let index = p.startIndex.distanceTo(p.startIndex.successor()) 0045 path = NSString(string: p).substringFromIndex(index) 0046 #else 0047 path = p.substringFromIndex(p.startIndex.successor()) 0048 #endif 0049 0050 } else { 0051 path = components.joinWithSeparator(Path.separator) 0052 } 0053 } 0054 } 0055 0056 0057 // MARK: StringLiteralConvertible 0058 0059 extension Path : StringLiteralConvertible { 0060 public typealias ExtendedGraphemeClusterLiteralType = StringLiteralType 0061 public typealias UnicodeScalarLiteralType = StringLiteralType 0062 0063 public init(extendedGraphemeClusterLiteral path: StringLiteralType) { 0064 self.init(stringLiteral: path) 0065 } 0066 0067 public init(unicodeScalarLiteral path: StringLiteralType) { 0068 self.init(stringLiteral: path) 0069 } 0070 0071 public init
PathKit.swift:703 return Path(components: lSlice + rSlice)(stringLiteral value: StringLiteralType) { 0072 self.path = value 0073 } 0074 } 0075 0076 0077 // MARK: CustomStringConvertible 0078 0079 extension Path : CustomStringConvertible { 0080 public var description
PathKit.swift:64 self.init(stringLiteral: path)PathKit.swift:68 self.init(stringLiteral: path): String { 0081 return self.path 0082 } 0083 } 0084 0085 0086 // MARK: Hashable 0087 0088 extension Path : Hashable { 0089 public var hashValue: Int { 0090 return path.hashValue 0091 } 0092 } 0093 0094 0095 // MARK: Path Info 0096 0097 extension Path { 0098 /// Test whether a path is absolute. 0099 /// 0100 /// - Returns: `true` iff the path begings with a slash 0101 /// 0102 public var isAbsolute
PathKit.swift:387 Path.fileManager.changeCurrentDirectoryPath(newValue.description)PathKit.swift:570 return Path.glob((self + pattern).description): Bool { 0103 return path.hasPrefix(Path.separator) 0104 } 0105 0106 /// Test whether a path is relative. 0107 /// 0108 /// - Returns: `true` iff a path is relative (not absolute) 0109 /// 0110 public var isRelative
PathKit.swift:111 return !isAbsolutePathKit.swift:119 if isAbsolute {: Bool { 0111 return !isAbsolute 0112 } 0113 0114 /// Concatenates relative paths to the current directory and derives the normalized path 0115 /// 0116 /// - Returns: the absolute path in the actual filesystem 0117 /// 0118 public func absolute() -> Path { 0119 if isAbsolute { 0120 return normalize() 0121 } 0122 0123 return (Path.current + self).normalize() 0124 } 0125 0126 /// Normalizes the path, this cleans up redundant ".." and ".", double slashes 0127 /// and resolves "~". 0128 /// 0129 /// - Returns: a new path made by removing extraneous path components from the underlying String 0130 /// representation. 0131 /// 0132 public func normalize
PathKit.swift:157 if symlinkPath.isRelative {() -> Path { 0133 return Path(NSString(string: self.path).stringByStandardizingPath) 0134 } 0135 0136 /// De-normalizes the path, by replacing the current user home directory with "~". 0137 /// 0138 /// - Returns: a new path made by removing extraneous path components from the underlying String 0139 /// representation. 0140 /// 0141 public func abbreviate() -> Path { 0142 #if os(Linux) 0143 // TODO: actually de-normalize the path 0144 return self 0145 #else 0146 return Path(NSString(string: self.path).stringByAbbreviatingWithTildeInPath) 0147 #endif 0148 } 0149 0150 /// Returns the path of the item pointed to by a symbolic link. 0151 /// 0152 /// - Returns: the path of directory or file to which the symbolic link refers 0153 /// 0154 public func symlinkDestination() throws -> Path { 0155 let symlinkDestination = try Path.fileManager.destinationOfSymbolicLinkAtPath(path) 0156 let symlinkPath = Path(symlinkDestination) 0157 if symlinkPath.isRelative { 0158 return self + ".." + symlinkPath 0159 } else { 0160 return symlinkPath 0161 } 0162 } 0163 } 0164 0165 0166 // MARK: Path Components 0167 0168 extension Path { 0169 /// The last path component 0170 /// 0171 /// - Returns: the last path component 0172 /// 0173 public var lastComponent
PathKit.swift:120 return normalize()PathKit.swift:123 return (Path.current + self).normalize()PathKit.swift:231 guard Path.fileManager.fileExistsAtPath(normalize().path, isDirectory: &directory) else {PathKit.swift:246 guard Path.fileManager.fileExistsAtPath(normalize().path, isDirectory: &directory) else {PathKit.swift:483 try data.writeToFile(normalize().path, options: .DataWritingAtomic)PathKit.swift:499 try NSString(string: string).writeToFile(normalize().path, atomically: true, encoding: encoding)PathKit.swift:639 || lhs.normalize() == rhs.normalize()PathKit.swift:639 || lhs.normalize() == rhs.normalize(): String { 0174 return NSString(string: path).lastPathComponent 0175 } 0176 0177 /// The last path component without file extension 0178 /// 0179 /// - Note: This returns "." for "..". 0180 /// 0181 /// - Returns: the last path component without file extension 0182 /// 0183 public var lastComponentWithoutExtension: String { 0184 return NSString(string: lastComponent).stringByDeletingPathExtension 0185 } 0186 0187 /// Splits the string representation on the directory separator. 0188 /// Absolute paths remain the leading slash as first component. 0189 /// 0190 /// - Returns: all path components 0191 /// 0192 public var components: [String] { 0193 return NSString(string: path).pathComponents 0194 } 0195 0196 /// The file extension behind the last dot of the last component. 0197 /// 0198 /// - Returns: the file extension 0199 /// 0200 public var `extension`: String? { 0201 let pathExtension = NSString(string: path).pathExtension 0202 if pathExtension.isEmpty { 0203 return nil 0204 } 0205 0206 return pathExtension 0207 } 0208 } 0209 0210 0211 // MARK: File Info 0212 0213 extension Path { 0214 /// Test whether a file or directory exists at a specified path 0215 /// 0216 /// - Returns: `false` iff the path doesn't exist on disk or its existence could not be 0217 /// determined 0218 /// 0219 public var exists
PathKit.swift:184 return NSString(string: lastComponent).stringByDeletingPathExtension: Bool { 0220 return Path.fileManager.fileExistsAtPath(self.path) 0221 } 0222 0223 /// Test whether a path is a directory. 0224 /// 0225 /// - Returns: `true` if the path is a directory or a symbolic link that points to a directory; 0226 /// `false` if the path is not a directory or the path doesn't exist on disk or its existence 0227 /// could not be determined 0228 /// 0229 public var isDirectory: Bool { 0230 var directory = ObjCBool(false) 0231 guard Path.fileManager.fileExistsAtPath(normalize().path, isDirectory: &directory) else { 0232 return false 0233 } 0234 return directory.boolValue 0235 } 0236 0237 /// Test whether a path is a regular file. 0238 /// 0239 /// - Returns: `true` if the path is neither a directory nor a symbolic link that points to a 0240 /// directory; `false` if the path is a directory or a symbolic link that points to a 0241 /// directory or the path doesn't exist on disk or its existence 0242 /// could not be determined 0243 /// 0244 public var isFile: Bool { 0245 var directory = ObjCBool(false) 0246 guard Path.fileManager.fileExistsAtPath(normalize().path, isDirectory: &directory) else { 0247 return false 0248 } 0249 return !directory.boolValue 0250 } 0251 0252 /// Test whether a path is a symbolic link. 0253 /// 0254 /// - Returns: `true` if the path is a symbolic link; `false` if the path doesn't exist on disk 0255 /// or its existence could not be determined 0256 /// 0257 public var isSymlink: Bool { 0258 do { 0259 let _ = try Path.fileManager.destinationOfSymbolicLinkAtPath(path) 0260 return true 0261 } catch { 0262 return false 0263 } 0264 } 0265 0266 /// Test whether a path is readable 0267 /// 0268 /// - Returns: `true` if the current process has read privileges for the file at path; 0269 /// otherwise `false` if the process does not have read privileges or the existence of the 0270 /// file could not be determined. 0271 /// 0272 public var isReadable: Bool { 0273 return Path.fileManager.isReadableFileAtPath(self.path) 0274 } 0275 0276 /// Test whether a path is writeable 0277 /// 0278 /// - Returns: `true` if the current process has write privileges for the file at path; 0279 /// otherwise `false` if the process does not have write privileges or the existence of the 0280 /// file could not be determined. 0281 /// 0282 public var isWritable: Bool { 0283 return Path.fileManager.isWritableFileAtPath(self.path) 0284 } 0285 0286 /// Test whether a path is executable 0287 /// 0288 /// - Returns: `true` if the current process has execute privileges for the file at path; 0289 /// otherwise `false` if the process does not have execute privileges or the existence of the 0290 /// file could not be determined. 0291 /// 0292 public var isExecutable: Bool { 0293 return Path.fileManager.isExecutableFileAtPath(self.path) 0294 } 0295 0296 /// Test whether a path is deletable 0297 /// 0298 /// - Returns: `true` if the current process has delete privileges for the file at path; 0299 /// otherwise `false` if the process does not have delete privileges or the existence of the 0300 /// file could not be determined. 0301 /// 0302 public var isDeletable: Bool { 0303 return Path.fileManager.isDeletableFileAtPath(self.path) 0304 } 0305 } 0306 0307 0308 // MARK: File Manipulation 0309 0310 extension Path { 0311 /// Create the directory. 0312 /// 0313 /// - Note: This method fails if any of the intermediate parent directories does not exist. 0314 /// This method also fails if any of the intermediate path elements corresponds to a file and 0315 /// not a directory. 0316 /// 0317 public func mkdir
PathKit.swift:436 if !path.exists {() throws -> () { 0318 try Path.fileManager.createDirectoryAtPath(self.path, withIntermediateDirectories: false, attributes: nil) 0319 } 0320 0321 /// Create the directory and any intermediate parent directories that do not exist. 0322 /// 0323 /// - Note: This method fails if any of the intermediate path elements corresponds to a file and 0324 /// not a directory. 0325 /// 0326 public func mkpath() throws -> () { 0327 try Path.fileManager.createDirectoryAtPath(self.path, withIntermediateDirectories: true, attributes: nil) 0328 } 0329 0330 /// Delete the file or directory. 0331 /// 0332 /// - Note: If the path specifies a directory, the contents of that directory are recursively 0333 /// removed. 0334 /// 0335 public func delete() throws -> () { 0336 try Path.fileManager.removeItemAtPath(self.path) 0337 } 0338 0339 /// Move the file or directory to a new location synchronously. 0340 /// 0341 /// - Parameter destination: The new path. This path must include the name of the file or 0342 /// directory in its new location. 0343 /// 0344 public func move(destination: Path) throws -> () { 0345 try Path.fileManager.moveItemAtPath(self.path, toPath: destination.path) 0346 } 0347 0348 /// Copy the file or directory to a new location synchronously. 0349 /// 0350 /// - Parameter destination: The new path. This path must include the name of the file or 0351 /// directory in its new location. 0352 /// 0353 public func copy(destination: Path) throws -> () { 0354 try Path.fileManager.copyItemAtPath(self.path, toPath: destination.path) 0355 } 0356 0357 /// Creates a hard link at a new destination. 0358 /// 0359 /// - Parameter destination: The location where the link will be created. 0360 /// 0361 public func link(destination: Path) throws -> () { 0362 try Path.fileManager.linkItemAtPath(self.path, toPath: destination.path) 0363 } 0364 0365 /// Creates a symbolic link at a new destination. 0366 /// 0367 /// - Parameter destintation: The location where the link will be created. 0368 /// 0369 public func symlink(destination: Path) throws -> () { 0370 try Path.fileManager.createSymbolicLinkAtPath(self.path, withDestinationPath: destination.path) 0371 } 0372 } 0373 0374 0375 // MARK: Current Directory 0376 0377 extension Path { 0378 /// The current working directory of the process 0379 /// 0380 /// - Returns: the current working directory of the process 0381 /// 0382 public static var current
PathKit.swift:437 try path.mkdir()PathKit.swift:447 try path.mkdir(): Path { 0383 get { 0384 return self.init(Path.fileManager.currentDirectoryPath) 0385 } 0386 set { 0387 Path.fileManager.changeCurrentDirectoryPath(newValue.description) 0388 } 0389 } 0390 0391 /// Changes the current working directory of the process to the path during the execution of the 0392 /// given block. 0393 /// 0394 /// - Note: The original working directory is restored when the block returns or throws. 0395 /// - Parameter closure: A closure to be executed while the current directory is configured to 0396 /// the path. 0397 /// 0398 public func chdir(@noescape closure: () throws -> ()) rethrows { 0399 let previous = Path.current 0400 Path.current = self 0401 defer { Path.current = previous } 0402 try closure() 0403 } 0404 } 0405 0406 0407 // MARK: Temporary 0408 0409 extension Path { 0410 /// - Returns: the path to either the user’s or application’s home directory, 0411 /// depending on the platform. 0412 /// 0413 public static var home: Path { 0414 #if os(Linux) 0415 return Path(NSProcessInfo.processInfo().environment["HOME"] ?? "/") 0416 #else 0417 return Path(NSHomeDirectory()) 0418 #endif 0419 } 0420 0421 /// - Returns: the path of the temporary directory for the current user. 0422 /// 0423 public static var temporary
PathKit.swift:123 return (Path.current + self).normalize()PathKit.swift:399 let previous = Path.currentPathKit.swift:400 Path.current = selfPathKit.swift:401 defer { Path.current = previous }: Path { 0424 #if os(Linux) 0425 return Path(NSProcessInfo.processInfo().environment["TMP"] ?? "/tmp") 0426 #else 0427 return Path(NSTemporaryDirectory()) 0428 #endif 0429 } 0430 0431 /// - Returns: the path of a temporary directory unique for the process. 0432 /// - Note: Based on `NSProcessInfo.globallyUniqueString`. 0433 /// 0434 public static func processUniqueTemporary
PathKit.swift:435 let path = temporary + NSProcessInfo.processInfo().globallyUniqueString() throws -> Path { 0435 let path = temporary + NSProcessInfo.processInfo().globallyUniqueString 0436 if !path.exists { 0437 try path.mkdir() 0438 } 0439 return path 0440 } 0441 0442 /// - Returns: the path of a temporary directory unique for each call. 0443 /// - Note: Based on `NSUUID`. 0444 /// 0445 public static func uniqueTemporary() throws -> Path { 0446 let path = try processUniqueTemporary() + NSUUID().UUIDString 0447 try path.mkdir() 0448 return path 0449 } 0450 } 0451 0452 0453 // MARK: Contents 0454 0455 extension Path { 0456 /// Reads the file. 0457 /// 0458 /// - Returns: the contents of the file at the specified path. 0459 /// 0460 public func read() throws -> NSData { 0461 return try NSData(contentsOfFile: path, options: NSDataReadingOptions(rawValue: 0)) 0462 } 0463 0464 /// Reads the file contents and encoded its bytes to string applying the given encoding. 0465 /// 0466 /// - Parameter encoding: the encoding which should be used to decode the data. 0467 /// (by default: `NSUTF8StringEncoding`) 0468 /// 0469 /// - Returns: the contents of the file at the specified path as string. 0470 /// 0471 public func read(encoding: NSStringEncoding = NSUTF8StringEncoding) throws -> String { 0472 return try NSString(contentsOfFile: path, encoding: encoding).substringFromIndex(0) as String 0473 } 0474 0475 /// Write a file. 0476 /// 0477 /// - Note: Works atomically: the data is written to a backup file, and then — assuming no 0478 /// errors occur — the backup file is renamed to the name specified by path. 0479 /// 0480 /// - Parameter data: the contents to write to file. 0481 /// 0482 public func write(data: NSData) throws { 0483 try data.writeToFile(normalize().path, options: .DataWritingAtomic) 0484 } 0485 0486 /// Reads the file. 0487 /// 0488 /// - Note: Works atomically: the data is written to a backup file, and then — assuming no 0489 /// errors occur — the backup file is renamed to the name specified by path. 0490 /// 0491 /// - Parameter string: the string to write to file. 0492 /// 0493 /// - Parameter encoding: the encoding which should be used to represent the string as bytes. 0494 /// (by default: `NSUTF8StringEncoding`) 0495 /// 0496 /// - Returns: the contents of the file at the specified path as string. 0497 /// 0498 public func write(string: String, encoding: NSStringEncoding = NSUTF8StringEncoding) throws { 0499 try NSString(string: string).writeToFile(normalize().path, atomically: true, encoding: encoding) 0500 } 0501 } 0502 0503 0504 // MARK: Traversing 0505 0506 extension Path { 0507 /// Get the parent directory 0508 /// 0509 /// - Returns: the normalized path of the parent directory 0510 /// 0511 public func parent() -> Path { 0512 return self + ".." 0513 } 0514 0515 /// Performs a shallow enumeration in a directory 0516 /// 0517 /// - Returns: paths to all files, directories and symbolic links contained in the directory 0518 /// 0519 public func children() throws -> [Path] { 0520 return try Path.fileManager.contentsOfDirectoryAtPath(path).map { 0521 self + Path($0) 0522 } 0523 } 0524 0525 /// Performs a deep enumeration in a directory 0526 /// 0527 /// - Returns: paths to all files, directories and symbolic links contained in the directory or 0528 /// any subdirectory. 0529 /// 0530 public func recursiveChildren() throws -> [Path] { 0531 return try Path.fileManager.subpathsOfDirectoryAtPath(path).map { 0532 self + Path($0) 0533 } 0534 } 0535 } 0536 0537 0538 // MARK: Globbing 0539 0540 extension Path { 0541 public static func glob
PathKit.swift:446 let path = try processUniqueTemporary() + NSUUID().UUIDString(pattern: String) -> [Path] { 0542 var gt = glob_t() 0543 let cPattern = strdup(pattern) 0544 defer { 0545 globfree(>) 0546 free(cPattern) 0547 } 0548 0549 let flags = GLOB_TILDE | GLOB_BRACE | GLOB_MARK 0550 if system_glob(cPattern, flags, nil, >) == 0 { 0551 #if os(Linux) 0552 let matchc = gt.gl_pathc 0553 #else 0554 let matchc = gt.gl_matchc 0555 #endif 0556 return (0..<Int(matchc)).flatMap { index in 0557 if let path = String.fromCString(gt.gl_pathv[index]) { 0558 return Path(path) 0559 } 0560 0561 return nil 0562 } 0563 } 0564 0565 // GLOB_NOMATCH 0566 return [] 0567 } 0568 0569 public func glob(pattern: String) -> [Path] { 0570 return Path.glob((self + pattern).description) 0571 } 0572 } 0573 0574 0575 // MARK: SequenceType 0576 0577 extension Path : SequenceType { 0578 /// Enumerates the contents of a directory, returning the paths of all files and directories 0579 /// contained within that directory. These paths are relative to the directory. 0580 public struct DirectoryEnumerator
PathKit.swift:570 return Path.glob((self + pattern).description): GeneratorType { 0581 public typealias Element = Path 0582 0583 let path
PathKit.swift:609 public func generate() -> DirectoryEnumerator {PathKit.swift:610 return DirectoryEnumerator(path: self): Path 0584 let directoryEnumerator
PathKit.swift:587 self.path = pathPathKit.swift:593 return path + next: NSDirectoryEnumerator 0585 0586 init
PathKit.swift:588 self.directoryEnumerator = Path.fileManager.enumeratorAtPath(path.path)!PathKit.swift:592 if let next = directoryEnumerator.nextObject() as! String? {PathKit.swift:600 directoryEnumerator.skipDescendants()(path: Path) { 0587 self.path = path 0588 self.directoryEnumerator = Path.fileManager.enumeratorAtPath(path.path)! 0589 } 0590 0591 public func next() -> Path? { 0592 if let next = directoryEnumerator.nextObject() as! String? { 0593 return path + next 0594 } 0595 return nil 0596 } 0597 0598 /// Skip recursion into the most recently obtained subdirectory. 0599 public func skipDescendants() { 0600 directoryEnumerator.skipDescendants() 0601 } 0602 } 0603 0604 /// Perform a deep enumeration of a directory. 0605 /// 0606 /// - Returns: a directory enumerator that can be used to perform a deep enumeration of the 0607 /// directory. 0608 /// 0609 public func generate() -> DirectoryEnumerator { 0610 return DirectoryEnumerator(path: self) 0611 } 0612 } 0613 0614 0615 // MARK: Equatable 0616 0617 extension Path : Equatable {} 0618 0619 /// Determines if two paths are identical 0620 /// 0621 /// - Note: The comparison is string-based. Be aware that two different paths (foo.txt and 0622 /// ./foo.txt) can refer to the same file. 0623 /// 0624 public func ==(lhs: Path, rhs: Path) -> Bool { 0625 return lhs.path == rhs.path 0626 } 0627 0628 0629 // MARK: Pattern Matching 0630 0631 /// Implements pattern-matching for paths. 0632 /// 0633 /// - Returns: `true` iff one of the following conditions is true: 0634 /// - the paths are equal (based on `Path`'s `Equatable` implementation) 0635 /// - the paths can be normalized to equal Paths. 0636 /// 0637 public func ~=(lhs: Path, rhs: Path) -> Bool { 0638 return lhs == rhs 0639 || lhs.normalize() == rhs.normalize() 0640 } 0641 0642 0643 // MARK: Comparable 0644 0645 extension Path : Comparable {} 0646 0647 /// Defines a strict total order over Paths based on their underlying string representation. 0648 public func <(lhs: Path, rhs: Path) -> Bool { 0649 return lhs.path < rhs.path 0650 } 0651 0652 0653 // MARK: Operators 0654 0655 /// Appends a Path fragment to another Path to produce a new Path 0656 public func +(lhs: Path, rhs: Path) -> Path { 0657 return lhs.path + rhs.path 0658 } 0659 0660 /// Appends a String fragment to another Path to produce a new Path 0661 public func +(lhs: Path, rhs: String) -> Path { 0662 return lhs.path + rhs 0663 } 0664 0665 /// Appends a String fragment to another String to produce a new Path 0666 internal func +(lhs: String, rhs: String) -> Path { 0667 if rhs.hasPrefix(Path.separator) { 0668 // Absolute paths replace relative paths 0669 return Path(rhs) 0670 } else { 0671 var lSlice = NSString(string: lhs).pathComponents.fullSlice 0672 var rSlice = NSString(string: rhs).pathComponents.fullSlice 0673 0674 // Get rid of trailing "/" at the left side 0675 if lSlice.count > 1 && lSlice.last == Path.separator { 0676 lSlice.removeLast() 0677 } 0678 0679 // Advance after the first relevant "." 0680 lSlice = lSlice.filter { $0 != "." }.fullSlice 0681 rSlice = rSlice.filter { $0 != "." }.fullSlice 0682 0683 // Eats up trailing components of the left and leading ".." of the right side 0684 while lSlice.last != ".." && rSlice.first == ".." { 0685 if (lSlice.count > 1 || lSlice.first != Path.separator) && !lSlice.isEmpty { 0686 // A leading "/" is never popped 0687 lSlice.removeLast() 0688 } 0689 if !rSlice.isEmpty { 0690 rSlice.removeFirst() 0691 } 0692 0693 switch (lSlice.isEmpty, rSlice.isEmpty) { 0694 case (true, _): 0695 break 0696 case (_, true): 0697 break 0698 default: 0699 continue 0700 } 0701 } 0702 0703 return Path(components: lSlice + rSlice) 0704 } 0705 } 0706 0707 extension Array { 0708 var fullSlice
PathKit.swift:610 return DirectoryEnumerator(path: self): ArraySlice<Element> { 0709 return self[0..<self.endIndex] 0710 } 0711 } 0712
PathKit.swift:671 var lSlice = NSString(string: lhs).pathComponents.fullSlicePathKit.swift:672 var rSlice = NSString(string: rhs).pathComponents.fullSlicePathKit.swift:680 lSlice = lSlice.filter { $0 != "." }.fullSlicePathKit.swift:681 rSlice = rSlice.filter { $0 != "." }.fullSlice