Pathish
public protocol Pathish : Comparable, Hashable
A type that represents a filesystem path, if you conform your type
to Pathish it is your responsibility to ensure the string is correctly normalized
-
The normalized string representation of the underlying filesystem path
Declaration
Swift
var string: String { get }
-
ctimeExtension methodReturns the creation-time of the file.
Note
Returnsnilif there is no creation-time, this should only happen if the file doesn’t exist.Important
On Linux this is filesystem dependendent and may not exist.Declaration
Swift
var ctime: Date? { get } -
mtimeExtension methodReturns the modification-time of the file.
Note
If this returnsniland the file exists, something is very wrong.Declaration
Swift
var mtime: Date? { get } -
kindExtension methodThe type of the entry.
See also
Path.EntryTypeDeclaration
Swift
@available(*, deprecated, message: "- SeeAlso: Path.type") var kind: Path.EntryType? { get } -
typeExtension methodThe type of the entry.
See also
Path.EntryTypeDeclaration
Swift
var type: Path.EntryType? { get } -
chmod(_:Extension method) Sets the file’s attributes using UNIX octal notation.
Path.home.join("foo").chmod(0o555)Declaration
Swift
@discardableResult func chmod(_ octal: Int) throws -> Path
-
copy(to:Extension methodoverwrite: ) Copies a file.
try Path.root.join("bar").copy(to: Path.home/"foo") // => "/Users/mxcl/foo"Note
throwsiftois a directory.Note
If eitherselforto are directories,overwrite` is ignored.Note
Throws ifoverwriteisfalseyettois already identical toselfbecause even though Path.swift’s policy is to noop if the desired end result preexists, checking for this condition is too expensive a trade-off.See also
copy(into:overwrite:)Declaration
Swift
@discardableResult func copy<P>(to: P, overwrite: Bool = false) throws -> Path where P : PathishParameters
toDestination filename.
overwriteIf
trueand bothselfandtoare files, overwritesto.Return Value
toto allow chaining -
copy(into:Extension methodoverwrite: ) Copies a file into a directory
try Path.root.join("bar").copy(into: .home) // => "/Users/mxcl/bar" // Create ~/.local/bin, copy `ls` there and make the new copy executable try Path.root.join("bin/ls").copy(into: Path.home.join(".local/bin").mkdir(.p)).chmod(0o500)If the destination does not exist, this function creates the directory (including intermediary directories if necessary) first.
Note
throwsifintois a file.Note
Throws ifoverwriteisfalseyettois already identical toselfbecause even though Path.swift’s policy is to noop if the desired end result preexists, checking for this condition is too expensive a trade-off.See also
copy(to:overwrite:)Declaration
Swift
@discardableResult func copy<P>(into: P, overwrite: Bool = false) throws -> Path where P : PathishParameters
intoDestination directory
overwriteIf true overwrites any file that already exists at
into.Return Value
The
Pathof the newly copied file. -
move(to:Extension methodoverwrite: ) Moves a file.
try Path.root.join("bar").move(to: Path.home/"foo") // => "/Users/mxcl/foo"Note
throwsiftois a directory.Note
Throws ifoverwriteisfalseyettois already identical toselfbecause even though Path.swift’s policy is to noop if the desired end result preexists, checking for this condition is too expensive a trade-off.See also
move(into:overwrite:)Declaration
Swift
@discardableResult func move<P>(to: P, overwrite: Bool = false) throws -> Path where P : PathishParameters
toDestination filename.
overwriteIf true overwrites any file that already exists at
to.Return Value
toto allow chaining -
move(into:Extension methodoverwrite: ) Moves a file into a directory
try Path.root.join("bar").move(into: .home) // => "/Users/mxcl/bar"If the destination does not exist, this function creates the directory (including intermediary directories if necessary) first.
Note
throwsifintois a file.See also
move(to:overwrite:)Declaration
Swift
@discardableResult func move<P>(into: P, overwrite: Bool = false) throws -> Path where P : PathishParameters
intoDestination directory
overwriteIf true overwrites any file that already exists at
into.Return Value
The
Pathof destination filename. -
delete()Extension methodDeletes the path, recursively if a directory.
Note
noop: if the path doesn’t exist ∵ Path.swift doesn’t error if desired end result preexists.Note
On UNIX will this function will succeed if the parent directory is writable and the current user has permission.Note
This function will fail if the file or directory is “locked”Note
If entry is a symlink, deletes the symlink.See also
lock()Declaration
Swift
@inlinable func delete() throws -
touch()Extension methodCreates an empty file at this path or if the file exists, updates its modification time.
Declaration
Swift
@discardableResult @inlinable func touch() throws -> PathReturn Value
A copy of
selfto allow chaining. -
mkdir(_:Extension method) Creates the directory at this path.
Note
We do not error if the directory already exists (even without.p) because Path.swift noops if the desired end result preexists.Declaration
Swift
@discardableResult func mkdir(_ options: MakeDirectoryOptions? = nil) throws -> PathParameters
optionsSpecify
mkdir(.p)to create intermediary directories.Return Value
A copy of
selfto allow chaining. -
rename(to:Extension method) Renames the file (basename only) at path.
Path.root.foo.bar.rename(to: "baz") // => /foo/bazDeclaration
Swift
@discardableResult func rename(to newname: String) throws -> PathParameters
tothe new basename for the file
Return Value
The renamed path.
-
symlink(as:Extension method) Creates a symlink of this file at
as.Note
Ifselfdoes not exist, is not an error.Declaration
Swift
@discardableResult func symlink<P>(as: P) throws -> Path where P : Pathish -
symlink(into:Extension method) Creates a symlink of this file with the same filename in the
intodirectory.Note
If into does not exist, creates the directory with intermediate directories if necessary.Declaration
Swift
@discardableResult func symlink<P>(into dir: P) throws -> Path where P : Pathish
-
ls(_:Extension method) Same as the
lscommand ∴ output is ”shallow” and unsorted.Note
as perls, by default we do not return hidden files. Specify.afor hidden files.Important
On Linux the listing is alwaysls -aDeclaration
Swift
func ls(_ options: ListDirectoryOptions? = nil) -> [Path]Parameters
optionsConfigure the listing.
-
find()Extension methodRecursively find files under this path. If the path is a file, no files will be found.
Declaration
Swift
func find() -> Path.Finder
-
existsExtension methodNote
Ifselfis a symlink the return value represents the destination, thus if the destination doesn’t exist this returnsfalseeven if the symlink exists.Note
To determine if a symlink exists, use.type.Declaration
Swift
var exists: Bool { get }Return Value
trueif the path represents an actual filesystem entry. -
isFileExtension methodReturns true if the path represents an actual filesystem entry that is not a directory.
Declaration
Swift
var isFile: Bool { get } -
isDirectoryExtension methodReturns true if the path represents an actual directory.
Declaration
Swift
var isDirectory: Bool { get } -
isReadableExtension methodReturns true if the path represents an actual file that is also readable by the current user.
Declaration
Swift
var isReadable: Bool { get } -
isWritableExtension methodReturns true if the path represents an actual file that is also writable by the current user.
Declaration
Swift
var isWritable: Bool { get } -
isDeletableExtension methodReturns true if the path represents an actual file that is also deletable by the current user.
Declaration
Swift
var isDeletable: Bool { get } -
isExecutableExtension methodReturns true if the path represents an actual file that is also executable by the current user.
Declaration
Swift
var isExecutable: Bool { get } -
isSymlinkExtension methodReturns
trueif the file is a symbolic-link (symlink).Declaration
Swift
var isSymlink: Bool { get }
-
urlExtension methodReturns a
URLrepresenting this file path.Declaration
Swift
var url: URL { get } -
fileReferenceURLExtension methodReturns a file-reference URL.
Note
Only NSURL can be a file-reference-URL, hence we return NSURL.Important
On Linux returns an file scheme NSURL for this path string.Declaration
Swift
var fileReferenceURL: NSURL? { get } -
parentExtension method -
extensionExtension methodReturns the filename extension of this path.
Remark
If there is no extension returns “”.Remark
If the filename ends with any number of “.”, returns “”.Note
We special case eg.foo.tar.gz—there are a limited number of these specializations, feel free to PR more.Declaration
Swift
@inlinable var `extension`: String { get } -
componentsExtension methodSplits the string representation on the directory separator.
Important
NSString.pathComponentswill always return an initial/in its array for absolute paths to indicate that the path was absolute, we don’t do this because we are always absolute paths.Declaration
Swift
@inlinable var components: [String] { get } -
join(_:Extension method) Joins a path and a string to produce a new path.
Path.root.join("a") // => /a Path.root.join("a/b") // => /a/b Path.root.join("a").join("b") // => /a/b Path.root.join("a").join("/b") // => /a/bNote
..and.components are interpreted.Note
pathComponent may be multiple components.Note
symlinks are not resolved.See also
Path./(_:_:)Declaration
Swift
func join<S>(_ addendum: S) -> Path where S : StringProtocolParameters
pathComponentThe string to join with this path.
Return Value
A new joined path.
-
/(_:Extension method_: ) Joins a path and a string to produce a new path.
Path.root/"a" // => /a Path.root/"a/b" // => /a/b Path.root/"a"/"b" // => /a/b Path.root/"a"/"/b" // => /a/bNote
..and.components are interpreted.Note
pathComponent may be multiple components.Note
symlinks are not resolved.See also
join(_:)Declaration
Swift
@inlinable static func / <S>(lhs: Self, rhs: S) -> Path where S : StringProtocolParameters
lhsThe base path to join with
rhs.rhsThe string to join with this
lhs.Return Value
A new joined path.
-
relative(to:Extension method) Returns a string representing the relative path to
base.Note
Ifbaseis not a logical prefix forselfyour result will be prefixed some number of../components.To do
Another variant that returnsnilif result would start with..Declaration
Swift
func relative<P>(to base: P) -> String where P : PathishParameters
baseThe base to which we calculate the relative path.
-
basename(dropExtension:Extension method) The basename for the provided file, optionally dropping the file extension.
Path.root.join("foo.swift").basename() // => "foo.swift" Path.root.join("foo.swift").basename(dropExtension: true) // => "foo"Declaration
Swift
func basename(dropExtension: Bool = false) -> StringParameters
dropExtensionIf
truereturns the basename without its file extension.Return Value
A string that is the filename’s basename.
-
readlink()Extension methodIf the path represents an actual entry that is a symlink, returns the symlink’s absolute destination.
Important
This is not exhaustive, the resulting path may still contain a symlink.Important
The path will only be different if the last path component is a symlink, any symlinks in prior components are not resolved.Note
If file exists but isn’t a symlink, returnsself.Note
If symlink destination does not exist, is not an error.Declaration
Swift
func readlink() throws -> Path -
realpath()Extension methodRecursively resolves symlinks in this path.
Declaration
Swift
func realpath() throws -> Path
View on GitHub
Pathish Protocol Reference