Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Created by NEO
- -- You can find me at : http://computercraft.ru/user/13-neo/
- internet = require('internet')
- shell = require('shell')
- X, Y = require('component').gpu.getResolution()
- fs = require('filesystem')
- repository = {}
- repoPath = 'repo.meta'
- url = 'http://pastebin.com/raw/vtKanXDC'
- args = {...}
- function getPages()
- if repository ~= nil then
- return math.ceil(#repository/Y)
- end
- return 0
- end
- function isId(id)
- if id <= 0 or id > #repository then error('Id not found.') end
- end
- function getNameById(id)
- isId(id)
- return repository[id].name
- end
- function getVersionById(id)
- isId(id)
- return repository[id].version
- end
- function getAuthorById(id)
- isId(id)
- return repository[id].author
- end
- function getDescById(id)
- isId(id)
- return repository[id].desc
- end
- function getUrlsById(id)
- isId(id)
- return repository[id].urls
- end
- function getDependencies(id)
- isId(id)
- return repository[id].dependencies
- end
- function isDependenciesPastebin(id, idd)
- return getDependencies(id)[idd]['pastebin'] ~= nil
- end
- function isDependenciesGitHub(id, idd)
- return getDependencies(id)[idd]['github'] ~= nil
- end
- function getDependenciesPastebin(id, idd)
- return getDependencies(id)[idd]['pastebin']
- end
- function getDependenciesGitHub(id, idd)
- return getDependencies(id)[idd]['github']
- end
- function isPastebin(id)
- urls = getUrlsById(id)
- return urls['pastebin'] ~= nil
- end
- function isGitHub(id)
- urls = getUrlsById(id)
- return urls['github'] ~= nil
- end
- function getPastebin(id)
- urls = getUrlsById(id)
- return urls['pastebin']
- end
- function getGitHub(id)
- urls = getUrlsById(id)
- return urls['github']
- end
- function view(page)
- if page == nil then
- print('Pages: ', getPages())
- else
- if page > getPages() or page <= 0 then error('Page not found.') end
- min = (page - 1) * Y + 1
- max = page * Y
- repo_len = #repository
- if min <= 0 or max <= 0 then return error() end
- if max > repo_len then max = repo_len end
- for i = min, max do
- drawInfo(i)
- end
- end
- end
- function getMode(str)
- if str == 'pastebin' or str == 'p' then return 1 end
- if str == 'github' or str == 'g' then return 2 end
- return -1
- end
- function status(err, stat, file)
- if err then
- print(file..' downloading finish.')
- else
- print(file..' error: '..stat)
- end
- end
- function download(path, id, mode)
- isId(id)
- if path ~= nil then
- if string.sub(path, #path, #path) ~= '/' then
- path = path..'/'
- end
- end
- name = getNameById(id)..'-v'..string.gsub(getVersionById(id), '%.', '_')..'.lua'
- if mode == 1 and isPastebin(id) then
- if path == nil then
- err, stat = shell.execute('pastebin get -f '..getPastebin(id)..' '..name)
- status(err, stat, getNameById(id))
- else
- err, stat = shell.execute('pastebin get -f '..getPastebin(id)..' '..path..name)
- status(err, stat, getNameById(id))
- end
- downloadDependencies(path, id)
- elseif mode == 1 then
- print('the program is not on Pastebin')
- end
- if mode == 2 and isGitHub(id) then
- if path == nil then
- err, stat = shell.execute('wget -Q '..getGitHub(id)..' '..name)
- status(err, stat, getNameById(id))
- else
- err, stat = shell.execute('wget -Q '..getGitHub(id)..' '..path..name)
- status(err, stat, getNameById(id))
- end
- downloadDependencies(path, id)
- elseif mode == 2 then
- error('The program is not on GitHub')
- end
- end
- function downloadDependencies(path, id)
- print('Downloading dependencies...')
- isId(id)
- if path == nil then
- dependencies = getDependencies(id)
- for i = 1, #dependencies do
- if isDependenciesPastebin(id, i) then
- err, stat = shell.execute('pastebin get -f '..getDependenciesPastebin(id, i)..' '..dependencies[i].path)
- status(err, stat, dependencies[i].path)
- elseif isDependenciesGitHub(id, i) then
- err, stat = shell.execute('wget -Q '..getDependenciesGitHub(id, i)..' '..dependencies[i].path)
- status(err, stat, dependencies[i].path)
- end
- end
- else
- for i = 1, #dependencies do
- if isDependenciesPastebin(id, i) then
- err, stat = shell.execute('pastebin get -f '..getDependenciesPastebin(id, i).github..' '..path..dependencies[i].path)
- status(err, stat, dependencies[i].path)
- elseif isDependenciesGitHub(id, i) then
- err, stat = shell.execute('wget -Q '..getDependenciesGitHub(id, i).github..' '..path..dependencies[i].path)
- status(err, stat, dependencies[i].path)
- end
- end
- end
- end
- function drawInfo(id)
- isId(id)
- print(id, '"'..getNameById(id)..'" V:'..getVersionById(id), ' author: '..getAuthorById(id))
- end
- function find(name)
- if name ~= nil then
- name = string.gsub(name, '*', '.+')
- name = string.gsub(name, '?', '.')
- for i = 1, #repository do
- res = string.match(getNameById(i), name)
- if res ~= nil then
- drawInfo(i)
- end
- end
- end
- end
- function loadMeta()
- f = fs.open(repoPath)
- if f == nil then
- loadRepository()
- end
- str = f:read(math.huge)
- func = load(str, '= load')
- repository = func()
- end
- function loadRepository()
- print('Loading repository...')
- str = ''
- for chunk in internet.request(url) do
- str = str .. chunk
- io.write('...')
- end
- func = load(str, '= load')
- repository = func()
- f = fs.open(repoPath, 'w')
- f:write(str)
- f:close()
- end
- function init()
- loadMeta()
- end
- function main()
- init()
- if args ~= nil then
- len = #args
- if len == 1 then
- if args[1] == 'load' then loadRepository() end
- if args[1] == 'view' then view() end
- end
- if len == 2 then
- if args[1] == 'view' then page = tonumber(args[2]) view(page) end
- if args[1] == 'desc' then id = tonumber(args[2]) print(getDescById(id)) end
- if args[1] == 'find' then find(args[2]) end
- end
- if len == 3 then
- if args[1] == 'download' then download(nil, tonumber(args[2]), getMode(args[3])) end
- end
- if len == 4 then
- if args[1] == 'download' then download(args[4], tonumber(args[2]), getMode(args[3])) end
- end
- end
- end
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement