diff options
| author | Hisham Muhammad <hisham@gobolinux.org> | 2024-02-17 00:18:05 -0300 |
|---|---|---|
| committer | Hisham Muhammad <hisham@gobolinux.org> | 2024-02-17 04:37:34 -0300 |
| commit | 1f4a567867633d87ff1d5d102ffc890339b9adb0 (patch) | |
| tree | 63a2cf485e567642fdc70794b98fa12b01505e25 | |
| parent | 882c7b1ff2df7a7b05f42eb0ba6f781d9ccfe5fe (diff) | |
| download | luarocks-1f4a567867633d87ff1d5d102ffc890339b9adb0.tar.gz luarocks-1f4a567867633d87ff1d5d102ffc890339b9adb0.tar.bz2 luarocks-1f4a567867633d87ff1d5d102ffc890339b9adb0.zip | |
feat(init): add --no-gitignore, --no-wrapper-scripts, --wrapper-dir
Closes #1513.
| -rw-r--r-- | spec/init_spec.lua | 63 | ||||
| -rw-r--r-- | 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() | |||
| 36 | end, finally) | 36 | end, finally) |
| 37 | end) | 37 | end) |
| 38 | 38 | ||
| 39 | it("with --no-gitignore", function() | ||
| 40 | test_env.run_in_tmp(function(tmpdir) | ||
| 41 | local myproject = tmpdir .. "/myproject" | ||
| 42 | lfs.mkdir(myproject) | ||
| 43 | lfs.chdir(myproject) | ||
| 44 | |||
| 45 | assert(run.luarocks("init --no-gitignore")) | ||
| 46 | if is_win then | ||
| 47 | assert.truthy(lfs.attributes(myproject .. "/lua.bat")) | ||
| 48 | assert.truthy(lfs.attributes(myproject .. "/luarocks.bat")) | ||
| 49 | else | ||
| 50 | assert.truthy(lfs.attributes(myproject .. "/lua")) | ||
| 51 | assert.truthy(lfs.attributes(myproject .. "/luarocks")) | ||
| 52 | end | ||
| 53 | assert.truthy(lfs.attributes(myproject .. "/lua_modules")) | ||
| 54 | assert.truthy(lfs.attributes(myproject .. "/.luarocks")) | ||
| 55 | assert.truthy(lfs.attributes(myproject .. "/.luarocks/config-" .. test_env.lua_version .. ".lua")) | ||
| 56 | assert.falsy(lfs.attributes(myproject .. "/.gitignore")) | ||
| 57 | assert.truthy(lfs.attributes(myproject .. "/myproject-dev-1.rockspec")) | ||
| 58 | end, finally) | ||
| 59 | end) | ||
| 60 | |||
| 61 | it("with --no-wrapper-scripts", function() | ||
| 62 | test_env.run_in_tmp(function(tmpdir) | ||
| 63 | local myproject = tmpdir .. "/myproject" | ||
| 64 | lfs.mkdir(myproject) | ||
| 65 | lfs.chdir(myproject) | ||
| 66 | |||
| 67 | assert(run.luarocks("init --no-wrapper-scripts")) | ||
| 68 | assert.falsy(lfs.attributes(myproject .. "/lua.bat")) | ||
| 69 | assert.falsy(lfs.attributes(myproject .. "/luarocks.bat")) | ||
| 70 | assert.falsy(lfs.attributes(myproject .. "/lua")) | ||
| 71 | assert.falsy(lfs.attributes(myproject .. "/luarocks")) | ||
| 72 | assert.truthy(lfs.attributes(myproject .. "/lua_modules")) | ||
| 73 | assert.truthy(lfs.attributes(myproject .. "/.luarocks")) | ||
| 74 | assert.truthy(lfs.attributes(myproject .. "/.luarocks/config-" .. test_env.lua_version .. ".lua")) | ||
| 75 | assert.truthy(lfs.attributes(myproject .. "/.gitignore")) | ||
| 76 | assert.truthy(lfs.attributes(myproject .. "/myproject-dev-1.rockspec")) | ||
| 77 | end, finally) | ||
| 78 | end) | ||
| 79 | |||
| 80 | it("with --wrapper-dir", function() | ||
| 81 | test_env.run_in_tmp(function(tmpdir) | ||
| 82 | local myproject = tmpdir .. "/myproject" | ||
| 83 | lfs.mkdir(myproject) | ||
| 84 | lfs.chdir(myproject) | ||
| 85 | |||
| 86 | assert(run.luarocks("init --wrapper-dir=./bin")) | ||
| 87 | if is_win then | ||
| 88 | assert.truthy(lfs.attributes(myproject .. "/bin/lua.bat")) | ||
| 89 | assert.truthy(lfs.attributes(myproject .. "/bin/luarocks.bat")) | ||
| 90 | else | ||
| 91 | assert.truthy(lfs.attributes(myproject .. "/bin/lua")) | ||
| 92 | assert.truthy(lfs.attributes(myproject .. "/bin/luarocks")) | ||
| 93 | end | ||
| 94 | assert.truthy(lfs.attributes(myproject .. "/lua_modules")) | ||
| 95 | assert.truthy(lfs.attributes(myproject .. "/.luarocks")) | ||
| 96 | assert.truthy(lfs.attributes(myproject .. "/.luarocks/config-" .. test_env.lua_version .. ".lua")) | ||
| 97 | assert.truthy(lfs.attributes(myproject .. "/.gitignore")) | ||
| 98 | assert.truthy(lfs.attributes(myproject .. "/myproject-dev-1.rockspec")) | ||
| 99 | end, finally) | ||
| 100 | end) | ||
| 101 | |||
| 39 | it("lua wrapper works", function() | 102 | it("lua wrapper works", function() |
| 40 | test_env.run_in_tmp(function(tmpdir) | 103 | test_env.run_in_tmp(function(tmpdir) |
| 41 | local myproject = tmpdir .. "/myproject" | 104 | 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) | |||
| 17 | :args("?") | 17 | :args("?") |
| 18 | cmd:argument("version", "An optional project version.") | 18 | cmd:argument("version", "An optional project version.") |
| 19 | :args("?") | 19 | :args("?") |
| 20 | cmd:flag("--reset", "Delete .luarocks/config-5.x.lua and ./lua and generate new ones.") | 20 | cmd:option("--wrapper-dir", "Location where the 'lua' and 'luarocks' wrapper scripts " .. |
| 21 | "should be generated; if not given, the current directory is used as a default.") | ||
| 22 | cmd:flag("--reset", "Delete any .luarocks/config-5.x.lua and ./lua and generate new ones.") | ||
| 23 | cmd:flag("--no-wrapper-scripts", "Do not generate wrapper ./lua and ./luarocks launcher scripts.") | ||
| 24 | cmd:flag("--no-gitignore", "Do not generate a .gitignore file.") | ||
| 21 | 25 | ||
| 22 | cmd:group("Options for specifying rockspec data", write_rockspec.cmd_options(cmd)) | 26 | cmd:group("Options for specifying rockspec data", write_rockspec.cmd_options(cmd)) |
| 23 | end | 27 | end |
| 24 | 28 | ||
| 29 | local function gitignore_path(pwd, wrapper_dir, filename) | ||
| 30 | local norm_cur = dir.normalize(fs.absolute_name(pwd)) | ||
| 31 | local norm_file = dir.normalize(fs.absolute_name(dir.path(wrapper_dir, filename))) | ||
| 32 | if norm_file:sub(1, #norm_cur) == norm_cur then | ||
| 33 | return norm_file:sub(#norm_cur) | ||
| 34 | else | ||
| 35 | return "/" .. filename | ||
| 36 | end | ||
| 37 | end | ||
| 38 | |||
| 25 | local function write_gitignore(entries) | 39 | local function write_gitignore(entries) |
| 26 | local gitignore = "" | 40 | local gitignore = "" |
| 27 | local fd = io.open(".gitignore", "r") | 41 | local fd = io.open(".gitignore", "r") |
| @@ -41,9 +55,46 @@ local function write_gitignore(entries) | |||
| 41 | fd:close() | 55 | fd:close() |
| 42 | end | 56 | end |
| 43 | 57 | ||
| 58 | local function write_wrapper_scripts(wrapper_dir, luarocks_wrapper, lua_wrapper) | ||
| 59 | local tree = dir.path(fs.current_dir(), "lua_modules") | ||
| 60 | |||
| 61 | fs.make_dir(wrapper_dir) | ||
| 62 | |||
| 63 | luarocks_wrapper = dir.path(wrapper_dir, luarocks_wrapper) | ||
| 64 | if not fs.exists(luarocks_wrapper) then | ||
| 65 | util.printout("Preparing " .. luarocks_wrapper .. " ...") | ||
| 66 | fs.wrap_script(arg[0], luarocks_wrapper, "none", nil, nil, "--project-tree", tree) | ||
| 67 | else | ||
| 68 | util.printout(luarocks_wrapper .. " already exists. Not overwriting it!") | ||
| 69 | end | ||
| 70 | |||
| 71 | lua_wrapper = dir.path(wrapper_dir, lua_wrapper) | ||
| 72 | local write_lua_wrapper = true | ||
| 73 | if fs.exists(lua_wrapper) then | ||
| 74 | if not util.lua_is_wrapper(lua_wrapper) then | ||
| 75 | util.printout(lua_wrapper .. " already exists and does not look like a wrapper script. Not overwriting.") | ||
| 76 | write_lua_wrapper = false | ||
| 77 | end | ||
| 78 | end | ||
| 79 | |||
| 80 | if write_lua_wrapper then | ||
| 81 | local interp = dir.path(cfg.variables["LUA_BINDIR"], cfg.lua_interpreter) | ||
| 82 | if util.check_lua_version(interp, cfg.lua_version) then | ||
| 83 | util.printout("Preparing " .. lua_wrapper .. " for version " .. cfg.lua_version .. "...") | ||
| 84 | path.use_tree(tree) | ||
| 85 | fs.wrap_script(nil, lua_wrapper, "all") | ||
| 86 | else | ||
| 87 | util.warning("No Lua interpreter detected for version " .. cfg.lua_version .. ". Not creating " .. lua_wrapper) | ||
| 88 | end | ||
| 89 | end | ||
| 90 | end | ||
| 91 | |||
| 44 | --- Driver function for "init" command. | 92 | --- Driver function for "init" command. |
| 45 | -- @return boolean: True if succeeded, nil on errors. | 93 | -- @return boolean: True if succeeded, nil on errors. |
| 46 | function init.command(args) | 94 | function init.command(args) |
| 95 | local do_gitignore = not args.no_gitignore | ||
| 96 | local do_wrapper_scripts = not args.no_wrapper_scripts | ||
| 97 | local wrapper_dir = args.wrapper_dir or "." | ||
| 47 | 98 | ||
| 48 | local pwd = fs.current_dir() | 99 | local pwd = fs.current_dir() |
| 49 | 100 | ||
| @@ -86,15 +137,24 @@ function init.command(args) | |||
| 86 | local luarocks_wrapper = "luarocks" .. ext | 137 | local luarocks_wrapper = "luarocks" .. ext |
| 87 | local lua_wrapper = "lua" .. ext | 138 | local lua_wrapper = "lua" .. ext |
| 88 | 139 | ||
| 89 | util.printout("Adding entries to .gitignore ...") | 140 | if do_gitignore then |
| 90 | write_gitignore({ luarocks_wrapper, lua_wrapper, "lua_modules", ".luarocks" }) | 141 | util.printout("Adding entries to .gitignore ...") |
| 142 | local ignores = { "lua_modules", ".luarocks" } | ||
| 143 | if do_wrapper_scripts then | ||
| 144 | table.insert(ignores, 1, gitignore_path(pwd, wrapper_dir, luarocks_wrapper)) | ||
| 145 | table.insert(ignores, 2, gitignore_path(pwd, wrapper_dir, lua_wrapper)) | ||
| 146 | end | ||
| 147 | write_gitignore(ignores) | ||
| 148 | end | ||
| 91 | 149 | ||
| 92 | util.printout("Preparing ./.luarocks/ ...") | 150 | util.printout("Preparing ./.luarocks/ ...") |
| 93 | fs.make_dir(".luarocks") | 151 | fs.make_dir(".luarocks") |
| 94 | local config_file = ".luarocks/config-" .. cfg.lua_version .. ".lua" | 152 | local config_file = ".luarocks/config-" .. cfg.lua_version .. ".lua" |
| 95 | 153 | ||
| 96 | if args.reset then | 154 | if args.reset then |
| 97 | fs.delete(lua_wrapper) | 155 | if do_wrapper_scripts then |
| 156 | fs.delete(dir.path(wrapper_dir, lua_wrapper)) | ||
| 157 | end | ||
| 98 | fs.delete(config_file) | 158 | fs.delete(config_file) |
| 99 | end | 159 | end |
| 100 | 160 | ||
| @@ -138,36 +198,10 @@ function init.command(args) | |||
| 138 | end | 198 | end |
| 139 | 199 | ||
| 140 | util.printout("Preparing ./lua_modules/ ...") | 200 | util.printout("Preparing ./lua_modules/ ...") |
| 141 | |||
| 142 | fs.make_dir("lua_modules/lib/luarocks/rocks-" .. cfg.lua_version) | 201 | fs.make_dir("lua_modules/lib/luarocks/rocks-" .. cfg.lua_version) |
| 143 | local tree = dir.path(pwd, "lua_modules") | ||
| 144 | |||
| 145 | luarocks_wrapper = dir.path(".", luarocks_wrapper) | ||
| 146 | if not fs.exists(luarocks_wrapper) then | ||
| 147 | util.printout("Preparing " .. luarocks_wrapper .. " ...") | ||
| 148 | fs.wrap_script(arg[0], luarocks_wrapper, "none", nil, nil, "--project-tree", tree) | ||
| 149 | else | ||
| 150 | util.printout(luarocks_wrapper .. " already exists. Not overwriting it!") | ||
| 151 | end | ||
| 152 | |||
| 153 | lua_wrapper = dir.path(".", lua_wrapper) | ||
| 154 | local write_lua_wrapper = true | ||
| 155 | if fs.exists(lua_wrapper) then | ||
| 156 | if not util.lua_is_wrapper(lua_wrapper) then | ||
| 157 | util.printout(lua_wrapper .. " already exists and does not look like a wrapper script. Not overwriting.") | ||
| 158 | write_lua_wrapper = false | ||
| 159 | end | ||
| 160 | end | ||
| 161 | 202 | ||
| 162 | if write_lua_wrapper then | 203 | if do_wrapper_scripts then |
| 163 | local interp = dir.path(cfg.variables["LUA_BINDIR"], cfg.lua_interpreter) | 204 | write_wrapper_scripts(wrapper_dir, luarocks_wrapper, lua_wrapper) |
| 164 | if util.check_lua_version(interp, cfg.lua_version) then | ||
| 165 | util.printout("Preparing " .. lua_wrapper .. " for version " .. cfg.lua_version .. "...") | ||
| 166 | path.use_tree(tree) | ||
| 167 | fs.wrap_script(nil, lua_wrapper, "all") | ||
| 168 | else | ||
| 169 | util.warning("No Lua interpreter detected for version " .. cfg.lua_version .. ". Not creating " .. lua_wrapper) | ||
| 170 | end | ||
| 171 | end | 205 | end |
| 172 | 206 | ||
| 173 | return true | 207 | return true |
