From 98a0bd9a97190be8ba10f14a3d67e42de827b04a Mon Sep 17 00:00:00 2001 From: Hisham Muhammad Date: Fri, 5 Jan 2018 15:38:37 -0200 Subject: path: use versioned LUA_xPATH_5_x variables `luarocks path` now exports versioned variables `LUA_PATH_5_x` and `LUA_CPATH_5_x` instead of `LUA_PATH` and `LUA_CPATH` when those are in use in your system. Fixes #760. --- CHANGELOG.md | 3 +++ spec/path_spec.lua | 34 ++++++++++++++++++++++++++++++++++ src/luarocks/cmd/path.lua | 20 +++++++++++++++++--- src/luarocks/core/cfg.lua | 6 ------ src/luarocks/fs/unix.lua | 4 ++++ src/luarocks/fs/win32.lua | 4 ++++ 6 files changed, 62 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6d028713..0bd8c0c0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,9 @@ What's new in LuaRocks 3.0 ========================== +* `luarocks path` now exports versioned variables `LUA_PATH_5_x` and + `LUA_CPATH_5_x` instead of `LUA_PATH` and `LUA_CPATH` + when those are in use in your system. * Package paths are sanitized to only reference the current Lua version. For example, if you have `/some/dir/lua/5.1/` in your `$LUA_PATH` and you are running Lua 5.2, `luarocks.loader` and the `luarocks` command-line diff --git a/spec/path_spec.lua b/spec/path_spec.lua index 22f07591..536233b1 100644 --- a/spec/path_spec.lua +++ b/spec/path_spec.lua @@ -8,6 +8,40 @@ describe("LuaRocks path tests #blackbox #b_path", function() test_env.setup_specs() end) + it("LuaRocks path", function() + local output = run.luarocks("path") + assert.match("LUA_PATH=", output) + assert.match("LUA_CPATH=", output) + end) + + if _VERSION:match("[23]") then + local v = _VERSION:gsub("Lua (%d+)%.(%d+)", "%1_%2") + + it("LuaRocks path with LUA_PATH_"..v, function() + local output = run.luarocks("path", { + ["LUA_PATH_"..v] = package.path, + }) + assert.match("LUA_PATH_"..v.."=", output) + end) + + it("LuaRocks path with LUA_CPATH_"..v, function() + local output = run.luarocks("path", { + ["LUA_CPATH_"..v] = package.cpath, + }) + assert.match("LUA_CPATH_"..v.."=", output) + end) + + it("LuaRocks path with LUA_PATH_"..v.." and LUA_CPATH_"..v, function() + local output = run.luarocks("path", { + ["LUA_PATH_"..v] = package.path, + ["LUA_CPATH_"..v] = package.cpath, + }) + assert.match("LUA_PATH_"..v.."=", output) + assert.match("LUA_CPATH_"..v.."=", output) + end) + + end + it("LuaRocks path bin", function() assert.is_true(run.luarocks_bool("path --bin")) end) diff --git a/src/luarocks/cmd/path.lua b/src/luarocks/cmd/path.lua index 3cf43091..9bcd1c81 100644 --- a/src/luarocks/cmd/path.lua +++ b/src/luarocks/cmd/path.lua @@ -5,6 +5,7 @@ local path_cmd = {} local util = require("luarocks.util") local cfg = require("luarocks.core.cfg") +local fs = require("luarocks.fs") path_cmd.help_summary = "Return the currently configured package path." path_cmd.help_arguments = "" @@ -56,11 +57,24 @@ function path_cmd.command(flags) lr_cpath = lr_cpath .. ";" .. package.cpath lr_bin = lr_bin .. path_sep .. os.getenv("PATH") end + + local lpath_var = "LUA_PATH" + local lcpath_var = "LUA_CPATH" + + local lv = cfg.lua_version:gsub("%.", "_") + if lv ~= "5_1" then + if os.getenv("LUA_PATH_" .. lv) then + lpath_var = "LUA_PATH_" .. lv + end + if os.getenv("LUA_CPATH_" .. lv) then + lcpath_var = "LUA_CPATH_" .. lv + end + end - util.printout(cfg.export_lua_path:format(util.cleanup_path(lr_path, ';', cfg.lua_version))) - util.printout(cfg.export_lua_cpath:format(util.cleanup_path(lr_cpath, ';', cfg.lua_version))) + util.printout(fs.export_cmd(lpath_var, util.cleanup_path(lr_path, ';', cfg.lua_version))) + util.printout(fs.export_cmd(lcpath_var, util.cleanup_path(lr_cpath, ';', cfg.lua_version))) if flags["bin"] then - util.printout(cfg.export_path:format(util.cleanup_path(lr_bin, path_sep))) + util.printout(fs.export_cmd("PATH", util.cleanup_path(lr_bin, path_sep))) end return true end diff --git a/src/luarocks/core/cfg.lua b/src/luarocks/core/cfg.lua index e6351add..3326375f 100644 --- a/src/luarocks/core/cfg.lua +++ b/src/luarocks/core/cfg.lua @@ -473,10 +473,7 @@ if cfg.platforms.windows then lib = { "?.dll", "lib?.dll" }, include = { "?.h" } } - defaults.export_path = "SET PATH=%s" defaults.export_path_separator = ";" - defaults.export_lua_path = "SET LUA_PATH=%s" - defaults.export_lua_cpath = "SET LUA_CPATH=%s" defaults.wrapper_suffix = ".bat" local localappdata = os.getenv("LOCALAPPDATA") @@ -541,10 +538,7 @@ if cfg.platforms.unix then lib = { "lib?.so", "lib?.so.*" }, include = { "?.h" } } - defaults.export_path = "export PATH='%s'" defaults.export_path_separator = ":" - defaults.export_lua_path = "export LUA_PATH='%s'" - defaults.export_lua_cpath = "export LUA_CPATH='%s'" defaults.wrapper_suffix = "" defaults.local_cache = cfg.home.."/.cache/luarocks" if not defaults.variables.CFLAGS:match("-fPIC") then diff --git a/src/luarocks/fs/unix.lua b/src/luarocks/fs/unix.lua index 52634246..c99e1566 100644 --- a/src/luarocks/fs/unix.lua +++ b/src/luarocks/fs/unix.lua @@ -136,4 +136,8 @@ function unix.current_user() return os.getenv("USER") end +function unix.export_cmd(var, val) + return ("export %s='%s'"):format(var, val) +end + return unix diff --git a/src/luarocks/fs/win32.lua b/src/luarocks/fs/win32.lua index 92068e39..6fd24fa5 100644 --- a/src/luarocks/fs/win32.lua +++ b/src/luarocks/fs/win32.lua @@ -272,4 +272,8 @@ function win32.current_user() return os.getenv("USERNAME") end +function win32.export_cmd(var, val) + return ("SET %s=%s"):format(var, val) +end + return win32 -- cgit v1.2.3-55-g6feb