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 /src | |
| 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.
Diffstat (limited to 'src')
| -rw-r--r-- | src/luarocks/cmd/init.lua | 98 |
1 files changed, 66 insertions, 32 deletions
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 |
