-
-
Notifications
You must be signed in to change notification settings - Fork 38
/
init.lua
360 lines (311 loc) · 11.2 KB
/
init.lua
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
--- === cp.developer ===
---
--- Developer Tools
---
--[[
hs.hotkey.bind({"cmd", "alt", "ctrl", "shift"}, "q", function()
require("cp.developer")
print(_inspectAtMouse())
end)
--]]
--------------------------------------------------------------------------------
--
-- EXTENSIONS:
--
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-- Logger:
--------------------------------------------------------------------------------
local log = require("hs.logger").new("develop")
--------------------------------------------------------------------------------
-- Hammerspoon Extensions:
--------------------------------------------------------------------------------
local ax = require("hs._asm.axuielement")
local drawing = require("hs.drawing")
local geometry = require("hs.geometry")
local inspect = require("hs.inspect")
local mouse = require("hs.mouse")
local timer = require("hs.timer")
local json = require("hs.json")
--------------------------------------------------------------------------------
-- CommandPost Extensions:
--------------------------------------------------------------------------------
local config = require("cp.config")
--------------------------------------------------------------------------------
--
-- THE MODULE:
--
--------------------------------------------------------------------------------
local dev = {}
--------------------------------------------------------------------------------
-- DESTROY DEVELOPER MODE:
--------------------------------------------------------------------------------
function dev.destroyDeveloperMode()
for k,_ in pairs(dev) do
_G[k] = nil
end
package.loaded["cp.developer"] = nil
end
--------------------------------------------------------------------------------
-- DEVELOPER SHORTCUTS FOR USE IN ERROR LOG:
--------------------------------------------------------------------------------
dev._plugins = require("cp.plugins")
dev._fcp = require("cp.apple.finalcutpro")
--------------------------------------------------------------------------------
-- FIND UNUSED LANGUAGES STRINGS:
--------------------------------------------------------------------------------
function dev._findUnusedLanguageStrings()
local path = config.languagePath .. "English.json"
local data = io.open(path, "r")
local content, decoded
if data then
content = data:read("*all")
data:close()
end
if content then
decoded = json.decode(content)
end
local translations = decoded["en"]
local result = "\nUNUSED STRINGS IN English.json:\n"
local stringCount = 0
local ignoreStart = {"plugin_group_", "shareDetails_", "plugin_status_", "plugin_action_", "shortcut_group_"}
local ignoreEnd = {"_action", "_label", "_title", "_customTitle", "_group"}
for string, _ in pairs(translations) do
local skip = false
for _, ignoreFile in pairs(ignoreStart) do
if string.sub(string, 1, string.len(ignoreFile)) == ignoreFile then
skip = true
end
end
for _, ignoreFile in pairs(ignoreEnd) do
if string.sub(string, string.len(ignoreFile) * -1) == ignoreFile then
skip = true
end
end
if not skip then
local executeString = [[grep -r --max-count=1 --exclude-dir=resources --include \*.html --include \*.htm --include \*.lua ']] .. string .. [[' ']] .. hs.processInfo.bundlePath .. [[/']]
local _, status = hs.execute(executeString)
if not status then
result = result .. string .. "\n"
stringCount = stringCount + 1
end
end
end
if stringCount == 0 then
result = result .. "None"
end
log.df(result)
end
--------------------------------------------------------------------------------
-- FIND TEXT:
--------------------------------------------------------------------------------
local whiches = {}
function dev._which(cmd)
local path = whiches[cmd]
if not path then
local output, ok = hs.execute(string.format("which %q", cmd))
if ok then
path = output:match("([^\r\n]*)")
whiches[cmd] = path
else
return nil, output
end
end
return path
end
--------------------------------------------------------------------------------
-- ELEMENT AT MOUSE:
--------------------------------------------------------------------------------
function dev._elementAtMouse()
return ax.systemElementAtPosition(mouse.getAbsolutePosition())
end
--------------------------------------------------------------------------------
-- INSPECT ELEMENT AT MOUSE:
--------------------------------------------------------------------------------
function dev._inspectAtMouse(options)
options = options or {}
local element = _G._elementAtMouse()
if options.parents then
for _=1,options.parents do
element = element ~= nil and element:parent()
end
end
if element then
local result = ""
if options.type == "path" then
local path = element:path()
for i,e in ipairs(path) do
result = result .. _G._inspectElement(e, options, i)
end
return result
else
return inspect(element:buildTree(options.depth))
end
else
return "<no element found>"
end
end
--------------------------------------------------------------------------------
-- INSPECT:
--------------------------------------------------------------------------------
function dev._inspect(e, options)
if e == nil then
return "<nil>"
elseif type(e) ~= "userdata" or not e.attributeValue then
if type(e) == "table" and #e > 0 then
local item
local result = ""
for i=1,#e do
item = e[i]
result = result ..
"\n= " .. string.format("%3d", i) ..
" ========================================" ..
_G._inspect(item, options)
end
return result
else
return inspect(e, options)
end
else
return "\n==============================================" ..
_G._inspectElement(e, options)
end
end
--------------------------------------------------------------------------------
-- INSPECT ELEMENT:
--------------------------------------------------------------------------------
function dev._inspectElement(e, options)
dev._highlight(e)
local depth = options and options.depth or 1
local out = "\n Role = " .. inspect(e:attributeValue("AXRole"))
local id = e:attributeValue("AXIdentifier")
if id then
out = out.. "\n Identifier = " .. inspect(id)
end
out = out.. "\n Children = " .. inspect(#e)
out = out.. "\n==============================================" ..
"\n" .. inspect(e:buildTree(depth)) .. "\n"
return out
end
--------------------------------------------------------------------------------
-- HIGHLIGHT ELEMENT:
--------------------------------------------------------------------------------
function dev._highlight(e)
if not e or not e.frame then
return e
end
local eFrame = e:frame()
if eFrame then
eFrame = geometry.rect(eFrame)
else
return e
end
--------------------------------------------------------------------------------
-- Get Highlight Colour Preferences:
--------------------------------------------------------------------------------
local highlightColor = {["red"]=1,["blue"]=0,["green"]=0,["alpha"]=0.75}
local highlight = drawing.rectangle(eFrame)
highlight:setStrokeColor(highlightColor)
highlight:setFill(false)
highlight:setStrokeWidth(3)
highlight:show()
--------------------------------------------------------------------------------
-- Set a timer to delete the highlight after 3 seconds:
--------------------------------------------------------------------------------
timer.doAfter(3,
function()
highlight:delete()
end)
return e
end
local SIZE = 100
function dev._highlightPoint(point)
--------------------------------------------------------------------------------
-- Get Highlight Colour Preferences:
--------------------------------------------------------------------------------
local hColor = {red=1, blue=0, green=0, alpha=0.75}
local vert = drawing.line({x=point.x, y=point.y-SIZE}, {x=point.x, y=point.y+SIZE})
vert:setStrokeColor(hColor)
vert:setFill(false)
vert:setStrokeWidth(1)
local horiz = drawing.line({x=point.x-SIZE, y=point.y}, {x=point.x+SIZE, y=point.y})
horiz:setStrokeColor(hColor)
horiz:setFill(false)
horiz:setStrokeWidth(1)
vert:show()
horiz:show()
--------------------------------------------------------------------------------
-- Set a timer to delete the highlight after 10 seconds:
--------------------------------------------------------------------------------
timer.doAfter(10,
function()
vert:delete()
horiz:delete()
end)
end
--------------------------------------------------------------------------------
-- INSPECT ELEMENT AT MOUSE PATH:
--------------------------------------------------------------------------------
function dev._inspectElementAtMousePath()
return inspect(_G._elementAtMouse():path())
end
-- _test(id) -> cp.test
-- Function
-- This function will return a [cp.test](cp.test.md) with either the
-- name `<id>_test` or `<id>._test` if the `<id>` is pointing at a folder.
--
-- For example, you have an extensions called
-- `foo.bar`, and you want to create a test for it.
--
-- Option 1: `<id>_test`
-- * File: `/src/tests/foo/bar_test.lua`
--
-- Option 2: `<id>._test`
-- * File: `/src/tests/foo/bar/_test.lua`
--
-- You could then run all the contained tests like so:
-- ```lua
-- _test("foo.bar")()
-- ```
--
-- Parameters:
-- * id - the `id` to test.
--
-- Returns:
-- * A [cp.test] to execute.
function dev._test(id)
id = id or ""
local testsRoot = config.testsPath
if not testsRoot then
error "Unable to locate the test scripts."
end
local testPath = testsRoot .. "/?.lua;" .. testsRoot .. "/?/init.lua"
local testId = id .. "_test"
if not package.searchpath(testId, testPath) then
if package.searchpath(id .. "._test", testPath) then
testId = id .. "._test"
else
error(string.format("Unable to find tests for '%s'", id))
end
end
local originalPath = package.path
local tempPath = testPath .. ";" .. originalPath
package.path = tempPath
local ok, result = xpcall(function() return require(testId) end, debug.traceback)
package.path = originalPath
if not ok then
error(result)
else
return result
end
end
function dev._debug(port)
port = port or 21110
dev.lrdb = require("lrdb_server")
dev.lrdb.activate(port)
end
-- apply dev functions to the global scope.
for k,v in pairs(dev) do
_G[k] = v
end
return dev