From ccf92207e1092ae339b74454168acca79af72d0e Mon Sep 17 00:00:00 2001 From: Hisham Muhammad Date: Fri, 30 Aug 2019 11:02:58 -0300 Subject: cmd: add compatibility mode for pre-argparse external commands See #1070. --- spec/external_spec.lua | 33 +++++++++++++++++++++ spec/fixtures/legacyexternalcommand-0.1-1.rockspec | 17 +++++++++++ spec/fixtures/legacyexternalcommand.lua | 34 ++++++++++++++++++++++ src/luarocks/cmd.lua | 15 ++++++++++ 4 files changed, 99 insertions(+) create mode 100644 spec/external_spec.lua create mode 100644 spec/fixtures/legacyexternalcommand-0.1-1.rockspec create mode 100644 spec/fixtures/legacyexternalcommand.lua diff --git a/spec/external_spec.lua b/spec/external_spec.lua new file mode 100644 index 00000000..4f1b8dce --- /dev/null +++ b/spec/external_spec.lua @@ -0,0 +1,33 @@ +local test_env = require("spec.util.test_env") +local run = test_env.run +local testing_paths = test_env.testing_paths + +test_env.setup_specs() + +describe("LuaRocks external commands #integration", function() + setup(function() + test_env.mock_server_init() + end) + + teardown(function() + test_env.mock_server_done() + end) + + it("installs a legacy external command", function() + local rockspec = testing_paths.fixtures_dir .. "/legacyexternalcommand-0.1-1.rockspec" + assert.is_truthy(run.luarocks_bool("build " .. rockspec)) + assert.is.truthy(run.luarocks("show legacyexternalcommand")) + local output = run.luarocks("legacyexternalcommand") + assert.match("Argument missing", output) + output = run.luarocks("legacyexternalcommand foo") + assert.match("ARG1\tfoo", output) + assert.match("ARG2\tnil", output) + output = run.luarocks("legacyexternalcommand foo bar") + assert.match("ARG1\tfoo", output) + assert.match("ARG2\tbar", output) + output = run.luarocks("legacyexternalcommand foo bar bla") + assert.match("ARG1\tfoo", output) + assert.match("ARG2\tbar", output) + end) +end) + diff --git a/spec/fixtures/legacyexternalcommand-0.1-1.rockspec b/spec/fixtures/legacyexternalcommand-0.1-1.rockspec new file mode 100644 index 00000000..47bcbb0c --- /dev/null +++ b/spec/fixtures/legacyexternalcommand-0.1-1.rockspec @@ -0,0 +1,17 @@ +package = "legacyexternalcommand" +version = "0.1-1" +source = { + url = "http://localhost:8080/file/legacyexternalcommand.lua" +} +description = { + summary = "an external command with legacy arg parsing", +} +dependencies = { + "lua >= 5.1" +} +build = { + type = "builtin", + modules = { + ["luarocks.cmd.external.legacyexternalcommand"] = "legacyexternalcommand.lua", + } +} diff --git a/spec/fixtures/legacyexternalcommand.lua b/spec/fixtures/legacyexternalcommand.lua new file mode 100644 index 00000000..af575372 --- /dev/null +++ b/spec/fixtures/legacyexternalcommand.lua @@ -0,0 +1,34 @@ + +--- Module implementing an external command with legacy arg parsing. +local legacyexternalcommand = {} + +local util = require("luarocks.util") + +legacyexternalcommand.help_summary = "generate legacyexternalcommand package files of a rock." +legacyexternalcommand.help_arguments = "arg1 [arg2]" +legacyexternalcommand.help = [[ +This addon generates legacyexternalcommand package files of a rock. +First argument is the name of a rock, the second argument is optional +and needed when legacyexternalcommand uses another name (usually prefixed by lua-). +Files are generated with the source content of the rock and more +especially the rockspec. So, the rock is downloaded and unpacked. +]] + +--- Driver function for the "legacyexternalcommand" command. +-- @param arg1 string: arg1. +-- @param arg2 string: arg2 (optional) +-- @return boolean: true if successful +function legacyexternalcommand.command(flags, arg1, arg2) + if type(arg1) ~= 'string' then + return nil, "Argument missing. "..util.see_help('legacyexternalcommand') + end + + for k,v in pairs(flags) do + print("FLAGS", k,v) + end + print("ARG1", tostring(arg1)) + print("ARG2", tostring(arg2)) + return true +end + +return legacyexternalcommand diff --git a/src/luarocks/cmd.lua b/src/luarocks/cmd.lua index 126bbe76..328afe3f 100644 --- a/src/luarocks/cmd.lua +++ b/src/luarocks/cmd.lua @@ -11,6 +11,8 @@ local fun = require("luarocks.fun") local fs = require("luarocks.fs") local argparse = require("luarocks.argparse") +local unpack = unpack or table.unpack + local hc_ok, hardcoded = pcall(require, "luarocks.core.hardcoded") if not hc_ok then hardcoded = {} @@ -459,6 +461,19 @@ function cmd.run_command(description, commands, external_namespace, ...) local cmd_modules = {} for name, module in pairs(commands) do cmd_modules[name] = require(module) + if not cmd_modules[name].add_to_parser then + cmd_modules[name].add_to_parser = function(parser) + parser:command(name, cmd_modules[name].help, util.see_also()) + :summary(cmd_modules[name].help_summary) + :handle_options(false) + :argument("input") + :args("*") + end + local original_command = cmd_modules[name].command + cmd_modules[name].command = function(args) + return original_command(args, unpack(args.input)) + end + end end local function process_cmdline_vars(...) -- cgit v1.2.3-55-g6feb