-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathworkflow.swift
executable file
·311 lines (259 loc) · 8.63 KB
/
workflow.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
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
#!/usr/bin/env swift
import Foundation
// MARK: Workflow Protocol
class Workflow {
/// original result list
var items: [AlfredItem] = []
/// error message when error occored
var errorMessage: AlfredItem?
/// message when items is empty
var emptyMessage: AlfredItem = AlfredItem(title: "Nothing found", subtitle: "Please try another thing")
var queryArg: String {
CommandLine.arguments.count > 1 ? CommandLine.arguments[1] : ""
}
func run() {
if let errorMessage = errorMessage {
errorMessage.toAlfredResult().prettyPrint()
return
}
guard !items.isEmpty else {
emptyMessage.toAlfredResult().prettyPrint()
return
}
filter(by: queryArg).toAlfredResult().prettyPrint()
}
/// detect current machine chip arch
/// reference: https://stackoverflow.com/questions/69624731/programmatically-detect-apple-silicon-vs-intel-cpu-in-a-mac-app-at-runtime
static var isAppleChip: Bool = {
var sysInfo = utsname()
let retVal = uname(&sysInfo)
guard retVal == EXIT_SUCCESS else { return false }
return String(cString: &sysInfo.machine.0, encoding: .utf8) == "arm64"
}()
/// detect whether workflow is using as a swift script
/// false for binay
static var isScript: Bool = {
let env = ProcessInfo.processInfo.environment
return env["LIBRARY_PATH"] != nil && env["SDKROOT"] != nil
}()
}
// MARK: Alfred Structs
extension Workflow {
struct AlfredResult: Codable {
let items: [AlfredItem]
}
struct AlfredItem: Codable {
var title: String
var subtitle: String
var match: String?
var arg: String?
var mods: AlfredMods?
}
struct AlfredMods: Codable {
var cmd: AlfredItemModItem?
var alt:AlfredItemModItem?
}
struct AlfredItemModItem: Codable {
var valid: Bool
var arg: String
var subtitle: String
}
}
// MARK: pretty print for Encodable
extension Encodable {
func prettyPrint() {
let encoder = JSONEncoder()
encoder.outputFormatting = .prettyPrinted
guard let data = try? encoder.encode(self) else { return }
print(String(data: data, encoding: .utf8)!)
}
}
// MARK: convert AlfredItem to AlfredResult
extension Workflow.AlfredItem {
func toAlfredResult() -> Workflow.AlfredResult {
return Workflow.AlfredResult(items: [self])
}
}
extension Array where Element == Workflow.AlfredItem {
func toAlfredResult() -> Workflow.AlfredResult {
return Workflow.AlfredResult(items: self)
}
}
// MARK: string fuzzy search
// fork from https://github.com/khoi/fuzzy-swift/blob/master/Sources/Fuzzy/Fuzzy.swift
extension String {
/// fuzzySearch string
/// return matching weight, 0 for not match, bigger for less match
func fuzzySearch(_ needle: String) -> Int {
var weight = 1
guard needle.count <= self.count else {
return 0
}
if needle == self {
return weight
}
var needleIdx = needle.startIndex
var haystackIdx = self.startIndex
while needleIdx != needle.endIndex {
if haystackIdx == self.endIndex {
return 0
}
// compare ignore case and diacritic
if String(needle[needleIdx])
.compare(String(self[haystackIdx]), options: [.caseInsensitive, .diacriticInsensitive]) == .orderedSame {
needleIdx = needle.index(after: needleIdx)
} else {
weight += 1
}
haystackIdx = self.index(after: haystackIdx)
}
return weight
}
}
// MARK: filter func for Workflow
extension Workflow {
func filter(by query: String) -> [AlfredItem] {
guard !query.isEmpty else {
return items
}
return items
.map( { ($0, $0.title.fuzzySearch(query)) })
.filter({ $0.1 > 0 })
.sorted(by: { $0.1 < $1.1 } )
.map( { $0.0 } )
}
}
// MARK: SourceTree Workflow implements
class SourceTree: Workflow {
override init() {
super.init()
emptyMessage = AlfredItem(title: "Your SourceTree Bookmark Is Empty ", subtitle: "Please add repos to SourceTree first")
guard let data = try? Data(contentsOf: Self.plistPath) else {
errorMessage = AlfredItem(title: "SourceTree not installed", subtitle: "Press enter to open SourceTree homepage and download it", arg: "open \"https://sourcetreeapp.com/\"")
return
}
do {
let parsed = try PropertyListDecoder().decode(SourceTreePlist.self, from: data)
items = parsed.toAlfredItems()
} catch {
errorMessage = Self.getErrorMessage(error)
}
}
override func run() {
let query = queryArg
if let errorMessage = errorMessage {
errorMessage.toAlfredResult().prettyPrint()
return
}
guard !items.isEmpty else {
emptyMessage.toAlfredResult().prettyPrint()
return
}
var list = filter(by: query)
var destFile = #file
// remove the possible extension
destFile = destFile.replacingOccurrences(of: ".swift", with: "")
let sourceFile = "\(destFile).swift"
let compileScript = AlfredItem(
title: "✈️Compile workflow script",
subtitle: "Compile workflow script to binary to speed up its response time",
arg: "swiftc \"\(sourceFile)\" -O -o \"\(destFile)\""
)
if !Self.isAppleChip && Self.isScript {
list.insert(compileScript, at: 0)
} else if query == "$compile" {
list.append(compileScript)
}
list.toAlfredResult().prettyPrint()
}
static func getErrorMessage(_ error: Error) -> AlfredItem {
let githubNewIssueUrl = "https://github.com/oe/sourcetree-alfred-workflow/issues/new"
var urlComponents = URLComponents(string: githubNewIssueUrl)!
let issueBody = """
error message:
\(error.localizedDescription)
environment info:
macOS version: [pleaase fill your version]
swift version: [run `swift --version` to get its version]
"""
let queryItems = [
URLQueryItem(name: "title", value: "SourceTree plist parse error"),
URLQueryItem(name: "body", value: issueBody)
]
if urlComponents.queryItems == nil {
urlComponents.queryItems = []
}
urlComponents.queryItems!.append(contentsOf: queryItems)
return AlfredItem(
title: "Error occurred",
subtitle: "Press enter to open github and report an issue to me",
arg: "open \"\(urlComponents.url?.absoluteString ?? githubNewIssueUrl)\""
)
}
/** SourceTree browser.plist path */
static var plistPath: URL {
let url = FileManager.default.homeDirectoryForCurrentUser
return url.appendingPathComponent("Library/Application Support/SourceTree/browser.plist")
}
}
// MARK: SourceTree Plist
extension SourceTree {
struct SourceTreePlist: Codable {
let version: Int
let objects: [String]
enum CodingKeys: String, CodingKey {
case version = "$version"
case objects = "$objects"
}
}
}
// MARK: Decode SourceTree Plist then parse to Alfred struct
extension SourceTree.SourceTreePlist {
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
version = try container.decode(Int.self, forKey: .version)
var objectsContainer = try container.nestedUnkeyedContainer(forKey: .objects)
var objects: [String] = []
while !objectsContainer.isAtEnd {
if let value = try? objectsContainer.decode(String.self) {
objects.append(value)
} else {
try objectsContainer.skip()
}
}
self.objects = objects
}
func toAlfredItems() -> [Workflow.AlfredItem] {
var namePathGroups: [(name: String, path: String)] = []
var name = ""
objects.forEach { str in
if str.starts(with: "/") {
if name.isEmpty {
return
}
namePathGroups.append((name: name, path: str))
name = ""
} else {
name = str
}
}
return namePathGroups.map { (name, path) in
let alt = Workflow.AlfredItemModItem(valid: true, arg: "open \"\(path)\"", subtitle: "Reveal in Finder")
// default using `code` aka VS Code to open project
let editCli = ProcessInfo.processInfo.environment["EDITOR_CLI"] ?? "code"
let cmd = Workflow.AlfredItemModItem(valid: true, arg: "\(editCli) \"\(path)\"", subtitle: "Open repo in your preferred code editor")
return Workflow.AlfredItem(title: name, subtitle: path, arg: path, mods: Workflow.AlfredMods(cmd: cmd, alt: alt))
}
}
}
/**
* add skip to unkeyed container due to this missing feature in Swift
* https://forums.swift.org/t/pitch-unkeyeddecodingcontainer-movenext-to-skip-items-in-deserialization/22151/12
*/
struct Empty: Decodable { }
extension UnkeyedDecodingContainer {
public mutating func skip() throws {
_ = try decode(Empty.self)
}
}
SourceTree().run()