@@ -5,11 +5,11 @@ public class FileUtils {}
55
66
77public extension FileUtils {
8-
8+
99 class func clearFile( _ url: URL ? ) {
1010 clearFile ( url? . path)
1111 }
12-
12+
1313 class func clearFile( _ path: String ? ) {
1414 guard let path = path else { return }
1515 do {
@@ -18,35 +18,49 @@ public extension FileUtils {
1818 fatalError ( " CLEAR FILE: Can't write to file \( path) " )
1919 }
2020 }
21-
21+
2222 class func createFile( _ url: URL ? ) {
2323 createFile ( url? . path)
2424 }
25-
25+
26+ class func deleteFileOrFolder( _ url: URL ? ) {
27+ do {
28+ let fileManager = FileManager . default
29+
30+ if fileManager. fileExists ( atPath: url? . path ?? " " ) {
31+ try fileManager. removeItem ( atPath: url!. path)
32+ } else {
33+ fatalError ( " File does not exist " )
34+ }
35+ } catch {
36+ fatalError ( " An error took place: \( error. localizedDescription) " )
37+ }
38+ }
39+
2640 class func createFile( _ path: String ? ) {
2741 guard let path = path else { return }
2842 FileManager . default. createFile ( atPath: path, contents: nil , attributes: nil )
2943 }
30-
44+
3145 class func urls( for directory: FileManager . SearchPathDirectory , skipsHiddenFiles: Bool = true ) -> [ URL ] {
3246 var fileURLs : [ URL ] = . init( )
33-
47+
3448 if let documentsURL = FileManager . default. urls ( for: directory, in: . userDomainMask) . first {
3549 do {
3650 fileURLs = try FileManager . default. contentsOfDirectory ( at: documentsURL,
3751 includingPropertiesForKeys: nil ,
3852 options: skipsHiddenFiles ? [ . skipsHiddenFiles] : [ ] )
3953 } catch { }
4054 }
41-
55+
4256 return fileURLs
4357 }
44-
58+
4559 class func urls( for directory: String , skipsHiddenFiles: Bool = true ) -> [ URL ] {
4660 guard let documentsURL = URL ( string: directory) else { return [ ] }
4761 return urls ( for: documentsURL, skipsHiddenFiles: skipsHiddenFiles)
4862 }
49-
63+
5064 class func urls( for directory: URL , skipsHiddenFiles: Bool = true ) -> [ URL ] {
5165 do {
5266 return try FileManager . default. contentsOfDirectory ( at: directory,
@@ -56,85 +70,85 @@ public extension FileUtils {
5670 return [ ]
5771 }
5872 }
59-
73+
6074 class func readDirectory( for directory: URL , skipsHiddenFiles: Bool = true ) -> [ URL ] {
6175 urls ( for: directory, skipsHiddenFiles: skipsHiddenFiles)
6276 }
63-
77+
6478 class func readDirectory( for directory: String , skipsHiddenFiles: Bool = true ) -> [ URL ] {
6579 urls ( for: directory, skipsHiddenFiles: skipsHiddenFiles)
6680 }
67-
81+
6882 class func isDirectory( _ url: URL ) -> Bool {
6983 ( try ? url. resourceValues ( forKeys: [ . isDirectoryKey] ) ) ? . isDirectory ?? false
7084 }
71-
85+
7286 class func isDirectory( _ path: String ) -> Bool {
7387 guard let url = URL ( string: path) else { fatalError ( " \( path) - can not convert to URL " ) }
7488 return isDirectory ( url)
7589 }
76-
90+
7791 class func isFile( _ url: URL ) -> Bool {
7892 !isDirectory( url)
7993 }
80-
94+
8195 class func isFile( _ path: String ) -> Bool {
8296 guard let url = URL ( string: path) else { fatalError ( " \( path) - can not convert to URL " ) }
8397 return isFile ( url)
8498 }
85-
99+
86100 class func fileExist( _ url: URL ) -> Bool {
87101 fileExist ( url. path)
88102 }
89-
103+
90104 class func fileExist( _ path: String ) -> Bool {
91105 FileManager . default. fileExists ( atPath: path)
92106 }
93-
107+
94108 class func directoryExist( _ url: URL ) -> Bool {
95109 directoryExist ( url. path)
96110 }
97-
111+
98112 class func directoryExist( _ path: String ) -> Bool {
99113 var isDirectory = ObjCBool ( true )
100114 let exists = FileManager . default. fileExists ( atPath: path, isDirectory: & isDirectory)
101115 return exists && isDirectory. boolValue
102116 }
103-
117+
104118 class func absolutePath( _ path: String ) throws -> String {
105119 let pointer : UnsafeMutablePointer < Int8 > ? = realpath ( path, nil )
106120 guard
107121 let cStringPointer: UnsafeMutablePointer < Int8 > = pointer
108- else { throw fatalError ( " unknown error for path: \( path) \n Please, check your path. \n " ) }
122+ else { throw fatalError ( " unknown error for path: \( path) \n Please, check your path. \n " ) }
109123 defer { free ( cStringPointer) }
110-
124+
111125 return String ( cString: cStringPointer)
112126 }
113-
127+
114128 class func absolutePath( _ url: URL ) throws -> String {
115129 try absolutePath ( url. path)
116130 }
117-
131+
118132 class func urlEncode( _ string: String ) -> String {
119133 var allowedCharacters = CharacterSet . alphanumerics
120134 allowedCharacters. insert ( charactersIn: " .-_ " )
121-
135+
122136 return string. addingPercentEncoding ( withAllowedCharacters: allowedCharacters) ?? " "
123137 }
124-
138+
125139 class func makeRelativePath( from projectPath: String , to filePath: String ) -> String ? {
126140 guard let realProjectPath: String = try ? absolutePath ( projectPath) else { return nil }
127141 return filePath. replace ( realProjectPath, " " )
128142 }
129-
143+
130144 class func readDirectory( path: String , _ handler: ( URL ) -> Void ) {
131145 urls ( for: urlEncode ( path) ) . forEach { handler ( $0) }
132146 }
133-
147+
134148 class func readDirectory( path: URL , _ handler: ( URL ) -> Void ) {
135149 readDirectory ( path: path. path, handler)
136150 }
137-
151+
138152 class func recursiveReadDirectory( path: String , _ handler: ( _ folder: String , _ file: URL ) -> Void ) {
139153 readDirectory ( path: path) { ( url) in
140154 if isDirectory ( url) {
@@ -144,7 +158,7 @@ public extension FileUtils {
144158 }
145159 }
146160 }
147-
161+
148162 class func recursiveReadDirectory( path: URL , _ handler: ( _ folder: URL , _ file: URL ) -> Void ) {
149163 readDirectory ( path: path) { ( url) in
150164 if isDirectory ( url) {
@@ -154,5 +168,5 @@ public extension FileUtils {
154168 }
155169 }
156170 }
157-
171+
158172}
0 commit comments