Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make userDomainOf : public #16

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions SwiftFilePath.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -177,9 +177,11 @@
TargetAttributes = {
CC7832EB1A610124005E77C3 = {
CreatedOnToolsVersion = 6.1;
LastSwiftMigration = 0800;
};
CC7832F61A610125005E77C3 = {
CreatedOnToolsVersion = 6.1;
LastSwiftMigration = 0800;
};
};
};
Expand Down Expand Up @@ -349,6 +351,7 @@
PRODUCT_BUNDLE_IDENTIFIER = "me.nori0620.$(PRODUCT_NAME:rfc1034identifier)";
PRODUCT_NAME = "$(TARGET_NAME)";
SKIP_INSTALL = YES;
SWIFT_VERSION = 3.0;
};
name = Debug;
};
Expand All @@ -366,6 +369,7 @@
PRODUCT_BUNDLE_IDENTIFIER = "me.nori0620.$(PRODUCT_NAME:rfc1034identifier)";
PRODUCT_NAME = "$(TARGET_NAME)";
SKIP_INSTALL = YES;
SWIFT_VERSION = 3.0;
};
name = Release;
};
Expand All @@ -384,6 +388,7 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = "me.nori0620.$(PRODUCT_NAME:rfc1034identifier)";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 3.0;
};
name = Debug;
};
Expand All @@ -398,6 +403,7 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = "me.nori0620.$(PRODUCT_NAME:rfc1034identifier)";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 3.0;
};
name = Release;
};
Expand Down
22 changes: 11 additions & 11 deletions SwiftFilePath/Path.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ public class Path {

// MARK: - Class methods

public class func isDir(path:NSString) -> Bool {
public class func isDir(_ path:NSString) -> Bool {
var isDirectory: ObjCBool = false
NSFileManager.defaultManager().fileExistsAtPath(path as String, isDirectory:&isDirectory)
FileManager.default.fileExists(atPath: path as String, isDirectory:&isDirectory)
return isDirectory ? true : false
}

// MARK: - Instance properties and initializer

lazy var fileManager = NSFileManager.defaultManager()
lazy var fileManager = FileManager.default
public let path_string:String


Expand All @@ -37,7 +37,7 @@ public class Path {
}

public var exists: Bool {
return fileManager.fileExistsAtPath(path_string)
return fileManager.fileExists(atPath: path_string)
}

public var isDir: Bool {
Expand All @@ -49,7 +49,7 @@ public class Path {
}

public var parent: Path{
return Path( (path_string as NSString ).stringByDeletingLastPathComponent )
return Path( (path_string as NSString ).deletingLastPathComponent )
}

// MARK: - Instance methods
Expand All @@ -63,7 +63,7 @@ public class Path {
var error: NSError?
let result: Bool
do {
try fileManager.removeItemAtPath(path_string)
try fileManager.removeItem(atPath: path_string)
result = true
} catch let error1 as NSError {
error = error1
Expand All @@ -74,12 +74,12 @@ public class Path {
: Result(failure: error!);
}

public func copyTo(toPath:Path) -> Result<Path,NSError> {
public func copyTo(_ toPath:Path) -> Result<Path,NSError> {
assert(self.exists,"To copy file, file MUST be exists")
var error: NSError?
let result: Bool
do {
try fileManager.copyItemAtPath(path_string,
try fileManager.copyItem(atPath: path_string,
toPath: toPath.toString())
result = true
} catch let error1 as NSError {
Expand All @@ -91,12 +91,12 @@ public class Path {
: Result(failure: error!)
}

public func moveTo(toPath:Path) -> Result<Path,NSError> {
public func moveTo(_ toPath:Path) -> Result<Path,NSError> {
assert(self.exists,"To move file, file MUST be exists")
var error: NSError?
let result: Bool
do {
try fileManager.moveItemAtPath(path_string,
try fileManager.moveItem(atPath: path_string,
toPath: toPath.toString())
result = true
} catch let error1 as NSError {
Expand All @@ -113,7 +113,7 @@ public class Path {
var loadError: NSError?
let result: [NSObject: AnyObject]?
do {
result = try self.fileManager.attributesOfItemAtPath(path_string)
result = try self.fileManager.attributesOfItem(atPath: path_string)
} catch let error as NSError {
loadError = error
result = nil
Expand Down
34 changes: 19 additions & 15 deletions SwiftFilePath/PathExtensionDir.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,27 @@ extension Path {
}

public class var documentsDir:Path {
return Path.userDomainOf(.DocumentDirectory)
return Path.userDomainOf(.documentDirectory)
}

public class var cacheDir:Path {
return Path.userDomainOf(.CachesDirectory)
return Path.userDomainOf(.cachesDirectory)
}

private class func userDomainOf(pathEnum:NSSearchPathDirectory)->Path{
let pathString = NSSearchPathForDirectoriesInDomains(pathEnum, .UserDomainMask, true)[0]
public class var applicationSupportDir: Path {
return Path.userDomainOf(.applicationSupportDirectory)
}

public class func userDomainOf(_ pathEnum:FileManager.SearchPathDirectory)->Path{
let pathString = NSSearchPathForDirectoriesInDomains(pathEnum, .userDomainMask, true)[0]
return Path( pathString )
}

}
#endif

// Add Dir Behavior to Path by extension
extension Path: SequenceType {
extension Path: Sequence {

public subscript(filename: String) -> Path{
get { return self.content(filename) }
Expand All @@ -49,7 +53,7 @@ extension Path: SequenceType {
var loadError: NSError?
let contents: [AnyObject]?
do {
contents = try self.fileManager.contentsOfDirectoryAtPath(path_string
contents = try self.fileManager.contentsOfDirectory(atPath: path_string
)
} catch let error as NSError {
loadError = error
Expand All @@ -69,23 +73,23 @@ extension Path: SequenceType {
return self.children
}

public func content(path_string:NSString) -> Path {
public func content(_ path_string:NSString) -> Path {
return Path(
NSURL(fileURLWithPath: self.path_string)
.URLByAppendingPathComponent( path_string as String )
try! URL(fileURLWithPath: self.path_string)
.appendingPathComponent( path_string as String )
.path!
)
}

public func child(path:NSString) -> Path {
public func child(_ path:NSString) -> Path {
return self.content(path)
}

public func mkdir() -> Result<Path,NSError> {
var error: NSError?
let result: Bool
do {
try fileManager.createDirectoryAtPath(path_string,
try fileManager.createDirectory(atPath: path_string,
withIntermediateDirectories:true,
attributes:nil)
result = true
Expand All @@ -99,15 +103,15 @@ extension Path: SequenceType {

}

public func generate() -> AnyGenerator<Path> {
public func makeIterator() -> AnyIterator<Path> {
assert(self.isDir,"To get iterator, path must be dir< \(path_string) >")
let iterator = fileManager.enumeratorAtPath(path_string)
return anyGenerator() {
let iterator = fileManager.enumerator(atPath: path_string)
return AnyIterator() {
let optionalContent = iterator?.nextObject() as! String?
if let content = optionalContent {
return self.content(content)
} else {
return .None
return .none
}
}
}
Expand Down
22 changes: 11 additions & 11 deletions SwiftFilePath/PathExtensionFile.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
extension Path {

public var ext:NSString {
return NSURL(fileURLWithPath:path_string).pathExtension!
return URL(fileURLWithPath:path_string).pathExtension!
}

public func touch() -> Result<Path,NSError> {
Expand All @@ -20,12 +20,12 @@ extension Path {
: self.createEmptyFile()
}

public func updateModificationDate(date: NSDate = NSDate() ) -> Result<Path,NSError>{
public func updateModificationDate(_ date: Date = Date() ) -> Result<Path,NSError>{
var error: NSError?
let result: Bool
do {
try fileManager.setAttributes(
[NSFileModificationDate :date],
[FileAttributeKey.modificationDate :date],
ofItemAtPath:path_string)
result = true
} catch let error1 as NSError {
Expand All @@ -49,7 +49,7 @@ extension Path {
let read: String?
do {
read = try String(contentsOfFile: path_string,
encoding: NSUTF8StringEncoding)
encoding: String.Encoding.utf8)
} catch let error as NSError {
readError = error
read = nil
Expand All @@ -62,14 +62,14 @@ extension Path {
return read
}

public func writeString(string:String) -> Result<Path,NSError> {
public func writeString(_ string:String) -> Result<Path,NSError> {
assert(!self.isDir,"Can NOT write data from dir")
var error: NSError?
let result: Bool
do {
try string.writeToFile(path_string,
try string.write(toFile: path_string,
atomically:true,
encoding: NSUTF8StringEncoding)
encoding: String.Encoding.utf8)
result = true
} catch let error1 as NSError {
error = error1
Expand All @@ -82,17 +82,17 @@ extension Path {

// MARK: - read/write NSData

public func readData() -> NSData? {
public func readData() -> Data? {
assert(!self.isDir,"Can NOT read data from dir")
return NSData(contentsOfFile: path_string)
return (try? Data(contentsOf: URL(fileURLWithPath: path_string)))
}

public func writeData(data:NSData) -> Result<Path,NSError> {
public func writeData(_ data:Data) -> Result<Path,NSError> {
assert(!self.isDir,"Can NOT write data from dir")
var error: NSError?
let result: Bool
do {
try data.writeToFile(path_string, options:.DataWritingAtomic)
try data.write(to: URL(fileURLWithPath: path_string), options:.atomic)
result = true
} catch let error1 as NSError {
error = error1
Expand Down
42 changes: 21 additions & 21 deletions SwiftFilePath/Result.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,68 +8,68 @@

public enum Result<S,F> {

case Success(S)
case Failure(F)
case success(S)
case failure(F)

public init(success:S){
self = .Success(success)
self = .success(success)
}

public init(failure:F){
self = .Failure(failure)
self = .failure(failure)
}

public var isSuccess:Bool {
switch self {
case .Success: return true
case .Failure: return false
case .success: return true
case .failure: return false
}

}

public var isFailure:Bool {
switch self {
case .Success: return false
case .Failure: return true
case .success: return false
case .failure: return true
}
}

public var value:S? {
switch self {
case .Success(let success):
case .success(let success):
return success
case .Failure(_):
return .None
case .failure(_):
return .none
}
}

public var error:F? {
switch self {
case .Success(_):
return .None
case .Failure(let error):
case .success(_):
return .none
case .failure(let error):
return error
}
}

public func onFailure(handler:(F) -> Void ) -> Result<S,F> {
public func onFailure(_ handler:(F) -> Void ) -> Result<S,F> {
switch self {
case .Success(_):
case .success(_):
return self
case .Failure(let error):
case .failure(let error):
handler( error )
return self
}
}

public func onSuccess(handler:(S) -> Void ) -> Result<S,F> {
public func onSuccess(_ handler:(S) -> Void ) -> Result<S,F> {
switch self {
case .Success(let success):
case .success(let success):
handler(success )
return self
case .Failure(_):
case .failure(_):
return self
}
}

}
}