cc-libs/programs/ccpm.lua

234 lines
5.8 KiB
Lua

local createCcpm = require('/apis/libccpm');
local createVersion = require('/apis/libversion');
local args = table.pack(...);
local command = args[1];
local function printUsage()
print('ccpm usage:');
print();
print('\t\tccpm install <package>');
print('\t\tccpm reinstall <package>');
print('\t\tccpm uninstall <package>');
print('\t\tccpm update');
print('\t\tccpm upgrade');
print('\t\tccpm ls');
print('\t\tccpm available [term]');
print('\t\tccpm search [term]');
print('\t\tccpm info <package>');
print('\t\tccpm registry ls');
print('\t\tccpm registry add <name> [--branch <b>] [--type github|gitea|http]');
print('\t\tccpm registry rm <name>');
print('\t\tccpm version');
print('\t\tccpm help');
end
if command == 'version' or command == '-version' or command == '--version' then
print('v' .. createVersion().forSelf());
return;
end
if command == nil or command == '' or command == 'help' or command == '-help' or command == '--help' then
printUsage();
return;
end
local ccpm = createCcpm();
local function logLine(msg)
print(msg);
end
local function printColored(msg, color)
if term.isColor and term.isColor() then
local previous = term.getTextColor();
term.setTextColor(color);
print(msg);
term.setTextColor(previous);
else
print(msg);
end
end
local function printAvailableRow(r)
local installed = '';
if r.installedVersion then
installed = ' installed v' .. tostring(r.installedVersion);
end
local line = r.name .. ' v' .. tostring(r.version) .. ' (' .. r.registry .. ') ' .. r.status .. installed;
if r.status == 'up-to-date' then
printColored(line, colors.lime);
elseif r.status == 'updatable' then
printColored(line, colors.orange);
else
print(line);
end
end
if command == 'install' or command == 'reinstall' then
local pkg = args[2];
if not pkg then printUsage(); return; end
local ok, result = ccpm.install(pkg, { force = command == 'reinstall', log = logLine });
if not ok then
print(result);
return;
end
print('=> ' .. pkg .. ' installed.');
return;
end
if command == 'update' then
local cache = ccpm.update();
local count = 0;
for _ in pairs(cache.packages or {}) do count = count + 1; end
print('=> package cache updated (' .. count .. ' packages).');
return;
end
if command == 'upgrade' then
local ok, result = ccpm.upgrade({ log = logLine });
if not ok then
print(result);
return;
end
if #result == 0 then
print('=> all packages up-to-date.');
else
print('=> upgraded: ' .. table.concat(result, ', '));
end
return;
end
if command == 'uninstall' or command == 'remove' or command == 'rm' then
local pkg = args[2];
if not pkg then printUsage(); return; end
local ok, err = ccpm.uninstall(pkg, { log = logLine });
if not ok then
print(err);
return;
end
print('=> ' .. pkg .. ' uninstalled.');
return;
end
if command == 'ls' or command == 'list' then
local packages = ccpm.list();
local names = {};
for name in pairs(packages) do names[#names + 1] = name; end
table.sort(names);
if #names == 0 then
print('No packages installed.');
return;
end
for _, name in ipairs(names) do
print(name .. ' v' .. tostring(packages[name].version or '?'));
end
return;
end
if command == 'search' then
local results = ccpm.search(args[2]);
if #results == 0 then
print('No packages found.');
return;
end
for _, r in ipairs(results) do
print(r.name .. ' v' .. tostring(r.version) .. ' (' .. r.registry .. ')');
end
return;
end
if command == 'available' then
local results = ccpm.available(args[2]);
if #results == 0 then
print("No packages found. Run 'ccpm update' first if the cache is empty.");
return;
end
for _, r in ipairs(results) do
printAvailableRow(r);
end
return;
end
if command == 'info' then
local pkg = args[2];
if not pkg then printUsage(); return; end
local desc = ccpm.info(pkg);
if not desc then
print('package not found: ' .. pkg);
return;
end
print(desc.name .. ' v' .. tostring(desc.version or '?'));
if desc.description then print(desc.description); end
if desc.dependencies and #desc.dependencies > 0 then
print('dependencies: ' .. table.concat(desc.dependencies, ', '));
end
print('files:');
for _, f in ipairs(desc.files or {}) do print(' ' .. f); end
return;
end
if command == 'registry' then
local sub = args[2];
if sub == nil or sub == 'ls' or sub == 'list' then
local registries = ccpm.listRegistries();
if #registries == 0 then
print('No registries configured.');
return;
end
for _, r in ipairs(registries) do
if r.type == 'github' then
print(r.name .. ' (github:' .. tostring(r.branch or 'master') .. ')');
elseif r.type == 'gitea' then
print(r.name .. ' (gitea:' .. tostring(r.branch or 'master') .. ')');
else
print(r.name .. ' (' .. tostring(r.type or 'http') .. ')');
end
end
return;
end
if sub == 'add' then
local name = args[3];
if not name then printUsage(); return; end
local branch, rtype;
local i = 4;
while i <= args.n do
local a = args[i];
if a == '--branch' then
branch = args[i + 1];
i = i + 1;
elseif a == '--type' then
rtype = args[i + 1];
i = i + 1;
end
i = i + 1;
end
local ok, err = ccpm.addRegistry(name, { branch = branch, type = rtype });
if not ok then
print(err);
return;
end
print('=> registry added: ' .. name);
return;
end
if sub == 'rm' or sub == 'remove' then
local name = args[3];
if not name then printUsage(); return; end
local ok, err = ccpm.removeRegistry(name);
if not ok then
print(err);
return;
end
print('=> registry removed: ' .. name);
return;
end
printUsage();
return;
end
printUsage();