-
Notifications
You must be signed in to change notification settings - Fork 2
/
workflow.swift
executable file
·398 lines (331 loc) · 11.2 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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
#!/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 Self.isSourceTreeInstalled() else {
errorMessage = AlfredItem(title: "SourceTree not installed", subtitle: "Press enter to open SourceTree homepage and download it", arg: "open \"https://sourcetreeapp.com/\"")
return
}
guard let data = try? Data(contentsOf: Self.plistPath) else {
errorMessage = emptyMessage
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.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")
}
static func isSourceTreeInstalled() -> Bool {
return FileManager.default.fileExists(atPath: "/Applications/SourceTree.app")
}
}
// 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 = Self.parseEditorCliConfig(with: path)
let cmd = Workflow.AlfredItemModItem(valid: true, arg: "\(editCli) \"\(path)\"", subtitle: "Open in code editor")
return Workflow.AlfredItem(title: name, subtitle: path, arg: path, mods: Workflow.AlfredMods(cmd: cmd, alt: alt))
}
}
// parse configurations
// support comments(start with #)
private static let editConfigs: [(cli: String, extensions: [String])] = {
let editorCli = ProcessInfo.processInfo.environment["EDITOR_CLI"] ?? "code"
let defaultEditorCliConfig = """
\(editorCli)=*
"""
let editorCliConfig = ProcessInfo.processInfo.environment["EDITOR_CLI_CONFIG"] ?? defaultEditorCliConfig
let lines = editorCliConfig.components(separatedBy: .newlines)
.map { $0.trimmingCharacters(in: .whitespacesAndNewlines) }
// remove empty lines and comments(start with #)
.filter { !$0.starts(with: "#") && !$0.isEmpty }
let components = lines.map { $0.components(separatedBy: "=") }
.filter { $0.count == 2 }
return components.map {
// sanitize cli and extensions
let cli = $0[0].trimmingCharacters(in: .whitespacesAndNewlines)
let exts = $0[1].trimmingCharacters(in: .whitespacesAndNewlines)
.components(separatedBy: ",")
// lowercase for case insensitive compare
.map { $0.trimmingCharacters(in: .whitespacesAndNewlines).lowercased() }
.filter { !$0.isEmpty }
return (cli, exts)
}
.filter { !$0.1.isEmpty }
}()
private static let defaultEditorCli: String = {
for (cli, extensions) in editConfigs {
if extensions[0] == "*" {
return cli
}
}
// if we didn't find anything then we just return "code"
return "code"
}()
private static func parseEditorCliConfig(with path: String) -> String {
// cache enumerated files
var files: [String] = []
for (cli, extensions) in editConfigs {
if extensions[0] == "*" {
return cli
}
if !files.isEmpty {
for file in files {
if extensions.contains(where: { file.hasSuffix($0) }) {
return cli
}
}
continue
}
let fileManager = FileManager.default
let enumerator = fileManager.enumerator(atPath: path)
while let element = enumerator?.nextObject() as? String {
// search with lowercased
let file = element.lowercased()
if extensions.contains(where: { file.hasSuffix($0) }) {
return cli
}
files.append(file)
// only check the top level files
enumerator?.skipDescendants()
}
}
return defaultEditorCli
}
}
/**
* 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()