1
0
mirror of https://github.com/kepler155c/opus synced 2025-10-18 17:27:39 +00:00

package manager

This commit is contained in:
kepler155c@gmail.com
2018-11-04 13:00:37 -05:00
parent 67be1e0f2f
commit 99b2fa1b0b
7 changed files with 132 additions and 43 deletions

View File

@@ -1,40 +1,52 @@
local json = require('json')
local Util = require('util')
-- Limit queries to once per minecraft day
-- TODO: will not work if time is stopped
local TREE_URL = 'https://api.github.com/repos/%s/%s/git/trees/%s?recursive=1'
local FILE_URL = 'https://raw.githubusercontent.com/%s/%s/%s/%s'
local git = { }
local fs = _G.fs
local os = _G.os
if not _G.GIT then
_G.GIT = { }
end
function git.list(repository, cache)
function git.list(repository)
local t = Util.split(repository, '(.-)/')
local user = t[1]
local repo = t[2]
local branch = t[3] or 'master'
local user = table.remove(t, 1)
local repo = table.remove(t, 1)
local branch = table.remove(t, 1) or 'master'
local path
local dataUrl = string.format(TREE_URL, user, repo, branch)
if _G.GIT[dataUrl] then
_G.GIT[dataUrl].count = _G.GIT[dataUrl].count + 1
else
_G.GIT[dataUrl] = {
count = 1
}
end
local contents = Util.download(dataUrl)
if not contents then
error('Invalid repository')
if not Util.empty(t) then
path = table.concat(t, '/') .. '/'
end
local data = json.decode(contents)
Util.writeTable('.git/' .. dataUrl, { day = os.day(), data = data })
local cacheKey = table.concat({ user, repo, branch }, '-')
local fname = fs.combine('.git', cacheKey)
local function getContents()
if fs.exists(fname) then
local contents = Util.readTable(fname)
if contents and contents.data == os.day() then
return contents.data
end
fs.delete(fname)
end
local dataUrl = string.format(TREE_URL, user, repo, branch)
local contents = Util.download(dataUrl)
if contents then
return json.decode(contents)
end
end
local data = getContents() or error('Invalid repository')
if data.message and data.message:find("API rate limit exceeded") then
error("Out of API calls, try again later")
end
@@ -43,15 +55,26 @@ Util.writeTable('.git/' .. dataUrl, { day = os.day(), data = data })
error("Invalid repository")
end
local list = { }
if not fs.exists(fname) then
Util.writeTable('.git/' .. cacheKey, { day = os.day(), data = data })
end
local list = { }
for _,v in pairs(data.tree) do
if v.type == "blob" then
v.path = v.path:gsub("%s","%%20")
list[v.path] = {
url = string.format(FILE_URL, user, repo, branch, v.path),
size = v.size,
}
if not path then
list[v.path] = {
url = string.format(FILE_URL, user, repo, branch, v.path),
size = v.size,
}
elseif Util.startsWith(v.path, path) then
local p = string.sub(v.path, #path)
list[p] = {
url = string.format(FILE_URL, user, repo, branch, path .. p),
size = v.size,
}
end
end
end

View File

@@ -34,8 +34,7 @@ function Packages:isInstalled(package)
end
function Packages:getManifest(package)
-- local fname = 'packages/' .. package .. '/.package'
local fname = 'usr/milo/.package'
local fname = 'packages/' .. package .. '/.package'
if fs.exists(fname) then
return Util.readTable(fname)
end

View File

@@ -507,7 +507,7 @@ function Util.matches(str, pattern)
return t
end
function Util.startsWidth(s, match)
function Util.startsWith(s, match)
return string.sub(s, 1, #match) == match
end