-- 'package manager' local rbase = "https://g.gh0.pw/ccprogs/plain/" local function perr(err,what) if err then error(what..": "..err) end end local _manifests = {} local function clear_cache() for k in pairs(_manifests) do _manifests[k] = nil end end local function manifest(pkgname) if _manifests[pkgname] then return _manifests[pkgname] end print("fetching manifest of '"..pkgname.."'...") local files={} local files_present=false local deps={} local res,err,err_res = http.get(rbase..pkgname..".dat") if err then if err_res and (err_res.getResponseCode() == 404) then print("no manifest for '"..pkgname.."' found, using defaults") else error("http.get: "..err) end else for line in res.readAll():gmatch"[^\n]+" do local op,argstart = line:match "^(%w+)%s*()" if op then local argstr = line:sub(argstart) local args = {} for arg in argstr:gmatch"[^ ]+" do table.insert(args,arg) end if op == "dep" then for _,arg in ipairs(args) do deps[arg]=true end elseif op == "file" then files_present = true for _,arg in ipairs(args) do files[arg]=true end end end end end if not files_present then files[pkgname..".lua"]=true end local man = {files=files,deps=deps} _manifests[pkgname] = man return man end local function recursive_depsof(pkgname) local lookedat = {} local deps = {} local function F(pkgname) if not lookedat[pkgname] then lookedat[pkgname] = true for k,_ in pairs(manifest(pkgname).deps) do deps[k] = true F(k) end end end deps[pkgname] = true F(pkgname) return deps end local function recursive_filesof(pkgname) local deps = recursive_depsof(pkgname) local files = {} for dep in pairs(deps) do for file in pairs(manifest(dep).files) do files[file] = true end end return files end local function fmt_set(set) local keys = {} for k in pairs(set) do table.insert(keys,k) end table.sort(keys) return "("..#keys..") " .. table.concat(keys,", ") end local function dl(pkgname) local deps = recursive_depsof(pkgname) local files = recursive_filesof(pkgname) print("all dependencies: "..fmt_set(deps)) print("all files: "..fmt_set(files)) for file in pairs(files) do dlfile(file) end print"done" end local function dlfile(filename) local src=rbase..filename local dest="/"..filename print("downloading "..src.." to "..dest) local file,err=fs.open(dest,"w") if err then error("fs.open "..err) end local res,err = http.get(src) if err then error("http.get "..err) end file.write(res.readAll()) file.flush() file.close() end return {manifest=manifest,rd=recursive_depsof,rf=recursive_filesof,dl=dl} --dl(arg[1])