diff options
Diffstat (limited to '')
| -rw-r--r-- | src/luarocks/command_line.lua | 133 |
1 files changed, 133 insertions, 0 deletions
diff --git a/src/luarocks/command_line.lua b/src/luarocks/command_line.lua new file mode 100644 index 00000000..b3020284 --- /dev/null +++ b/src/luarocks/command_line.lua | |||
| @@ -0,0 +1,133 @@ | |||
| 1 | |||
| 2 | program_version = "1.0" | ||
| 3 | |||
| 4 | --- Functions for command-line scripts. | ||
| 5 | module("luarocks.command_line", package.seeall) | ||
| 6 | |||
| 7 | local util = require("luarocks.util") | ||
| 8 | local cfg = require("luarocks.cfg") | ||
| 9 | local fs = require("luarocks.fs") | ||
| 10 | |||
| 11 | --- Display an error message and exit. | ||
| 12 | -- @param message string: The error message. | ||
| 13 | local function die(message) | ||
| 14 | assert(type(message) == "string") | ||
| 15 | |||
| 16 | local ok, err = pcall(util.run_scheduled_functions) | ||
| 17 | if not ok then | ||
| 18 | print("\nLuaRocks "..program_version.." internal bug (please report at luarocks-developers@lists.luaforge.net):\n"..err) | ||
| 19 | end | ||
| 20 | print("\nError: "..message) | ||
| 21 | os.exit(1) | ||
| 22 | end | ||
| 23 | |||
| 24 | --- Main command-line processor. | ||
| 25 | -- Parses input arguments and calls the appropriate driver function | ||
| 26 | -- to execute the action requested on the command-line, forwarding | ||
| 27 | -- to it any additional arguments passed by the user. | ||
| 28 | -- Uses the global table "commands", which contains | ||
| 29 | -- the loaded modules representing commands. | ||
| 30 | -- @param ... string: Arguments given on the command-line. | ||
| 31 | function run_command(...) | ||
| 32 | local args = {...} | ||
| 33 | local cmdline_vars = {} | ||
| 34 | for i = #args, 1, -1 do | ||
| 35 | local arg = args[i] | ||
| 36 | if arg:match("^[^-][^=]*=") then | ||
| 37 | local var, val = arg:match("^([A-Z_][A-Z0-9_]*)=(.*)") | ||
| 38 | if val then | ||
| 39 | cmdline_vars[var] = val | ||
| 40 | table.remove(args, i) | ||
| 41 | else | ||
| 42 | die("Invalid assignment: "..arg) | ||
| 43 | end | ||
| 44 | end | ||
| 45 | end | ||
| 46 | local nonflags = { util.parse_flags(unpack(args)) } | ||
| 47 | local flags = table.remove(nonflags, 1) | ||
| 48 | |||
| 49 | if flags["to"] then | ||
| 50 | if flags["to"] == true then | ||
| 51 | die("Argument error: use --to=<path>") | ||
| 52 | end | ||
| 53 | local root_dir = fs.absolute_name(flags["to"]) | ||
| 54 | cfg.root_dir = root_dir | ||
| 55 | cfg.rocks_dir = root_dir.."/rocks" | ||
| 56 | cfg.scripts_dir = root_dir.."/bin" | ||
| 57 | else | ||
| 58 | local trees = cfg.rocks_trees | ||
| 59 | for i = #trees, 1, -1 do | ||
| 60 | local tree = trees[i] | ||
| 61 | if fs.make_dir(tree) and fs.is_writable(tree) then | ||
| 62 | cfg.root_dir = tree | ||
| 63 | cfg.rocks_dir = tree.."/rocks" | ||
| 64 | cfg.scripts_dir = rawget(cfg, "scripts_dir") or tree.."/bin" | ||
| 65 | break | ||
| 66 | end | ||
| 67 | end | ||
| 68 | end | ||
| 69 | |||
| 70 | cfg.root_dir = cfg.root_dir:gsub("/+$", "") | ||
| 71 | cfg.rocks_dir = cfg.rocks_dir:gsub("/+$", "") | ||
| 72 | cfg.scripts_dir = cfg.scripts_dir:gsub("/+$", "") | ||
| 73 | |||
| 74 | cfg.variables.ROCKS_TREE = cfg.root_dir | ||
| 75 | cfg.variables.SCRIPTS_DIR = cfg.scripts_dir | ||
| 76 | |||
| 77 | if flags["from"] then | ||
| 78 | if flags["from"] == true then | ||
| 79 | die("Argument error: use --from=<url>") | ||
| 80 | end | ||
| 81 | local protocol, path = fs.split_url(flags["from"]) | ||
| 82 | table.insert(cfg.rocks_servers, 1, protocol.."://"..path) | ||
| 83 | end | ||
| 84 | |||
| 85 | if flags["only-from"] then | ||
| 86 | if flags["only-from"] == true then | ||
| 87 | die("Argument error: use --only-from=<url>") | ||
| 88 | end | ||
| 89 | cfg.rocks_servers = { flags["only-from"] } | ||
| 90 | end | ||
| 91 | |||
| 92 | local command | ||
| 93 | |||
| 94 | if flags["version"] then | ||
| 95 | print(program_name.." "..program_version) | ||
| 96 | print(program_description) | ||
| 97 | print() | ||
| 98 | os.exit(0) | ||
| 99 | elseif flags["help"] or #nonflags == 0 then | ||
| 100 | command = "help" | ||
| 101 | args = nonflags | ||
| 102 | else | ||
| 103 | command = nonflags[1] | ||
| 104 | for i, arg in ipairs(args) do | ||
| 105 | if arg == command then | ||
| 106 | table.remove(args, i) | ||
| 107 | break | ||
| 108 | end | ||
| 109 | end | ||
| 110 | end | ||
| 111 | |||
| 112 | if command ~= "help" then | ||
| 113 | for k, v in pairs(cmdline_vars) do | ||
| 114 | cfg.variables[k] = v | ||
| 115 | end | ||
| 116 | end | ||
| 117 | |||
| 118 | command = command:gsub("-", "_") | ||
| 119 | if commands[command] then | ||
| 120 | local xp, ok, err = xpcall(function() return commands[command].run(unpack(args)) end, function(err) | ||
| 121 | die(debug.traceback("LuaRocks "..program_version | ||
| 122 | .." bug (please report at luarocks-developers@lists.luaforge.net).\n" | ||
| 123 | ..err, 2)) | ||
| 124 | end) | ||
| 125 | if xp and (not ok) then | ||
| 126 | die(err) | ||
| 127 | end | ||
| 128 | else | ||
| 129 | die("Unknown command: "..command) | ||
| 130 | end | ||
| 131 | |||
| 132 | util.run_scheduled_functions() | ||
| 133 | end | ||
