Skip to content

Commit

Permalink
add absolute path
Browse files Browse the repository at this point in the history
  • Loading branch information
nerzh committed Jun 27, 2020
1 parent 24ab841 commit e9c2e9e
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 3 deletions.
16 changes: 16 additions & 0 deletions Package.resolved
Original file line number Diff line number Diff line change
@@ -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
}
6 changes: 4 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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"]),
Expand Down
58 changes: 57 additions & 1 deletion Sources/FileUtils/FileUtils.swift
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import Foundation

import SwiftRegularExpression

public class FileUtils {}


public extension FileUtils {

class func clearFile(_ url: URL?) {
Expand Down Expand Up @@ -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<Int8>? = realpath(path, nil)
guard
let cStringPointer: UnsafeMutablePointer<Int8> = 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)
}
}
}

}

0 comments on commit e9c2e9e

Please sign in to comment.