From adf40aba69130f0fb2630cfa3da70399823d8399 Mon Sep 17 00:00:00 2001 From: Hisham Muhammad Date: Thu, 24 May 2018 16:03:06 -0300 Subject: init: begin implementing `luarocks init` --- src/bin/luarocks | 1 + src/luarocks/cmd/init.lua | 68 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+) create mode 100644 src/luarocks/cmd/init.lua diff --git a/src/bin/luarocks b/src/bin/luarocks index 066c72e3..583b50b8 100755 --- a/src/bin/luarocks +++ b/src/bin/luarocks @@ -10,6 +10,7 @@ program_description = "LuaRocks main command-line interface" commands = { help = "luarocks.cmd.help", + init = "luarocks.cmd.init", pack = "luarocks.cmd.pack", unpack = "luarocks.cmd.unpack", build = "luarocks.cmd.build", diff --git a/src/luarocks/cmd/init.lua b/src/luarocks/cmd/init.lua new file mode 100644 index 00000000..f4309945 --- /dev/null +++ b/src/luarocks/cmd/init.lua @@ -0,0 +1,68 @@ + +local init = {} + +local cfg = require("luarocks.core.cfg") +local fs = require("luarocks.fs") +local path = require("luarocks.path") +local dir = require("luarocks.dir") +local util = require("luarocks.util") +local write_rockspec = require("luarocks.cmd.write_rockspec") + +init.help_summary = "Initialize a directory for a Lua project using LuaRocks." +init.help_arguments = "[ []]" +init.help = [[ + is the project name. + is an optional project version. + +--license="" A license string, such as "MIT/X11" or "GNU GPL v3". +--summary="" A short one-line description summary. +--detailed="" A longer description string. +--homepage= Project homepage. +--lua-version= Supported Lua versions. Accepted values are "5.1", "5.2", + "5.3", "5.1,5.2", "5.2,5.3", or "5.1,5.2,5.3". +--rockspec-format= Rockspec format version, such as "1.0" or "1.1". +--lib=[,] A comma-separated list of libraries that C files need to + link to. +]] + +local function write_gitignore() + if fs.exists(".gitignore") then + return + end + local fd = io.open(".gitignore", "w") + fd:write("lua_modules\n") + fd:write("lua\n") + fd:close() +end + +--- Driver function for "init" command. +-- @return boolean: True if succeeded, nil on errors. +function init.command(flags, name, version) + + local pwd = fs.current_dir() + + if not name then + name = dir.base_name(pwd) + end + + util.printout("Initializing project " .. name) + + local ok, err = write_rockspec.command(flags, name, version or "dev", pwd) + if not ok then + util.printerr(err) + end + + write_gitignore() + + fs.make_dir("lua_modules/lib/luarocks/rocks-" .. cfg.lua_version) + local tree = dir.path(pwd, "lua_modules") + + fs.wrap_script(arg[0], "luarocks", nil, nil, "--tree", tree) + + path.use_tree(tree) + fs.wrap_script(nil, "lua") + + return true +end + +return init -- cgit v1.2.3-55-g6feb