-
-
Notifications
You must be signed in to change notification settings - Fork 15
/
ScriptCommand.swift
94 lines (81 loc) · 2.42 KB
/
ScriptCommand.swift
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
import Foundation
struct ScriptCommand: MetaDataProviding {
enum Kind: String, Codable, Sendable {
case appleScript = "scpt"
case shellScript = "sh"
}
enum Source: Hashable, Codable, Sendable, Equatable {
case path(String)
case inline(String)
var contents: String {
get {
switch self {
case .path(let contents): contents
case .inline(let contents): contents
}
}
set {
switch self {
case .path(let string):
self = .path(string)
case .inline(let string):
self = .inline(string)
}
}
}
}
var kind: Kind
var source: Source
var meta: Command.MetaData
init(id: String = UUID().uuidString,
name: String, kind: Kind, source: Source,
isEnabled: Bool = true,
notification: Command.Notification? = nil,
variableName: String? = nil) {
self.kind = kind
self.source = source
self.meta = Command.MetaData(
id: id, name: name, isEnabled: true,
notification: notification, variableName: variableName)
}
init(kind: Kind, source: Source, meta: Command.MetaData) {
self.kind = kind
self.source = source
self.meta = meta
}
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
do {
self.kind = try container.decode(Kind.self, forKey: .kind)
self.source = try container.decode(Source.self, forKey: .source)
self.meta = try container.decode(Command.MetaData.self, forKey: .meta)
} catch {
let oldScript = try OldScriptCommand(from: decoder)
let id: String
let name: String
let isEnabled: Bool
switch oldScript {
case .appleScript(let _id, let _isEnabled, let _name, _):
self.kind = .appleScript
id = _id
name = _name ?? ""
isEnabled = _isEnabled
case .shell(let _id, let _isEnabled, let _name, _):
self.kind = .shellScript
id = _id
name = _name ?? ""
isEnabled = _isEnabled
}
switch oldScript.sourceType {
case .path(let path):
self.source = .path(path)
case .inline(let source):
self.source = .inline(source)
}
self.meta = Command.MetaData(id: id, name: name, isEnabled: isEnabled, notification: nil)
}
}
func copy() -> ScriptCommand {
ScriptCommand(kind: self.kind, source: self.source, meta: self.meta.copy())
}
}