Last active
December 21, 2015 10:59
-
-
Save Tyderion/6296195 to your computer and use it in GitHub Desktop.
Computercraf package Manager
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Main Program taken from Computercraft mod | |
local upload_argument_name = "publish" | |
local install_argument_name = "install" | |
local update_argument_name = "update" | |
local list_argument_name = "list" | |
local program_name = fs.getName(shell.getRunningProgram()) -- "cpm" | |
local saveFileName = "listOfPrograms" | |
local program_code = "wP9VAie2" | |
local pastebin = {} | |
local installedPrograms = {} | |
-- Todo: Create repository-object with get/set functions to interface to turtlescripts and to pastebin | |
-- Todo: Create nodejs (or somethingn similar) repository-server because I think turtlescripts is not enough | |
local function printUsage() | |
print( "Usages:" ) | |
print( program_name .. " " .. install_argument_name .. " <code> <filename>" ) | |
print( program_name .. " " .. install_argument_name .. " <filename>" ) | |
print( program_name .. " " .. update_argument_name .. " [<filename>]" ) | |
print( program_name .. " " .. upload_argument_name .. " <filename>" ) | |
print( program_name .. " " .. list_argument_name .. " [installed]") | |
end | |
local function loadInstalledPrograms() | |
if fs.exists(saveFileName) then | |
local file = fs.open(saveFileName, "r") | |
for line in file.readLine do | |
for name,code in string.gmatch(line, "([^=]+)=([^=]+)") do | |
if shell.resolveProgram(name) == nil then | |
print(name .. " is not installed anymore") | |
else | |
installedPrograms[name] = code | |
end | |
end | |
end | |
file.close() | |
end | |
end | |
local function saveInstalledPrograms() | |
local file = fs.open(saveFileName, "w") | |
for name, code in pairs(installedPrograms) do | |
file.writeLine(name.."="..code) | |
end | |
file.close() | |
end | |
local function saveNewProgram(name, code, text) | |
--print(name) | |
--print(code) | |
--print("Adding " .. name .. " as " .. code .. " to the table length: " .. #installedPrograms) | |
installedPrograms[name] = code | |
--print("Now length should be 1 bigger: " .. #installedPrograms) | |
--print(name .. ": " .. installedPrograms[name]) | |
local path = shell.resolve( name ) | |
if not fs.exists(path) then | |
local file = fs.open( path, "w" ) | |
file.write( text ) | |
file.close() | |
end | |
end | |
local function upload(sFile) | |
-- Upload a file to pastebin.com | |
local sPath = shell.resolve( sFile ) | |
-- Determine file to upload | |
if not fs.exists( sPath ) or fs.isDir( sPath ) then | |
print( "No such file" ) | |
return | |
end | |
-- Read in the file | |
local sName = fs.getName( sPath ) | |
local file = fs.open( sPath, "r" ) | |
local sText = file.readAll() | |
file.close() | |
-- POST the contents to pastebin | |
write( "Connecting to pastebin.com... " ) | |
local key = "0ec2eb25b6166c0c27a394ae118ad829" | |
local response = http.post( | |
"http://pastebin.com/api/api_post.php", | |
"api_option=paste&".. | |
"api_dev_key="..key.."&".. | |
"api_paste_format=lua&".. | |
"api_paste_name="..textutils.urlEncode(sName).."&".. | |
"api_paste_code="..textutils.urlEncode(sText) | |
) | |
if response then | |
print( "Success." ) | |
local sResponse = response.readAll() | |
response.close() | |
local sCode = string.match( sResponse, "[^/]+$" ) | |
print( "Uploaded as "..sResponse ) | |
print( "Run \"pastebin get "..sCode.."\" to download anywhere" ) | |
saveNewProgram(sFile, sCode, sText) | |
else | |
print( "Failed." ) | |
end | |
end | |
local function get(url) | |
local response = http.get(url) | |
if response then | |
local text = response.readAll() | |
response.close() | |
return text | |
end | |
return "Failed" | |
end | |
local function listRemote() | |
local text = get("http://www.is-a-geek.ch:3000/api/v1/list") | |
if text == "Failed" then | |
print("Failed to get list from remote.") | |
else | |
local array = json.parseArray(text) | |
print("Available Programs:") | |
for _,name in ipairs(array) do | |
if name ~= "cpm" then | |
print(name) | |
end | |
end | |
--print(text) | |
end | |
end | |
local function listInstalled() | |
print("Installed Programs:") | |
for name, _ in pairs(installedPrograms) do | |
print(name) | |
end | |
end | |
local function getFromPastebin(code) | |
return get("http://pastebin.com/raw.php?i=".. textutils.urlEncode( code ) ) | |
end | |
local function getFromGithub(code) | |
--print("Trying to get " .. "https://gist.github.com/".. code .. "/raw/") | |
return get("https://gist.github.com/".. code .. "/raw/" ) | |
-- return get("https://gist.github.com/".. textutils.urlEncode( code ) .. "/raw/" ) | |
end | |
local function install(name, text,code, output) | |
local path = shell.resolve( name ) | |
if fs.exists( path ) then | |
print( "File already exists" ) | |
return false | |
end | |
print( output.download ) | |
if text == "Failed" then | |
print(output.failed) | |
return false | |
else | |
saveNewProgram(name, code, text) | |
print(output.saved) | |
return true | |
end | |
end | |
local function installFromPastebin(code, name, output) | |
return install(name, getFromPastebin(code),code, output) | |
end | |
local function installFromGithub(code, name, output) | |
return install(name, getFromGithub(code),code, output) | |
end | |
local function splitCode(code) | |
return string.sub(code,1,1), string.sub(code,3) | |
end | |
local function installByName( name, output) | |
local text = get("http://www.is-a-geek.ch:3000/api/v1/getSimple/:" .. name) | |
if (text == "Failed") or (text == "Does not exist") then | |
print(text) | |
else | |
local provider,code = splitCode(text) | |
if provider == "p" then | |
return installFromPastebin(code, name, output) | |
elseif provider == "g" then | |
return installFromGithub(code, name, output) | |
end | |
end | |
end | |
local function update(program) | |
local fullCode = installedPrograms[program] | |
if fullCode == nil then | |
print("program '" .. program .. "' is not installed.") | |
else | |
local installOutput = { | |
["download"] = "Updating " .. program .. "...", | |
["saved"] = nil, | |
["failed"] = "Failed to download." | |
} | |
local movedname = "tmpMove" .. program | |
--print("moving ".. program) | |
local path = shell.resolveProgram(program) | |
--print(path) | |
shell.run("mv " .. path .. " " .. movedname) | |
local success = false | |
local provider,code = splitCode(fullCode) | |
if provider == "p" then | |
success = installFromPastebin(code, path, installOutput) | |
elseif provider == "g" then | |
success = installFromGithub(code, path, installOutput) | |
end | |
if success then | |
shell.run("rm " .. movedname) | |
end | |
end | |
end | |
local function updateAllPrograms() | |
for program,_ in pairs(installedPrograms) do | |
update(program) | |
end | |
end | |
local tArgs = { ... } | |
if not http then | |
print( "Pastebin requires http API" ) | |
print( "Set enableAPI_http to 1 in mod_ComputerCraft.cfg" ) | |
return | |
end | |
loadInstalledPrograms() | |
local command = tArgs[1] | |
if command == upload_argument_name then | |
if #tArgs < 2 then | |
printUsage() | |
return | |
end | |
local sFile = tArgs[2] | |
upload(sFile) | |
elseif command == install_argument_name then | |
if #tArgs < 2 then | |
printUsage() | |
return | |
end | |
if #tArgs == 3 then | |
local code = tArgs[2] | |
local file = tArgs[3] | |
local installOutput = { | |
["download"] = "Downloading '" .. code .. "' from pastebin...", | |
["saved"] = "Saved as ".. file .. ".", | |
["failed"] = "Failed to download." | |
} | |
installFromPastebin(code, file, installOutput) | |
else | |
local file = tArgs[2] | |
local installOutput = { | |
["download"] = "Downloading from pastebin...", | |
["saved"] = "Saved as ".. file .. ".", | |
["failed"] = "Failed to download." | |
} | |
installByName(file, installOutput) | |
end | |
elseif command == update_argument_name then | |
if #tArgs < 2 then | |
updateAllPrograms() | |
else | |
local program = tArgs[2] | |
update(program) | |
end | |
elseif command == list_argument_name then | |
if #tArgs == 1 then | |
listRemote() | |
return | |
end | |
if tArgs[2] == "installed" then | |
print("installed") | |
listInstalled() | |
end | |
else | |
printUsage() | |
end | |
saveInstalledPrograms() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment