From ec0de3d09266f28cb9c45b635da50db113abeba2 Mon Sep 17 00:00:00 2001 From: V1K1NGbg Date: Tue, 20 Aug 2024 00:54:55 +0300 Subject: config, install, lint, make, new_version --- src/luarocks/cmd/config-original.lua | 392 ++++++++++++++++++++++++++++++ src/luarocks/cmd/config.lua | 114 +++++---- src/luarocks/cmd/config.tl | 51 ++-- src/luarocks/cmd/install-original.lua | 250 +++++++++++++++++++ src/luarocks/cmd/install.lua | 162 ++++++------ src/luarocks/cmd/install.tl | 8 +- src/luarocks/cmd/lint-original.lua | 50 ++++ src/luarocks/cmd/lint.lua | 37 ++- src/luarocks/cmd/lint.tl | 6 +- src/luarocks/cmd/make-original.lua | 165 +++++++++++++ src/luarocks/cmd/make.lua | 105 ++++---- src/luarocks/cmd/make.tl | 18 +- src/luarocks/cmd/new_version-original.lua | 228 +++++++++++++++++ src/luarocks/cmd/new_version.lua | 61 +++-- src/luarocks/cmd/new_version.tl | 6 +- 15 files changed, 1408 insertions(+), 245 deletions(-) create mode 100644 src/luarocks/cmd/config-original.lua create mode 100644 src/luarocks/cmd/install-original.lua create mode 100644 src/luarocks/cmd/lint-original.lua create mode 100644 src/luarocks/cmd/make-original.lua create mode 100644 src/luarocks/cmd/new_version-original.lua (limited to 'src') diff --git a/src/luarocks/cmd/config-original.lua b/src/luarocks/cmd/config-original.lua new file mode 100644 index 00000000..d67711a0 --- /dev/null +++ b/src/luarocks/cmd/config-original.lua @@ -0,0 +1,392 @@ +--- Module implementing the LuaRocks "config" command. +-- Queries information about the LuaRocks configuration. +local config_cmd = {} + +local persist = require("luarocks.persist") +local config = require("luarocks.config") +local cfg = require("luarocks.core.cfg") +local util = require("luarocks.util") +local deps = require("luarocks.deps") +local dir = require("luarocks.dir") +local fs = require("luarocks.fs") +local json = require("luarocks.vendor.dkjson") + +function config_cmd.add_to_parser(parser) + local cmd = parser:command("config", [[ +Query information about the LuaRocks configuration. + +* When given a configuration key, it prints the value of that key according to + the currently active configuration (taking into account all config files and + any command-line flags passed) + + Examples: + luarocks config variables.LUA_INCDIR + luarocks config lua_version + +* When given a configuration key and a value, it overwrites the config file (see + the --scope option below to determine which) and replaces the value of the + given key with the given value. + + * `lua_dir` is a special key as it checks for a valid Lua installation + (equivalent to --lua-dir) and sets several keys at once. + * `lua_version` is a special key as it changes the default Lua version + used by LuaRocks commands (equivalent to passing --lua-version). + + Examples: + luarocks config variables.OPENSSL_DIR /usr/local/openssl + luarocks config lua_dir /usr/local + luarocks config lua_version 5.3 + +* When given a configuration key and --unset, it overwrites the config file (see + the --scope option below to determine which) and deletes that key from the + file. + + Example: luarocks config variables.OPENSSL_DIR --unset + +* When given no arguments, it prints the entire currently active configuration, + resulting from reading the config files from all scopes. + + Example: luarocks config]], util.see_also([[ + https://github.com/luarocks/luarocks/wiki/Config-file-format + for detailed information on the LuaRocks config file format. +]])) + :summary("Query information about the LuaRocks configuration.") + + cmd:argument("key", "The configuration key.") + :args("?") + cmd:argument("value", "The configuration value.") + :args("?") + + cmd:option("--scope", "The scope indicates which config file should be rewritten.\n".. + '* Using a wrapper created with `luarocks init`, the default is "project".\n'.. + '* Using --local (or when `local_by_default` is `true`), the default is "user".\n'.. + '* Otherwise, the default is "system".') + :choices({"system", "user", "project"}) + cmd:flag("--unset", "Delete the key from the configuration file.") + cmd:flag("--json", "Output as JSON.") + + -- Deprecated flags + cmd:flag("--lua-incdir"):hidden(true) + cmd:flag("--lua-libdir"):hidden(true) + cmd:flag("--lua-ver"):hidden(true) + cmd:flag("--system-config"):hidden(true) + cmd:flag("--user-config"):hidden(true) + cmd:flag("--rock-trees"):hidden(true) +end + +local function config_file(conf) + print(dir.normalize(conf.file)) + if conf.found then + return true + else + return nil, "file not found" + end +end + +local function traverse_varstring(var, tbl, fn, missing_parent) + local k, r = var:match("^%[([0-9]+)%]%.(.*)$") + if k then + k = tonumber(k) + else + k, r = var:match("^([^.[]+)%.(.*)$") + if not k then + k, r = var:match("^([^[]+)(%[.*)$") + end + end + + if k then + if not tbl[k] and missing_parent then + missing_parent(tbl, k) + end + + if tbl[k] then + return traverse_varstring(r, tbl[k], fn, missing_parent) + else + return nil, "Unknown entry " .. k + end + end + + local i = var:match("^%[([0-9]+)%]$") + if i then + var = tonumber(i) + end + + return fn(tbl, var) +end + +local function print_json(value) + print(json.encode(value)) + return true +end + +local function print_entry(var, tbl, is_json) + return traverse_varstring(var, tbl, function(t, k) + if not t[k] then + return nil, "Unknown entry " .. k + end + local val = t[k] + + if not config.should_skip(var, val) then + if is_json then + return print_json(val) + elseif type(val) == "string" then + print(val) + else + persist.write_value(io.stdout, val) + end + end + return true + end) +end + +local function infer_type(var) + local typ + traverse_varstring(var, cfg, function(t, k) + if t[k] ~= nil then + typ = type(t[k]) + end + end) + return typ +end + +local function write_entries(keys, scope, do_unset) + if scope == "project" and not cfg.config_files.project then + return nil, "Current directory is not part of a project. You may want to run `luarocks init`." + end + + local file_name = cfg.config_files[scope].file + + local tbl, err = persist.load_config_file_if_basic(file_name, cfg) + if not tbl then + return nil, err + end + + for var, val in util.sortedpairs(keys) do + traverse_varstring(var, tbl, function(t, k) + if do_unset then + t[k] = nil + else + local typ = infer_type(var) + local v + if typ == "number" and tonumber(val) then + v = tonumber(val) + elseif typ == "boolean" and val == "true" then + v = true + elseif typ == "boolean" and val == "false" then + v = false + else + v = val + end + t[k] = v + keys[var] = v + end + return true + end, function(p, k) + p[k] = {} + end) + end + + local ok, err = fs.make_dir(dir.dir_name(file_name)) + if not ok then + return nil, err + end + + ok, err = persist.save_from_table(file_name, tbl) + if ok then + print(do_unset and "Removed" or "Wrote") + for var, val in util.sortedpairs(keys) do + if do_unset then + print(("\t%s"):format(var)) + else + if type(val) == "string" then + print(("\t%s = %q"):format(var, val)) + else + print(("\t%s = %s"):format(var, tostring(val))) + end + end + end + print(do_unset and "from" or "to") + print("\t" .. file_name) + return true + else + return nil, err + end +end + +local function get_scope(args) + return args.scope + or (args["local"] and "user") + or (args.project_tree and "project") + or (cfg.local_by_default and "user") + or (fs.is_writable(cfg.config_files["system"].file) and "system") + or "user" +end + +local function report_on_lua_incdir_config(value, lua_version) + local variables = { + ["LUA_DIR"] = cfg.variables.LUA_DIR, + ["LUA_BINDIR"] = cfg.variables.LUA_BINDIR, + ["LUA_INCDIR"] = value, + ["LUA_LIBDIR"] = cfg.variables.LUA_LIBDIR, + ["LUA"] = cfg.variables.LUA, + } + + local ok, err = deps.check_lua_incdir(variables, lua_version) + if not ok then + util.printerr() + util.warning((err:gsub(" You can use.*", ""))) + end + return ok +end + +local function report_on_lua_libdir_config(value, lua_version) + local variables = { + ["LUA_DIR"] = cfg.variables.LUA_DIR, + ["LUA_BINDIR"] = cfg.variables.LUA_BINDIR, + ["LUA_INCDIR"] = cfg.variables.LUA_INCDIR, + ["LUA_LIBDIR"] = value, + ["LUA"] = cfg.variables.LUA, + } + + local ok, err, _, err_files = deps.check_lua_libdir(variables, lua_version) + if not ok then + util.printerr() + util.warning((err:gsub(" You can use.*", ""))) + util.printerr("Tried:") + for _, l in pairs(err_files or {}) do + for _, d in ipairs(l) do + util.printerr("\t" .. d) + end + end + end + return ok +end + +local function warn_bad_c_config() + util.printerr() + util.printerr("LuaRocks may not work correctly when building C modules using this configuration.") + util.printerr() +end + +--- Driver function for "config" command. +-- @return boolean: True if succeeded, nil on errors. +function config_cmd.command(args) + local lua_version = args.lua_version or cfg.lua_version + + deps.check_lua_incdir(cfg.variables, lua_version) + deps.check_lua_libdir(cfg.variables, lua_version) + + -- deprecated flags + if args.lua_incdir then + print(cfg.variables.LUA_INCDIR) + return true + end + if args.lua_libdir then + print(cfg.variables.LUA_LIBDIR) + return true + end + if args.lua_ver then + print(cfg.lua_version) + return true + end + if args.system_config then + return config_file(cfg.config_files.system) + end + if args.user_config then + return config_file(cfg.config_files.user) + end + if args.rock_trees then + for _, tree in ipairs(cfg.rocks_trees) do + if type(tree) == "string" then + util.printout(dir.normalize(tree)) + else + local name = tree.name and "\t"..tree.name or "" + util.printout(dir.normalize(tree.root)..name) + end + end + return true + end + + if args.key == "lua_version" and args.value then + local scope = get_scope(args) + if scope == "project" and not cfg.config_files.project then + return nil, "Current directory is not part of a project. You may want to run `luarocks init`." + end + + local location = cfg.config_files[scope] + if (not location) or (not location.file) then + return nil, "could not get config file location for " .. tostring(scope) .. " scope" + end + + local prefix = dir.dir_name(location.file) + local ok, err = persist.save_default_lua_version(prefix, args.value) + if not ok then + return nil, "could not set default Lua version: " .. err + end + print("Lua version will default to " .. args.value .. " in " .. prefix) + end + + if args.key == "lua_dir" and args.value then + local scope = get_scope(args) + local keys = { + ["variables.LUA_DIR"] = cfg.variables.LUA_DIR, + ["variables.LUA_BINDIR"] = cfg.variables.LUA_BINDIR, + ["variables.LUA_INCDIR"] = cfg.variables.LUA_INCDIR, + ["variables.LUA_LIBDIR"] = cfg.variables.LUA_LIBDIR, + ["variables.LUA"] = cfg.variables.LUA, + } + if args.lua_version then + local prefix = dir.dir_name(cfg.config_files[scope].file) + persist.save_default_lua_version(prefix, args.lua_version) + end + local ok, err = write_entries(keys, scope, args.unset) + if ok then + local inc_ok = report_on_lua_incdir_config(cfg.variables.LUA_INCDIR, lua_version) + local lib_ok = ok and report_on_lua_libdir_config(cfg.variables.LUA_LIBDIR, lua_version) + if not (inc_ok and lib_ok) then + warn_bad_c_config() + end + end + + return ok, err + end + + if args.key then + if args.key:match("^[A-Z]") then + args.key = "variables." .. args.key + end + + if args.value or args.unset then + local scope = get_scope(args) + + local ok, err = write_entries({ [args.key] = args.value or args.unset }, scope, args.unset) + + if ok then + if args.key == "variables.LUA_INCDIR" then + local ok = report_on_lua_incdir_config(args.value, lua_version) + if not ok then + warn_bad_c_config() + end + elseif args.key == "variables.LUA_LIBDIR" then + local ok = report_on_lua_libdir_config(args.value, lua_version) + if not ok then + warn_bad_c_config() + end + end + end + + return ok, err + else + return print_entry(args.key, cfg, args.json) + end + end + + if args.json then + return print_json(config.get_config_for_display(cfg)) + else + print(config.to_string(cfg)) + return true + end +end + +return config_cmd diff --git a/src/luarocks/cmd/config.lua b/src/luarocks/cmd/config.lua index d67711a0..c2dd7d82 100644 --- a/src/luarocks/cmd/config.lua +++ b/src/luarocks/cmd/config.lua @@ -1,7 +1,8 @@ ---- Module implementing the LuaRocks "config" command. --- Queries information about the LuaRocks configuration. +local _tl_compat; if (tonumber((_VERSION or ''):match('[%d.]*$')) or 0) < 5.3 then local p, m = pcall(require, 'compat53.module'); if p then _tl_compat = m end end; local io = _tl_compat and _tl_compat.io or io; local ipairs = _tl_compat and _tl_compat.ipairs or ipairs; local pairs = _tl_compat and _tl_compat.pairs or pairs; local string = _tl_compat and _tl_compat.string or string + local config_cmd = {} + local persist = require("luarocks.persist") local config = require("luarocks.config") local cfg = require("luarocks.core.cfg") @@ -11,6 +12,14 @@ local dir = require("luarocks.dir") local fs = require("luarocks.fs") local json = require("luarocks.vendor.dkjson") +local argparse = require("luarocks.vendor.argparse") + + + + + + + function config_cmd.add_to_parser(parser) local cmd = parser:command("config", [[ Query information about the LuaRocks configuration. @@ -49,23 +58,23 @@ Query information about the LuaRocks configuration. Example: luarocks config]], util.see_also([[ https://github.com/luarocks/luarocks/wiki/Config-file-format for detailed information on the LuaRocks config file format. -]])) - :summary("Query information about the LuaRocks configuration.") - - cmd:argument("key", "The configuration key.") - :args("?") - cmd:argument("value", "The configuration value.") - :args("?") - - cmd:option("--scope", "The scope indicates which config file should be rewritten.\n".. - '* Using a wrapper created with `luarocks init`, the default is "project".\n'.. - '* Using --local (or when `local_by_default` is `true`), the default is "user".\n'.. - '* Otherwise, the default is "system".') - :choices({"system", "user", "project"}) +]])): + summary("Query information about the LuaRocks configuration.") + + cmd:argument("key", "The configuration key."): + args("?") + cmd:argument("value", "The configuration value."): + args("?") + + cmd:option("--scope", "The scope indicates which config file should be rewritten.\n" .. + '* Using a wrapper created with `luarocks init`, the default is "project".\n' .. + '* Using --local (or when `local_by_default` is `true`), the default is "user".\n' .. + '* Otherwise, the default is "system".'): + choices({ "system", "user", "project" }) cmd:flag("--unset", "Delete the key from the configuration file.") cmd:flag("--json", "Output as JSON.") - -- Deprecated flags + cmd:flag("--lua-incdir"):hidden(true) cmd:flag("--lua-libdir"):hidden(true) cmd:flag("--lua-ver"):hidden(true) @@ -84,7 +93,9 @@ local function config_file(conf) end local function traverse_varstring(var, tbl, fn, missing_parent) - local k, r = var:match("^%[([0-9]+)%]%.(.*)$") + local k + local r + k, r = var:match("^%[([0-9]+)%]%.(.*)$") if k then k = tonumber(k) else @@ -102,13 +113,13 @@ local function traverse_varstring(var, tbl, fn, missing_parent) if tbl[k] then return traverse_varstring(r, tbl[k], fn, missing_parent) else - return nil, "Unknown entry " .. k + return nil, "Unknown entry " .. tostring(k) end end local i = var:match("^%[([0-9]+)%]$") if i then - var = tonumber(i) + return fn(tbl, tonumber(i)) end return fn(tbl, var) @@ -150,11 +161,12 @@ local function infer_type(var) end local function write_entries(keys, scope, do_unset) + local wrote = {} if scope == "project" and not cfg.config_files.project then return nil, "Current directory is not part of a project. You may want to run `luarocks init`." end - local file_name = cfg.config_files[scope].file + local file_name = (cfg.config_files)[scope].file local tbl, err = persist.load_config_file_if_basic(file_name, cfg) if not tbl then @@ -165,6 +177,7 @@ local function write_entries(keys, scope, do_unset) traverse_varstring(var, tbl, function(t, k) if do_unset then t[k] = nil + wrote[var] = "" else local typ = infer_type(var) local v @@ -178,7 +191,7 @@ local function write_entries(keys, scope, do_unset) v = val end t[k] = v - keys[var] = v + wrote[var] = v end return true end, function(p, k) @@ -194,7 +207,7 @@ local function write_entries(keys, scope, do_unset) ok, err = persist.save_from_table(file_name, tbl) if ok then print(do_unset and "Removed" or "Wrote") - for var, val in util.sortedpairs(keys) do + for var, val in util.sortedpairs(wrote) do if do_unset then print(("\t%s"):format(var)) else @@ -214,15 +227,15 @@ local function write_entries(keys, scope, do_unset) end local function get_scope(args) - return args.scope - or (args["local"] and "user") - or (args.project_tree and "project") - or (cfg.local_by_default and "user") - or (fs.is_writable(cfg.config_files["system"].file) and "system") - or "user" + return args.scope or + (args["local"] and "user") or + (args.project_tree and "project") or + (cfg.local_by_default and "user") or + (fs.is_writable(cfg.config_files["system"].file) and "system") or + "user" end -local function report_on_lua_incdir_config(value, lua_version) +local function report_on_lua_incdir_config(value) local variables = { ["LUA_DIR"] = cfg.variables.LUA_DIR, ["LUA_BINDIR"] = cfg.variables.LUA_BINDIR, @@ -231,7 +244,7 @@ local function report_on_lua_incdir_config(value, lua_version) ["LUA"] = cfg.variables.LUA, } - local ok, err = deps.check_lua_incdir(variables, lua_version) + local ok, err = deps.check_lua_incdir(variables) if not ok then util.printerr() util.warning((err:gsub(" You can use.*", ""))) @@ -239,7 +252,7 @@ local function report_on_lua_incdir_config(value, lua_version) return ok end -local function report_on_lua_libdir_config(value, lua_version) +local function report_on_lua_libdir_config(value) local variables = { ["LUA_DIR"] = cfg.variables.LUA_DIR, ["LUA_BINDIR"] = cfg.variables.LUA_BINDIR, @@ -248,7 +261,7 @@ local function report_on_lua_libdir_config(value, lua_version) ["LUA"] = cfg.variables.LUA, } - local ok, err, _, err_files = deps.check_lua_libdir(variables, lua_version) + local ok, err, _, err_files = deps.check_lua_libdir(variables) if not ok then util.printerr() util.warning((err:gsub(" You can use.*", ""))) @@ -268,15 +281,14 @@ local function warn_bad_c_config() util.printerr() end ---- Driver function for "config" command. --- @return boolean: True if succeeded, nil on errors. + + function config_cmd.command(args) - local lua_version = args.lua_version or cfg.lua_version - deps.check_lua_incdir(cfg.variables, lua_version) - deps.check_lua_libdir(cfg.variables, lua_version) + deps.check_lua_incdir(cfg.variables) + deps.check_lua_libdir(cfg.variables) + - -- deprecated flags if args.lua_incdir then print(cfg.variables.LUA_INCDIR) return true @@ -297,12 +309,12 @@ function config_cmd.command(args) end if args.rock_trees then for _, tree in ipairs(cfg.rocks_trees) do - if type(tree) == "string" then - util.printout(dir.normalize(tree)) - else - local name = tree.name and "\t"..tree.name or "" - util.printout(dir.normalize(tree.root)..name) - end + if type(tree) == "string" then + util.printout(dir.normalize(tree)) + else + local name = tree.name and "\t" .. tree.name or "" + util.printout(dir.normalize(tree.root) .. name) + end end return true end @@ -313,7 +325,7 @@ function config_cmd.command(args) return nil, "Current directory is not part of a project. You may want to run `luarocks init`." end - local location = cfg.config_files[scope] + local location = (cfg.config_files)[scope] if (not location) or (not location.file) then return nil, "could not get config file location for " .. tostring(scope) .. " scope" end @@ -336,13 +348,13 @@ function config_cmd.command(args) ["variables.LUA"] = cfg.variables.LUA, } if args.lua_version then - local prefix = dir.dir_name(cfg.config_files[scope].file) + local prefix = dir.dir_name((cfg.config_files)[scope].file) persist.save_default_lua_version(prefix, args.lua_version) end local ok, err = write_entries(keys, scope, args.unset) if ok then - local inc_ok = report_on_lua_incdir_config(cfg.variables.LUA_INCDIR, lua_version) - local lib_ok = ok and report_on_lua_libdir_config(cfg.variables.LUA_LIBDIR, lua_version) + local inc_ok = report_on_lua_incdir_config(cfg.variables.LUA_INCDIR) + local lib_ok = ok and report_on_lua_libdir_config(cfg.variables.LUA_LIBDIR) if not (inc_ok and lib_ok) then warn_bad_c_config() end @@ -359,16 +371,16 @@ function config_cmd.command(args) if args.value or args.unset then local scope = get_scope(args) - local ok, err = write_entries({ [args.key] = args.value or args.unset }, scope, args.unset) + local ok, err = write_entries({ [args.key] = args.value or "" }, scope, args.unset) if ok then if args.key == "variables.LUA_INCDIR" then - local ok = report_on_lua_incdir_config(args.value, lua_version) + local ok = report_on_lua_incdir_config(args.value) if not ok then warn_bad_c_config() end elseif args.key == "variables.LUA_LIBDIR" then - local ok = report_on_lua_libdir_config(args.value, lua_version) + local ok = report_on_lua_libdir_config(args.value) if not ok then warn_bad_c_config() end diff --git a/src/luarocks/cmd/config.tl b/src/luarocks/cmd/config.tl index 75e99198..6fe63471 100644 --- a/src/luarocks/cmd/config.tl +++ b/src/luarocks/cmd/config.tl @@ -18,8 +18,7 @@ local type Parser = argparse.Parser local type a = require("luarocks.core.types.args") local type Args = a.Args -local type p = require("luarocks.core.types.persist") -local type PersistableTable = p.PersistableTable +local type PersistableTable = require("luarocks.core.types.persist").PersistableTable function config_cmd.add_to_parser(parser: Parser) local cmd = parser:command("config", [[ @@ -93,9 +92,10 @@ local function config_file(conf: cfg.conf): boolean, string end end -local function traverse_varstring(var: string, tbl: PersistableTable, fn: function(PersistableTable, string): (boolean, string), missing_parent?: function): boolean, string - local k, rs = var:match("^%[([0-9]+)%]%.(.*)$") +local function traverse_varstring(var: string, tbl: PersistableTable, fn: function(PersistableTable, string | number): (boolean, string), missing_parent?: function(PersistableTable, string | number)): boolean, string + local k: string | number local r: string + k, r = var:match("^%[([0-9]+)%]%.(.*)$") if k then k = tonumber(k) else @@ -113,13 +113,13 @@ local function traverse_varstring(var: string, tbl: PersistableTable, fn: functi if tbl[k] then return traverse_varstring(r, tbl[k] as PersistableTable, fn, missing_parent) else - return nil, "Unknown entry " .. k + return nil, "Unknown entry " .. tostring(k) end end local i = var:match("^%[([0-9]+)%]$") if i then - local var = tonumber(i) + return fn(tbl, tonumber(i)) end return fn(tbl, var) @@ -160,7 +160,8 @@ local function infer_type(var: string): string return typ end -local function write_entries(keys: {string: any}, scope: string, do_unset: boolean): boolean, string +local function write_entries(keys: {string: string}, scope: string, do_unset: boolean): boolean, string + local wrote: PersistableTable = {} if scope == "project" and not cfg.config_files.project then return nil, "Current directory is not part of a project. You may want to run `luarocks init`." end @@ -173,12 +174,13 @@ local function write_entries(keys: {string: any}, scope: string, do_unset: boole end for var, val in util.sortedpairs(keys) do - traverse_varstring(var, tbl, function(t: PersistableTable, k: string): boolean, string + traverse_varstring(var, tbl, function(t: PersistableTable, k: string | number): boolean, string if do_unset then t[k] = nil + wrote[var] = "" else local typ = infer_type(var) - local v + local v: string | number | boolean if typ == "number" and tonumber(val) then v = tonumber(val) elseif typ == "boolean" and val == "true" then @@ -189,10 +191,10 @@ local function write_entries(keys: {string: any}, scope: string, do_unset: boole v = val end t[k] = v - keys[var] = v + wrote[var] = v end return true - end, function(p, k) + end, function(p: PersistableTable, k: string | number) p[k] = {} end) end @@ -205,11 +207,11 @@ local function write_entries(keys: {string: any}, scope: string, do_unset: boole ok, err = persist.save_from_table(file_name, tbl) if ok then print(do_unset and "Removed" or "Wrote") - for var, val in util.sortedpairs(keys) do + for var, val in util.sortedpairs(wrote) do if do_unset then print(("\t%s"):format(var)) else - if type(val) == "string" then + if val is string then print(("\t%s = %q"):format(var, val)) else print(("\t%s = %s"):format(var, tostring(val))) @@ -233,7 +235,7 @@ local function get_scope(args: Args): string or "user" end -local function report_on_lua_incdir_config(value: string, lua_version: string): boolean +local function report_on_lua_incdir_config(value: string): boolean local variables = { ["LUA_DIR"] = cfg.variables.LUA_DIR, ["LUA_BINDIR"] = cfg.variables.LUA_BINDIR, @@ -242,7 +244,7 @@ local function report_on_lua_incdir_config(value: string, lua_version: string): ["LUA"] = cfg.variables.LUA, } - local ok, err = deps.check_lua_incdir(variables, lua_version) --! + local ok, err = deps.check_lua_incdir(variables) if not ok then util.printerr() util.warning((err:gsub(" You can use.*", ""))) @@ -250,7 +252,7 @@ local function report_on_lua_incdir_config(value: string, lua_version: string): return ok end -local function report_on_lua_libdir_config(value: string, lua_version: string): boolean +local function report_on_lua_libdir_config(value: string): boolean local variables = { ["LUA_DIR"] = cfg.variables.LUA_DIR, ["LUA_BINDIR"] = cfg.variables.LUA_BINDIR, @@ -259,7 +261,7 @@ local function report_on_lua_libdir_config(value: string, lua_version: string): ["LUA"] = cfg.variables.LUA, } - local ok, err, _, err_files = deps.check_lua_libdir(variables, lua_version) --! + local ok, err, _, err_files = deps.check_lua_libdir(variables) if not ok then util.printerr() util.warning((err:gsub(" You can use.*", ""))) @@ -282,10 +284,9 @@ end --- Driver function for "config" command. -- @return boolean: True if succeeded, nil on errors. function config_cmd.command(args: Args): boolean, string - local lua_version = args.lua_version or cfg.lua_version - deps.check_lua_incdir(cfg.variables, lua_version) - deps.check_lua_libdir(cfg.variables, lua_version) + deps.check_lua_incdir(cfg.variables) + deps.check_lua_libdir(cfg.variables) -- deprecated flags if args.lua_incdir then @@ -352,8 +353,8 @@ function config_cmd.command(args: Args): boolean, string end local ok, err = write_entries(keys, scope, args.unset) if ok then - local inc_ok = report_on_lua_incdir_config(cfg.variables.LUA_INCDIR, lua_version) - local lib_ok = ok and report_on_lua_libdir_config(cfg.variables.LUA_LIBDIR, lua_version) + local inc_ok = report_on_lua_incdir_config(cfg.variables.LUA_INCDIR) + local lib_ok = ok and report_on_lua_libdir_config(cfg.variables.LUA_LIBDIR) if not (inc_ok and lib_ok) then warn_bad_c_config() end @@ -370,16 +371,16 @@ function config_cmd.command(args: Args): boolean, string if args.value or args.unset then local scope = get_scope(args) - local ok, err = write_entries({ [args.key] = args.value or args.unset }, scope, args.unset) + local ok, err = write_entries({ [args.key] = args.value or "" }, scope, args.unset) if ok then if args.key == "variables.LUA_INCDIR" then - local ok = report_on_lua_incdir_config(args.value, lua_version) + local ok = report_on_lua_incdir_config(args.value) if not ok then warn_bad_c_config() end elseif args.key == "variables.LUA_LIBDIR" then - local ok = report_on_lua_libdir_config(args.value, lua_version) + local ok = report_on_lua_libdir_config(args.value) if not ok then warn_bad_c_config() end diff --git a/src/luarocks/cmd/install-original.lua b/src/luarocks/cmd/install-original.lua new file mode 100644 index 00000000..e00b964d --- /dev/null +++ b/src/luarocks/cmd/install-original.lua @@ -0,0 +1,250 @@ +--- Module implementing the LuaRocks "install" command. +-- Installs binary rocks. +local install = {} + +local dir = require("luarocks.dir") +local path = require("luarocks.path") +local repos = require("luarocks.repos") +local fetch = require("luarocks.fetch") +local util = require("luarocks.util") +local fs = require("luarocks.fs") +local deps = require("luarocks.deps") +local repo_writer = require("luarocks.repo_writer") +local remove = require("luarocks.remove") +local search = require("luarocks.search") +local queries = require("luarocks.queries") +local cfg = require("luarocks.core.cfg") + +function install.add_to_parser(parser) + local cmd = parser:command("install", "Install a rock.", util.see_also()) -- luacheck: ignore 431 + + cmd:argument("rock", "The name of a rock to be fetched from a repository ".. + "or a filename of a locally available rock.") + :action(util.namespaced_name_action) + cmd:argument("version", "Version of the rock.") + :args("?") + + cmd:flag("--keep", "Do not remove previously installed versions of the ".. + "rock after building a new one. This behavior can be made permanent by ".. + "setting keep_other_versions=true in the configuration file.") + cmd:flag("--force", "If --keep is not specified, force removal of ".. + "previously installed versions if it would break dependencies. ".. + "If rock is already installed, reinstall it anyway.") + cmd:flag("--force-fast", "Like --force, but performs a forced removal ".. + "without reporting dependency issues.") + cmd:flag("--only-deps --deps-only", "Install only the dependencies of the rock.") + cmd:flag("--no-doc", "Install the rock without its documentation.") + cmd:flag("--verify", "Verify signature of the rockspec or src.rock being ".. + "built. If the rockspec or src.rock is being downloaded, LuaRocks will ".. + "attempt to download the signature as well. Otherwise, the signature ".. + "file should be already available locally in the same directory.\n".. + "You need the signer’s public key in your local keyring for this ".. + "option to work properly.") + cmd:flag("--check-lua-versions", "If the rock can't be found, check repository ".. + "and report if it is available for another Lua version.") + util.deps_mode_option(cmd) + cmd:flag("--no-manifest", "Skip creating/updating the manifest") + cmd:flag("--pin", "If the installed rock is a Lua module, create a ".. + "luarocks.lock file listing the exact versions of each dependency found for ".. + "this rock (recursively), and store it in the rock's directory. ".. + "Ignores any existing luarocks.lock file in the rock's sources.") + -- luarocks build options + parser:flag("--pack-binary-rock"):hidden(true) + parser:option("--branch"):hidden(true) + parser:flag("--sign"):hidden(true) +end + + +--- Install a binary rock. +-- @param rock_file string: local or remote filename of a rock. +-- @param opts table: installation options +-- @return (string, string) or (nil, string, [string]): Name and version of +-- installed rock if succeeded or nil and an error message followed by an error code. +function install.install_binary_rock(rock_file, opts) + assert(type(rock_file) == "string") + + local namespace = opts.namespace + local deps_mode = opts.deps_mode + + local name, version, arch = path.parse_name(rock_file) + if not name then + return nil, "Filename "..rock_file.." does not match format 'name-version-revision.arch.rock'." + end + + if arch ~= "all" and arch ~= cfg.arch then + return nil, "Incompatible architecture "..arch, "arch" + end + if repos.is_installed(name, version) then + if not (opts.force or opts.force_fast) then + util.printout(name .. " " .. version .. " is already installed in " .. path.root_dir(cfg.root_dir)) + util.printout("Use --force to reinstall.") + return name, version + end + repo_writer.delete_version(name, version, opts.deps_mode) + end + + local install_dir = path.install_dir(name, version) + + local rollback = util.schedule_function(function() + fs.delete(install_dir) + fs.remove_dir_if_empty(path.versions_dir(name)) + end) + + local ok, err, errcode = fetch.fetch_and_unpack_rock(rock_file, install_dir, opts.verify) + if not ok then return nil, err, errcode end + + local rockspec, err = fetch.load_rockspec(path.rockspec_file(name, version)) + if err then + return nil, "Failed loading rockspec for installed package: "..err, errcode + end + + if opts.deps_mode ~= "none" then + ok, err, errcode = deps.check_external_deps(rockspec, "install") + if err then return nil, err, errcode end + end + + if deps_mode ~= "none" then + local deplock_dir = fs.exists(dir.path(".", "luarocks.lock")) + and "." + or install_dir + ok, err, errcode = deps.fulfill_dependencies(rockspec, "dependencies", deps_mode, opts.verify, deplock_dir) + if err then return nil, err, errcode end + end + + ok, err = repo_writer.deploy_files(name, version, repos.should_wrap_bin_scripts(rockspec), deps_mode, namespace) + if err then return nil, err end + + util.remove_scheduled_function(rollback) + rollback = util.schedule_function(function() + repo_writer.delete_version(name, version, deps_mode) + end) + + ok, err = repos.run_hook(rockspec, "post_install") + if err then return nil, err end + + util.announce_install(rockspec) + util.remove_scheduled_function(rollback) + return name, version +end + +--- Installs the dependencies of a binary rock. +-- @param rock_file string: local or remote filename of a rock. +-- @param opts table: installation options +-- @return (string, string) or (nil, string, [string]): Name and version of +-- the rock whose dependencies were installed if succeeded or nil and an error message +-- followed by an error code. +function install.install_binary_rock_deps(rock_file, opts) + assert(type(rock_file) == "string") + + local name, version, arch = path.parse_name(rock_file) + if not name then + return nil, "Filename "..rock_file.." does not match format 'name-version-revision.arch.rock'." + end + + if arch ~= "all" and arch ~= cfg.arch then + return nil, "Incompatible architecture "..arch, "arch" + end + + local install_dir = path.install_dir(name, version) + + local ok, err, errcode = fetch.fetch_and_unpack_rock(rock_file, install_dir, opts.verify) + if not ok then return nil, err, errcode end + + local rockspec, err = fetch.load_rockspec(path.rockspec_file(name, version)) + if err then + return nil, "Failed loading rockspec for installed package: "..err, errcode + end + + local deplock_dir = fs.exists(dir.path(".", "luarocks.lock")) + and "." + or install_dir + ok, err, errcode = deps.fulfill_dependencies(rockspec, "dependencies", opts.deps_mode, opts.verify, deplock_dir) + if err then return nil, err, errcode end + + util.printout() + util.printout("Successfully installed dependencies for " ..name.." "..version) + + return name, version +end + +local function install_rock_file_deps(filename, opts) + + local name, version = install.install_binary_rock_deps(filename, opts) + if not name then return nil, version end + + deps.check_dependencies(nil, opts.deps_mode) + return name, version +end + +local function install_rock_file(filename, opts) + assert(type(filename) == "string") + + local name, version = install.install_binary_rock(filename, opts) + if not name then return nil, version end + + if opts.no_doc then + util.remove_doc_dir(name, version) + end + + if (not opts.keep) and not cfg.keep_other_versions then + local ok, err, warn = remove.remove_other_versions(name, version, opts.force, opts.force_fast) + if not ok then + return nil, err + elseif warn then + util.printerr(err) + end + end + + deps.check_dependencies(nil, opts.deps_mode) + return name, version +end + +--- Driver function for the "install" command. +-- If an URL or pathname to a binary rock is given, fetches and installs it. +-- If a rockspec or a source rock is given, forwards the request to the "build" +-- command. +-- If a package name is given, forwards the request to "search" and, +-- if returned a result, installs the matching rock. +-- @return boolean or (nil, string, exitcode): True if installation was +-- successful, nil and an error message otherwise. exitcode is optionally returned. +function install.command(args) + if args.rock:match("%.rockspec$") or args.rock:match("%.src%.rock$") then + local build = require("luarocks.cmd.build") + return build.command(args) + elseif args.rock:match("%.rock$") then + local deps_mode = deps.get_deps_mode(args) + local opts = { + namespace = args.namespace, + keep = not not args.keep, + force = not not args.force, + force_fast = not not args.force_fast, + no_doc = not not args.no_doc, + deps_mode = deps_mode, + verify = not not args.verify, + } + if args.only_deps then + return install_rock_file_deps(args.rock, opts) + else + return install_rock_file(args.rock, opts) + end + else + local url, err = search.find_rock_checking_lua_versions( + queries.new(args.rock, args.namespace, args.version), + args.check_lua_versions) + if not url then + return nil, err + end + util.printout("Installing "..url) + args.rock = url + return install.command(args) + end +end + +install.needs_lock = function(args) + if args.pack_binary_rock then + return false + end + return true +end + +return install diff --git a/src/luarocks/cmd/install.lua b/src/luarocks/cmd/install.lua index e00b964d..11e36b8c 100644 --- a/src/luarocks/cmd/install.lua +++ b/src/luarocks/cmd/install.lua @@ -1,7 +1,9 @@ ---- Module implementing the LuaRocks "install" command. --- Installs binary rocks. +local _tl_compat; if (tonumber((_VERSION or ''):match('[%d.]*$')) or 0) < 5.3 then local p, m = pcall(require, 'compat53.module'); if p then _tl_compat = m end end; local assert = _tl_compat and _tl_compat.assert or assert + local install = {} + + local dir = require("luarocks.dir") local path = require("luarocks.path") local repos = require("luarocks.repos") @@ -15,51 +17,61 @@ local search = require("luarocks.search") local queries = require("luarocks.queries") local cfg = require("luarocks.core.cfg") +local argparse = require("luarocks.vendor.argparse") + + + + + + + + + function install.add_to_parser(parser) - local cmd = parser:command("install", "Install a rock.", util.see_also()) -- luacheck: ignore 431 - - cmd:argument("rock", "The name of a rock to be fetched from a repository ".. - "or a filename of a locally available rock.") - :action(util.namespaced_name_action) - cmd:argument("version", "Version of the rock.") - :args("?") - - cmd:flag("--keep", "Do not remove previously installed versions of the ".. - "rock after building a new one. This behavior can be made permanent by ".. - "setting keep_other_versions=true in the configuration file.") - cmd:flag("--force", "If --keep is not specified, force removal of ".. - "previously installed versions if it would break dependencies. ".. - "If rock is already installed, reinstall it anyway.") - cmd:flag("--force-fast", "Like --force, but performs a forced removal ".. - "without reporting dependency issues.") + local cmd = parser:command("install", "Install a rock.", util.see_also()) + + cmd:argument("rock", "The name of a rock to be fetched from a repository " .. + "or a filename of a locally available rock."): + action(util.namespaced_name_action) + cmd:argument("version", "Version of the rock."): + args("?") + + cmd:flag("--keep", "Do not remove previously installed versions of the " .. + "rock after building a new one. This behavior can be made permanent by " .. + "setting keep_other_versions=true in the configuration file.") + cmd:flag("--force", "If --keep is not specified, force removal of " .. + "previously installed versions if it would break dependencies. " .. + "If rock is already installed, reinstall it anyway.") + cmd:flag("--force-fast", "Like --force, but performs a forced removal " .. + "without reporting dependency issues.") cmd:flag("--only-deps --deps-only", "Install only the dependencies of the rock.") cmd:flag("--no-doc", "Install the rock without its documentation.") - cmd:flag("--verify", "Verify signature of the rockspec or src.rock being ".. - "built. If the rockspec or src.rock is being downloaded, LuaRocks will ".. - "attempt to download the signature as well. Otherwise, the signature ".. - "file should be already available locally in the same directory.\n".. - "You need the signer’s public key in your local keyring for this ".. - "option to work properly.") - cmd:flag("--check-lua-versions", "If the rock can't be found, check repository ".. - "and report if it is available for another Lua version.") + cmd:flag("--verify", "Verify signature of the rockspec or src.rock being " .. + "built. If the rockspec or src.rock is being downloaded, LuaRocks will " .. + "attempt to download the signature as well. Otherwise, the signature " .. + "file should be already available locally in the same directory.\n" .. + "You need the signer’s public key in your local keyring for this " .. + "option to work properly.") + cmd:flag("--check-lua-versions", "If the rock can't be found, check repository " .. + "and report if it is available for another Lua version.") util.deps_mode_option(cmd) cmd:flag("--no-manifest", "Skip creating/updating the manifest") - cmd:flag("--pin", "If the installed rock is a Lua module, create a ".. - "luarocks.lock file listing the exact versions of each dependency found for ".. - "this rock (recursively), and store it in the rock's directory. ".. - "Ignores any existing luarocks.lock file in the rock's sources.") - -- luarocks build options + cmd:flag("--pin", "If the installed rock is a Lua module, create a " .. + "luarocks.lock file listing the exact versions of each dependency found for " .. + "this rock (recursively), and store it in the rock's directory. " .. + "Ignores any existing luarocks.lock file in the rock's sources.") + parser:flag("--pack-binary-rock"):hidden(true) parser:option("--branch"):hidden(true) parser:flag("--sign"):hidden(true) end ---- Install a binary rock. --- @param rock_file string: local or remote filename of a rock. --- @param opts table: installation options --- @return (string, string) or (nil, string, [string]): Name and version of --- installed rock if succeeded or nil and an error message followed by an error code. + + + + + function install.install_binary_rock(rock_file, opts) assert(type(rock_file) == "string") @@ -68,11 +80,11 @@ function install.install_binary_rock(rock_file, opts) local name, version, arch = path.parse_name(rock_file) if not name then - return nil, "Filename "..rock_file.." does not match format 'name-version-revision.arch.rock'." + return nil, "Filename " .. rock_file .. " does not match format 'name-version-revision.arch.rock'." end if arch ~= "all" and arch ~= cfg.arch then - return nil, "Incompatible architecture "..arch, "arch" + return nil, "Incompatible architecture " .. arch, "arch" end if repos.is_installed(name, version) then if not (opts.force or opts.force_fast) then @@ -89,13 +101,13 @@ function install.install_binary_rock(rock_file, opts) fs.delete(install_dir) fs.remove_dir_if_empty(path.versions_dir(name)) end) - - local ok, err, errcode = fetch.fetch_and_unpack_rock(rock_file, install_dir, opts.verify) - if not ok then return nil, err, errcode end + local ok + local oks, err, errcode = fetch.fetch_and_unpack_rock(rock_file, install_dir, opts.verify) + if not oks then return nil, err, errcode end local rockspec, err = fetch.load_rockspec(path.rockspec_file(name, version)) if err then - return nil, "Failed loading rockspec for installed package: "..err, errcode + return nil, "Failed loading rockspec for installed package: " .. err, errcode end if opts.deps_mode ~= "none" then @@ -104,9 +116,9 @@ function install.install_binary_rock(rock_file, opts) end if deps_mode ~= "none" then - local deplock_dir = fs.exists(dir.path(".", "luarocks.lock")) - and "." - or install_dir + local deplock_dir = fs.exists(dir.path(".", "luarocks.lock")) and + "." or + install_dir ok, err, errcode = deps.fulfill_dependencies(rockspec, "dependencies", deps_mode, opts.verify, deplock_dir) if err then return nil, err, errcode end end @@ -127,42 +139,43 @@ function install.install_binary_rock(rock_file, opts) return name, version end ---- Installs the dependencies of a binary rock. --- @param rock_file string: local or remote filename of a rock. --- @param opts table: installation options --- @return (string, string) or (nil, string, [string]): Name and version of --- the rock whose dependencies were installed if succeeded or nil and an error message --- followed by an error code. + + + + + + function install.install_binary_rock_deps(rock_file, opts) assert(type(rock_file) == "string") local name, version, arch = path.parse_name(rock_file) if not name then - return nil, "Filename "..rock_file.." does not match format 'name-version-revision.arch.rock'." + return nil, "Filename " .. rock_file .. " does not match format 'name-version-revision.arch.rock'." end if arch ~= "all" and arch ~= cfg.arch then - return nil, "Incompatible architecture "..arch, "arch" + return nil, "Incompatible architecture " .. arch, "arch" end local install_dir = path.install_dir(name, version) - local ok, err, errcode = fetch.fetch_and_unpack_rock(rock_file, install_dir, opts.verify) - if not ok then return nil, err, errcode end + local ok + local oks, err, errcode = fetch.fetch_and_unpack_rock(rock_file, install_dir, opts.verify) + if not oks then return nil, err, errcode end local rockspec, err = fetch.load_rockspec(path.rockspec_file(name, version)) if err then - return nil, "Failed loading rockspec for installed package: "..err, errcode + return nil, "Failed loading rockspec for installed package: " .. err, errcode end - local deplock_dir = fs.exists(dir.path(".", "luarocks.lock")) - and "." - or install_dir + local deplock_dir = fs.exists(dir.path(".", "luarocks.lock")) and + "." or + install_dir ok, err, errcode = deps.fulfill_dependencies(rockspec, "dependencies", opts.deps_mode, opts.verify, deplock_dir) if err then return nil, err, errcode end util.printout() - util.printout("Successfully installed dependencies for " ..name.." "..version) + util.printout("Successfully installed dependencies for " .. name .. " " .. version) return name, version end @@ -173,11 +186,10 @@ local function install_rock_file_deps(filename, opts) if not name then return nil, version end deps.check_dependencies(nil, opts.deps_mode) - return name, version + return true end local function install_rock_file(filename, opts) - assert(type(filename) == "string") local name, version = install.install_binary_rock(filename, opts) if not name then return nil, version end @@ -196,17 +208,17 @@ local function install_rock_file(filename, opts) end deps.check_dependencies(nil, opts.deps_mode) - return name, version + return true end ---- Driver function for the "install" command. --- If an URL or pathname to a binary rock is given, fetches and installs it. --- If a rockspec or a source rock is given, forwards the request to the "build" --- command. --- If a package name is given, forwards the request to "search" and, --- if returned a result, installs the matching rock. --- @return boolean or (nil, string, exitcode): True if installation was --- successful, nil and an error message otherwise. exitcode is optionally returned. + + + + + + + + function install.command(args) if args.rock:match("%.rockspec$") or args.rock:match("%.src%.rock$") then local build = require("luarocks.cmd.build") @@ -229,12 +241,12 @@ function install.command(args) end else local url, err = search.find_rock_checking_lua_versions( - queries.new(args.rock, args.namespace, args.version), - args.check_lua_versions) + queries.new(args.rock, args.namespace, args.version), + args.check_lua_versions) if not url then return nil, err end - util.printout("Installing "..url) + util.printout("Installing " .. url) args.rock = url return install.command(args) end @@ -247,4 +259,6 @@ install.needs_lock = function(args) return true end +deps.installer = install.command + return install diff --git a/src/luarocks/cmd/install.tl b/src/luarocks/cmd/install.tl index 157ef772..ca3d3916 100644 --- a/src/luarocks/cmd/install.tl +++ b/src/luarocks/cmd/install.tl @@ -180,16 +180,16 @@ function install.install_binary_rock_deps(rock_file: string, opts: IOpts): strin return name, version end -local function install_rock_file_deps(filename: string, opts: IOpts): string, string +local function install_rock_file_deps(filename: string, opts: IOpts): boolean, string local name, version = install.install_binary_rock_deps(filename, opts) if not name then return nil, version end deps.check_dependencies(nil, opts.deps_mode) - return name, version + return true end -local function install_rock_file(filename: string, opts: IOpts): string, string +local function install_rock_file(filename: string, opts: IOpts): boolean, string local name, version = install.install_binary_rock(filename, opts) if not name then return nil, version end @@ -208,7 +208,7 @@ local function install_rock_file(filename: string, opts: IOpts): string, string end deps.check_dependencies(nil, opts.deps_mode) - return name, version + return true end --- Driver function for the "install" command. diff --git a/src/luarocks/cmd/lint-original.lua b/src/luarocks/cmd/lint-original.lua new file mode 100644 index 00000000..421803e1 --- /dev/null +++ b/src/luarocks/cmd/lint-original.lua @@ -0,0 +1,50 @@ + +--- Module implementing the LuaRocks "lint" command. +-- Utility function that checks syntax of the rockspec. +local lint = {} + +local util = require("luarocks.util") +local download = require("luarocks.download") +local fetch = require("luarocks.fetch") + +function lint.add_to_parser(parser) + local cmd = parser:command("lint", "Check syntax of a rockspec.\n\n".. + "Returns success if the text of the rockspec is syntactically correct, else failure.", + util.see_also()) + :summary("Check syntax of a rockspec.") + + cmd:argument("rockspec", "The rockspec to check.") +end + +function lint.command(args) + + local filename = args.rockspec + if not filename:match(".rockspec$") then + local err + filename, err = download.download_file("rockspec", filename:lower()) + if not filename then + return nil, err + end + end + + local rs, err = fetch.load_local_rockspec(filename) + if not rs then + return nil, "Failed loading rockspec: "..err + end + + local ok = true + + -- This should have been done in the type checker, + -- but it would break compatibility of other commands. + -- Making 'lint' alone be stricter shouldn't be a problem, + -- because extra-strict checks is what lint-type commands + -- are all about. + if not rs.description or not rs.description.license then + util.printerr("Rockspec has no description.license field.") + ok = false + end + + return ok, ok or filename.." failed consistency checks." +end + +return lint diff --git a/src/luarocks/cmd/lint.lua b/src/luarocks/cmd/lint.lua index 421803e1..cfff66c0 100644 --- a/src/luarocks/cmd/lint.lua +++ b/src/luarocks/cmd/lint.lua @@ -1,17 +1,24 @@ ---- Module implementing the LuaRocks "lint" command. --- Utility function that checks syntax of the rockspec. + + local lint = {} + local util = require("luarocks.util") local download = require("luarocks.download") local fetch = require("luarocks.fetch") +local argparse = require("luarocks.vendor.argparse") + + + + + function lint.add_to_parser(parser) - local cmd = parser:command("lint", "Check syntax of a rockspec.\n\n".. - "Returns success if the text of the rockspec is syntactically correct, else failure.", - util.see_also()) - :summary("Check syntax of a rockspec.") + local cmd = parser:command("lint", "Check syntax of a rockspec.\n\n" .. + "Returns success if the text of the rockspec is syntactically correct, else failure.", + util.see_also()): + summary("Check syntax of a rockspec.") cmd:argument("rockspec", "The rockspec to check.") end @@ -29,22 +36,26 @@ function lint.command(args) local rs, err = fetch.load_local_rockspec(filename) if not rs then - return nil, "Failed loading rockspec: "..err + return nil, "Failed loading rockspec: " .. err end local ok = true - -- This should have been done in the type checker, - -- but it would break compatibility of other commands. - -- Making 'lint' alone be stricter shouldn't be a problem, - -- because extra-strict checks is what lint-type commands - -- are all about. + + + + + if not rs.description or not rs.description.license then util.printerr("Rockspec has no description.license field.") ok = false end - return ok, ok or filename.." failed consistency checks." + if ok then + return ok + end + + return nil, filename .. " failed consistency checks." end return lint diff --git a/src/luarocks/cmd/lint.tl b/src/luarocks/cmd/lint.tl index 41fdf096..f3845bfa 100644 --- a/src/luarocks/cmd/lint.tl +++ b/src/luarocks/cmd/lint.tl @@ -51,7 +51,11 @@ function lint.command(args: Args): boolean, string, string ok = false end - return ok, ok or filename.." failed consistency checks." + if ok then + return ok + end + + return nil, filename.." failed consistency checks." end return lint diff --git a/src/luarocks/cmd/make-original.lua b/src/luarocks/cmd/make-original.lua new file mode 100644 index 00000000..811078b8 --- /dev/null +++ b/src/luarocks/cmd/make-original.lua @@ -0,0 +1,165 @@ + +--- Module implementing the LuaRocks "make" command. +-- Builds sources in the current directory, but unlike "build", +-- it does not fetch sources, etc., assuming everything is +-- available in the current directory. +local make = {} + +local build = require("luarocks.build") +local util = require("luarocks.util") +local cfg = require("luarocks.core.cfg") +local fetch = require("luarocks.fetch") +local pack = require("luarocks.pack") +local remove = require("luarocks.remove") +local deps = require("luarocks.deps") +local dir = require("luarocks.dir") +local fs = require("luarocks.fs") + +function make.cmd_options(parser) + parser:flag("--no-install", "Do not install the rock.") + parser:flag("--no-doc", "Install the rock without its documentation.") + parser:flag("--pack-binary-rock", "Do not install rock. Instead, produce a ".. + ".rock file with the contents of compilation in the current directory.") + parser:flag("--keep", "Do not remove previously installed versions of the ".. + "rock after building a new one. This behavior can be made permanent by ".. + "setting keep_other_versions=true in the configuration file.") + parser:flag("--force", "If --keep is not specified, force removal of ".. + "previously installed versions if it would break dependencies. ".. + "If rock is already installed, reinstall it anyway.") + parser:flag("--force-fast", "Like --force, but performs a forced removal ".. + "without reporting dependency issues.") + parser:flag("--verify", "Verify signature of the rockspec or src.rock being ".. + "built. If the rockspec or src.rock is being downloaded, LuaRocks will ".. + "attempt to download the signature as well. Otherwise, the signature ".. + "file should be already available locally in the same directory.\n".. + "You need the signer’s public key in your local keyring for this ".. + "option to work properly.") + parser:flag("--sign", "To be used with --pack-binary-rock. Also produce a ".. + "signature file for the generated .rock file.") + parser:flag("--check-lua-versions", "If the rock can't be found, check repository ".. + "and report if it is available for another Lua version.") + parser:flag("--pin", "Pin the exact dependencies used for the rockspec".. + "being built into a luarocks.lock file in the current directory.") + parser:flag("--no-manifest", "Skip creating/updating the manifest") + parser:flag("--only-deps --deps-only", "Install only the dependencies of the rock.") + util.deps_mode_option(parser) +end + +function make.add_to_parser(parser) + -- luacheck: push ignore 431 + local cmd = parser:command("make", [[ +Builds sources in the current directory, but unlike "build", it does not fetch +sources, etc., assuming everything is available in the current directory. If no +argument is given, it looks for a rockspec in the current directory and in +"rockspec/" and "rockspecs/" subdirectories, picking the rockspec with newest +version or without version name. If rockspecs for different rocks are found or +there are several rockspecs without version, you must specify which to use, +through the command-line. + +This command is useful as a tool for debugging rockspecs. +To install rocks, you'll normally want to use the "install" and "build" +commands. See the help on those for details. + +If the current directory contains a luarocks.lock file, it is used as the +authoritative source for exact version of dependencies. The --pin flag +overrides and recreates this file scanning dependency based on ranges. +]], util.see_also()) + :summary("Compile package in current directory using a rockspec.") + -- luacheck: pop + + cmd:argument("rockspec", "Rockspec for the rock to build.") + :args("?") + + make.cmd_options(cmd) +end + +--- Driver function for "make" command. +-- @return boolean or (nil, string, exitcode): True if build was successful; nil and an +-- error message otherwise. exitcode is optionally returned. +function make.command(args) + local rockspec_filename = args.rockspec + if not rockspec_filename then + local err + rockspec_filename, err = util.get_default_rockspec() + if not rockspec_filename then + return nil, err + end + end + if not rockspec_filename:match("rockspec$") then + return nil, "Invalid argument: 'make' takes a rockspec as a parameter. "..util.see_help("make") + end + + local cwd = fs.absolute_name(dir.path(".")) + local rockspec, err, errcode = fetch.load_rockspec(rockspec_filename) + if not rockspec then + return nil, err + end + + local name, namespace = util.split_namespace(rockspec.name) + namespace = namespace or args.namespace + + local opts = { + need_to_fetch = false, + minimal_mode = true, + deps_mode = deps.get_deps_mode(args), + build_only_deps = not not (args.only_deps and not args.pack_binary_rock), + namespace = namespace, + branch = args.branch, + verify = not not args.verify, + check_lua_versions = not not args.check_lua_versions, + pin = not not args.pin, + rebuild = true, + no_install = not not args.no_install + } + + if args.sign and not args.pack_binary_rock then + return nil, "In the make command, --sign is meant to be used only with --pack-binary-rock" + end + + if args.no_install then + return build.build_rockspec(rockspec, opts, cwd) + elseif args.pack_binary_rock then + return pack.pack_binary_rock(name, namespace, rockspec.version, args.sign, function() + local name, version = build.build_rockspec(rockspec, opts, cwd) -- luacheck: ignore 431 + if name and args.no_doc then + util.remove_doc_dir(name, version) + end + return name, version + end) + else + local ok, err = build.build_rockspec(rockspec, opts, cwd) + if not ok then return nil, err end + local name, version = ok, err -- luacheck: ignore 421 + + if opts.build_only_deps then + util.printout("Stopping after installing dependencies for " ..name.." "..version) + util.printout() + return name, version + end + + if args.no_doc then + util.remove_doc_dir(name, version) + end + + if (not args.keep) and not cfg.keep_other_versions then + local ok, err, warn = remove.remove_other_versions(name, version, args.force, args.force_fast) + if not ok then + return nil, err + elseif warn then + util.printerr(warn) + end + end + + deps.check_dependencies(nil, deps.get_deps_mode(args)) + return name, version + end +end + +make.needs_lock = function(args) + if args.pack_binary_rock or args.no_install then + return false + end + return true +end + +return make diff --git a/src/luarocks/cmd/make.lua b/src/luarocks/cmd/make.lua index 811078b8..f373c94e 100644 --- a/src/luarocks/cmd/make.lua +++ b/src/luarocks/cmd/make.lua @@ -1,10 +1,12 @@ ---- Module implementing the LuaRocks "make" command. --- Builds sources in the current directory, but unlike "build", --- it does not fetch sources, etc., assuming everything is --- available in the current directory. + + + + local make = {} + + local build = require("luarocks.build") local util = require("luarocks.util") local cfg = require("luarocks.core.cfg") @@ -15,38 +17,47 @@ local deps = require("luarocks.deps") local dir = require("luarocks.dir") local fs = require("luarocks.fs") +local argparse = require("luarocks.vendor.argparse") + + + + + + + + function make.cmd_options(parser) parser:flag("--no-install", "Do not install the rock.") parser:flag("--no-doc", "Install the rock without its documentation.") - parser:flag("--pack-binary-rock", "Do not install rock. Instead, produce a ".. - ".rock file with the contents of compilation in the current directory.") - parser:flag("--keep", "Do not remove previously installed versions of the ".. - "rock after building a new one. This behavior can be made permanent by ".. - "setting keep_other_versions=true in the configuration file.") - parser:flag("--force", "If --keep is not specified, force removal of ".. - "previously installed versions if it would break dependencies. ".. - "If rock is already installed, reinstall it anyway.") - parser:flag("--force-fast", "Like --force, but performs a forced removal ".. - "without reporting dependency issues.") - parser:flag("--verify", "Verify signature of the rockspec or src.rock being ".. - "built. If the rockspec or src.rock is being downloaded, LuaRocks will ".. - "attempt to download the signature as well. Otherwise, the signature ".. - "file should be already available locally in the same directory.\n".. - "You need the signer’s public key in your local keyring for this ".. - "option to work properly.") - parser:flag("--sign", "To be used with --pack-binary-rock. Also produce a ".. - "signature file for the generated .rock file.") - parser:flag("--check-lua-versions", "If the rock can't be found, check repository ".. - "and report if it is available for another Lua version.") - parser:flag("--pin", "Pin the exact dependencies used for the rockspec".. - "being built into a luarocks.lock file in the current directory.") + parser:flag("--pack-binary-rock", "Do not install rock. Instead, produce a " .. + ".rock file with the contents of compilation in the current directory.") + parser:flag("--keep", "Do not remove previously installed versions of the " .. + "rock after building a new one. This behavior can be made permanent by " .. + "setting keep_other_versions=true in the configuration file.") + parser:flag("--force", "If --keep is not specified, force removal of " .. + "previously installed versions if it would break dependencies. " .. + "If rock is already installed, reinstall it anyway.") + parser:flag("--force-fast", "Like --force, but performs a forced removal " .. + "without reporting dependency issues.") + parser:flag("--verify", "Verify signature of the rockspec or src.rock being " .. + "built. If the rockspec or src.rock is being downloaded, LuaRocks will " .. + "attempt to download the signature as well. Otherwise, the signature " .. + "file should be already available locally in the same directory.\n" .. + "You need the signer's public key in your local keyring for this " .. + "option to work properly.") + parser:flag("--sign", "To be used with --pack-binary-rock. Also produce a " .. + "signature file for the generated .rock file.") + parser:flag("--check-lua-versions", "If the rock can't be found, check repository " .. + "and report if it is available for another Lua version.") + parser:flag("--pin", "Pin the exact dependencies used for the rockspec" .. + "being built into a luarocks.lock file in the current directory.") parser:flag("--no-manifest", "Skip creating/updating the manifest") parser:flag("--only-deps --deps-only", "Install only the dependencies of the rock.") util.deps_mode_option(parser) end function make.add_to_parser(parser) - -- luacheck: push ignore 431 + local cmd = parser:command("make", [[ Builds sources in the current directory, but unlike "build", it does not fetch sources, etc., assuming everything is available in the current directory. If no @@ -63,20 +74,21 @@ commands. See the help on those for details. If the current directory contains a luarocks.lock file, it is used as the authoritative source for exact version of dependencies. The --pin flag overrides and recreates this file scanning dependency based on ranges. -]], util.see_also()) - :summary("Compile package in current directory using a rockspec.") - -- luacheck: pop +]], util.see_also()): + summary("Compile package in current directory using a rockspec.") + - cmd:argument("rockspec", "Rockspec for the rock to build.") - :args("?") + cmd:argument("rockspec", "Rockspec for the rock to build."): + args("?") make.cmd_options(cmd) end ---- Driver function for "make" command. --- @return boolean or (nil, string, exitcode): True if build was successful; nil and an --- error message otherwise. exitcode is optionally returned. + + + function make.command(args) + local name, namespace, version local rockspec_filename = args.rockspec if not rockspec_filename then local err @@ -86,7 +98,7 @@ function make.command(args) end end if not rockspec_filename:match("rockspec$") then - return nil, "Invalid argument: 'make' takes a rockspec as a parameter. "..util.see_help("make") + return nil, "Invalid argument: 'make' takes a rockspec as a parameter. " .. util.see_help("make") end local cwd = fs.absolute_name(dir.path(".")) @@ -95,7 +107,7 @@ function make.command(args) return nil, err end - local name, namespace = util.split_namespace(rockspec.name) + name, namespace = util.split_namespace(rockspec.name) namespace = namespace or args.namespace local opts = { @@ -109,7 +121,7 @@ function make.command(args) check_lua_versions = not not args.check_lua_versions, pin = not not args.pin, rebuild = true, - no_install = not not args.no_install + no_install = not not args.no_install, } if args.sign and not args.pack_binary_rock then @@ -117,10 +129,15 @@ function make.command(args) end if args.no_install then - return build.build_rockspec(rockspec, opts, cwd) + name, version = build.build_rockspec(rockspec, opts, cwd) + if name then + return true + else + return nil, version + end elseif args.pack_binary_rock then return pack.pack_binary_rock(name, namespace, rockspec.version, args.sign, function() - local name, version = build.build_rockspec(rockspec, opts, cwd) -- luacheck: ignore 431 + name, version = build.build_rockspec(rockspec, opts, cwd) if name and args.no_doc then util.remove_doc_dir(name, version) end @@ -129,12 +146,12 @@ function make.command(args) else local ok, err = build.build_rockspec(rockspec, opts, cwd) if not ok then return nil, err end - local name, version = ok, err -- luacheck: ignore 421 + name, version = ok, err if opts.build_only_deps then - util.printout("Stopping after installing dependencies for " ..name.." "..version) + util.printout("Stopping after installing dependencies for " .. name .. " " .. version) util.printout() - return name, version + return name ~= nil, version end if args.no_doc then @@ -151,7 +168,7 @@ function make.command(args) end deps.check_dependencies(nil, deps.get_deps_mode(args)) - return name, version + return name ~= nil, version end end diff --git a/src/luarocks/cmd/make.tl b/src/luarocks/cmd/make.tl index 4d29c6d6..17af28ae 100644 --- a/src/luarocks/cmd/make.tl +++ b/src/luarocks/cmd/make.tl @@ -88,6 +88,7 @@ end -- @return boolean or (nil, string, exitcode): True if build was successful; nil and an -- error message otherwise. exitcode is optionally returned. function make.command(args: Args): boolean, string + local name, namespace, version: string, string, string local rockspec_filename = args.rockspec if not rockspec_filename then local err: string @@ -106,7 +107,7 @@ function make.command(args: Args): boolean, string return nil, err end - local name, namespace = util.split_namespace(rockspec.name) + name, namespace = util.split_namespace(rockspec.name) namespace = namespace or args.namespace local opts: BOpts = { @@ -128,10 +129,15 @@ function make.command(args: Args): boolean, string end if args.no_install then - return build.build_rockspec(rockspec, opts, cwd) + name, version = build.build_rockspec(rockspec, opts, cwd) + if name then + return true + else + return nil, version + end elseif args.pack_binary_rock then return pack.pack_binary_rock(name, namespace, rockspec.version, args.sign, function(): string, string - local name, version = build.build_rockspec(rockspec, opts, cwd) -- luacheck: ignore 431 + name, version = build.build_rockspec(rockspec, opts, cwd) -- luacheck: ignore 431 if name and args.no_doc then util.remove_doc_dir(name, version) end @@ -140,12 +146,12 @@ function make.command(args: Args): boolean, string else local ok, err = build.build_rockspec(rockspec, opts, cwd) if not ok then return nil, err end - local name, version = ok, err -- luacheck: ignore 421 + name, version = ok, err -- luacheck: ignore 421 if opts.build_only_deps then util.printout("Stopping after installing dependencies for " ..name.." "..version) util.printout() - return name, version + return name ~= nil, version end if args.no_doc then @@ -162,7 +168,7 @@ function make.command(args: Args): boolean, string end deps.check_dependencies(nil, deps.get_deps_mode(args)) - return name, version + return name ~= nil, version end end diff --git a/src/luarocks/cmd/new_version-original.lua b/src/luarocks/cmd/new_version-original.lua new file mode 100644 index 00000000..2ec084e0 --- /dev/null +++ b/src/luarocks/cmd/new_version-original.lua @@ -0,0 +1,228 @@ + +--- Module implementing the LuaRocks "new_version" command. +-- Utility function that writes a new rockspec, updating data from a previous one. +local new_version = {} + +local util = require("luarocks.util") +local download = require("luarocks.download") +local fetch = require("luarocks.fetch") +local persist = require("luarocks.persist") +local fs = require("luarocks.fs") +local dir = require("luarocks.dir") +local type_rockspec = require("luarocks.type.rockspec") + +function new_version.add_to_parser(parser) + local cmd = parser:command("new_version", [[ +This is a utility function that writes a new rockspec, updating data from a +previous one. + +If a package name is given, it downloads the latest rockspec from the default +server. If a rockspec is given, it uses it instead. If no argument is given, it +looks for a rockspec same way 'luarocks make' does. + +If the version number is not given and tag is passed using --tag, it is used as +the version, with 'v' removed from beginning. Otherwise, it only increments the +revision number of the given (or downloaded) rockspec. + +If a URL is given, it replaces the one from the old rockspec with the given URL. +If a URL is not given and a new version is given, it tries to guess the new URL +by replacing occurrences of the version number in the URL or tag; if the guessed +URL is invalid, the old URL is restored. It also tries to download the new URL +to determine the new MD5 checksum. + +If a tag is given, it replaces the one from the old rockspec. If there is an old +tag but no new one passed, it is guessed in the same way URL is. + +If a directory is not given, it defaults to the current directory. + +WARNING: it writes the new rockspec to the given directory, overwriting the file +if it already exists.]], util.see_also()) + :summary("Auto-write a rockspec for a new version of a rock.") + + cmd:argument("rock", "Package name or rockspec.") + :args("?") + cmd:argument("new_version", "New version of the rock.") + :args("?") + cmd:argument("new_url", "New URL of the rock.") + :args("?") + + cmd:option("--dir", "Output directory for the new rockspec.") + cmd:option("--tag", "New SCM tag.") +end + + +local function try_replace(tbl, field, old, new) + if not tbl[field] then + return false + end + local old_field = tbl[field] + local new_field = tbl[field]:gsub(old, new) + if new_field ~= old_field then + util.printout("Guessing new '"..field.."' field as "..new_field) + tbl[field] = new_field + return true + end + return false +end + +-- Try to download source file using URL from a rockspec. +-- If it specified MD5, update it. +-- @return (true, false) if MD5 was not specified or it stayed same, +-- (true, true) if MD5 changed, (nil, string) on error. +local function check_url_and_update_md5(out_rs, invalid_is_error) + local file, temp_dir = fetch.fetch_url_at_temp_dir(out_rs.source.url, "luarocks-new-version-"..out_rs.package) + if not file then + if invalid_is_error then + return nil, "invalid URL - "..temp_dir + end + util.warning("invalid URL - "..temp_dir) + return true, false + end + do + local inferred_dir, found_dir = fetch.find_base_dir(file, temp_dir, out_rs.source.url, out_rs.source.dir) + if not inferred_dir then + return nil, found_dir + end + + if found_dir and found_dir ~= inferred_dir then + out_rs.source.dir = found_dir + end + end + if file then + if out_rs.source.md5 then + util.printout("File successfully downloaded. Updating MD5 checksum...") + local new_md5, err = fs.get_md5(file) + if not new_md5 then + return nil, err + end + local old_md5 = out_rs.source.md5 + out_rs.source.md5 = new_md5 + return true, new_md5 ~= old_md5 + else + util.printout("File successfully downloaded.") + return true, false + end + end +end + +local function update_source_section(out_rs, url, tag, old_ver, new_ver) + if tag then + out_rs.source.tag = tag + end + if url then + out_rs.source.url = url + return check_url_and_update_md5(out_rs) + end + if new_ver == old_ver then + return true + end + if out_rs.source.dir then + try_replace(out_rs.source, "dir", old_ver, new_ver) + end + if out_rs.source.file then + try_replace(out_rs.source, "file", old_ver, new_ver) + end + + local old_url = out_rs.source.url + if try_replace(out_rs.source, "url", old_ver, new_ver) then + local ok, md5_changed = check_url_and_update_md5(out_rs, true) + if ok then + return ok, md5_changed + end + out_rs.source.url = old_url + end + if tag or try_replace(out_rs.source, "tag", old_ver, new_ver) then + return true + end + -- Couldn't replace anything significant, use the old URL. + local ok, md5_changed = check_url_and_update_md5(out_rs) + if not ok then + return nil, md5_changed + end + if md5_changed then + util.warning("URL is the same, but MD5 has changed. Old rockspec is broken.") + end + return true +end + +function new_version.command(args) + if not args.rock then + local err + args.rock, err = util.get_default_rockspec() + if not args.rock then + return nil, err + end + end + + local filename, err + if args.rock:match("rockspec$") then + filename, err = fetch.fetch_url(args.rock) + if not filename then + return nil, err + end + else + filename, err = download.download_file("rockspec", args.rock:lower()) + if not filename then + return nil, err + end + end + + local valid_rs, err = fetch.load_rockspec(filename) + if not valid_rs then + return nil, err + end + + local old_ver, old_rev = valid_rs.version:match("(.*)%-(%d+)$") + local new_ver, new_rev + + if args.tag and not args.new_version then + args.new_version = args.tag:gsub("^v", "") + end + + local out_dir + if args.dir then + out_dir = dir.normalize(args.dir) + end + + if args.new_version then + new_ver, new_rev = args.new_version:match("(.*)%-(%d+)$") + new_rev = tonumber(new_rev) + if not new_rev then + new_ver = args.new_version + new_rev = 1 + end + else + new_ver = old_ver + new_rev = tonumber(old_rev) + 1 + end + local new_rockver = new_ver:gsub("-", "") + + local out_rs, err = persist.load_into_table(filename) + local out_name = out_rs.package:lower() + out_rs.version = new_rockver.."-"..new_rev + + local ok, err = update_source_section(out_rs, args.new_url, args.tag, old_ver, new_ver) + if not ok then return nil, err end + + if out_rs.build and out_rs.build.type == "module" then + out_rs.build.type = "builtin" + end + + local out_filename = out_name.."-"..new_rockver.."-"..new_rev..".rockspec" + if out_dir then + out_filename = dir.path(out_dir, out_filename) + fs.make_dir(out_dir) + end + persist.save_from_table(out_filename, out_rs, type_rockspec.order) + + util.printout("Wrote "..out_filename) + + local valid_out_rs, err = fetch.load_local_rockspec(out_filename) + if not valid_out_rs then + return nil, "Failed loading generated rockspec: "..err + end + + return true +end + +return new_version diff --git a/src/luarocks/cmd/new_version.lua b/src/luarocks/cmd/new_version.lua index 2ec084e0..33b98801 100644 --- a/src/luarocks/cmd/new_version.lua +++ b/src/luarocks/cmd/new_version.lua @@ -1,8 +1,9 @@ +local _tl_compat; if (tonumber((_VERSION or ''):match('[%d.]*$')) or 0) < 5.3 then local p, m = pcall(require, 'compat53.module'); if p then _tl_compat = m end end; local string = _tl_compat and _tl_compat.string or string + ---- Module implementing the LuaRocks "new_version" command. --- Utility function that writes a new rockspec, updating data from a previous one. local new_version = {} + local util = require("luarocks.util") local download = require("luarocks.download") local fetch = require("luarocks.fetch") @@ -11,6 +12,18 @@ local fs = require("luarocks.fs") local dir = require("luarocks.dir") local type_rockspec = require("luarocks.type.rockspec") +local argparse = require("luarocks.vendor.argparse") + + + + + + + + + + + function new_version.add_to_parser(parser) local cmd = parser:command("new_version", [[ This is a utility function that writes a new rockspec, updating data from a @@ -36,15 +49,15 @@ tag but no new one passed, it is guessed in the same way URL is. If a directory is not given, it defaults to the current directory. WARNING: it writes the new rockspec to the given directory, overwriting the file -if it already exists.]], util.see_also()) - :summary("Auto-write a rockspec for a new version of a rock.") +if it already exists.]], util.see_also()): + summary("Auto-write a rockspec for a new version of a rock.") - cmd:argument("rock", "Package name or rockspec.") - :args("?") - cmd:argument("new_version", "New version of the rock.") - :args("?") - cmd:argument("new_url", "New URL of the rock.") - :args("?") + cmd:argument("rock", "Package name or rockspec."): + args("?") + cmd:argument("new_version", "New version of the rock."): + args("?") + cmd:argument("new_url", "New URL of the rock."): + args("?") cmd:option("--dir", "Output directory for the new rockspec.") cmd:option("--tag", "New SCM tag.") @@ -58,24 +71,24 @@ local function try_replace(tbl, field, old, new) local old_field = tbl[field] local new_field = tbl[field]:gsub(old, new) if new_field ~= old_field then - util.printout("Guessing new '"..field.."' field as "..new_field) + util.printout("Guessing new '" .. field .. "' field as " .. new_field) tbl[field] = new_field return true end return false end --- Try to download source file using URL from a rockspec. --- If it specified MD5, update it. --- @return (true, false) if MD5 was not specified or it stayed same, --- (true, true) if MD5 changed, (nil, string) on error. + + + + local function check_url_and_update_md5(out_rs, invalid_is_error) - local file, temp_dir = fetch.fetch_url_at_temp_dir(out_rs.source.url, "luarocks-new-version-"..out_rs.package) + local file, temp_dir = fetch.fetch_url_at_temp_dir(out_rs.source.url, "luarocks-new-version-" .. out_rs.package) if not file then if invalid_is_error then - return nil, "invalid URL - "..temp_dir + return nil, "invalid URL - " .. temp_dir end - util.warning("invalid URL - "..temp_dir) + util.warning("invalid URL - " .. temp_dir) return true, false end do @@ -134,7 +147,7 @@ local function update_source_section(out_rs, url, tag, old_ver, new_ver) if tag or try_replace(out_rs.source, "tag", old_ver, new_ver) then return true end - -- Couldn't replace anything significant, use the old URL. + local ok, md5_changed = check_url_and_update_md5(out_rs) if not ok then return nil, md5_changed @@ -197,9 +210,9 @@ function new_version.command(args) end local new_rockver = new_ver:gsub("-", "") - local out_rs, err = persist.load_into_table(filename) + local out_rs, err = persist.load_into_table(filename), string local out_name = out_rs.package:lower() - out_rs.version = new_rockver.."-"..new_rev + out_rs.version = new_rockver .. "-" .. tostring(new_rev) local ok, err = update_source_section(out_rs, args.new_url, args.tag, old_ver, new_ver) if not ok then return nil, err end @@ -208,18 +221,18 @@ function new_version.command(args) out_rs.build.type = "builtin" end - local out_filename = out_name.."-"..new_rockver.."-"..new_rev..".rockspec" + local out_filename = out_name .. "-" .. new_rockver .. "-" .. tostring(new_rev) .. ".rockspec" if out_dir then out_filename = dir.path(out_dir, out_filename) fs.make_dir(out_dir) end persist.save_from_table(out_filename, out_rs, type_rockspec.order) - util.printout("Wrote "..out_filename) + util.printout("Wrote " .. out_filename) local valid_out_rs, err = fetch.load_local_rockspec(out_filename) if not valid_out_rs then - return nil, "Failed loading generated rockspec: "..err + return nil, "Failed loading generated rockspec: " .. err end return true diff --git a/src/luarocks/cmd/new_version.tl b/src/luarocks/cmd/new_version.tl index d61cb48b..b2fdf81a 100644 --- a/src/luarocks/cmd/new_version.tl +++ b/src/luarocks/cmd/new_version.tl @@ -186,7 +186,7 @@ function new_version.command(args: Args): boolean, string | boolean end local old_ver, old_rev = valid_rs.version:match("(.*)%-(%d+)$") - local new_ver, new_rev: string, string + local new_ver, new_rev: string, string | number if args.tag and not args.new_version then args.new_version = args.tag:gsub("^v", "") @@ -212,7 +212,7 @@ function new_version.command(args: Args): boolean, string | boolean local out_rs, err = persist.load_into_table(filename) as Rockspec, string local out_name = out_rs.package:lower() - out_rs.version = new_rockver.."-"..new_rev + out_rs.version = new_rockver.."-"..tostring(new_rev) local ok, err = update_source_section(out_rs, args.new_url, args.tag, old_ver, new_ver) if not ok then return nil, err end @@ -221,7 +221,7 @@ function new_version.command(args: Args): boolean, string | boolean out_rs.build.type = "builtin" end - local out_filename = out_name.."-"..new_rockver.."-"..new_rev..".rockspec" + local out_filename = out_name.."-"..new_rockver.."-"..tostring(new_rev)..".rockspec" if out_dir then out_filename = dir.path(out_dir, out_filename) fs.make_dir(out_dir) -- cgit v1.2.3-55-g6feb