Skip to content

Commit 6c14ecd

Browse files
authored
Merge pull request #32 from mc-cc-scripts/Issue-30-Spilt-SCM
Issue 30 spilt scm
2 parents 9a73db1 + f49b5f6 commit 6c14ecd

File tree

18 files changed

+1828
-1014
lines changed

18 files changed

+1828
-1014
lines changed

.busted

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
return {
2+
_all = {
3+
coverage = false
4+
},
5+
default = {
6+
verbose = true
7+
},
8+
apiUnit = {
9+
tags = "api",
10+
ROOT = {"tests"},
11+
verbose = false
12+
}
13+
}

.github/workflows/test.yml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
name: Busted
2+
3+
on: [push, pull_request]
4+
5+
jobs:
6+
test:
7+
runs-on: ubuntu-latest
8+
9+
steps:
10+
- name: checkout
11+
uses: actions/checkout@v2
12+
13+
- name: get lua
14+
uses: leafo/gh-actions-lua@v10
15+
with:
16+
luaVersion: "5.1"
17+
18+
- name: get luarocks
19+
uses: leafo/gh-actions-luarocks@v4
20+
with:
21+
luaVersion: "5.1"
22+
23+
- name: get busted and luasocket
24+
run: |
25+
luarocks install busted
26+
luarocks install luasocket
27+
luarocks install luasec
28+
29+
- name: test
30+
run: |
31+
busted .

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
1-
libs/
21
config/
32
TODO

files.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
scm.lua
2+
/libs/scm/autocomplete.lua
3+
/libs/scm/config.lua
4+
/libs/scm/log.lua
5+
/libs/scm/net.lua
6+
/libs/scm/scriptManager.lua
7+
/libs/scm/ui.lua

libs/scm/autocomplete.lua

Lines changed: 242 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,242 @@
1+
---@class Autocomplete
2+
local Autocomplete = {}
3+
SCM.Autocomplete = Autocomplete
4+
5+
do
6+
local log = function(...) SCM.Log:log(...) end
7+
Autocomplete.commands = {
8+
["require"] = {
9+
---@param args table
10+
func = function(args)
11+
SCM.Net:download(args[2], "library", nil)
12+
end,
13+
description = [[
14+
Adds a library with all its dependencies.
15+
If only a name is given, it will try to download from the official GitHub repositories.
16+
$ require <name>
17+
$ require <name>@<pastebinCode>
18+
]]
19+
},
20+
["add"] = {
21+
---@param args table
22+
func = function(args)
23+
SCM.Net:download(args[2], "program", nil)
24+
end,
25+
description = [[
26+
Adds a program with all its dependencies.
27+
If only a name is given, it will try to download from the official GitHub repositories.
28+
$ add <name>
29+
$ add <name>@<pastebinCode>
30+
]]
31+
},
32+
["update"] = {
33+
---@param args table
34+
func = function(args)
35+
if args[2] == "all" then
36+
SCM.ScriptManager:updateAllScripts()
37+
elseif args[3] then
38+
SCM.ScriptManager:updateScript(args[2], args[3])
39+
elseif args[2] then
40+
SCM.ScriptManager:updateScript(args[2], nil)
41+
else
42+
SCM.Net:updateSCM()
43+
end
44+
end,
45+
description = [[
46+
$ update
47+
Updates this program (SCM)
48+
$ update <name>
49+
Updates the script with the given name
50+
$ update all
51+
Updates all installed programs and libraries
52+
$ update <name> <srcName>
53+
Updates the script with an specific source
54+
]]
55+
},
56+
["remove"] = {
57+
---@param args table
58+
func = function(args)
59+
if args[2] == "all" then
60+
SCM.ScriptManager:removeAllScripts()
61+
else
62+
SCM.ScriptManager:removeScript(args[2])
63+
end
64+
end,
65+
description = [[
66+
$ remove <name>
67+
Removes the given script
68+
$ remove all
69+
Removes all scripts
70+
]]
71+
},
72+
["list"] = {
73+
---@param _ table
74+
func = function(_)
75+
SCM.UI:listScripts()
76+
end,
77+
description = [[
78+
$ list
79+
Lists all installed scripts
80+
]]
81+
},
82+
["config"] = {
83+
---@param args table
84+
func = function(args)
85+
if args[3] then
86+
SCM.Config:updateConfig(args[2], args[3])
87+
elseif args[2] then
88+
if SCM.Config.getAll(SCM.Config)[args[2]] ~= nil then
89+
print(args[2], tostring(SCM.Config.getAll(SCM.Config)[args[2]]))
90+
end
91+
else
92+
print("You can currently configure the following variables:")
93+
for cname, cvalue in pairs(SCM.Config.getAll(SCM.Config)) do
94+
textutils.pagedPrint(cname .. "\t" .. tostring(cvalue))
95+
end
96+
end
97+
end,
98+
description = [[
99+
$ config
100+
Lists all available configurations
101+
$ config <name>
102+
Shows a specific configuration
103+
$ config <name> <value>
104+
Updates the configuration
105+
]]
106+
},
107+
["refresh"] = {
108+
func = function(args)
109+
SCM.Autocomplete:refreshAutocomplete()
110+
end,
111+
description = [[
112+
$ refresh
113+
Downloads the names of all programs and libraries of the official repository.
114+
Refreshes autocomplete.
115+
]]
116+
},
117+
["help"] = {
118+
---@param args table
119+
func = function(args)
120+
if args[2] then
121+
if SCM.Autocomplete.commands[args[2]] then
122+
textutils.pagedPrint(args[2] .. "\n" .. SCM.Autocomplete.commands[args[2]]["description"])
123+
end
124+
else
125+
for k, v in pairs(SCM.Autocomplete.commands) do
126+
textutils.pagedPrint("# " .. k .. "\n" .. v.description)
127+
end
128+
end
129+
end,
130+
description = [[
131+
$ help
132+
Shows all available commands and their description
133+
$ help <name>
134+
Shows the description of the given command
135+
]]
136+
}
137+
}
138+
139+
function Autocomplete:addScriptToAutoComplete(source)
140+
self.commands["update"]["args"] = self.commands["update"]["args"] or {}
141+
self.commands["remove"]["args"] = self.commands["remove"]["args"] or {}
142+
self.commands["update"]["args"][source.name] = {}
143+
self.commands["remove"]["args"][source.name] = {}
144+
end
145+
146+
function Autocomplete:prepareAutocomplete()
147+
-- prepare update and remove
148+
SCM.ScriptManager:loadScripts()
149+
local installedScripts = {}
150+
for i = 1, #SCM.ScriptManager.scripts, 1 do
151+
installedScripts[SCM.ScriptManager.scripts[i].name] = {}
152+
end
153+
installedScripts["all"] = {}
154+
155+
self.commands["update"]["args"] = installedScripts
156+
self.commands["remove"]["args"] = installedScripts
157+
158+
-- prepare add and require
159+
SCM.Net:loadRepoScripts()
160+
161+
-- prepare config
162+
local availableConfigs = {}
163+
164+
for k, _ in pairs(SCM.Config:getAll()) do
165+
availableConfigs[k] = {}
166+
end
167+
168+
self.commands["config"]["args"] = availableConfigs
169+
170+
-- prepare help
171+
local availableCommands = {}
172+
173+
for k, _ in pairs(self.commands) do
174+
availableCommands[k] = {}
175+
end
176+
177+
self.commands["help"]["args"] = availableCommands
178+
end
179+
180+
---@param shell table
181+
---@param index integer
182+
---@param argument string
183+
---@param previous table
184+
---@return table | nil
185+
local function completionFunction(shell, index, argument, previous)
186+
local commands = {}
187+
for k, _ in pairs(Autocomplete.commands) do
188+
commands[k] = Autocomplete.commands[k]["args"] or {}
189+
end
190+
191+
local currArg = commands
192+
for i = 2, #previous do
193+
if currArg[previous[i]] then
194+
currArg = currArg[previous[i]]
195+
else
196+
return nil
197+
end
198+
end
199+
200+
local results = {}
201+
for word, _ in pairs(currArg) do
202+
if word:sub(1, #argument) == argument then
203+
results[#results + 1] = word:sub(#argument + 1)
204+
end
205+
end
206+
return results;
207+
end
208+
209+
function Autocomplete:updateAutocomplete()
210+
shell.setCompletionFunction("scm.lua", completionFunction)
211+
end
212+
213+
function Autocomplete:refreshAutocomplete()
214+
SCM.Net:refreshRepoScripts()
215+
self:prepareAutocomplete()
216+
self:updateAutocomplete()
217+
end
218+
219+
---@param t table
220+
function Autocomplete:setPrograms(t)
221+
self.commands["add"]["args"] = t
222+
end
223+
224+
---@param t table
225+
function Autocomplete:setLibaries(t)
226+
self.commands["require"]["args"] = t
227+
end
228+
229+
function Autocomplete:handleArguments(args)
230+
if #args == 0 then
231+
SCM.UI:cli(false, args)
232+
return
233+
end
234+
235+
if args[1] and self.commands[args[1]] then
236+
self.commands[args[1]]["func"](args)
237+
if SCM.Config.config["cliPrefix"] then
238+
shell.run(read(nil, nil, shell.complete, "scm "))
239+
end
240+
end
241+
end
242+
end

0 commit comments

Comments
 (0)