From 65b1722c12d1ffe99093a2d8ed80d9493ec079a1 Mon Sep 17 00:00:00 2001 From: Hisham Muhammad Date: Wed, 20 Jun 2018 14:23:06 -0300 Subject: cmd, cfg: read project-dir configuration --- src/luarocks/cmd.lua | 7 ++++++- src/luarocks/cmd/help.lua | 9 ++++++--- src/luarocks/core/cfg.lua | 33 ++++++++++++++++++++++++++++----- 3 files changed, 40 insertions(+), 9 deletions(-) diff --git a/src/luarocks/cmd.lua b/src/luarocks/cmd.lua index 813587e0..d4ee5bc0 100644 --- a/src/luarocks/cmd.lua +++ b/src/luarocks/cmd.lua @@ -372,8 +372,13 @@ function cmd.run_command(description, commands, ...) lua_data = cmd.find_lua(flags["lua-dir"], flags["lua-version"]) end + local project_dir + if flags["project-tree"] then + project_dir = flags["project-tree"]:gsub("[/\\][^/\\]+$", "") + end + ----------------------------------------------------------------------------- - local ok, err = cfg.init(lua_data, util.warning) + local ok, err = cfg.init(lua_data, project_dir, util.warning) if not ok then die(err) end diff --git a/src/luarocks/cmd/help.lua b/src/luarocks/cmd/help.lua index 3b13acc2..77e73254 100644 --- a/src/luarocks/cmd/help.lua +++ b/src/luarocks/cmd/help.lua @@ -85,11 +85,14 @@ function help.command(description, commands, command) util.printout("\tLua version: " .. cfg.lua_version) util.printout() util.printout("\tConfiguration files:") - util.printout("\t\tSystem: ".. dir.normalize(conf.system.file) .. " (" .. get_status(conf.system.ok) ..")") + util.printout("\t\tSystem : ".. dir.normalize(conf.system.file) .. " (" .. get_status(conf.system.ok) ..")") if conf.user.file then - util.printout("\t\tUser : ".. dir.normalize(conf.user.file) .. " (" .. get_status(conf.user.ok) ..")") + util.printout("\t\tUser : ".. dir.normalize(conf.user.file) .. " (" .. get_status(conf.user.ok) ..")") else - util.printout("\t\tUser : disabled in this LuaRocks installation.") + util.printout("\t\tUser : disabled in this LuaRocks installation.") + end + if conf.project then + util.printout("\t\tProject : ".. dir.normalize(conf.project.file) .. " (" .. get_status(conf.project.ok) ..")") end util.printout() util.printout("\tRocks trees in use: ") diff --git a/src/luarocks/core/cfg.lua b/src/luarocks/core/cfg.lua index cd3aadbc..2508fa1d 100644 --- a/src/luarocks/core/cfg.lua +++ b/src/luarocks/core/cfg.lua @@ -500,7 +500,7 @@ end local cfg = {} -function cfg.init(lua_data, warning) +function cfg.init(lua_data, project_dir, warning) lua_data = lua_data or {} local hc_ok, hardcoded = pcall(require, "luarocks.core.hardcoded") @@ -581,9 +581,11 @@ function cfg.init(lua_data, warning) local sys_config_file local home_config_file + local project_config_file do local sysconfdir = os.getenv("LUAROCKS_SYSCONFDIR") or hardcoded.SYSCONFDIR local sdir, hdir + local name = "config-"..cfg.lua_version..".lua" if platforms.windows then cfg.home = os.getenv("APPDATA") or "c:" sdir = sysconfdir or (os.getenv("PROGRAMFILES") or "c:") .. "/luarocks" @@ -595,11 +597,15 @@ function cfg.init(lua_data, warning) hdir = cfg.home.."/.luarocks" cfg.home_tree = (os.getenv("USER") ~= "root") and cfg.home.."/.luarocks/" end - sys_config_file = sdir .. "/config-"..cfg.lua_version..".lua" - home_config_file = hdir .. "/config-"..cfg.lua_version..".lua" + sys_config_file = sdir .. "/" .. name + home_config_file = hdir .. "/" .. name sys_config_file = sys_config_file:gsub("\\", "/") home_config_file = home_config_file:gsub("\\", "/") + + if project_dir then + project_config_file = project_dir .. "/.luarocks/" .. name + end end -- Load system configuration file @@ -610,6 +616,7 @@ function cfg.init(lua_data, warning) -- Load user configuration file (if allowed) local home_config_ok + local project_config_ok if not hardcoded.FORCE_CONFIG then local env_var = "LUAROCKS_CONFIG_" .. lua_version:gsub("%.", "_") local env_value = os.getenv(env_var) @@ -633,7 +640,15 @@ function cfg.init(lua_data, warning) -- try the alternative defaults if there was no environment specified file or it didn't work if not home_config_ok then - home_config_ok = load_config_file(cfg, platforms, home_config_file) + home_config_ok, err = load_config_file(cfg, platforms, home_config_file) + if err then + return nil, err, "config" + end + end + + -- finally, use the project-specific config file if any + if project_dir then + project_config_ok, err = load_config_file(cfg, platforms, project_config_file) if err then return nil, err, "config" end @@ -677,6 +692,10 @@ function cfg.init(lua_data, warning) function cfg.which_config() return { + project = project_dir and { + file = project_config_file, + ok = project_config_ok, + }, system = { file = sys_config_file, ok = sys_config_ok, @@ -685,7 +704,11 @@ function cfg.init(lua_data, warning) file = home_config_file, ok = home_config_ok, }, - nearest = home_config_ok and home_config_file or sys_config_file, + nearest = project_config_ok + and project_config_file + or (home_config_ok + and home_config_file + or sys_config_file), } end -- cgit v1.2.3-55-g6feb