143 lines
3.7 KiB
Lua
143 lines
3.7 KiB
Lua
local _VERSION = '3.0.0';
|
|
|
|
local REPO_BASE = 'https://raw.githubusercontent.com/guillaumearm/cc-libs/';
|
|
local LOCAL_STATE_DIR = '/trapos';
|
|
local LOCAL_MANIFEST_PATH = '/trapos/manifest.json';
|
|
|
|
local function printUsage()
|
|
print('install usage:');
|
|
print();
|
|
print('\t\twget run <install-url>');
|
|
print('\t\twget run <install-url> --beta');
|
|
print('\t\twget run <install-url> --stable');
|
|
end
|
|
|
|
local function readLocalManifest()
|
|
if not fs.exists(LOCAL_MANIFEST_PATH) then return nil end
|
|
local f = fs.open(LOCAL_MANIFEST_PATH, 'r');
|
|
if not f then return nil end
|
|
local data = f.readAll();
|
|
f.close();
|
|
if not data or data == '' then return nil end
|
|
return textutils.unserializeJSON(data);
|
|
end
|
|
|
|
local function writeLocalManifest(manifest)
|
|
fs.makeDir(LOCAL_STATE_DIR);
|
|
local f = fs.open(LOCAL_MANIFEST_PATH, 'w');
|
|
if not f then return false end
|
|
f.write(textutils.serializeJSON(manifest));
|
|
f.close();
|
|
return true;
|
|
end
|
|
|
|
local function confirmBeta()
|
|
print();
|
|
print('You are about to install the BETA branch (next).');
|
|
print('Beta builds may be unstable. Continue? (y/N)');
|
|
write('> ');
|
|
local answer = read();
|
|
if not answer then return false end
|
|
answer = answer:lower();
|
|
return answer == 'y' or answer == 'yes';
|
|
end
|
|
|
|
local function fetchManifest(branch)
|
|
local url = REPO_BASE .. branch .. '/manifest.json';
|
|
local response = http.get(url);
|
|
if not response then return nil end
|
|
local body = response.readAll();
|
|
response.close();
|
|
if not body or body == '' then return nil end
|
|
return textutils.unserializeJSON(body);
|
|
end
|
|
|
|
local command = ...;
|
|
local forceBeta = false;
|
|
local forceStable = false;
|
|
|
|
if command == 'version' or command == '-version' or command == '--version' then
|
|
print('install v' .. _VERSION);
|
|
return;
|
|
end
|
|
|
|
if command == 'help' or command == '-help' or command == '--help' then
|
|
printUsage();
|
|
return;
|
|
end
|
|
|
|
if command == '--beta' or command == '-beta' then
|
|
forceBeta = true;
|
|
elseif command == '--stable' or command == '-stable' then
|
|
forceStable = true;
|
|
elseif command ~= nil and command ~= '' then
|
|
printUsage();
|
|
return;
|
|
end
|
|
|
|
local localManifest = readLocalManifest();
|
|
local localBranch = localManifest and localManifest.branch or nil;
|
|
local branch;
|
|
|
|
if forceBeta then
|
|
branch = 'next';
|
|
if localBranch ~= 'next' then
|
|
if not confirmBeta() then
|
|
print('Aborted.');
|
|
return;
|
|
end
|
|
end
|
|
elseif forceStable then
|
|
branch = 'master';
|
|
else
|
|
branch = localBranch or 'master';
|
|
end
|
|
|
|
print('Fetching manifest from branch: ' .. branch);
|
|
local manifest = fetchManifest(branch);
|
|
if not manifest or type(manifest.files) ~= 'table' then
|
|
print('Failed to fetch or parse manifest.json from ' .. branch);
|
|
return;
|
|
end
|
|
|
|
-- The persisted branch reflects the actually-used branch, not the manifest default.
|
|
manifest.branch = branch;
|
|
|
|
local REPO_PREFIX = REPO_BASE .. branch .. '/';
|
|
|
|
-- Legacy file cleanup (pre-manifest installs).
|
|
fs.delete('ping-server.lua');
|
|
fs.delete('ping.lua');
|
|
fs.delete('cube.lua');
|
|
fs.delete('router.lua');
|
|
fs.delete('servers/cube-startup.lua');
|
|
fs.delete('programs/cube.lua');
|
|
fs.delete('programs/goo.lua');
|
|
fs.delete('servers/cube-server.lua');
|
|
fs.delete('servers/cube-boot.lua');
|
|
|
|
local previousDir = shell.dir();
|
|
shell.setDir('/');
|
|
|
|
fs.makeDir('/programs');
|
|
fs.makeDir('/apis');
|
|
fs.makeDir('/startup');
|
|
fs.makeDir('/servers');
|
|
fs.makeDir(LOCAL_STATE_DIR);
|
|
|
|
for _, filePath in ipairs(manifest.files) do
|
|
fs.delete(filePath);
|
|
shell.execute('wget', REPO_PREFIX .. filePath, filePath);
|
|
end
|
|
|
|
if not writeLocalManifest(manifest) then
|
|
print('Warning: failed to write local manifest');
|
|
end
|
|
|
|
print();
|
|
print('=> TrapOS v' .. (manifest.version or '?') .. ' installed (branch: ' .. branch .. ')');
|
|
print('=> Execute startup/servers.lua');
|
|
shell.execute('/startup/servers.lua');
|
|
|
|
shell.setDir(previousDir);
|