-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.lua
89 lines (72 loc) · 2.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
--- === SpoonsPackageManager ===
---
--- Support
---
--- TODO: link to install Spoon
--- Download: []()
local obj = {}
obj.__index = obj
-- Metadata
obj.name = "SpoonsPackageManager"
obj.version = "1.0"
obj.author = "yehvaed <[email protected]>"
obj.homepage = "https://github.com/yehvaed/SpoonsPackageManager.spoon"
obj.license = "MIT - https://opensource.org/licenses/MIT"
--- SpoonsPackageManager.logger
--- Variable
--- Logger object used within the Spoon. Can be accessed to set the default log level for the messages coming from the Spoon.
obj.logger = hs.logger.new('SpoonsPackageManager')
-- Get path to Spoon's init.lua script
obj.spoonPath = hs.spoons.scriptPath()
-- Store id and props of spoons
obj.spoons = {}
-- helpers to check if spoon is installed
local function isSpoonInstalled(name)
return hs.spoons.isInstalled(name)
end
-- helper to convert value to bool (ex. check if object is nil)
local function bool(val)
return val and true or false
end
local function split(str, delimiter)
local t = {}
for word in str:gmatch('[^/%s]+') do
table.insert(t, word)
end
return table.unpack(t)
end
-- Place to store middlewares
obj.middlewares = {
-- middleware for spoons in github reposittory (ex. in Spoons directory)
function(id, props)
local _, name = split(id, "/")
if not isSpoonInstalled(name) and bool(props.repo) then
local url = ("https://raw.githubusercontent.com/%s/master/%s.spoon.zip"):format(props.repo, id)
hs.execute("curl -O -L " .. url)
hs.execute("unzip -o ".. name .. ".spoon.zip -d Spoons/")
hs.execute("rm ".. name .. ".spoon.zip")
end
end,
-- middleware to to create loading function
function(id, props)
table.insert(obj.spoons, function ()
local _, name = split(id, "/")
hs.spoons.use(name, {
fn = props.fn
})
end)
end
}
function obj.plug(id)
return function(props)
for _, middleware in ipairs(obj.middlewares) do
middleware(id, props)
end
end
end
function obj.bootstrap()
for _, spoon in ipairs(obj.spoons) do
spoon()
end
end
return obj