Skip to content

Commit

Permalink
Merge pull request #32 from mc-cc-scripts/Issue-30-Spilt-SCM
Browse files Browse the repository at this point in the history
Issue 30 spilt scm
  • Loading branch information
ChristophLHR authored Dec 7, 2023
2 parents 9a73db1 + f49b5f6 commit 6c14ecd
Show file tree
Hide file tree
Showing 18 changed files with 1,828 additions and 1,014 deletions.
13 changes: 13 additions & 0 deletions .busted
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
return {
_all = {
coverage = false
},
default = {
verbose = true
},
apiUnit = {
tags = "api",
ROOT = {"tests"},
verbose = false
}
}
31 changes: 31 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: Busted

on: [push, pull_request]

jobs:
test:
runs-on: ubuntu-latest

steps:
- name: checkout
uses: actions/checkout@v2

- name: get lua
uses: leafo/gh-actions-lua@v10
with:
luaVersion: "5.1"

- name: get luarocks
uses: leafo/gh-actions-luarocks@v4
with:
luaVersion: "5.1"

- name: get busted and luasocket
run: |
luarocks install busted
luarocks install luasocket
luarocks install luasec
- name: test
run: |
busted .
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
libs/
config/
TODO
7 changes: 7 additions & 0 deletions files.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
scm.lua
/libs/scm/autocomplete.lua
/libs/scm/config.lua
/libs/scm/log.lua
/libs/scm/net.lua
/libs/scm/scriptManager.lua
/libs/scm/ui.lua
242 changes: 242 additions & 0 deletions libs/scm/autocomplete.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,242 @@
---@class Autocomplete
local Autocomplete = {}
SCM.Autocomplete = Autocomplete

do
local log = function(...) SCM.Log:log(...) end
Autocomplete.commands = {
["require"] = {
---@param args table
func = function(args)
SCM.Net:download(args[2], "library", nil)
end,
description = [[
Adds a library with all its dependencies.
If only a name is given, it will try to download from the official GitHub repositories.
$ require <name>
$ require <name>@<pastebinCode>
]]
},
["add"] = {
---@param args table
func = function(args)
SCM.Net:download(args[2], "program", nil)
end,
description = [[
Adds a program with all its dependencies.
If only a name is given, it will try to download from the official GitHub repositories.
$ add <name>
$ add <name>@<pastebinCode>
]]
},
["update"] = {
---@param args table
func = function(args)
if args[2] == "all" then
SCM.ScriptManager:updateAllScripts()
elseif args[3] then
SCM.ScriptManager:updateScript(args[2], args[3])
elseif args[2] then
SCM.ScriptManager:updateScript(args[2], nil)
else
SCM.Net:updateSCM()
end
end,
description = [[
$ update
Updates this program (SCM)
$ update <name>
Updates the script with the given name
$ update all
Updates all installed programs and libraries
$ update <name> <srcName>
Updates the script with an specific source
]]
},
["remove"] = {
---@param args table
func = function(args)
if args[2] == "all" then
SCM.ScriptManager:removeAllScripts()
else
SCM.ScriptManager:removeScript(args[2])
end
end,
description = [[
$ remove <name>
Removes the given script
$ remove all
Removes all scripts
]]
},
["list"] = {
---@param _ table
func = function(_)
SCM.UI:listScripts()
end,
description = [[
$ list
Lists all installed scripts
]]
},
["config"] = {
---@param args table
func = function(args)
if args[3] then
SCM.Config:updateConfig(args[2], args[3])
elseif args[2] then
if SCM.Config.getAll(SCM.Config)[args[2]] ~= nil then
print(args[2], tostring(SCM.Config.getAll(SCM.Config)[args[2]]))
end
else
print("You can currently configure the following variables:")
for cname, cvalue in pairs(SCM.Config.getAll(SCM.Config)) do
textutils.pagedPrint(cname .. "\t" .. tostring(cvalue))
end
end
end,
description = [[
$ config
Lists all available configurations
$ config <name>
Shows a specific configuration
$ config <name> <value>
Updates the configuration
]]
},
["refresh"] = {
func = function(args)
SCM.Autocomplete:refreshAutocomplete()
end,
description = [[
$ refresh
Downloads the names of all programs and libraries of the official repository.
Refreshes autocomplete.
]]
},
["help"] = {
---@param args table
func = function(args)
if args[2] then
if SCM.Autocomplete.commands[args[2]] then
textutils.pagedPrint(args[2] .. "\n" .. SCM.Autocomplete.commands[args[2]]["description"])
end
else
for k, v in pairs(SCM.Autocomplete.commands) do
textutils.pagedPrint("# " .. k .. "\n" .. v.description)
end
end
end,
description = [[
$ help
Shows all available commands and their description
$ help <name>
Shows the description of the given command
]]
}
}

function Autocomplete:addScriptToAutoComplete(source)
self.commands["update"]["args"] = self.commands["update"]["args"] or {}
self.commands["remove"]["args"] = self.commands["remove"]["args"] or {}
self.commands["update"]["args"][source.name] = {}
self.commands["remove"]["args"][source.name] = {}
end

function Autocomplete:prepareAutocomplete()
-- prepare update and remove
SCM.ScriptManager:loadScripts()
local installedScripts = {}
for i = 1, #SCM.ScriptManager.scripts, 1 do
installedScripts[SCM.ScriptManager.scripts[i].name] = {}
end
installedScripts["all"] = {}

self.commands["update"]["args"] = installedScripts
self.commands["remove"]["args"] = installedScripts

-- prepare add and require
SCM.Net:loadRepoScripts()

-- prepare config
local availableConfigs = {}

for k, _ in pairs(SCM.Config:getAll()) do
availableConfigs[k] = {}
end

self.commands["config"]["args"] = availableConfigs

-- prepare help
local availableCommands = {}

for k, _ in pairs(self.commands) do
availableCommands[k] = {}
end

self.commands["help"]["args"] = availableCommands
end

---@param shell table
---@param index integer
---@param argument string
---@param previous table
---@return table | nil
local function completionFunction(shell, index, argument, previous)
local commands = {}
for k, _ in pairs(Autocomplete.commands) do
commands[k] = Autocomplete.commands[k]["args"] or {}
end

local currArg = commands
for i = 2, #previous do
if currArg[previous[i]] then
currArg = currArg[previous[i]]
else
return nil
end
end

local results = {}
for word, _ in pairs(currArg) do
if word:sub(1, #argument) == argument then
results[#results + 1] = word:sub(#argument + 1)
end
end
return results;
end

function Autocomplete:updateAutocomplete()
shell.setCompletionFunction("scm.lua", completionFunction)
end

function Autocomplete:refreshAutocomplete()
SCM.Net:refreshRepoScripts()
self:prepareAutocomplete()
self:updateAutocomplete()
end

---@param t table
function Autocomplete:setPrograms(t)
self.commands["add"]["args"] = t
end

---@param t table
function Autocomplete:setLibaries(t)
self.commands["require"]["args"] = t
end

function Autocomplete:handleArguments(args)
if #args == 0 then
SCM.UI:cli(false, args)
return
end

if args[1] and self.commands[args[1]] then
self.commands[args[1]]["func"](args)
if SCM.Config.config["cliPrefix"] then
shell.run(read(nil, nil, shell.complete, "scm "))
end
end
end
end
Loading

0 comments on commit 6c14ecd

Please sign in to comment.