Skip to content

Commit 19b8e55

Browse files
committed
[feat]: source division, parameter processing
1 parent 4c5e97e commit 19b8e55

File tree

4 files changed

+154
-61
lines changed

4 files changed

+154
-61
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
//
2+
// File.swift
3+
// ScriptToolkit
4+
//
5+
// Created by Dan Cech on 15.02.2019.
6+
//
7+
8+
import Foundation
9+
10+
public extension File {
11+
@discardableResult public func createDuplicate(withName newName: String, keepExtension: Bool = true) throws -> File {
12+
guard let parent = parent else {
13+
throw OperationError.renameFailed(self)
14+
}
15+
16+
var newName = newName
17+
18+
if keepExtension {
19+
if let `extension` = `extension` {
20+
let extensionString = ".\(`extension`)"
21+
22+
if !newName.hasSuffix(extensionString) {
23+
newName += extensionString
24+
}
25+
}
26+
}
27+
28+
let newPath = parent.path + newName
29+
30+
do {
31+
try FileManager.default.copyItem(atPath: path, toPath: newPath)
32+
return try File(path: newPath)
33+
} catch {
34+
throw OperationError.renameFailed(self)
35+
}
36+
}
37+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
//
2+
// FolderExtension.swift
3+
// ScriptToolkit
4+
//
5+
// Created by Dan Cech on 15.02.2019.
6+
//
7+
8+
import Foundation
9+
10+
public extension Folder {
11+
@discardableResult public func createDuplicate(withName newName: String, keepExtension: Bool = true) throws -> Folder {
12+
guard let parent = parent else {
13+
throw OperationError.renameFailed(self)
14+
}
15+
16+
var newName = newName
17+
18+
if `extension` != nil {
19+
if keepExtension {
20+
if let `extension` = `extension` {
21+
let extensionString = ".\(`extension`)"
22+
23+
if !newName.hasSuffix(extensionString) {
24+
newName += extensionString
25+
}
26+
}
27+
}
28+
}
29+
30+
let newPath = parent.path + newName
31+
32+
do {
33+
try FileManager.default.copyItem(atPath: path, toPath: newPath)
34+
return try Folder(path: newPath)
35+
} catch {
36+
throw OperationError.renameFailed(self)
37+
}
38+
}
39+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
//
2+
// ParameterProcessing.swift
3+
// ScriptToolkit
4+
//
5+
// Created by Dan Cech on 15.02.2019.
6+
//
7+
8+
import Foundation
9+
10+
public protocol StringAssignable {
11+
mutating func assign(value: String) throws
12+
}
13+
14+
extension Int: StringAssignable {
15+
public mutating func assign(value: String) throws {
16+
if let intValue = Int(value) {
17+
self = intValue
18+
}
19+
else {
20+
throw ArgumentError(errormessage: "Argument is not integer")
21+
}
22+
}
23+
}
24+
25+
extension Optional: StringAssignable where Wrapped == String {
26+
public mutating func assign(value: String) throws {
27+
self = value
28+
}
29+
}
30+
31+
32+
public protocol OptionalyHavingValue {
33+
func hasValue() -> Bool
34+
}
35+
36+
extension Int: OptionalyHavingValue {
37+
public func hasValue() -> Bool {
38+
return true
39+
}
40+
}
41+
42+
extension Optional: OptionalyHavingValue where Wrapped == String {
43+
public func hasValue() -> Bool {
44+
return self != .none
45+
}
46+
}
47+
48+
public func askForMissingParams<T: StringAssignable & OptionalyHavingValue>(_ params: [(Argument<T>, FutureValue<T>)]) throws {
49+
for paramItem in params {
50+
// Skip already assigned parameters
51+
if paramItem.1.value.hasValue() { continue }
52+
53+
let paramTitle = paramItem.0.usage?.title ?? ""
54+
print("\(paramTitle):")
55+
let input = readLine() ?? ""
56+
try paramItem.1.value.assign(value: input)
57+
}
58+
}

Sources/ScriptToolkit/ScriptToolkit.swift

Lines changed: 20 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
//
2+
// ScriptToolkit.swift
3+
// ScriptToolkit
4+
//
5+
// Created by Dan Cech on 15.02.2019.
6+
//
7+
18
import Foundation
29
import Files
310
import SwiftShell
@@ -10,66 +17,6 @@ public struct ScriptToolkit {
1017
}()
1118
}
1219

13-
public extension File {
14-
@discardableResult public func createDuplicate(withName newName: String, keepExtension: Bool = true) throws -> File {
15-
guard let parent = parent else {
16-
throw OperationError.renameFailed(self)
17-
}
18-
19-
var newName = newName
20-
21-
if keepExtension {
22-
if let `extension` = `extension` {
23-
let extensionString = ".\(`extension`)"
24-
25-
if !newName.hasSuffix(extensionString) {
26-
newName += extensionString
27-
}
28-
}
29-
}
30-
31-
let newPath = parent.path + newName
32-
33-
do {
34-
try FileManager.default.copyItem(atPath: path, toPath: newPath)
35-
return try File(path: newPath)
36-
} catch {
37-
throw OperationError.renameFailed(self)
38-
}
39-
}
40-
}
41-
42-
public extension Folder {
43-
@discardableResult public func createDuplicate(withName newName: String, keepExtension: Bool = true) throws -> Folder {
44-
guard let parent = parent else {
45-
throw OperationError.renameFailed(self)
46-
}
47-
48-
var newName = newName
49-
50-
if `extension` != nil {
51-
if keepExtension {
52-
if let `extension` = `extension` {
53-
let extensionString = ".\(`extension`)"
54-
55-
if !newName.hasSuffix(extensionString) {
56-
newName += extensionString
57-
}
58-
}
59-
}
60-
}
61-
62-
let newPath = parent.path + newName
63-
64-
do {
65-
try FileManager.default.copyItem(atPath: path, toPath: newPath)
66-
return try Folder(path: newPath)
67-
} catch {
68-
throw OperationError.renameFailed(self)
69-
}
70-
}
71-
}
72-
7320
////////////////////////////////////////////////////////////////////////////////
7421
// MARK: - Helpers
7522

@@ -316,7 +263,19 @@ public func removeEmptyDirectories(in folder: Folder) throws {
316263
////////////////////////////////////////////////////////////////////////////////
317264
// MARK: - Resize image
318265

319-
320266
public func resizeImage(original: String, newName: String, size: CGSize) {
321267
run("/usr/local/bin/convert", original, "-resize", "\(size.width)x\(size.height)",newName)
322268
}
269+
270+
public func resizeAt123x(_ file: File, width: Int, height: Int, outputDir: Folder) throws {
271+
print(file.name)
272+
273+
let res1name = outputDir.path.appendingPathComponent(path: file.name)
274+
resizeImage(original: file.path, newName: res1name, size: CGSize(width: width, height: height))
275+
276+
let res2name = outputDir.path.appendingPathComponent(path: file.nameExcludingExtension + "@2x." + (file.extension ?? ""))
277+
resizeImage(original: file.path, newName: res2name, size: CGSize(width: 2 * width, height: 2 * height))
278+
279+
let res3name = outputDir.path.appendingPathComponent(path: file.nameExcludingExtension + "@3x." + (file.extension ?? ""))
280+
resizeImage(original: file.path, newName: res3name, size: CGSize(width: 3 * width, height: 3 * height))
281+
}

0 commit comments

Comments
 (0)