From 1f4a567867633d87ff1d5d102ffc890339b9adb0 Mon Sep 17 00:00:00 2001 From: Hisham Muhammad Date: Sat, 17 Feb 2024 00:18:05 -0300 Subject: feat(init): add --no-gitignore, --no-wrapper-scripts, --wrapper-dir Closes #1513. --- spec/init_spec.lua | 63 ++++++++++++++++++++++++++++++ src/luarocks/cmd/init.lua | 98 +++++++++++++++++++++++++++++++---------------- 2 files changed, 129 insertions(+), 32 deletions(-) diff --git a/spec/init_spec.lua b/spec/init_spec.lua index dadeb300..be10b5b1 100644 --- a/spec/init_spec.lua +++ b/spec/init_spec.lua @@ -36,6 +36,69 @@ describe("luarocks init #integration", function() end, finally) end) + it("with --no-gitignore", function() + test_env.run_in_tmp(function(tmpdir) + local myproject = tmpdir .. "/myproject" + lfs.mkdir(myproject) + lfs.chdir(myproject) + + assert(run.luarocks("init --no-gitignore")) + if is_win then + assert.truthy(lfs.attributes(myproject .. "/lua.bat")) + assert.truthy(lfs.attributes(myproject .. "/luarocks.bat")) + else + assert.truthy(lfs.attributes(myproject .. "/lua")) + assert.truthy(lfs.attributes(myproject .. "/luarocks")) + end + assert.truthy(lfs.attributes(myproject .. "/lua_modules")) + assert.truthy(lfs.attributes(myproject .. "/.luarocks")) + assert.truthy(lfs.attributes(myproject .. "/.luarocks/config-" .. test_env.lua_version .. ".lua")) + assert.falsy(lfs.attributes(myproject .. "/.gitignore")) + assert.truthy(lfs.attributes(myproject .. "/myproject-dev-1.rockspec")) + end, finally) + end) + + it("with --no-wrapper-scripts", function() + test_env.run_in_tmp(function(tmpdir) + local myproject = tmpdir .. "/myproject" + lfs.mkdir(myproject) + lfs.chdir(myproject) + + assert(run.luarocks("init --no-wrapper-scripts")) + assert.falsy(lfs.attributes(myproject .. "/lua.bat")) + assert.falsy(lfs.attributes(myproject .. "/luarocks.bat")) + assert.falsy(lfs.attributes(myproject .. "/lua")) + assert.falsy(lfs.attributes(myproject .. "/luarocks")) + assert.truthy(lfs.attributes(myproject .. "/lua_modules")) + assert.truthy(lfs.attributes(myproject .. "/.luarocks")) + assert.truthy(lfs.attributes(myproject .. "/.luarocks/config-" .. test_env.lua_version .. ".lua")) + assert.truthy(lfs.attributes(myproject .. "/.gitignore")) + assert.truthy(lfs.attributes(myproject .. "/myproject-dev-1.rockspec")) + end, finally) + end) + + it("with --wrapper-dir", function() + test_env.run_in_tmp(function(tmpdir) + local myproject = tmpdir .. "/myproject" + lfs.mkdir(myproject) + lfs.chdir(myproject) + + assert(run.luarocks("init --wrapper-dir=./bin")) + if is_win then + assert.truthy(lfs.attributes(myproject .. "/bin/lua.bat")) + assert.truthy(lfs.attributes(myproject .. "/bin/luarocks.bat")) + else + assert.truthy(lfs.attributes(myproject .. "/bin/lua")) + assert.truthy(lfs.attributes(myproject .. "/bin/luarocks")) + end + assert.truthy(lfs.attributes(myproject .. "/lua_modules")) + assert.truthy(lfs.attributes(myproject .. "/.luarocks")) + assert.truthy(lfs.attributes(myproject .. "/.luarocks/config-" .. test_env.lua_version .. ".lua")) + assert.truthy(lfs.attributes(myproject .. "/.gitignore")) + assert.truthy(lfs.attributes(myproject .. "/myproject-dev-1.rockspec")) + end, finally) + end) + it("lua wrapper works", function() test_env.run_in_tmp(function(tmpdir) local myproject = tmpdir .. "/myproject" diff --git a/src/luarocks/cmd/init.lua b/src/luarocks/cmd/init.lua index dcd6b759..efd612e4 100644 --- a/src/luarocks/cmd/init.lua +++ b/src/luarocks/cmd/init.lua @@ -17,11 +17,25 @@ function init.add_to_parser(parser) :args("?") cmd:argument("version", "An optional project version.") :args("?") - cmd:flag("--reset", "Delete .luarocks/config-5.x.lua and ./lua and generate new ones.") + cmd:option("--wrapper-dir", "Location where the 'lua' and 'luarocks' wrapper scripts " .. + "should be generated; if not given, the current directory is used as a default.") + cmd:flag("--reset", "Delete any .luarocks/config-5.x.lua and ./lua and generate new ones.") + cmd:flag("--no-wrapper-scripts", "Do not generate wrapper ./lua and ./luarocks launcher scripts.") + cmd:flag("--no-gitignore", "Do not generate a .gitignore file.") cmd:group("Options for specifying rockspec data", write_rockspec.cmd_options(cmd)) end +local function gitignore_path(pwd, wrapper_dir, filename) + local norm_cur = dir.normalize(fs.absolute_name(pwd)) + local norm_file = dir.normalize(fs.absolute_name(dir.path(wrapper_dir, filename))) + if norm_file:sub(1, #norm_cur) == norm_cur then + return norm_file:sub(#norm_cur) + else + return "/" .. filename + end +end + local function write_gitignore(entries) local gitignore = "" local fd = io.open(".gitignore", "r") @@ -41,9 +55,46 @@ local function write_gitignore(entries) fd:close() end +local function write_wrapper_scripts(wrapper_dir, luarocks_wrapper, lua_wrapper) + local tree = dir.path(fs.current_dir(), "lua_modules") + + fs.make_dir(wrapper_dir) + + luarocks_wrapper = dir.path(wrapper_dir, luarocks_wrapper) + if not fs.exists(luarocks_wrapper) then + util.printout("Preparing " .. luarocks_wrapper .. " ...") + fs.wrap_script(arg[0], luarocks_wrapper, "none", nil, nil, "--project-tree", tree) + else + util.printout(luarocks_wrapper .. " already exists. Not overwriting it!") + end + + lua_wrapper = dir.path(wrapper_dir, lua_wrapper) + local write_lua_wrapper = true + if fs.exists(lua_wrapper) then + if not util.lua_is_wrapper(lua_wrapper) then + util.printout(lua_wrapper .. " already exists and does not look like a wrapper script. Not overwriting.") + write_lua_wrapper = false + end + end + + if write_lua_wrapper then + local interp = dir.path(cfg.variables["LUA_BINDIR"], cfg.lua_interpreter) + if util.check_lua_version(interp, cfg.lua_version) then + util.printout("Preparing " .. lua_wrapper .. " for version " .. cfg.lua_version .. "...") + path.use_tree(tree) + fs.wrap_script(nil, lua_wrapper, "all") + else + util.warning("No Lua interpreter detected for version " .. cfg.lua_version .. ". Not creating " .. lua_wrapper) + end + end +end + --- Driver function for "init" command. -- @return boolean: True if succeeded, nil on errors. function init.command(args) + local do_gitignore = not args.no_gitignore + local do_wrapper_scripts = not args.no_wrapper_scripts + local wrapper_dir = args.wrapper_dir or "." local pwd = fs.current_dir() @@ -86,15 +137,24 @@ function init.command(args) local luarocks_wrapper = "luarocks" .. ext local lua_wrapper = "lua" .. ext - util.printout("Adding entries to .gitignore ...") - write_gitignore({ luarocks_wrapper, lua_wrapper, "lua_modules", ".luarocks" }) + if do_gitignore then + util.printout("Adding entries to .gitignore ...") + local ignores = { "lua_modules", ".luarocks" } + if do_wrapper_scripts then + table.insert(ignores, 1, gitignore_path(pwd, wrapper_dir, luarocks_wrapper)) + table.insert(ignores, 2, gitignore_path(pwd, wrapper_dir, lua_wrapper)) + end + write_gitignore(ignores) + end util.printout("Preparing ./.luarocks/ ...") fs.make_dir(".luarocks") local config_file = ".luarocks/config-" .. cfg.lua_version .. ".lua" if args.reset then - fs.delete(lua_wrapper) + if do_wrapper_scripts then + fs.delete(dir.path(wrapper_dir, lua_wrapper)) + end fs.delete(config_file) end @@ -138,36 +198,10 @@ function init.command(args) end util.printout("Preparing ./lua_modules/ ...") - fs.make_dir("lua_modules/lib/luarocks/rocks-" .. cfg.lua_version) - local tree = dir.path(pwd, "lua_modules") - - luarocks_wrapper = dir.path(".", luarocks_wrapper) - if not fs.exists(luarocks_wrapper) then - util.printout("Preparing " .. luarocks_wrapper .. " ...") - fs.wrap_script(arg[0], luarocks_wrapper, "none", nil, nil, "--project-tree", tree) - else - util.printout(luarocks_wrapper .. " already exists. Not overwriting it!") - end - - lua_wrapper = dir.path(".", lua_wrapper) - local write_lua_wrapper = true - if fs.exists(lua_wrapper) then - if not util.lua_is_wrapper(lua_wrapper) then - util.printout(lua_wrapper .. " already exists and does not look like a wrapper script. Not overwriting.") - write_lua_wrapper = false - end - end - if write_lua_wrapper then - local interp = dir.path(cfg.variables["LUA_BINDIR"], cfg.lua_interpreter) - if util.check_lua_version(interp, cfg.lua_version) then - util.printout("Preparing " .. lua_wrapper .. " for version " .. cfg.lua_version .. "...") - path.use_tree(tree) - fs.wrap_script(nil, lua_wrapper, "all") - else - util.warning("No Lua interpreter detected for version " .. cfg.lua_version .. ". Not creating " .. lua_wrapper) - end + if do_wrapper_scripts then + write_wrapper_scripts(wrapper_dir, luarocks_wrapper, lua_wrapper) end return true -- cgit v1.2.3-55-g6feb