-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFiles.swift
More file actions
167 lines (123 loc) · 3.71 KB
/
Copy pathFiles.swift
File metadata and controls
167 lines (123 loc) · 3.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
//
// ByvLocalizableStringsGenerator.swift
// ByvLocalizableStringsGenerator
//
// Created by Adrian Apodaca on 3/5/18.
// Copyright © 2017 byvapps. All rights reserved.
//
import Foundation
let path = FileManager.default.currentDirectoryPath
let dir = Directory(path: path + "/..")
extension FileHandle {
public func read (encoding: String.Encoding = String.Encoding.utf8) -> String {
let data: Data = self.readDataToEndOfFile()
guard let result = String(data: data, encoding: encoding) else {
fatalError("Could not convert binary data to text.")
}
return result
}
}
/**
* Filesystem node (either a file or directory)
* Is used as a base class for the file and directory classes
*/
class DiskNode {
let manager: FileManager = FileManager.default
let path: String
init?(path: String) {
self.path = path
if type(of: self) == DiskNode.Type.self {
return nil
}
if !exists() {
return nil
}
}
func exists() -> Bool {
var isDir : ObjCBool = false
let result: Bool
if manager.fileExists(atPath: path, isDirectory: &isDir) {
if type(of: self) == Directory.Type.self {
if isDir.boolValue {
result = true
}
else {
result = false
}
}
else {
result = true
}
}
else {
result = false
}
return result
}
}
/**
* Local filesystem file
*/
class File: DiskNode {
let handle: FileHandle
let name: String
let ext: String
override init?(path: String) {
guard let handle = FileHandle(forReadingAtPath: path) else {
return nil
}
let uri = URL(fileURLWithPath: path)
self.handle = handle
self.ext = URL(fileURLWithPath: path).pathExtension
self.name = uri.pathComponents.last ?? ""
super.init(path: path)
}
func contents() -> String {
return handle.read()
}
}
/**
* Local filesystem directory
*/
class Directory: DiskNode {
func findFilesRecursively(byExtension: String) -> [File]? {
guard let enumerator = manager.enumerator(atPath: path) else {
return nil
}
var result: [File] = []
enumerator.forEach({ (path) in
guard let path = path as? String else {
return
}
let ext = URL(fileURLWithPath: path).pathExtension
guard ext == byExtension else {
return
}
guard let file = File(path: self.path + "/" + path) else {
return
}
result.append(file)
})
return result
}
func findFilesRecursively(byName: String) -> [File]? {
guard let enumerator = manager.enumerator(atPath: path) else {
return nil
}
var result: [File] = []
enumerator.forEach({ (path) in
guard let path = path as? String else {
return
}
let filename = URL(fileURLWithPath: path).lastPathComponent
guard filename == byName else {
return
}
guard let file = File(path: self.path + "/" + path) else {
return
}
result.append(file)
})
return result
}
}