diff options
author | Hisham Muhammad <hisham@gobolinux.org> | 2019-08-30 11:02:58 -0300 |
---|---|---|
committer | Hisham Muhammad <hisham@gobolinux.org> | 2019-09-03 15:00:01 -0300 |
commit | ccf92207e1092ae339b74454168acca79af72d0e (patch) | |
tree | 71ea1ea96e76274e14b523ac2b0f62306dd870f6 | |
parent | 0a5c5eee15072b712d0cee4cce8d642d1177ff76 (diff) | |
download | luarocks-ccf92207e1092ae339b74454168acca79af72d0e.tar.gz luarocks-ccf92207e1092ae339b74454168acca79af72d0e.tar.bz2 luarocks-ccf92207e1092ae339b74454168acca79af72d0e.zip |
cmd: add compatibility mode for pre-argparse external commands
See #1070.
-rw-r--r-- | spec/external_spec.lua | 33 | ||||
-rw-r--r-- | spec/fixtures/legacyexternalcommand-0.1-1.rockspec | 17 | ||||
-rw-r--r-- | spec/fixtures/legacyexternalcommand.lua | 34 | ||||
-rw-r--r-- | src/luarocks/cmd.lua | 15 |
4 files changed, 99 insertions, 0 deletions
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 @@ | |||
1 | local test_env = require("spec.util.test_env") | ||
2 | local run = test_env.run | ||
3 | local testing_paths = test_env.testing_paths | ||
4 | |||
5 | test_env.setup_specs() | ||
6 | |||
7 | describe("LuaRocks external commands #integration", function() | ||
8 | setup(function() | ||
9 | test_env.mock_server_init() | ||
10 | end) | ||
11 | |||
12 | teardown(function() | ||
13 | test_env.mock_server_done() | ||
14 | end) | ||
15 | |||
16 | it("installs a legacy external command", function() | ||
17 | local rockspec = testing_paths.fixtures_dir .. "/legacyexternalcommand-0.1-1.rockspec" | ||
18 | assert.is_truthy(run.luarocks_bool("build " .. rockspec)) | ||
19 | assert.is.truthy(run.luarocks("show legacyexternalcommand")) | ||
20 | local output = run.luarocks("legacyexternalcommand") | ||
21 | assert.match("Argument missing", output) | ||
22 | output = run.luarocks("legacyexternalcommand foo") | ||
23 | assert.match("ARG1\tfoo", output) | ||
24 | assert.match("ARG2\tnil", output) | ||
25 | output = run.luarocks("legacyexternalcommand foo bar") | ||
26 | assert.match("ARG1\tfoo", output) | ||
27 | assert.match("ARG2\tbar", output) | ||
28 | output = run.luarocks("legacyexternalcommand foo bar bla") | ||
29 | assert.match("ARG1\tfoo", output) | ||
30 | assert.match("ARG2\tbar", output) | ||
31 | end) | ||
32 | end) | ||
33 | |||
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 @@ | |||
1 | package = "legacyexternalcommand" | ||
2 | version = "0.1-1" | ||
3 | source = { | ||
4 | url = "http://localhost:8080/file/legacyexternalcommand.lua" | ||
5 | } | ||
6 | description = { | ||
7 | summary = "an external command with legacy arg parsing", | ||
8 | } | ||
9 | dependencies = { | ||
10 | "lua >= 5.1" | ||
11 | } | ||
12 | build = { | ||
13 | type = "builtin", | ||
14 | modules = { | ||
15 | ["luarocks.cmd.external.legacyexternalcommand"] = "legacyexternalcommand.lua", | ||
16 | } | ||
17 | } | ||
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 @@ | |||
1 | |||
2 | --- Module implementing an external command with legacy arg parsing. | ||
3 | local legacyexternalcommand = {} | ||
4 | |||
5 | local util = require("luarocks.util") | ||
6 | |||
7 | legacyexternalcommand.help_summary = "generate legacyexternalcommand package files of a rock." | ||
8 | legacyexternalcommand.help_arguments = "arg1 [arg2]" | ||
9 | legacyexternalcommand.help = [[ | ||
10 | This addon generates legacyexternalcommand package files of a rock. | ||
11 | First argument is the name of a rock, the second argument is optional | ||
12 | and needed when legacyexternalcommand uses another name (usually prefixed by lua-). | ||
13 | Files are generated with the source content of the rock and more | ||
14 | especially the rockspec. So, the rock is downloaded and unpacked. | ||
15 | ]] | ||
16 | |||
17 | --- Driver function for the "legacyexternalcommand" command. | ||
18 | -- @param arg1 string: arg1. | ||
19 | -- @param arg2 string: arg2 (optional) | ||
20 | -- @return boolean: true if successful | ||
21 | function legacyexternalcommand.command(flags, arg1, arg2) | ||
22 | if type(arg1) ~= 'string' then | ||
23 | return nil, "Argument missing. "..util.see_help('legacyexternalcommand') | ||
24 | end | ||
25 | |||
26 | for k,v in pairs(flags) do | ||
27 | print("FLAGS", k,v) | ||
28 | end | ||
29 | print("ARG1", tostring(arg1)) | ||
30 | print("ARG2", tostring(arg2)) | ||
31 | return true | ||
32 | end | ||
33 | |||
34 | 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") | |||
11 | local fs = require("luarocks.fs") | 11 | local fs = require("luarocks.fs") |
12 | local argparse = require("luarocks.argparse") | 12 | local argparse = require("luarocks.argparse") |
13 | 13 | ||
14 | local unpack = unpack or table.unpack | ||
15 | |||
14 | local hc_ok, hardcoded = pcall(require, "luarocks.core.hardcoded") | 16 | local hc_ok, hardcoded = pcall(require, "luarocks.core.hardcoded") |
15 | if not hc_ok then | 17 | if not hc_ok then |
16 | hardcoded = {} | 18 | hardcoded = {} |
@@ -459,6 +461,19 @@ function cmd.run_command(description, commands, external_namespace, ...) | |||
459 | local cmd_modules = {} | 461 | local cmd_modules = {} |
460 | for name, module in pairs(commands) do | 462 | for name, module in pairs(commands) do |
461 | cmd_modules[name] = require(module) | 463 | cmd_modules[name] = require(module) |
464 | if not cmd_modules[name].add_to_parser then | ||
465 | cmd_modules[name].add_to_parser = function(parser) | ||
466 | parser:command(name, cmd_modules[name].help, util.see_also()) | ||
467 | :summary(cmd_modules[name].help_summary) | ||
468 | :handle_options(false) | ||
469 | :argument("input") | ||
470 | :args("*") | ||
471 | end | ||
472 | local original_command = cmd_modules[name].command | ||
473 | cmd_modules[name].command = function(args) | ||
474 | return original_command(args, unpack(args.input)) | ||
475 | end | ||
476 | end | ||
462 | end | 477 | end |
463 | 478 | ||
464 | local function process_cmdline_vars(...) | 479 | local function process_cmdline_vars(...) |