Skip to content

Commit 9fe5659

Browse files
committed
[feat]: comments and shell command
1 parent 5e1b049 commit 9fe5659

File tree

8 files changed

+73
-13
lines changed

8 files changed

+73
-13
lines changed

Sources/ScriptToolkit/FileExtension.swift

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import SwiftShell
1212

1313
public extension File {
1414

15+
/// Appending suffix to file name
1516
func nameWithSuffix(_ suffix: String) -> String {
1617
if let unwrappedExtension = `extension` {
1718
return nameExcludingExtension + suffix + "." + unwrappedExtension
@@ -21,9 +22,8 @@ public extension File {
2122
}
2223
}
2324

24-
////////////////////////////////////////////////////////////////////////////////
25-
// MARK: - Duplication
26-
25+
26+
/// Create file duplicate
2727
@discardableResult func createDuplicate(withName newName: String, keepExtension: Bool = true, overwrite: Bool = true) throws -> File? {
2828
if !overwrite && FileManager.default.fileExists(atPath: newName) { return nil }
2929

@@ -53,9 +53,8 @@ public extension File {
5353
}
5454
}
5555

56-
////////////////////////////////////////////////////////////////////////////////
57-
// MARK: - Modification date
58-
56+
57+
/// File modification date
5958
func modificationDate() throws -> Date {
6059

6160
let fileAttributes = try FileManager.default.attributesOfItem(atPath: path) as [FileAttributeKey: Any]
@@ -64,9 +63,8 @@ public extension File {
6463
return modificationDate
6564
}
6665

67-
////////////////////////////////////////////////////////////////////////////////
68-
// MARK: - Tag file
6966

67+
/// Tag file with date/time/version signature
7068
func tag(copy: Bool) throws {
7169
let date = try modificationDate()
7270
let suffix = ScriptToolkit.dateFormatter.string(from: date)
@@ -92,6 +90,7 @@ public extension File {
9290
////////////////////////////////////////////////////////////////////////////////
9391
// MARK: - Photo Processing
9492

93+
/// Moving file to appropriate folder during photo processing
9594
func incorporateFile(using folderRecords: [(Folder, [Int])]) throws {
9695
print("\(path)")
9796
let numberString = nameExcludingExtension.replacingOccurrences(of: "IMG_", with: "")
@@ -124,6 +123,7 @@ public extension File {
124123
////////////////////////////////////////////////////////////////////////////////
125124
// MARK: - Resize image
126125

126+
/// Image resizing
127127
@discardableResult func resizeImage(newName: String, size: CGSize, overwrite: Bool = true) throws -> File {
128128
let image: NSImage? = NSImage(contentsOfFile: self.path)
129129
let newImage = image.map { try? $0.copy(size: size) } ?? nil
@@ -135,6 +135,7 @@ public extension File {
135135
return try File(path: newName)
136136
}
137137

138+
/// Create three sizes of image for iOS asset
138139
func resizeAt123x(width: Int, height: Int, outputDir: Folder, overwrite: Bool = true) throws {
139140
print(name)
140141

@@ -151,6 +152,7 @@ public extension File {
151152
////////////////////////////////////////////////////////////////////////////////
152153
// MARK: - Audio Processing
153154

155+
/// Slow down audio
154156
@discardableResult func slowDownAudio(newName: String, percent: Float, overwrite: Bool = true) throws -> File {
155157
if FileManager.default.fileExists(atPath: newName) {
156158
if !overwrite { return try File(path: newName) }
@@ -161,6 +163,7 @@ public extension File {
161163
return try File(path: newName)
162164
}
163165

166+
/// Conversion to .wav
164167
@discardableResult func convertToWav(newName: String, overwrite: Bool = true) throws -> File {
165168
if FileManager.default.fileExists(atPath: newName) {
166169
if !overwrite { return try File(path: newName) }
@@ -171,6 +174,7 @@ public extension File {
171174
return try File(path: newName)
172175
}
173176

177+
/// Sample rate normalization
174178
@discardableResult func normalizeSampleRate(newName: String, overwrite: Bool = true) throws -> File {
175179
if FileManager.default.fileExists(atPath: newName) {
176180
if !overwrite { return try File(path: newName) }
@@ -180,7 +184,8 @@ public extension File {
180184
runAndDebug(ScriptToolkit.soxPath, path, "-r", "44100", "--channels", "2", newName)
181185
return try File(path: newName)
182186
}
183-
187+
188+
/// Conversion to .m4a
184189
@discardableResult func convertToM4A(newName: String, overwrite: Bool = true) throws -> File {
185190
if FileManager.default.fileExists(atPath: newName) {
186191
if !overwrite { return try File(path: newName) }
@@ -191,6 +196,7 @@ public extension File {
191196
return try File(path: newName)
192197
}
193198

199+
/// Add a 5s silence and the begining of audio file
194200
@discardableResult func addSilence(newName: String, overwrite: Bool = true) throws -> File {
195201
if FileManager.default.fileExists(atPath: newName) {
196202
if !overwrite { return try File(path: newName) }
@@ -201,6 +207,7 @@ public extension File {
201207
return try File(path: newName)
202208
}
203209

210+
/// Prepare song for practise - generate audio in 50%, 75%, 90% and 100% speed
204211
func prepareSongForPractise(outputFolder: Folder, overwrite: Bool = true) throws {
205212
let inputFolder = parent!
206213

@@ -275,6 +282,7 @@ public extension File {
275282
////////////////////////////////////////////////////////////////////////////////
276283
// MARK: - Video Processing
277284

285+
/// Video reduction to smaller size and quality
278286
@discardableResult func reduceVideo(newName: String, overwrite: Bool = true) throws -> File {
279287
if FileManager.default.fileExists(atPath: newName) {
280288
if !overwrite { return try File(path: newName) }
@@ -288,6 +296,7 @@ public extension File {
288296
////////////////////////////////////////////////////////////////////////////////
289297
// MARK: - PDF
290298

299+
/// Crop margins from PDF
291300
@discardableResult func cropPDF(newName: String, insets: NSEdgeInsets, overwrite: Bool = true) throws -> File {
292301
if FileManager.default.fileExists(atPath: newName) {
293302
if !overwrite { return try File(path: newName) }

Sources/ScriptToolkit/FileManagerExtension.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import Foundation
99
import Files
1010

1111
public extension FileManager {
12+
/// Does the location exist?
1213
func locationExists(path: String, kind: LocationKind) -> Bool {
1314
var isFolder: ObjCBool = false
1415

@@ -22,6 +23,7 @@ public extension FileManager {
2223
}
2324
}
2425

26+
/// Type of location in path
2527
func locationKind(for path: String) -> LocationKind {
2628
var isFolder: ObjCBool = false
2729

Sources/ScriptToolkit/FolderExtension.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import SwiftShell
1111

1212
public extension Folder {
1313

14+
/// Create folder duplicate
1415
@discardableResult func createDuplicate(withName newName: String, keepExtension: Bool = true) throws -> Folder {
1516
guard let parent = parent else {
1617
throw ScriptError.renameFailed(message: newName)
@@ -43,6 +44,7 @@ public extension Folder {
4344
////////////////////////////////////////////////////////////////////////////////
4445
// MARK: - Modification date
4546

47+
/// Get folder modification date
4648
func modificationDate() throws -> Date {
4749

4850
let fileAttributes = try FileManager.default.attributesOfItem(atPath: path) as [FileAttributeKey: Any]
@@ -54,6 +56,7 @@ public extension Folder {
5456
////////////////////////////////////////////////////////////////////////////////
5557
// MARK: - Tag folder
5658

59+
/// Tag folder with date/time/version signature
5760
func tag(copy: Bool) throws {
5861
let date = try modificationDate()
5962
let suffix = ScriptToolkit.dateFormatter.string(from: date)
@@ -82,6 +85,7 @@ public extension Folder {
8285
////////////////////////////////////////////////////////////////////////////////
8386
// MARK: - Flatten folder structure
8487

88+
/// Make directory structure flat - use longer file names
8589
func flattenFolderStructure(outputDir: String, move: Bool, overwrite: Bool = true) throws {
8690
let outputFolder = try Folder(path: outputDir)
8791

@@ -111,6 +115,7 @@ public extension Folder {
111115
////////////////////////////////////////////////////////////////////////////////
112116
// MARK: - Sort Photos
113117

118+
/// Get photo metadata
114119
func exifTool(byCameraModel: Bool, processM4V: Bool) throws {
115120
let inputFolder = try Folder(path: path)
116121

@@ -170,6 +175,7 @@ public extension Folder {
170175
}
171176
}
172177

178+
/// Organize photos by metadata
173179
func organizePhotos() throws {
174180
print("📂 Organizing...")
175181

@@ -204,6 +210,7 @@ public extension Folder {
204210
////////////////////////////////////////////////////////////////////////////////
205211
// MARK: - Remove Empty Directories
206212

213+
/// Remove empty subdirectories
207214
func removeEmptyDirectories() throws {
208215
for subfolder in subfolders {
209216
try subfolder.removeEmptyDirectories()
@@ -218,6 +225,7 @@ public extension Folder {
218225
////////////////////////////////////////////////////////////////////////////////
219226
// MARK: - Find files
220227

228+
/// Find first file with name recursively
221229
func findFirstFile(name: String) -> File? {
222230
for file in files.recursive {
223231
if file.name == name {
@@ -227,6 +235,7 @@ public extension Folder {
227235
return nil
228236
}
229237

238+
/// Find all files with name recursively
230239
func findFiles(name: String) -> [File] {
231240
var foundFiles = [File]()
232241
for file in files.recursive {
@@ -237,6 +246,7 @@ public extension Folder {
237246
return foundFiles
238247
}
239248

249+
/// Find all files matching the regex recursively
240250
func findFiles(regex: String) -> [File] {
241251
var foundFiles = [File]()
242252
for file in files.recursive {
@@ -250,6 +260,7 @@ public extension Folder {
250260
////////////////////////////////////////////////////////////////////////////////
251261
// MARK: - Find folders
252262

263+
/// Find first folder with name recursively
253264
func findFirstFolder(name: String) -> Folder? {
254265
for folder in subfolders.recursive {
255266
if folder.name == name {
@@ -260,6 +271,7 @@ public extension Folder {
260271
return nil
261272
}
262273

274+
/// Find all folders with name recursively
263275
func findFolders(name: String) -> [Folder] {
264276
var foundFolders = [Folder]()
265277
for folder in subfolders.recursive {
@@ -270,6 +282,7 @@ public extension Folder {
270282
return foundFolders
271283
}
272284

285+
/// Find all folders matching the regex recursively
273286
func findFolders(regex: String) -> [Folder] {
274287
var foundFolders = [Folder]()
275288
for folder in subfolders.recursive {

Sources/ScriptToolkit/NSColorExtension.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import Foundation
99
import AppKit
1010

1111
public extension NSColor {
12+
13+
/// Create color from hex string
1214
convenience init?(hexString: String) {
1315
let hexString = hexString.trimmingCharacters(in: .whitespacesAndNewlines)
1416
let scanner = Scanner(string: hexString)

Sources/ScriptToolkit/NSImageExtension.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,9 @@ public extension NSImage {
6767
try png.write(to: url, options: .atomicWrite)
6868
}
6969
}
70-
70+
71+
72+
/// Combine two images together
7173
func combine(withImage image: NSImage) throws -> NSImage {
7274
guard
7375
let firstImageData = image.tiffRepresentation,
@@ -93,6 +95,7 @@ public extension NSImage {
9395
return finalResult
9496
}
9597

98+
/// Image annotation
9699
func image(
97100
withText text: String,
98101
alignmentMode: NSTextAlignment,
@@ -127,6 +130,7 @@ public extension NSImage {
127130
return image
128131
}
129132

133+
/// Image annotation
130134
func annotate(
131135
text: String,
132136
font: String,

Sources/ScriptToolkit/ParameterProcessing.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ extension Optional: OptionalyHavingValue where Wrapped == String {
4747
}
4848
}
4949

50+
/// Parameters processing helper
5051
public func askForMissingParams<T: StringAssignable & OptionalyHavingValue>(_ params: [(Argument<T>, FutureValue<T>)]) throws {
5152
for paramItem in params {
5253
// Skip already assigned parameters

Sources/ScriptToolkit/ScriptToolkit.swift

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,21 @@ import Moderator
1212

1313
public struct ScriptToolkit {
1414

15-
// Setup of absolute paths
15+
// MARK: - Setup of absolute paths
16+
17+
/// PDFCrop - part of TeXlive distribution
1618
public static let pdfCropPath = "/usr/local/bin/pdfcrop"
19+
20+
/// FFMPEG toolkit
1721
public static let ffmpegPath = "/usr/local/bin/ffmpeg"
22+
23+
/// Sound eXchange tool for sound manipulation
1824
public static let soxPath = "/usr/local/bin/sox"
19-
// public static let convertPath = "/usr/local/bin/convert" // Trying to get rid of ImageMagick dependency
20-
public static let compositePath = "/usr/local/bin/composite"
25+
26+
/// ImageMagick
27+
public static let convertPath = "/usr/local/bin/convert"
28+
29+
/// Short audio with silence
2130
public static let silenceFilePath = "/Users/dan/Documents/[Development]/[Projects]/SwiftScripts/practise/silence.wav"
2231

2332

@@ -32,12 +41,14 @@ public struct ScriptToolkit {
3241
////////////////////////////////////////////////////////////////////////////////
3342
// MARK: - Helpers
3443

44+
/// Running command line tool with debug logs
3545
@discardableResult public func runAndDebug(_ executable: String, _ args: Any ..., combineOutput: Bool = false) -> RunOutput {
3646
let stringargs = args.map(String.init(describing:))
3747
print(executable, String(describing: stringargs.joined(separator: "")))
3848
return run(executable, args, combineOutput: combineOutput)
3949
}
4050

51+
/// A list of regular expression matches
4152
public func matches(for regex: String, in text: String) -> [String] {
4253
do {
4354
let regex = try NSRegularExpression(pattern: regex)
@@ -52,5 +63,21 @@ public func matches(for regex: String, in text: String) -> [String] {
5263
}
5364
}
5465

66+
/// Run shell command in bash
67+
@discardableResult func shell(_ command: String) -> String {
68+
let task = Process()
69+
let pipe = Pipe()
70+
71+
task.standardOutput = pipe
72+
task.arguments = ["-c", command]
73+
task.launchPath = "/bin/bash"
74+
task.launch()
75+
76+
let data = pipe.fileHandleForReading.readDataToEndOfFile()
77+
let output = String(data: data, encoding: .utf8)!
78+
79+
return output
80+
}
81+
5582

5683

Sources/ScriptToolkit/String+Paths.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
import Foundation
99

10+
11+
/// Path manipulation methods that are available for NSString
1012
public extension String {
1113

1214
var lastPathComponent: String {

0 commit comments

Comments
 (0)