Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- (C) PROMETHEUS TECHNLOGIES 2022
- pccpkg.lua v0.1
- Prometheus Technologies Computer Craft Package Manager
- ]]
- local pb = require("pastebinlib")
- local REPO_PB_CODE = "t1n53vfw"
- local PCCPKG_DIR = "/pccpkg"
- local REPO_FILENAME = PCCPKG_DIR.."/repo.list"
- local INSTALLED_LIST_FILENAME = PCCPKG_DIR.."/installed.list"
- if not fs.exists(PCCPKG_DIR) then
- fs.makeDir(PCCPKG_DIR)
- end
- if not fs.exists(REPO_FILENAME) then
- print("WARNING: LOCAL REPOSITORY INDEX MISSING!")
- io.write("FETCHING FROM REMOTE...")
- local f = io.open(REPO_FILENAME,"w")
- f:write(pb.get(REPO_PB_CODE))
- f:flush()
- f:close()
- print(" DONE!")
- end
- if not fs.exists(INSTALLED_LIST_FILENAME) then
- io.open(INSTALLED_LIST_FILENAME,"w"):close()
- end
- local header = [[Copyright (C) 2022 Prometheus Technologies
- pccpkg.lua v0.1
- Prometheus Technologies ComputerCraft Package Manager
- ]]
- local helptxt = [[Usage: pccpkg [command] [options]
- Most commonly used commands:
- update: updates local copy of repository from remote
- install: installs (or updates) package from remote
- list: list packages using regex (followed by 'any' or 'local' and then by quoted regex)]]
- local function printHelp()
- print(header)
- print(helptxt)
- end
- if #arg==0 then
- printHelp()
- return
- end
- if arg[1]=="help" then
- printHelp()
- return
- end
- if arg[1]=="update" then
- io.write("Downloading repository data...")
- local f = io.open(REPO_FILENAME,"w")
- f:write(pb.get(REPO_PB_CODE))
- f:flush()
- f:close()
- print(" Success")
- print("DONE!")
- return
- end
- local fullrepo = loadfile(REPO_FILENAME)()
- local packages = fullrepo.packages
- local function installPackage(id)
- for p in io.lines(INSTALLED_LIST_FILENAME) do
- if id==p then
- print("Package is already installed!\nExiting...")
- return
- end
- end
- io.write(string.format("Installing package '%s'...\nFetching manifest...",id))
- local pd = fs.combine(PCCPKG_DIR,id)
- fs.makeDir(pd)
- local mf = fs.combine(pd,"manifest")
- local f = io.open(mf,"w")
- f:write(pb.get(packages[id]))
- f:flush()
- f:close()
- io.write(" Done!\nRunning installation script...")
- local s,mf = pcall(loadfile(mf))
- if not s then print(string.format("ERROR!\nERROR PARSING MANIFEST: %s\nABORTING...",tostring(mf))) end
- if (type(mf)~="table")or (type(mf.install)~="function") then io.write(" ERROR!\nBAD MANIFEST FILE!\nABORTING!!!\n") end
- local s,e = pcall(mf.install,pb,PCCPKG_DIR,pd)
- if not s then print(string.format("ERROR RUNNING INSTALLATION SCRIPT: %s\nABORTING...",tostring(e))) end
- print(string.format(" Done!\nPackage '%s' successfully installed!",id))
- local i = io.open(INSTALLED_LIST_FILENAME,"a")
- i:write("\n"..tostring(id))
- i:flush()
- i:close()
- end
- local function removePackage(id)
- io.write(string.format("Removing package '%s'...\nReading manifest...",id))
- local pd = fs.combine(PCCPKG_DIR,id)
- if not fs.exists(pd) then print(" ERROR!\nNO SUCH PACKAGE INSTALLATION FOUND!")return end
- local mf = fs.combine(pd,"manifest")
- if not fs.exists(mf) then print(" ERROR!\nERROR MISSING MANIFEST FILE!") end
- local s,v = pcall(loadfile(mf))
- if not s then io.write(" ERROR!\nERROR PARSING MANIFEST FILE:\n"..tostring(v).."\nABORTING!!!\n") end
- if (type(v)~="table")or(type(v.remove)~="function") then io.write(" ERROR!\nBAD MANIFEST FILE!\nABORTING!!!\n") end
- local s,e = pcall(v.remove,pb,PCCPKG_DIR,pd)
- if not s then io.write(string.format("ERROR RUNNING REMOVAL SCRIPT: %s\nABORTING!!!\n",tostring(e))) end
- fs.delete(pd)
- io.write(string.format(" Done!\nPackage '%s' successfully removed!\n",id))
- end
- if arg[1]=="install" then
- if arg[2]==nil then print("SPECIFY PACKAGE TO INSTALL!")return -1 end
- if packages[arg[2]]==nil then print("NO SUCH PACKAGE FOUND! Try `pccpkg update`...")return -1 end
- installPackage(arg[2])
- end
- if arg[1]=="remove" then
- if arg[2]==nil then print("SPECIFY PACKAGE TO REMOVE!")return -1 end
- removePackage(arg[2])
- end
- if arg[1]=="list" then
- arg[2]=arg[2]or"any"
- arg[3] = arg[3]or".+"
- if arg[2]=="any" then
- print("Listing all available packages...")
- for i,_ in pairs(packages) do if string.match(i,arg[3]) then print(i) end end
- elseif arg[2]=="local" then
- print("Listing locally installed packages...")
- for v in io.lines(INSTALLED_LIST_FILENAME) do if true or string.match(v,arg[3]) then print(v) end end
- end
- print("Done!")
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement