From 352a9c50ccb8c6e190031d5f9db47e028d408cfd Mon Sep 17 00:00:00 2001 From: V1K1NGbg Date: Thu, 22 Aug 2024 17:49:08 -0300 Subject: Teal: convert luarocks.admin.cmd.add --- src/luarocks/admin/cmd/add.lua | 134 ---------------------------------------- src/luarocks/admin/cmd/add.tl | 135 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 135 insertions(+), 134 deletions(-) delete mode 100644 src/luarocks/admin/cmd/add.lua create mode 100644 src/luarocks/admin/cmd/add.tl (limited to 'src') diff --git a/src/luarocks/admin/cmd/add.lua b/src/luarocks/admin/cmd/add.lua deleted file mode 100644 index aa444c50..00000000 --- a/src/luarocks/admin/cmd/add.lua +++ /dev/null @@ -1,134 +0,0 @@ - ---- Module implementing the luarocks-admin "add" command. --- Adds a rock or rockspec to a rocks server. -local add = {} - -local cfg = require("luarocks.core.cfg") -local util = require("luarocks.util") -local dir = require("luarocks.dir") -local writer = require("luarocks.manif.writer") -local fs = require("luarocks.fs") -local cache = require("luarocks.admin.cache") -local index = require("luarocks.admin.index") - -function add.add_to_parser(parser) - local cmd = parser:command("add", "Add a rock or rockspec to a rocks server.", util.see_also()) - - cmd:argument("rock", "A local rockspec or rock file.") - :args("+") - - cmd:option("--server", "The server to use. If not given, the default server ".. - "set in the upload_server variable from the configuration file is used instead.") - :target("add_server") - cmd:flag("--no-refresh", "Do not refresh the local cache prior to ".. - "generation of the updated manifest.") - cmd:flag("--index", "Produce an index.html file for the manifest. This ".. - "flag is automatically set if an index.html file already exists.") -end - -local function zip_manifests() - for ver in util.lua_versions() do - local file = "manifest-"..ver - local zip = file..".zip" - fs.delete(dir.path(fs.current_dir(), zip)) - fs.zip(zip, file) - end -end - -local function add_files_to_server(refresh, rockfiles, server, upload_server, do_index) - assert(type(refresh) == "boolean" or not refresh) - assert(type(rockfiles) == "table") - assert(type(server) == "string") - assert(type(upload_server) == "table" or not upload_server) - - local download_url, login_url = cache.get_server_urls(server, upload_server) - local at = fs.current_dir() - local refresh_fn = refresh and cache.refresh_local_cache or cache.split_server_url - - local local_cache, protocol, server_path, user, password = refresh_fn(download_url, cfg.upload_user, cfg.upload_password) - if not local_cache then - return nil, protocol - end - - if not login_url then - login_url = protocol.."://"..server_path - end - - local ok, err = fs.change_dir(at) - if not ok then return nil, err end - - local files = {} - for _, rockfile in ipairs(rockfiles) do - if fs.exists(rockfile) then - util.printout("Copying file "..rockfile.." to "..local_cache.."...") - local absolute = fs.absolute_name(rockfile) - fs.copy(absolute, local_cache, "read") - table.insert(files, dir.base_name(absolute)) - else - util.printerr("File "..rockfile.." not found") - end - end - if #files == 0 then - return nil, "No files found" - end - - local ok, err = fs.change_dir(local_cache) - if not ok then return nil, err end - - util.printout("Updating manifest...") - writer.make_manifest(local_cache, "one", true) - - zip_manifests() - - if fs.exists("index.html") then - do_index = true - end - - if do_index then - util.printout("Updating index.html...") - index.make_index(local_cache) - end - - local login_info = "" - if user then login_info = " -u "..user end - if password then login_info = login_info..":"..password end - if not login_url:match("/$") then - login_url = login_url .. "/" - end - - if do_index then - table.insert(files, "index.html") - end - table.insert(files, "manifest") - for ver in util.lua_versions() do - table.insert(files, "manifest-"..ver) - table.insert(files, "manifest-"..ver..".zip") - end - - -- TODO abstract away explicit 'curl' call - - local cmd - if protocol == "rsync" then - local srv, path = server_path:match("([^/]+)(/.+)") - cmd = cfg.variables.RSYNC.." "..cfg.variables.RSYNCFLAGS.." -e ssh "..local_cache.."/ "..user.."@"..srv..":"..path.."/" - elseif protocol == "file" then - return fs.copy_contents(local_cache, server_path) - elseif upload_server and upload_server.sftp then - local part1, part2 = upload_server.sftp:match("^([^/]*)/(.*)$") - cmd = cfg.variables.SCP.." "..table.concat(files, " ").." "..user.."@"..part1..":/"..part2 - else - cmd = cfg.variables.CURL.." "..login_info.." -T '{"..table.concat(files, ",").."}' "..login_url - end - - util.printout(cmd) - return fs.execute(cmd) -end - -function add.command(args) - local server, server_table = cache.get_upload_server(args.add_server or args.server) - if not server then return nil, server_table end - return add_files_to_server(not args.no_refresh, args.rock, server, server_table, args.index) -end - - -return add diff --git a/src/luarocks/admin/cmd/add.tl b/src/luarocks/admin/cmd/add.tl new file mode 100644 index 00000000..e544be96 --- /dev/null +++ b/src/luarocks/admin/cmd/add.tl @@ -0,0 +1,135 @@ + +--- Module implementing the luarocks-admin "add" command. +-- Adds a rock or rockspec to a rocks server. +local record add +end + +local cfg = require("luarocks.core.cfg") +local util = require("luarocks.util") +local dir = require("luarocks.dir") +local writer = require("luarocks.manif.writer") +local fs = require("luarocks.fs") +local cache = require("luarocks.admin.cache") +local index = require("luarocks.admin.index") + +local type Parser = require("luarocks.vendor.argparse").Parser + +local type Args = require("luarocks.core.types.args").Args + +function add.add_to_parser(parser: Parser) + local cmd = parser:command("add", "Add a rock or rockspec to a rocks server.", util.see_also()) + + cmd:argument("rocks", "A local rockspec or rock file.") + :args("+") + + cmd:option("--server", "The server to use. If not given, the default server ".. + "set in the upload_server variable from the configuration file is used instead.") + :target("add_server") + cmd:flag("--no-refresh", "Do not refresh the local cache prior to ".. + "generation of the updated manifest.") + cmd:flag("--index", "Produce an index.html file for the manifest. This ".. + "flag is automatically set if an index.html file already exists.") +end + +local function zip_manifests() + for ver in util.lua_versions() do + local file = "manifest-"..ver + local zip = file..".zip" + fs.delete(dir.path(fs.current_dir(), zip)) + fs.zip(zip, file) + end +end + +local function add_files_to_server(refresh: boolean, rockfiles: {string}, server: string, upload_server: {string: string}, do_index: boolean): boolean, string, string + + local download_url, login_url = cache.get_server_urls(server, upload_server) + local at = fs.current_dir() + local refresh_fn = refresh and cache.refresh_local_cache or cache.split_server_url + + local local_cache, protocol, server_path, user, password = refresh_fn(download_url, cfg.upload_user, cfg.upload_password) + if not local_cache then + return nil, protocol + end + + if not login_url then + login_url = protocol.."://"..server_path + end + + local ok, err = fs.change_dir(at) + if not ok then return nil, err end + + local files = {} + for _, rockfile in ipairs(rockfiles) do + if fs.exists(rockfile) then + util.printout("Copying file "..rockfile.." to "..local_cache.."...") + local absolute = fs.absolute_name(rockfile) + fs.copy(absolute, local_cache, "read") + table.insert(files, dir.base_name(absolute)) + else + util.printerr("File "..rockfile.." not found") + end + end + if #files == 0 then + return nil, "No files found" + end + + local ok, err = fs.change_dir(local_cache) + if not ok then return nil, err end + + util.printout("Updating manifest...") + writer.make_manifest(local_cache, "one", true) + + zip_manifests() + + if fs.exists("index.html") then + do_index = true + end + + if do_index then + util.printout("Updating index.html...") + index.make_index(local_cache) + end + + local login_info = "" + if user then login_info = " -u "..user end + if password then login_info = login_info..":"..password end + if not login_url:match("/$") then + login_url = login_url .. "/" + end + + if do_index then + table.insert(files, "index.html") + end + table.insert(files, "manifest") + for ver in util.lua_versions() do + table.insert(files, "manifest-"..ver) + table.insert(files, "manifest-"..ver..".zip") + end + + -- TODO abstract away explicit 'curl' call + + local cmd: string + if protocol == "rsync" then + local srv, path = server_path:match("([^/]+)(/.+)") + cmd = cfg.variables.RSYNC.." "..cfg.variables.RSYNCFLAGS.." -e ssh "..local_cache.."/ "..user.."@"..srv..":"..path.."/" + elseif protocol == "file" then + return fs.copy_contents(local_cache, server_path) + elseif upload_server and upload_server.sftp then + local part1, part2 = upload_server.sftp:match("^([^/]*)/(.*)$") + cmd = cfg.variables.SCP.." "..table.concat(files, " ").." "..user.."@"..part1..":/"..part2 + else + cmd = cfg.variables.CURL.." "..login_info.." -T '{"..table.concat(files, ",").."}' "..login_url + end + + util.printout(cmd) + return fs.execute(cmd) +end + +function add.command(args: Args): boolean, string, string --! + local server, server_table, err = cache.get_upload_server(args.add_server or args.server) + if not server then return nil, err end + return add_files_to_server(not args.no_refresh, args.rocks, server, server_table, args.index) +end + + +return add -- cgit v1.2.3-55-g6feb