aboutsummaryrefslogtreecommitdiff
path: root/src/luarocks/cmd/init.lua
blob: 3b8c9fea0312bf2edf83b1bdb1072ed1a6bdb797 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157

local init = {}

local cfg = require("luarocks.core.cfg")
local fs = require("luarocks.fs")
local path = require("luarocks.path")
local deps = require("luarocks.deps")
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 = "[<name> [<version>]]"
init.help = [[
<name> is the project name.
<version> is an optional project version.

--reset                  Regenerate .luarocks/config-5.x.lua and ./lua wrapper
                         if those already exist.

Options for specifying rockspec data:

--license="<string>"     A license string, such as "MIT/X11" or "GNU GPL v3".
--summary="<txt>"        A short one-line description summary.
--detailed="<txt>"       A longer description string.
--homepage=<url>         Project homepage.
--lua-versions=<ver>     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=<ver>  Rockspec format version, such as "1.0" or "1.1".
--lib=<lib>[,<lib>]      A comma-separated list of libraries that C files need to
                         link to.
]]

local function write_gitignore(entries)
   local gitignore = ""
   local fd = io.open(".gitignore", "r")
   if fd then
      gitignore = fd:read("*a")
      fd:close()
      gitignore = "\n" .. gitignore .. "\n"
   end
   
   fd = io.open(".gitignore", gitignore and "a" or "w")
   for _, entry in ipairs(entries) do
      entry = "/" .. entry
      if not gitignore:find("\n"..entry.."\n", 1, true) then
         fd:write(entry.."\n")
      end
   end
   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)
      if name == "/" then
         return nil, "When running from the root directory, please specify the <name> argument"
      end
   end

   util.printout("Initializing project " .. name .. " ...")
   
   local has_rockspec = false
   for file in fs.dir() do
      if file:match("%.rockspec$") then
         has_rockspec = true
         break
      end
   end

   if not has_rockspec then
      local ok, err = write_rockspec.command(flags, name, version or "dev", pwd)
      if not ok then
         util.printerr(err)
      end
   end

   util.printout("Checking your Lua installation ...")
   local ok, err = deps.check_lua(cfg.variables)
   if not ok then
      return nil, err
   end

   local ext = cfg.wrapper_suffix
   local luarocks_wrapper = "luarocks" .. ext
   local lua_wrapper = "lua" .. ext

   if flags["reset"] then
      fs.delete(lua_wrapper)
      fs.delete(dir.path(".luarocks", "default_lua_version.lua"))
   end

   util.printout("Adding entries to .gitignore ...")
   write_gitignore({ luarocks_wrapper, lua_wrapper, "lua_modules" })

   util.printout("Preparing ./.luarocks/ ...")
   fs.make_dir(".luarocks")
   local config_file = ".luarocks/config-" .. cfg.lua_version .. ".lua"
   if not fs.exists(config_file) then
      local fd = io.open(config_file, "w")
      fd:write("-- LuaRocks configuration for use with Lua " .. cfg.lua_version .. "\n")
      if cfg.lua_interpreter then
         fd:write(("lua_interpreter = %q\n"):format(cfg.lua_interpreter))
      end
      if cfg.luajit_version then
         fd:write(("luajit_version = %q\n"):format(cfg.luajit_version))
      end
      fd:write("variables = {\n")
      local varnames = {
         "LUA_DIR",
         "LUA_INCDIR",
         "LUA_LIBDIR",
         "LUA_BINDIR",
         "LUA_INTERPRETER",
      }
      for _, varname in ipairs(varnames) do
         if cfg.variables[varname] then
            fd:write(("   %s = %q,\n"):format(varname, cfg.variables[varname]))
         end
      end
      fd:write("}\n")
      fd:close()
   else
      util.printout(config_file .. " already exists. Not overwriting it!")
   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", "none", nil, nil, "--project-tree", tree)
   else
      util.printout(luarocks_wrapper .. " already exists. Not overwriting it!")
   end

   lua_wrapper = dir.path(".", lua_wrapper)
   if not fs.exists(lua_wrapper) then
      util.printout("Preparing " .. lua_wrapper .. " for version " .. cfg.lua_version .. "...")
      path.use_tree(tree)
      fs.wrap_script(nil, "lua", "all")
   else
      util.printout(lua_wrapper .. " already exists. Not overwriting it!")
   end

   return true
end

return init