From 3afab9d46de3889241c0f882cb312b8ec4a385ed Mon Sep 17 00:00:00 2001 From: Paul Ouellette Date: Wed, 12 Jun 2019 18:57:06 -0400 Subject: Move build command to argparse --- src/luarocks/cmd.lua | 8 ++- src/luarocks/cmd/build.lua | 115 ++++++++++++++++++++----------------------- src/luarocks/cmd/install.lua | 2 +- src/luarocks/cmd/remove.lua | 2 +- src/luarocks/cmd/test.lua | 2 +- src/luarocks/deps.lua | 15 +----- src/luarocks/util.lua | 31 ++++++------ 7 files changed, 81 insertions(+), 94 deletions(-) diff --git a/src/luarocks/cmd.lua b/src/luarocks/cmd.lua index 23ca023a..4929b326 100644 --- a/src/luarocks/cmd.lua +++ b/src/luarocks/cmd.lua @@ -370,6 +370,7 @@ Variables: :argname("") parser:option("--only-sources", "Restrict downloads to paths matching the given URL.") :argname("") + parser:option("--namespace"):hidden(true) -- TODO: Description parser:option("--lua-dir", "Which Lua installation to use.") :argname("") parser:option("--lua-version", "Which Lua version to use.") @@ -461,6 +462,11 @@ function cmd.run_command(program_name, description, commands, external_namespace os.exit(cmd.errorcodes.OK) end + -- Compatibility for old flag + if args.nodeps then + args.deps_mode = "none" + end + if args.timeout then -- setting it in the config file will kick-in earlier in the process cfg.connection_timeout = args.timeout end @@ -472,7 +478,7 @@ function cmd.run_command(program_name, description, commands, external_namespace args.lua_dir = args.value end end - + ----------------------------------------------------------------------------- local lua_found, err = init_config(args) if err then diff --git a/src/luarocks/cmd/build.lua b/src/luarocks/cmd/build.lua index 23a94fa7..2b9f4dd6 100644 --- a/src/luarocks/cmd/build.lua +++ b/src/luarocks/cmd/build.lua @@ -18,40 +18,37 @@ local search = require("luarocks.search") local make = require("luarocks.cmd.make") local cmd = require("luarocks.cmd") -cmd_build.help_summary = "build/compile a rock." -cmd_build.help_arguments = "[] {|| []}" -cmd_build.help = [[ -Build and install a rock, compiling its C parts if any. -Argument may be a rockspec file, a source rock file -or the name of a rock to be fetched from a repository. - ---pack-binary-rock Do not install rock. Instead, produce a .rock file - with the contents of compilation in the current - directory. - ---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. - ---branch= Override the `source.branch` field in the loaded - rockspec. Allows to specify a different branch to - fetch. Particularly for "dev" rocks. - ---only-deps Installs only the dependencies of the rock. - ---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. - You need the signer’s public key in your local - keyring for this option to work properly. - ---sign To be used with --pack-binary-rock. Also produce - a signature file for the generated .rock file. - -]]..util.deps_mode_help() +function cmd_build.add_to_parser(parser) + local cmd = parser:command("build", "Build and install a rock, compiling ".. + "its C parts if any.", util.see_also()) + :summary("Build/compile a rock.") + :add_help(false) + + cmd:argument("rock", "A rockspec file, a source rock file, or the name of ".. + "a rock to be fetched from a repository.") + cmd:argument("version", "Rock version.") + :args("?") + + cmd:flag("--pack-binary-rock", "Do not install rock. Instead, produce a ".. + ".rock file with the contents of compilation in the current directory.") + 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:option("--branch", "Override the `source.branch` field in the loaded ".. + "rockspec. Allows to specify a different branch to fetch. Particularly ".. + 'for "dev" rocks.') + :argname("") + cmd:flag("--only-deps", "Installs only the dependencies of the rock.") + 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("--sign", "To be used with --pack-binary-rock. Also produce a ".. + "signature file for the generated .rock file.") + util.deps_mode_option(cmd) +end --- Build and install a rock. -- @param rock_filename string: local or remote filename of a rock. @@ -132,58 +129,54 @@ local function remove_doc_dir(name, version) end --- Driver function for "build" command. --- @param name string: A local or remote rockspec or rock file. -- If a package name is given, forwards the request to "search" and, -- if returned a result, installs the matching rock. --- @param version string: When passing a package name, a version number may --- also be given. +-- When passing a package name, a version number may also be given. -- @return boolean or (nil, string, exitcode): True if build was successful; nil and an -- error message otherwise. exitcode is optionally returned. -function cmd_build.command(flags, name, version) - assert(type(name) == "string" or not name) - assert(type(version) == "string" or not version) - - if not name then - return make.command(flags) +function cmd_build.command(args) + if not args.rock then + return make.command(args) end - name = util.adjust_name_and_namespace(name, flags) + local name = util.adjust_name_and_namespace(args.rock, args) local opts = build.opts({ need_to_fetch = true, minimal_mode = false, - deps_mode = deps.get_deps_mode(flags), - build_only_deps = not not flags["only-deps"], - namespace = flags["namespace"], - branch = not not flags["branch"], - verify = not not flags["verify"], + deps_mode = deps.get_deps_mode(args), + build_only_deps = not not args.only_deps, + namespace = args.namespace, + branch = not not args.branch, + verify = not not args.verify, }) - if flags["sign"] and not flags["pack-binary-rock"] then + if args.sign and not args.pack_binary_rock then return nil, "In the build command, --sign is meant to be used only with --pack-binary-rock" end - if flags["pack-binary-rock"] then - return pack.pack_binary_rock(name, version, flags["sign"], function() + if args.pack_binary_rock then + return pack.pack_binary_rock(name, args.version, args.sign, function() opts.build_only_deps = false - local status, err, errcode = do_build(name, version, opts) - if status and flags["no-doc"] then - remove_doc_dir(name, version) + local status, err, errcode = do_build(name, args.version, opts) + if status and args.no_doc then + remove_doc_dir(name, args.version) end return status, err, errcode end) end - local ok, err = fs.check_command_permissions(flags) + local ok, err = fs.check_command_permissions(args) if not ok then return nil, err, cmd.errorcodes.PERMISSIONDENIED end - ok, err = do_build(name, version, opts) + ok, err = do_build(name, args.version, opts) if not ok then return nil, err end + local version name, version = ok, err - if flags["no-doc"] then + if args.no_doc then remove_doc_dir(name, version) end @@ -191,15 +184,15 @@ function cmd_build.command(flags, name, version) util.printout("Stopping after installing dependencies for " ..name.." "..version) util.printout() else - if (not flags["keep"]) and not cfg.keep_other_versions then - local ok, err = remove.remove_other_versions(name, version, flags["force"], flags["force-fast"]) + if (not args.keep) and not cfg.keep_other_versions then + local ok, err = remove.remove_other_versions(name, version, args.force, args.force_fast) if not ok then util.printerr(err) end end end - writer.check_dependencies(nil, deps.get_deps_mode(flags)) + writer.check_dependencies(nil, deps.get_deps_mode(args)) return name, version end diff --git a/src/luarocks/cmd/install.lua b/src/luarocks/cmd/install.lua index d1d7bf6c..ec6da14f 100644 --- a/src/luarocks/cmd/install.lua +++ b/src/luarocks/cmd/install.lua @@ -41,7 +41,7 @@ or a filename of a locally available rock. You need the signer’s public key in your local keyring for this option to work properly. -]]..util.deps_mode_help() +]]--..util.deps_mode_help() install.opts = util.opts_table("install.opts", { namespace = "string?", diff --git a/src/luarocks/cmd/remove.lua b/src/luarocks/cmd/remove.lua index 5ddf7477..f9345670 100644 --- a/src/luarocks/cmd/remove.lua +++ b/src/luarocks/cmd/remove.lua @@ -24,7 +24,7 @@ To override this check and force the removal, use --force. To perform a forced removal without reporting dependency issues, use --force-fast. -]]..util.deps_mode_help() +]]--..util.deps_mode_help() --- Driver function for the "remove" command. -- @param name string: name of a rock. If a version is given, refer to diff --git a/src/luarocks/cmd/test.lua b/src/luarocks/cmd/test.lua index 413a029c..06ac0068 100644 --- a/src/luarocks/cmd/test.lua +++ b/src/luarocks/cmd/test.lua @@ -23,7 +23,7 @@ test suite arguments. specified in the rockspec and it could not be auto-detected. -]]..util.deps_mode_help() +]]--..util.deps_mode_help() function cmd_test.command(flags, argument, ...) assert(type(argument) == "string" or not argument) diff --git a/src/luarocks/deps.lua b/src/luarocks/deps.lua index 9ee21772..af05ed05 100644 --- a/src/luarocks/deps.lua +++ b/src/luarocks/deps.lua @@ -569,20 +569,9 @@ function deps.check_lua_libdir(vars) return nil, "Failed finding Lua library. You may need to configure LUA_LIBDIR.", "dependency" end -local valid_deps_modes = { - one = true, - order = true, - all = true, - none = true, -} - -function deps.check_deps_mode_flag(flag) - return valid_deps_modes[flag] -end - function deps.get_deps_mode(flags) - if flags["deps-mode"] then - return flags["deps-mode"] + if flags["deps_mode"] then + return flags["deps_mode"] else return cfg.deps_mode end diff --git a/src/luarocks/util.lua b/src/luarocks/util.lua index 60cfb3d8..635d3a97 100644 --- a/src/luarocks/util.lua +++ b/src/luarocks/util.lua @@ -215,23 +215,22 @@ function util.this_program(default) return prog end -function util.deps_mode_help(program) +function util.deps_mode_option(parser) local cfg = require("luarocks.core.cfg") - return [[ ---deps-mode= How to handle dependencies. Four modes are supported: - * all - use all trees from the rocks_trees list - for finding dependencies - * one - use only the current tree (possibly set - with --tree) - * order - use trees based on order (use the current - tree and all trees below it on the rocks_trees list) - * none - ignore dependencies altogether. - The default mode may be set with the deps_mode entry - in the configuration file. - The current default is "]]..cfg.deps_mode..[[". - Type ']]..util.this_program(program or "luarocks")..[[' with no arguments to see - your list of rocks trees. -]] + + parser:option("--deps-mode", "How to handle dependencies. Four modes are supported:\n".. + "* all - use all trees from the rocks_trees list for finding dependencies\n".. + "* one - use only the current tree (possibly set with --tree)\n".. + "* order - use trees based on order (use the current tree and all ".. + "trees below it on the rocks_trees list)\n".. + "* none - ignore dependencies altogether.\n".. + "The default mode may be set with the deps_mode entry in the configuration file.\n".. + 'The current default is "'..cfg.deps_mode..'".\n'.. + "Type '"..util.this_program(program or "luarocks").."' with no ".. + "arguments to see your list of rocks trees.") + :argname("") + :choices({"all", "one", "order", "none"}) + parser:flag("--nodeps"):hidden(true) end function util.see_help(command, program) -- cgit v1.2.3-55-g6feb