Skip to content

Commit a349015

Browse files
committed
[feat]: finding improvements
1 parent 3d8846d commit a349015

File tree

3 files changed

+49
-43
lines changed

3 files changed

+49
-43
lines changed

Sources/ScriptToolkit/FileExtension.swift

Lines changed: 1 addition & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public extension File {
8989

9090
@discardableResult public func resizeImage(newName: String, size: CGSize, overwrite: Bool = true) throws -> File {
9191
if !overwrite && FileManager.default.fileExists(atPath: newName) { return try File(path: newName) }
92-
run("/usr/local/bin/convert", path, "-resize", "\(size.width)x\(size.height)",newName)
92+
run(ScriptToolkit.convertPath, path, "-resize", "\(Int(size.width))x\(Int(size.height))",newName)
9393
return try File(path: newName)
9494
}
9595

@@ -142,35 +142,4 @@ public extension File {
142142
return try File(path: newName)
143143
}
144144

145-
////////////////////////////////////////////////////////////////////////////////
146-
// MARK: - Find files
147-
148-
public func findFirstFile(name: String, root: Folder) -> File? {
149-
for file in root.makeFileSequence(recursive: true) {
150-
if file.name == name {
151-
return file
152-
}
153-
}
154-
return nil
155-
}
156-
157-
public func findFiles(name: String, root: Folder) -> [File] {
158-
var foundFiles = [File]()
159-
for file in root.makeFileSequence(recursive: true) {
160-
if file.name == name {
161-
foundFiles.append(file)
162-
}
163-
}
164-
return foundFiles
165-
}
166-
167-
public func findFiles(regex: String, root: Folder) -> [File] {
168-
var foundFiles = [File]()
169-
for file in root.makeFileSequence(recursive: true) {
170-
if !matches(for: regex, in: file.name).isEmpty {
171-
foundFiles.append(file)
172-
}
173-
}
174-
return foundFiles
175-
}
176145
}

Sources/ScriptToolkit/FolderExtension.swift

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -175,31 +175,63 @@ public extension Folder {
175175
}
176176
}
177177

178+
////////////////////////////////////////////////////////////////////////////////
179+
// MARK: - Find files
180+
181+
public func findFirstFile(name: String) -> File? {
182+
for file in makeFileSequence(recursive: true) {
183+
if file.name == name {
184+
return file
185+
}
186+
}
187+
return nil
188+
}
189+
190+
public func findFiles(name: String) -> [File] {
191+
var foundFiles = [File]()
192+
for file in makeFileSequence(recursive: true) {
193+
if file.name == name {
194+
foundFiles.append(file)
195+
}
196+
}
197+
return foundFiles
198+
}
199+
200+
public func findFiles(regex: String) -> [File] {
201+
var foundFiles = [File]()
202+
for file in makeFileSequence(recursive: true) {
203+
if !matches(for: regex, in: file.name).isEmpty {
204+
foundFiles.append(file)
205+
}
206+
}
207+
return foundFiles
208+
}
209+
178210
////////////////////////////////////////////////////////////////////////////////
179211
// MARK: - Find folders
180212

181-
public func findFirstFolder(name: String, root: Folder) -> Folder? {
182-
for folder in root.makeSubfolderSequence(recursive: true) {
213+
public func findFirstFolder(name: String) -> Folder? {
214+
for folder in makeSubfolderSequence(recursive: true) {
183215
if folder.name == name {
184216
return folder
185217
}
186218
}
187219
return nil
188220
}
189221

190-
public func findFolders(name: String, root: Folder) -> [Folder] {
222+
public func findFolders(name: String) -> [Folder] {
191223
var foundFolders = [Folder]()
192-
for folder in root.makeSubfolderSequence(recursive: true) {
224+
for folder in makeSubfolderSequence(recursive: true) {
193225
if folder.name == name {
194226
foundFolders.append(folder)
195227
}
196228
}
197229
return foundFolders
198230
}
199231

200-
public func findFolders(regex: String, root: Folder) -> [Folder] {
232+
public func findFolders(regex: String) -> [Folder] {
201233
var foundFolders = [Folder]()
202-
for folder in root.makeSubfolderSequence(recursive: true) {
234+
for folder in makeSubfolderSequence(recursive: true) {
203235
if !matches(for: regex, in: folder.name).isEmpty {
204236
foundFolders.append(folder)
205237
}

Sources/ScriptToolkit/ScriptToolkit.swift

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,23 @@ import Foundation
99
import Files
1010
import SwiftShell
1111

12-
enum ScriptError: Error {
12+
public enum ScriptError: Error {
1313
case fileExists
14+
case fileNotFound
1415
}
1516

1617
public struct ScriptToolkit {
1718

1819
// Setup of absolute paths
19-
static let pdfCropPath = "/usr/local/bin/pdfcrop"
20-
static let ffmpegPath = "/usr/local/bin/ffmpeg"
21-
static let soxPath = "/usr/local/bin/sox"
22-
static let silenceFilePath = "/Users/dan/Documents/[Development]/[Projects]/SwiftScripts/practise/silence.wav"
20+
public static let pdfCropPath = "/usr/local/bin/pdfcrop"
21+
public static let ffmpegPath = "/usr/local/bin/ffmpeg"
22+
public static let soxPath = "/usr/local/bin/sox"
23+
public static let convertPath = "/usr/local/bin/convert"
24+
public static let compositePath = "/usr/local/bin/composite"
25+
public static let silenceFilePath = "/Users/dan/Documents/[Development]/[Projects]/SwiftScripts/practise/silence.wav"
2326

27+
28+
2429
public static let dateFormatter: DateFormatter = {
2530
let formatter = DateFormatter()
2631
formatter.dateFormat = "YYYY-MM-dd"

0 commit comments

Comments
 (0)