diff --git a/Package.resolved b/Package.resolved new file mode 100644 index 0000000..3f13148 --- /dev/null +++ b/Package.resolved @@ -0,0 +1,16 @@ +{ + "object": { + "pins": [ + { + "package": "SwiftRegularExpression", + "repositoryURL": "https://github.com/nerzh/swift-regular-expression.git", + "state": { + "branch": null, + "revision": "957f5a37eff8e6a4efde69a32054e82428ae85a8", + "version": "0.2.2" + } + } + ] + }, + "version": 1 +} diff --git a/Package.swift b/Package.swift index cc4c290..edadee4 100644 --- a/Package.swift +++ b/Package.swift @@ -11,12 +11,14 @@ let package = Package( targets: ["FileUtils"]), ], dependencies: [ - // .package(url: /* package url */, from: "1.0.0"), + .package(name: "SwiftRegularExpression", url: "https://github.com/nerzh/swift-regular-expression.git", .upToNextMajor(from: "0.2.2")), ], targets: [ .target( name: "FileUtils", - dependencies: []), + dependencies: [ + .product(name: "SwiftRegularExpression", package: "SwiftRegularExpression"), + ]), .testTarget( name: "FileUtilsTests", dependencies: ["FileUtils"]), diff --git a/Sources/FileUtils/FileUtils.swift b/Sources/FileUtils/FileUtils.swift index 29844f5..0facc4b 100644 --- a/Sources/FileUtils/FileUtils.swift +++ b/Sources/FileUtils/FileUtils.swift @@ -1,8 +1,9 @@ import Foundation - +import SwiftRegularExpression public class FileUtils {} + public extension FileUtils { class func clearFile(_ url: URL?) { @@ -99,4 +100,59 @@ public extension FileUtils { let exists = FileManager.default.fileExists(atPath: path, isDirectory: &isDirectory) return exists && isDirectory.boolValue } + + class func absolutePath(_ path: String) throws -> String { + let pointer: UnsafeMutablePointer? = realpath(path, nil) + guard + let cStringPointer: UnsafeMutablePointer = pointer + else { throw fatalError("unknown error for path: \(path)\nPlease, check your path.\n") } + defer { free(cStringPointer) } + + return String(cString: cStringPointer) + } + + class func absolutePath(_ url: URL) throws -> String { + try absolutePath(url.path) + } + + class func urlEncode(_ string: String) -> String { + var allowedCharacters = CharacterSet.alphanumerics + allowedCharacters.insert(charactersIn: ".-_") + + return string.addingPercentEncoding(withAllowedCharacters: allowedCharacters) ?? "" + } + + class func makeRelativePath(from projectPath: String, to filePath: String) -> String? { + guard let realProjectPath: String = try? absolutePath(projectPath) else { return nil } + return filePath.replace(realProjectPath, "") + } + + class func readDirectory(path: String, _ handler: (URL) -> Void) { + urls(for: urlEncode(path)).forEach { handler($0) } + } + + class func readDirectory(path: URL, _ handler: (URL) -> Void) { + readDirectory(path: path.path, handler) + } + + class func recursiveReadDirectory(path: String, _ handler: (_ folder: String, _ file: URL) -> Void) { + readDirectory(path: path) { (url) in + if isDirectory(url) { + recursiveReadDirectory(path: url.path, handler) + } else { + handler(path, url) + } + } + } + + class func recursiveReadDirectory(path: URL, _ handler: (_ folder: URL, _ file: URL) -> Void) { + readDirectory(path: path) { (url) in + if isDirectory(url) { + recursiveReadDirectory(path: url, handler) + } else { + handler(path, url) + } + } + } + }