This repository has been archived by the owner on Jan 5, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Draveness
committed
Apr 12, 2017
1 parent
539f200
commit d34b689
Showing
7 changed files
with
626 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
// | ||
// Dir.swift | ||
// RbSwift | ||
// | ||
// Created by draveness on 10/04/2017. | ||
// Copyright © 2017 draveness. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
public class Dir { | ||
/// Returns the path to the current working directory of this process as a string. | ||
public static var getwd: String { | ||
return FileManager.default.currentDirectoryPath | ||
} | ||
|
||
/// Returns the path to the current working directory of this process as a string. | ||
/// An alias to `Dir#getwd` var. | ||
public static var pwd: String { | ||
return getwd | ||
} | ||
|
||
public static func glob(_ pattern: String) -> [String] { | ||
return [] | ||
} | ||
|
||
/// Returns the home directory of the current user or the named user if given. | ||
/// | ||
/// Dir.home #=> "/Users/username" | ||
/// | ||
public static var home: String { | ||
return NSHomeDirectory() | ||
} | ||
|
||
/// Error throws when user doesn't exists. | ||
/// | ||
/// - notExists: User doesn't exists | ||
public enum HomeDirectory: Error { | ||
case notExists | ||
} | ||
|
||
/// Returns the home directory of the current user or the named user if given. | ||
/// | ||
/// Dir.home() #=> "/Users/username" | ||
/// Dir.home("user") #=> "/user" | ||
/// | ||
/// - Parameter path: The name of user. | ||
/// - Returns: Home directory. | ||
/// - Throws: HomeDirectory.notExists if user doesn't exist. | ||
public static func home(_ user: String? = nil) throws -> String { | ||
if let home = NSHomeDirectoryForUser(user) { return home } | ||
throw HomeDirectory.notExists | ||
} | ||
|
||
/// Changes the current working directory of the process to the given string. | ||
/// | ||
/// Dir.chdir("/var/spool/mail") | ||
/// Dir.pwd #=> "/var/spool/mail" | ||
/// | ||
/// - Parameter path: A new current working directory. | ||
/// - Returns: A bool indicates the result of `Dir#chdir(path:)` | ||
@discardableResult public static func chdir(_ path: String = "~") -> Bool { | ||
return FileManager.default.changeCurrentDirectoryPath(path) | ||
} | ||
|
||
/// Changes the current working directory of the process to the given string. | ||
/// Block is passed the name of the new current directory, and the block | ||
/// is executed with that as the current directory. The original working directory | ||
/// is restored when the block exits. The return value of chdir is the value of the block. | ||
/// | ||
/// Dir.pwd #=> "/work" | ||
/// let value = Dir.chdir("/var") { | ||
/// Dir.pwd #=> "/var" | ||
/// return 1 | ||
/// } | ||
/// Dir.pwd #=> "/work" | ||
/// value #=> 1 | ||
/// | ||
/// - Parameters: | ||
/// - path: A new current working directory. | ||
/// - closure: A block executed in target directory. | ||
/// - Returns: The value of the closure. | ||
@discardableResult public static func chdir<T>(_ path: String = "~", closure: ((Void) -> T)) -> T { | ||
let pwd = Dir.pwd | ||
Dir.chdir(path) | ||
let result = closure() | ||
Dir.chdir(pwd) | ||
return result | ||
} | ||
|
||
/// Makes a new directory named by string, with permissions specified by the optional parameter anInteger. | ||
/// | ||
/// try Dir.mkdir("draveness") | ||
/// try! Dir.mkdir("draveness/spool/mail", recursive: true) | ||
/// | ||
/// - Parameters: | ||
/// - path: A new directory path. | ||
/// - recursive: Whether creats intermeidate directories. | ||
/// - permissions: An integer represents the posix permission. | ||
/// - Throws: When `FileManager#createDirectory(atPath:withIntermediateDirectories:attributes:)` throws. | ||
public static func mkdir(_ path: String, recursive: Bool = false, _ permissions: Int? = nil) throws { | ||
var attributes: [String: Any] = [:] | ||
if let permissions = permissions { | ||
attributes[FileAttributeKey.posixPermissions.rawValue] = permissions | ||
} | ||
try FileManager.default.createDirectory(atPath: path, withIntermediateDirectories: recursive, attributes: attributes) | ||
} | ||
|
||
/// Error throws when called `Dir#rmdir(path:)` method. | ||
/// | ||
/// - notEmpty: Directory is not empty. | ||
/// - notExists: Directory is not exists. | ||
public enum RemoveDirectoryError: Error { | ||
case notEmpty | ||
case cocoa(String) | ||
} | ||
|
||
/// Deletes the named directory. | ||
/// | ||
/// try Dir.rmdir("/a/folder/path") | ||
/// | ||
/// - Parameter path: A directory path | ||
/// - Throws: `RemoveDirectoryError` if the directory isn’t empty. | ||
public static func rmdir(_ path: String) throws { | ||
if !Dir.isEmpty(path) { | ||
throw RemoveDirectoryError.notEmpty | ||
} | ||
do { | ||
try FileManager.default.removeItem(atPath: path) | ||
} catch { | ||
throw RemoveDirectoryError.cocoa(error.localizedDescription) | ||
} | ||
} | ||
|
||
/// Returns true if the named file is a directory, false otherwise. | ||
/// | ||
/// Dir.isExist("/a/folder/path/not/exists") #=> false | ||
/// Dir.isExist("/a/folder/path/exists") #=> true | ||
/// | ||
/// - Parameter path: A file path. | ||
/// - Returns: A bool value indicates whether there is a directory in given path. | ||
public static func isExist(_ path: String) -> Bool { | ||
var isDirectory = false as ObjCBool | ||
let exist = FileManager.default.fileExists(atPath: path, isDirectory: &isDirectory) | ||
return exist && isDirectory.boolValue | ||
} | ||
|
||
/// Returns true if the named file is an empty directory, false if it is | ||
/// not a directory or non-empty. | ||
/// | ||
/// Dir.isEmpty("/a/empty/folder") #=> true | ||
/// Dir.isEmpty("/a/folder/not/exists") #=> false | ||
/// Dir.isEmpty("/a/folder/with/files") #=> true | ||
/// | ||
/// - Parameter path: A directory path. | ||
/// - Returns: A bool value indicates the directory is not exists or is empty. | ||
public static func isEmpty(_ path: String) -> Bool { | ||
if let result = try? FileManager.default.contentsOfDirectory(atPath: path) { | ||
return result.isEmpty | ||
} | ||
return true | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
// | ||
// File.swift | ||
// RbSwift | ||
// | ||
// Created by Draveness on 10/04/2017. | ||
// Copyright © 2017 draveness. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
public class File { | ||
public let path: String | ||
public init(_ path: String) { | ||
self.path = path | ||
guard FileManager.default.createFile(atPath: path, contents: nil, attributes: [:]) else { | ||
print("Failed to create file at path: \(path)") | ||
return | ||
} | ||
} | ||
|
||
@discardableResult public static func new(_ path: String) -> File { | ||
return File(path) | ||
} | ||
|
||
/// Returns the last component of the filename given in `filename`. | ||
/// | ||
/// File.basename("/home/work/file.swift") #=> "file.swift" | ||
/// | ||
/// If `suffix` is given and present at the end of `filename`, it is removed. | ||
/// | ||
/// File.basename("/home/work/file.swift", ".swift") #=> "file" | ||
/// | ||
/// If `suffix` is `.*`, any extension will be removed. | ||
/// | ||
/// File.basename("/home/work/file.swift", ".*") #=> "file" | ||
/// File.basename("/home/work/file.rb", ".*") #=> "file" | ||
/// | ||
/// - Parameters: | ||
/// - filename: A file path string. | ||
/// - suffix: A string will be removed from the filename. | ||
/// - Returns: The last component with or without extension. | ||
public static func basename(_ filename: String, _ suffix: String = "") -> String { | ||
let filename = filename as NSString | ||
let base = filename.lastPathComponent | ||
if suffix == ".*" { return (base as NSString).deletingPathExtension } | ||
let pattern = suffix + "$" | ||
guard base =~ pattern else { return base } | ||
return base.gsub(pattern, "") | ||
} | ||
|
||
/// Returns all components of the filename given in `filename` except the last one. | ||
/// | ||
/// File.dirname("/home/work/file.swift") #=> "/home/work" | ||
/// | ||
/// - Parameter filename: A file path string. | ||
/// - Returns: The directory of given filename. | ||
public static func dirname(_ filename: String) -> String { | ||
let filename = filename as NSString | ||
return filename.deletingLastPathComponent | ||
} | ||
|
||
/// Returns the extension (the portion of file name in path starting from the last period). | ||
/// If path is a dotfile, or starts with a period, then the starting dot is not dealt | ||
/// with the start of the extension. | ||
/// | ||
/// | ||
/// An empty string will also be returned when the period is the last character in path. | ||
/// | ||
/// - Parameter path: A file path. | ||
/// - Returns: A file extension of empty string. | ||
public static func extname(_ path: String) -> String { | ||
let ext = (path as NSString).pathExtension | ||
guard ext.isEmpty else { return "." + ext } | ||
return ext | ||
} | ||
|
||
/// Converts a pathname to an absolute pathname. Relative paths are referenced from the current | ||
/// working directory of the process unless `dir` is given, in which case it will be used as the starting | ||
/// point. The given pathname may start with a “~”, which expands to the process owner’s home directory | ||
/// (the environment variable HOME must be set correctly). | ||
/// | ||
/// File.expand(path: "~/file.swift") #=> "/absolute/path/to/home/file.swift" | ||
/// File.expand(path: "file.swift", in: "/usr/bin") #=> "/usr/bin/file.swift" | ||
/// | ||
/// - Parameters: | ||
/// - path: A file path. | ||
/// - dir: A directory path. | ||
/// - Returns: A absolute path. | ||
public static func expand(_ path: String, in dir: String? = nil) -> String { | ||
var filepath = Pathname(path) | ||
if let dir = dir { | ||
filepath = Pathname(dir) + filepath | ||
return (filepath.path as NSString).standardizingPath | ||
} | ||
return File.absolutePath(filepath.path) | ||
} | ||
|
||
/// Converts a pathname to an absolute pathname. | ||
/// | ||
/// | ||
/// File.absolutePath("~/file.swift") #=> Dir.home + "/file.swift" | ||
/// File.absolutePath("./file.swift") #=> Dir.pwd + "/file.swift" | ||
/// | ||
/// - Parameter path: A files path. | ||
/// - Returns: A absolute path of given file path. | ||
public static func absolutePath(_ path: String) -> String { | ||
let pathname = Pathname(path) | ||
if pathname.isAbsolute { | ||
return (path as NSString).standardizingPath | ||
} | ||
|
||
let expandingPath = Pathname((path as NSString).expandingTildeInPath) | ||
if expandingPath.isAbsolute { | ||
return (expandingPath.path as NSString).standardizingPath | ||
} | ||
|
||
return (Pathname(Dir.pwd) + pathname).path | ||
} | ||
|
||
/// Splits the given string into a directory and a file component and returns a tuple with `(File.dirname, File.basename)`. | ||
/// | ||
/// File.split("/home/gumby/.profile") #=> ("/home/gumby", ".profile") | ||
/// | ||
/// - Parameter path: A file path. | ||
/// - Returns: A tuple with a directory and a file component. | ||
public static func split(_ path: String) -> (String, String) { | ||
return (File.dirname(path), File.basename(path)) | ||
} | ||
|
||
/// Returns true if the named file is a directory, and false otherwise. | ||
/// | ||
/// - Parameter path: A file path. | ||
/// - Returns: A bool value. | ||
public static func isDirectory(_ path: String) -> Bool { | ||
return Dir.isExist(path) | ||
} | ||
|
||
/// Returns a new string formed by joining the strings using "/". | ||
/// | ||
/// File.join("usr", "bin", "swift") #=> "usr/bin/swift" | ||
/// | ||
/// - Parameter paths: An array of file path. | ||
/// - Returns: A new file path. | ||
public static func join(_ paths: String...) -> String { | ||
var pathnames = paths.flatMap(Pathname.init) | ||
if pathnames.count == 1 { | ||
return pathnames.first!.path | ||
} else if pathnames.count >= 2 { | ||
var result = pathnames.shift()! + pathnames.shift()! | ||
while pathnames.count > 0 { | ||
result = result + pathnames.shift()! | ||
} | ||
return result.path | ||
} | ||
return "" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// | ||
// FileUtils.swift | ||
// RbSwift | ||
// | ||
// Created by Draveness on 11/04/2017. | ||
// Copyright © 2017 draveness. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
public class FileUtils { | ||
/// Removes directories and its all contents recursively. | ||
/// | ||
/// - Parameter paths: An array of file path. | ||
public static func rm_rf(_ paths: String...) { | ||
do { | ||
try paths.forEach { try FileManager.default.removeItem(atPath: $0) } | ||
} catch { } | ||
} | ||
|
||
@discardableResult public static func touch(_ path: String) -> Bool { | ||
return FileManager.default.createFile(atPath: path, contents: nil, attributes: [:]) | ||
} | ||
} |
Oops, something went wrong.