diff options
Diffstat (limited to 'src')
61 files changed, 1722 insertions, 1649 deletions
diff --git a/src/bin/luarocks b/src/bin/luarocks index be6c2b81..21f17da9 100755 --- a/src/bin/luarocks +++ b/src/bin/luarocks | |||
@@ -1,7 +1,7 @@ | |||
1 | #!/usr/bin/env lua | 1 | #!/usr/bin/env lua |
2 | 2 | ||
3 | -- this should be loaded first. | 3 | -- this should be loaded first. |
4 | local cfg = require("luarocks.cfg") | 4 | local cfg = require("luarocks.core.cfg") |
5 | 5 | ||
6 | local loader = require("luarocks.loader") | 6 | local loader = require("luarocks.loader") |
7 | local command_line = require("luarocks.command_line") | 7 | local command_line = require("luarocks.command_line") |
diff --git a/src/bin/luarocks-admin b/src/bin/luarocks-admin index 2890d1f1..660c0a70 100755 --- a/src/bin/luarocks-admin +++ b/src/bin/luarocks-admin | |||
@@ -1,7 +1,7 @@ | |||
1 | #!/usr/bin/env lua | 1 | #!/usr/bin/env lua |
2 | 2 | ||
3 | -- this should be loaded first. | 3 | -- this should be loaded first. |
4 | local cfg = require("luarocks.cfg") | 4 | local cfg = require("luarocks.core.cfg") |
5 | 5 | ||
6 | local loader = require("luarocks.loader") | 6 | local loader = require("luarocks.loader") |
7 | local command_line = require("luarocks.command_line") | 7 | local command_line = require("luarocks.command_line") |
@@ -10,10 +10,10 @@ program_description = "LuaRocks repository administration interface" | |||
10 | 10 | ||
11 | commands = { | 11 | commands = { |
12 | help = "luarocks.help", | 12 | help = "luarocks.help", |
13 | make_manifest = "luarocks.make_manifest", | 13 | make_manifest = "luarocks.admin.make_manifest", |
14 | add = "luarocks.add", | 14 | add = "luarocks.admin.add", |
15 | remove = "luarocks.admin_remove", | 15 | remove = "luarocks.admin.remove", |
16 | refresh_cache = "luarocks.refresh_cache", | 16 | refresh_cache = "luarocks.admin.refresh_cache", |
17 | } | 17 | } |
18 | 18 | ||
19 | command_line.run_command(...) | 19 | command_line.run_command(...) |
diff --git a/src/luarocks/add.lua b/src/luarocks/admin/add.lua index f37d334d..daf46c1d 100644 --- a/src/luarocks/add.lua +++ b/src/luarocks/admin/add.lua | |||
@@ -2,17 +2,15 @@ | |||
2 | --- Module implementing the luarocks-admin "add" command. | 2 | --- Module implementing the luarocks-admin "add" command. |
3 | -- Adds a rock or rockspec to a rocks server. | 3 | -- Adds a rock or rockspec to a rocks server. |
4 | local add = {} | 4 | local add = {} |
5 | package.loaded["luarocks.add"] = add | ||
6 | 5 | ||
7 | local cfg = require("luarocks.cfg") | 6 | local cfg = require("luarocks.core.cfg") |
8 | local util = require("luarocks.util") | 7 | local util = require("luarocks.util") |
9 | local dir = require("luarocks.dir") | 8 | local dir = require("luarocks.dir") |
10 | local manif = require("luarocks.manif") | 9 | local writer = require("luarocks.manif.writer") |
11 | local index = require("luarocks.index") | 10 | local index = require("luarocks.index") |
12 | local fs = require("luarocks.fs") | 11 | local fs = require("luarocks.fs") |
13 | local cache = require("luarocks.cache") | 12 | local cache = require("luarocks.admin.cache") |
14 | 13 | ||
15 | util.add_run_function(add) | ||
16 | add.help_summary = "Add a rock or rockspec to a rocks server." | 14 | add.help_summary = "Add a rock or rockspec to a rocks server." |
17 | add.help_arguments = "[--server=<server>] [--no-refresh] {<rockspec>|<rock>...}" | 15 | add.help_arguments = "[--server=<server>] [--no-refresh] {<rockspec>|<rock>...}" |
18 | add.help = [[ | 16 | add.help = [[ |
@@ -24,6 +22,15 @@ The flag --no-refresh indicates the local cache should not be refreshed | |||
24 | prior to generation of the updated manifest. | 22 | prior to generation of the updated manifest. |
25 | ]] | 23 | ]] |
26 | 24 | ||
25 | local function zip_manifests() | ||
26 | for ver in util.lua_versions() do | ||
27 | local file = "manifest-"..ver | ||
28 | local zip = file..".zip" | ||
29 | fs.delete(dir.path(fs.current_dir(), zip)) | ||
30 | fs.zip(zip, file) | ||
31 | end | ||
32 | end | ||
33 | |||
27 | local function add_files_to_server(refresh, rockfiles, server, upload_server) | 34 | local function add_files_to_server(refresh, rockfiles, server, upload_server) |
28 | assert(type(refresh) == "boolean" or not refresh) | 35 | assert(type(refresh) == "boolean" or not refresh) |
29 | assert(type(rockfiles) == "table") | 36 | assert(type(rockfiles) == "table") |
@@ -68,9 +75,9 @@ local function add_files_to_server(refresh, rockfiles, server, upload_server) | |||
68 | if not ok then return nil, err end | 75 | if not ok then return nil, err end |
69 | 76 | ||
70 | util.printout("Updating manifest...") | 77 | util.printout("Updating manifest...") |
71 | manif.make_manifest(local_cache, "one", true) | 78 | writer.make_manifest(local_cache, "one", true) |
72 | 79 | ||
73 | manif.zip_manifests() | 80 | zip_manifests() |
74 | 81 | ||
75 | util.printout("Updating index.html...") | 82 | util.printout("Updating index.html...") |
76 | index.make_index(local_cache) | 83 | index.make_index(local_cache) |
diff --git a/src/luarocks/cache.lua b/src/luarocks/admin/cache.lua index 4a95f70e..0daa0fc0 100644 --- a/src/luarocks/cache.lua +++ b/src/luarocks/admin/cache.lua | |||
@@ -2,10 +2,9 @@ | |||
2 | --- Module handling the LuaRocks local cache. | 2 | --- Module handling the LuaRocks local cache. |
3 | -- Adds a rock or rockspec to a rocks server. | 3 | -- Adds a rock or rockspec to a rocks server. |
4 | local cache = {} | 4 | local cache = {} |
5 | package.loaded["luarocks.cache"] = cache | ||
6 | 5 | ||
7 | local fs = require("luarocks.fs") | 6 | local fs = require("luarocks.fs") |
8 | local cfg = require("luarocks.cfg") | 7 | local cfg = require("luarocks.core.cfg") |
9 | local dir = require("luarocks.dir") | 8 | local dir = require("luarocks.dir") |
10 | local util = require("luarocks.util") | 9 | local util = require("luarocks.util") |
11 | 10 | ||
diff --git a/src/luarocks/make_manifest.lua b/src/luarocks/admin/make_manifest.lua index c39c2939..57851942 100644 --- a/src/luarocks/make_manifest.lua +++ b/src/luarocks/admin/make_manifest.lua | |||
@@ -2,17 +2,15 @@ | |||
2 | --- Module implementing the luarocks-admin "make_manifest" command. | 2 | --- Module implementing the luarocks-admin "make_manifest" command. |
3 | -- Compile a manifest file for a repository. | 3 | -- Compile a manifest file for a repository. |
4 | local make_manifest = {} | 4 | local make_manifest = {} |
5 | package.loaded["luarocks.make_manifest"] = make_manifest | ||
6 | 5 | ||
7 | local manif = require("luarocks.manif") | 6 | local writer = require("luarocks.manif.writer") |
8 | local index = require("luarocks.index") | 7 | local index = require("luarocks.index") |
9 | local cfg = require("luarocks.cfg") | 8 | local cfg = require("luarocks.core.cfg") |
10 | local util = require("luarocks.util") | 9 | local util = require("luarocks.util") |
11 | local deps = require("luarocks.deps") | 10 | local deps = require("luarocks.deps") |
12 | local fs = require("luarocks.fs") | 11 | local fs = require("luarocks.fs") |
13 | local dir = require("luarocks.dir") | 12 | local dir = require("luarocks.dir") |
14 | 13 | ||
15 | util.add_run_function(make_manifest) | ||
16 | make_manifest.help_summary = "Compile a manifest file for a repository." | 14 | make_manifest.help_summary = "Compile a manifest file for a repository." |
17 | 15 | ||
18 | make_manifest.help = [[ | 16 | make_manifest.help = [[ |
@@ -37,7 +35,7 @@ function make_manifest.command(flags, repo) | |||
37 | util.warning("This looks like a local rocks tree, but you did not pass --local-tree.") | 35 | util.warning("This looks like a local rocks tree, but you did not pass --local-tree.") |
38 | end | 36 | end |
39 | 37 | ||
40 | local ok, err = manif.make_manifest(repo, deps.get_deps_mode(flags), not flags["local-tree"]) | 38 | local ok, err = writer.make_manifest(repo, deps.get_deps_mode(flags), not flags["local-tree"]) |
41 | if ok and not flags["local-tree"] then | 39 | if ok and not flags["local-tree"] then |
42 | util.printout("Generating index.html for "..repo) | 40 | util.printout("Generating index.html for "..repo) |
43 | index.make_index(repo) | 41 | index.make_index(repo) |
diff --git a/src/luarocks/refresh_cache.lua b/src/luarocks/admin/refresh_cache.lua index bbfd1f4d..947dbfb0 100644 --- a/src/luarocks/refresh_cache.lua +++ b/src/luarocks/admin/refresh_cache.lua | |||
@@ -1,13 +1,10 @@ | |||
1 | 1 | ||
2 | --- Module implementing the luarocks-admin "refresh_cache" command. | 2 | --- Module implementing the luarocks-admin "refresh_cache" command. |
3 | local refresh_cache = {} | 3 | local refresh_cache = {} |
4 | package.loaded["luarocks.refresh_cache"] = refresh_cache | ||
5 | 4 | ||
6 | local util = require("luarocks.util") | 5 | local cfg = require("luarocks.core.cfg") |
7 | local cfg = require("luarocks.cfg") | 6 | local cache = require("luarocks.admin.cache") |
8 | local cache = require("luarocks.cache") | ||
9 | 7 | ||
10 | util.add_run_function(refresh_cache) | ||
11 | refresh_cache.help_summary = "Refresh local cache of a remote rocks server." | 8 | refresh_cache.help_summary = "Refresh local cache of a remote rocks server." |
12 | refresh_cache.help_arguments = "[--from=<server>]" | 9 | refresh_cache.help_arguments = "[--from=<server>]" |
13 | refresh_cache.help = [[ | 10 | refresh_cache.help = [[ |
diff --git a/src/luarocks/admin_remove.lua b/src/luarocks/admin/remove.lua index 621f1317..763a166f 100644 --- a/src/luarocks/admin_remove.lua +++ b/src/luarocks/admin/remove.lua | |||
@@ -2,17 +2,15 @@ | |||
2 | --- Module implementing the luarocks-admin "remove" command. | 2 | --- Module implementing the luarocks-admin "remove" command. |
3 | -- Removes a rock or rockspec from a rocks server. | 3 | -- Removes a rock or rockspec from a rocks server. |
4 | local admin_remove = {} | 4 | local admin_remove = {} |
5 | package.loaded["luarocks.admin_remove"] = admin_remove | ||
6 | 5 | ||
7 | local cfg = require("luarocks.cfg") | 6 | local cfg = require("luarocks.core.cfg") |
8 | local util = require("luarocks.util") | 7 | local util = require("luarocks.util") |
9 | local dir = require("luarocks.dir") | 8 | local dir = require("luarocks.dir") |
10 | local manif = require("luarocks.manif") | 9 | local writer = require("luarocks.manif.writer") |
11 | local index = require("luarocks.index") | 10 | local index = require("luarocks.index") |
12 | local fs = require("luarocks.fs") | 11 | local fs = require("luarocks.fs") |
13 | local cache = require("luarocks.cache") | 12 | local cache = require("luarocks.admin.cache") |
14 | 13 | ||
15 | util.add_run_function(admin_remove) | ||
16 | admin_remove.help_summary = "Remove a rock or rockspec from a rocks server." | 14 | admin_remove.help_summary = "Remove a rock or rockspec from a rocks server." |
17 | admin_remove.help_arguments = "[--server=<server>] [--no-refresh] {<rockspec>|<rock>...}" | 15 | admin_remove.help_arguments = "[--server=<server>] [--no-refresh] {<rockspec>|<rock>...}" |
18 | admin_remove.help = [[ | 16 | admin_remove.help = [[ |
@@ -65,7 +63,7 @@ local function remove_files_from_server(refresh, rockfiles, server, upload_serve | |||
65 | if not ok then return nil, err end | 63 | if not ok then return nil, err end |
66 | 64 | ||
67 | util.printout("Updating manifest...") | 65 | util.printout("Updating manifest...") |
68 | manif.make_manifest(local_cache, "one", true) | 66 | writer.make_manifest(local_cache, "one", true) |
69 | util.printout("Updating index.html...") | 67 | util.printout("Updating index.html...") |
70 | index.make_index(local_cache) | 68 | index.make_index(local_cache) |
71 | 69 | ||
diff --git a/src/luarocks/build.lua b/src/luarocks/build.lua index f52fb7a8..3601dc9b 100644 --- a/src/luarocks/build.lua +++ b/src/luarocks/build.lua | |||
@@ -2,7 +2,6 @@ | |||
2 | --- Module implementing the LuaRocks "build" command. | 2 | --- Module implementing the LuaRocks "build" command. |
3 | -- Builds a rock, compiling its C parts if any. | 3 | -- Builds a rock, compiling its C parts if any. |
4 | local build = {} | 4 | local build = {} |
5 | package.loaded["luarocks.build"] = build | ||
6 | 5 | ||
7 | local pack = require("luarocks.pack") | 6 | local pack = require("luarocks.pack") |
8 | local path = require("luarocks.path") | 7 | local path = require("luarocks.path") |
@@ -12,11 +11,10 @@ local fetch = require("luarocks.fetch") | |||
12 | local fs = require("luarocks.fs") | 11 | local fs = require("luarocks.fs") |
13 | local dir = require("luarocks.dir") | 12 | local dir = require("luarocks.dir") |
14 | local deps = require("luarocks.deps") | 13 | local deps = require("luarocks.deps") |
15 | local manif = require("luarocks.manif") | 14 | local writer = require("luarocks.manif.writer") |
16 | local remove = require("luarocks.remove") | 15 | local remove = require("luarocks.remove") |
17 | local cfg = require("luarocks.cfg") | 16 | local cfg = require("luarocks.core.cfg") |
18 | 17 | ||
19 | util.add_run_function(build) | ||
20 | build.help_summary = "Build/compile a rock." | 18 | build.help_summary = "Build/compile a rock." |
21 | build.help_arguments = "[--pack-binary-rock] [--keep] {<rockspec>|<rock>|<name> [<version>]}" | 19 | build.help_arguments = "[--pack-binary-rock] [--keep] {<rockspec>|<rock>|<name> [<version>]}" |
22 | build.help = [[ | 20 | build.help = [[ |
@@ -148,6 +146,31 @@ local function install_default_docs(name, version) | |||
148 | end | 146 | end |
149 | end | 147 | end |
150 | 148 | ||
149 | local function check_macosx_deployment_target(rockspec) | ||
150 | local target = rockspec.build.macosx_deployment_target | ||
151 | local function minor(version) | ||
152 | return tonumber(version and version:match("^[^.]+%.([^.]+)")) | ||
153 | end | ||
154 | local function patch_variable(var, target) | ||
155 | if rockspec.variables[var]:match("MACOSX_DEPLOYMENT_TARGET") then | ||
156 | rockspec.variables[var] = (rockspec.variables[var]):gsub("MACOSX_DEPLOYMENT_TARGET=[^ ]*", "MACOSX_DEPLOYMENT_TARGET="..target) | ||
157 | else | ||
158 | rockspec.variables[var] = "env MACOSX_DEPLOYMENT_TARGET="..target.." "..rockspec.variables[var] | ||
159 | end | ||
160 | end | ||
161 | if cfg.platforms.macosx and rockspec:format_is_at_least("3.0") and target then | ||
162 | local version = util.popen_read("sw_vers -productVersion") | ||
163 | local versionminor = minor(version) | ||
164 | local targetminor = minor(target) | ||
165 | if targetminor > versionminor then | ||
166 | return nil, ("This rock requires Mac OSX 10.%d, and you are running 10.%d."):format(targetminor, versionminor) | ||
167 | end | ||
168 | patch_variable("CC", target) | ||
169 | patch_variable("LD", target) | ||
170 | end | ||
171 | return true | ||
172 | end | ||
173 | |||
151 | --- Build and install a rock given a rockspec. | 174 | --- Build and install a rock given a rockspec. |
152 | -- @param rockspec_file string: local or remote filename of a rockspec. | 175 | -- @param rockspec_file string: local or remote filename of a rockspec. |
153 | -- @param need_to_fetch boolean: true if sources need to be fetched, | 176 | -- @param need_to_fetch boolean: true if sources need to be fetched, |
@@ -245,6 +268,11 @@ function build.build_rockspec(rockspec_file, need_to_fetch, minimal_mode, deps_m | |||
245 | end | 268 | end |
246 | end | 269 | end |
247 | 270 | ||
271 | ok, err = check_macosx_deployment_target(rockspec) | ||
272 | if not ok then | ||
273 | return nil, err | ||
274 | end | ||
275 | |||
248 | if build_spec.type ~= "none" then | 276 | if build_spec.type ~= "none" then |
249 | 277 | ||
250 | -- Temporary compatibility | 278 | -- Temporary compatibility |
@@ -314,7 +342,7 @@ function build.build_rockspec(rockspec_file, need_to_fetch, minimal_mode, deps_m | |||
314 | fs.pop_dir() | 342 | fs.pop_dir() |
315 | end | 343 | end |
316 | 344 | ||
317 | ok, err = manif.make_rock_manifest(name, version) | 345 | ok, err = writer.make_rock_manifest(name, version) |
318 | if err then return nil, err end | 346 | if err then return nil, err end |
319 | 347 | ||
320 | ok, err = repos.deploy_files(name, version, repos.should_wrap_bin_scripts(rockspec), deps_mode) | 348 | ok, err = repos.deploy_files(name, version, repos.should_wrap_bin_scripts(rockspec), deps_mode) |
@@ -400,7 +428,7 @@ function build.command(flags, name, version) | |||
400 | if not ok then return nil, err, cfg.errorcodes.PERMISSIONDENIED end | 428 | if not ok then return nil, err, cfg.errorcodes.PERMISSIONDENIED end |
401 | ok, err = do_build(name, version, deps.get_deps_mode(flags), flags["only-deps"]) | 429 | ok, err = do_build(name, version, deps.get_deps_mode(flags), flags["only-deps"]) |
402 | if not ok then return nil, err end | 430 | if not ok then return nil, err end |
403 | local name, version = ok, err | 431 | name, version = ok, err |
404 | if flags["only-deps"] then | 432 | if flags["only-deps"] then |
405 | return name, version | 433 | return name, version |
406 | end | 434 | end |
diff --git a/src/luarocks/build/builtin.lua b/src/luarocks/build/builtin.lua index 6719be81..3a4beb17 100644 --- a/src/luarocks/build/builtin.lua +++ b/src/luarocks/build/builtin.lua | |||
@@ -7,7 +7,7 @@ local unpack = unpack or table.unpack | |||
7 | local fs = require("luarocks.fs") | 7 | local fs = require("luarocks.fs") |
8 | local path = require("luarocks.path") | 8 | local path = require("luarocks.path") |
9 | local util = require("luarocks.util") | 9 | local util = require("luarocks.util") |
10 | local cfg = require("luarocks.cfg") | 10 | local cfg = require("luarocks.core.cfg") |
11 | local dir = require("luarocks.dir") | 11 | local dir = require("luarocks.dir") |
12 | 12 | ||
13 | --- Run a command displaying its execution on standard output. | 13 | --- Run a command displaying its execution on standard output. |
@@ -49,7 +49,7 @@ end | |||
49 | -- nil and an error message otherwise. | 49 | -- nil and an error message otherwise. |
50 | function builtin.run(rockspec) | 50 | function builtin.run(rockspec) |
51 | assert(type(rockspec) == "table") | 51 | assert(type(rockspec) == "table") |
52 | local compile_object, compile_library, compile_wrapper_binary --TODO EXEWRAPPER | 52 | local compile_object, compile_library, compile_static_library, compile_wrapper_binary --TODO EXEWRAPPER |
53 | 53 | ||
54 | local build = rockspec.build | 54 | local build = rockspec.build |
55 | local variables = rockspec.variables | 55 | local variables = rockspec.variables |
@@ -82,6 +82,13 @@ function builtin.run(rockspec) | |||
82 | local ok = execute(variables.LD.." "..variables.LIBFLAG, "-o", library, unpack(extras)) | 82 | local ok = execute(variables.LD.." "..variables.LIBFLAG, "-o", library, unpack(extras)) |
83 | return ok | 83 | return ok |
84 | end | 84 | end |
85 | compile_static_library = function(library, objects, libraries, libdirs, name) | ||
86 | local ok = execute(variables.AR, "rc", library, unpack(objects)) | ||
87 | if ok then | ||
88 | ok = execute(variables.RANLIB, library) | ||
89 | end | ||
90 | return ok | ||
91 | end | ||
85 | compile_wrapper_binary = function(fullname, name) | 92 | compile_wrapper_binary = function(fullname, name) |
86 | --TODO EXEWRAPPER | 93 | --TODO EXEWRAPPER |
87 | local fullbasename = fullname:gsub("%.lua$", ""):gsub("/", "\\") | 94 | local fullbasename = fullname:gsub("%.lua$", ""):gsub("/", "\\") |
@@ -128,6 +135,10 @@ function builtin.run(rockspec) | |||
128 | end | 135 | end |
129 | return ok | 136 | return ok |
130 | end | 137 | end |
138 | compile_static_library = function(library, objects, libraries, libdirs, name) | ||
139 | local ok = execute(variables.AR, "-out:"..library, unpack(objects)) | ||
140 | return ok | ||
141 | end | ||
131 | compile_wrapper_binary = function(fullname, name) | 142 | compile_wrapper_binary = function(fullname, name) |
132 | --TODO EXEWRAPPER | 143 | --TODO EXEWRAPPER |
133 | local fullbasename = fullname:gsub("%.lua$", ""):gsub("/", "\\") | 144 | local fullbasename = fullname:gsub("%.lua$", ""):gsub("/", "\\") |
@@ -169,6 +180,13 @@ function builtin.run(rockspec) | |||
169 | end | 180 | end |
170 | return execute(variables.LD.." "..variables.LIBFLAG, "-o", library, "-L"..variables.LUA_LIBDIR, unpack(extras)) | 181 | return execute(variables.LD.." "..variables.LIBFLAG, "-o", library, "-L"..variables.LUA_LIBDIR, unpack(extras)) |
171 | end | 182 | end |
183 | compile_static_library = function(library, objects, libraries, libdirs, name) | ||
184 | local ok = execute(variables.AR, "rc", library, unpack(objects)) | ||
185 | if ok then | ||
186 | ok = execute(variables.RANLIB, library) | ||
187 | end | ||
188 | return ok | ||
189 | end | ||
172 | compile_wrapper_binary = function(_, name) return true, name end | 190 | compile_wrapper_binary = function(_, name) return true, name end |
173 | --TODO EXEWRAPPER | 191 | --TODO EXEWRAPPER |
174 | end | 192 | end |
@@ -244,6 +262,15 @@ function builtin.run(rockspec) | |||
244 | if not ok then | 262 | if not ok then |
245 | return nil, "Failed compiling module "..module_name | 263 | return nil, "Failed compiling module "..module_name |
246 | end | 264 | end |
265 | module_name = name:match("([^.]*)$").."."..util.matchquote(cfg.static_lib_extension) | ||
266 | if moddir ~= "" then | ||
267 | module_name = dir.path(moddir, module_name) | ||
268 | end | ||
269 | lib_modules[module_name] = dir.path(libdir, module_name) | ||
270 | ok = compile_static_library(module_name, objects, info.libraries, info.libdirs, name) | ||
271 | if not ok then | ||
272 | return nil, "Failed compiling static library "..module_name | ||
273 | end | ||
247 | end | 274 | end |
248 | end | 275 | end |
249 | for _, mods in ipairs({{ tbl = lua_modules, perms = cfg.perm_read }, { tbl = lib_modules, perms = cfg.perm_exec }}) do | 276 | for _, mods in ipairs({{ tbl = lua_modules, perms = cfg.perm_read }, { tbl = lib_modules, perms = cfg.perm_exec }}) do |
diff --git a/src/luarocks/build/cmake.lua b/src/luarocks/build/cmake.lua index da5a31f1..8ee6b6b2 100644 --- a/src/luarocks/build/cmake.lua +++ b/src/luarocks/build/cmake.lua | |||
@@ -4,7 +4,7 @@ local cmake = {} | |||
4 | 4 | ||
5 | local fs = require("luarocks.fs") | 5 | local fs = require("luarocks.fs") |
6 | local util = require("luarocks.util") | 6 | local util = require("luarocks.util") |
7 | local cfg = require("luarocks.cfg") | 7 | local cfg = require("luarocks.core.cfg") |
8 | 8 | ||
9 | --- Driver function for the "cmake" build back-end. | 9 | --- Driver function for the "cmake" build back-end. |
10 | -- @param rockspec table: the loaded rockspec. | 10 | -- @param rockspec table: the loaded rockspec. |
@@ -52,13 +52,26 @@ function cmake.run(rockspec) | |||
52 | return nil, "Failed cmake." | 52 | return nil, "Failed cmake." |
53 | end | 53 | end |
54 | 54 | ||
55 | if not fs.execute_string(rockspec.variables.CMAKE.." --build build.luarocks --config Release") then | 55 | local do_build, do_install |
56 | return nil, "Failed building." | 56 | if rockspec:format_is_at_least("3.0") then |
57 | do_build = (build.build_pass == nil) and true or build.build_pass | ||
58 | do_install = (build.install_pass == nil) and true or build.install_pass | ||
59 | else | ||
60 | do_build = true | ||
61 | do_install = true | ||
57 | end | 62 | end |
58 | 63 | ||
59 | if not fs.execute_string(rockspec.variables.CMAKE.." --build build.luarocks --target install --config Release") then | 64 | if do_build then |
60 | return nil, "Failed installing." | 65 | if not fs.execute_string(rockspec.variables.CMAKE.." --build build.luarocks --config Release") then |
66 | return nil, "Failed building." | ||
67 | end | ||
61 | end | 68 | end |
69 | if do_install then | ||
70 | if not fs.execute_string(rockspec.variables.CMAKE.." --build build.luarocks --target install --config Release") then | ||
71 | return nil, "Failed installing." | ||
72 | end | ||
73 | end | ||
74 | |||
62 | return true | 75 | return true |
63 | end | 76 | end |
64 | 77 | ||
diff --git a/src/luarocks/build/make.lua b/src/luarocks/build/make.lua index 69e73c2e..ded015b7 100644 --- a/src/luarocks/build/make.lua +++ b/src/luarocks/build/make.lua | |||
@@ -6,7 +6,7 @@ local unpack = unpack or table.unpack | |||
6 | 6 | ||
7 | local fs = require("luarocks.fs") | 7 | local fs = require("luarocks.fs") |
8 | local util = require("luarocks.util") | 8 | local util = require("luarocks.util") |
9 | local cfg = require("luarocks.cfg") | 9 | local cfg = require("luarocks.core.cfg") |
10 | 10 | ||
11 | --- Call "make" with given target and variables | 11 | --- Call "make" with given target and variables |
12 | -- @param make_cmd string: the make command to be used (typically | 12 | -- @param make_cmd string: the make command to be used (typically |
diff --git a/src/luarocks/command_line.lua b/src/luarocks/command_line.lua index 1a8c0fe7..936e9950 100644 --- a/src/luarocks/command_line.lua +++ b/src/luarocks/command_line.lua | |||
@@ -5,7 +5,7 @@ local command_line = {} | |||
5 | local unpack = unpack or table.unpack | 5 | local unpack = unpack or table.unpack |
6 | 6 | ||
7 | local util = require("luarocks.util") | 7 | local util = require("luarocks.util") |
8 | local cfg = require("luarocks.cfg") | 8 | local cfg = require("luarocks.core.cfg") |
9 | local path = require("luarocks.path") | 9 | local path = require("luarocks.path") |
10 | local dir = require("luarocks.dir") | 10 | local dir = require("luarocks.dir") |
11 | local deps = require("luarocks.deps") | 11 | local deps = require("luarocks.deps") |
diff --git a/src/luarocks/config_cmd.lua b/src/luarocks/config_cmd.lua index fe3cc637..b68f7898 100644 --- a/src/luarocks/config_cmd.lua +++ b/src/luarocks/config_cmd.lua | |||
@@ -2,11 +2,10 @@ | |||
2 | -- Queries information about the LuaRocks configuration. | 2 | -- Queries information about the LuaRocks configuration. |
3 | local config_cmd = {} | 3 | local config_cmd = {} |
4 | 4 | ||
5 | local cfg = require("luarocks.cfg") | 5 | local cfg = require("luarocks.core.cfg") |
6 | local util = require("luarocks.util") | 6 | local util = require("luarocks.util") |
7 | local dir = require("luarocks.dir") | 7 | local dir = require("luarocks.dir") |
8 | 8 | ||
9 | util.add_run_function(config_cmd) | ||
10 | config_cmd.help_summary = "Query information about the LuaRocks configuration." | 9 | config_cmd.help_summary = "Query information about the LuaRocks configuration." |
11 | config_cmd.help_arguments = "<flag>" | 10 | config_cmd.help_arguments = "<flag>" |
12 | config_cmd.help = [[ | 11 | config_cmd.help = [[ |
diff --git a/src/luarocks/cfg.lua b/src/luarocks/core/cfg.lua index 33176161..17f76d14 100644 --- a/src/luarocks/cfg.lua +++ b/src/luarocks/core/cfg.lua | |||
@@ -1,3 +1,4 @@ | |||
1 | |||
1 | --- Configuration for LuaRocks. | 2 | --- Configuration for LuaRocks. |
2 | -- Tries to load the user's configuration file and | 3 | -- Tries to load the user's configuration file and |
3 | -- defines defaults for unset values. See the | 4 | -- defines defaults for unset values. See the |
@@ -12,11 +13,7 @@ | |||
12 | local rawset, next, table, pairs, require, io, os, setmetatable, pcall, ipairs, package, tonumber, type, assert, _VERSION = | 13 | local rawset, next, table, pairs, require, io, os, setmetatable, pcall, ipairs, package, tonumber, type, assert, _VERSION = |
13 | rawset, next, table, pairs, require, io, os, setmetatable, pcall, ipairs, package, tonumber, type, assert, _VERSION | 14 | rawset, next, table, pairs, require, io, os, setmetatable, pcall, ipairs, package, tonumber, type, assert, _VERSION |
14 | 15 | ||
15 | --module("luarocks.cfg") | ||
16 | local cfg = {} | 16 | local cfg = {} |
17 | package.loaded["luarocks.cfg"] = cfg | ||
18 | |||
19 | local util = require("luarocks.util") | ||
20 | 17 | ||
21 | cfg.lua_version = _VERSION:match(" (5%.[123])$") or "5.1" | 18 | cfg.lua_version = _VERSION:match(" (5%.[123])$") or "5.1" |
22 | local version_suffix = cfg.lua_version:gsub("%.", "_") | 19 | local version_suffix = cfg.lua_version:gsub("%.", "_") |
@@ -31,15 +28,18 @@ if not ok then | |||
31 | site_config = {} | 28 | site_config = {} |
32 | end | 29 | end |
33 | 30 | ||
31 | local util = require("luarocks.core.util") | ||
32 | local persist = require("luarocks.core.persist") | ||
33 | local require = nil | ||
34 | -------------------------------------------------------------------------------- | ||
35 | |||
34 | cfg.program_version = "scm" | 36 | cfg.program_version = "scm" |
35 | cfg.program_series = "2.2" | 37 | cfg.program_series = "3.0" |
36 | cfg.major_version = (cfg.program_version:match("([^.]%.[^.])")) or cfg.program_series | 38 | cfg.major_version = (cfg.program_version:match("([^.]%.[^.])")) or cfg.program_series |
37 | cfg.variables = {} | 39 | cfg.variables = {} |
38 | cfg.rocks_trees = {} | 40 | cfg.rocks_trees = {} |
39 | cfg.platforms = {} | 41 | cfg.platforms = {} |
40 | 42 | ||
41 | local persist = require("luarocks.persist") | ||
42 | |||
43 | cfg.errorcodes = setmetatable({ | 43 | cfg.errorcodes = setmetatable({ |
44 | OK = 0, | 44 | OK = 0, |
45 | UNSPECIFIED = 1, | 45 | UNSPECIFIED = 1, |
@@ -75,8 +75,8 @@ end | |||
75 | -- so that this detection does not run every time. When it is | 75 | -- so that this detection does not run every time. When it is |
76 | -- performed, we use the Unix way to identify the system, | 76 | -- performed, we use the Unix way to identify the system, |
77 | -- even on Windows (assuming UnxUtils or Cygwin). | 77 | -- even on Windows (assuming UnxUtils or Cygwin). |
78 | local system = site_config.LUAROCKS_UNAME_S or io.popen("uname -s"):read("*l") | 78 | local system = site_config.LUAROCKS_UNAME_S or util.popen_read("uname -s") |
79 | local proc = site_config.LUAROCKS_UNAME_M or io.popen("uname -m"):read("*l") | 79 | local proc = site_config.LUAROCKS_UNAME_M or util.popen_read("uname -m") |
80 | if proc:match("i[%d]86") then | 80 | if proc:match("i[%d]86") then |
81 | cfg.target_cpu = "x86" | 81 | cfg.target_cpu = "x86" |
82 | elseif proc:match("amd64") or proc:match("x86_64") then | 82 | elseif proc:match("amd64") or proc:match("x86_64") then |
@@ -342,6 +342,8 @@ local defaults = { | |||
342 | MAKE = "make", | 342 | MAKE = "make", |
343 | CC = "cc", | 343 | CC = "cc", |
344 | LD = "ld", | 344 | LD = "ld", |
345 | AR = "ar", | ||
346 | RANLIB = "ranlib", | ||
345 | 347 | ||
346 | CVS = "cvs", | 348 | CVS = "cvs", |
347 | GIT = "git", | 349 | GIT = "git", |
@@ -397,7 +399,8 @@ local defaults = { | |||
397 | include = "include" | 399 | include = "include" |
398 | }, | 400 | }, |
399 | 401 | ||
400 | rocks_provided = {} | 402 | rocks_provided = {}, |
403 | rocks_provided_3_0 = {}, | ||
401 | } | 404 | } |
402 | 405 | ||
403 | if cfg.platforms.windows then | 406 | if cfg.platforms.windows then |
@@ -409,6 +412,7 @@ if cfg.platforms.windows then | |||
409 | defaults.arch = "win32-"..cfg.target_cpu | 412 | defaults.arch = "win32-"..cfg.target_cpu |
410 | defaults.lib_extension = "dll" | 413 | defaults.lib_extension = "dll" |
411 | defaults.external_lib_extension = "dll" | 414 | defaults.external_lib_extension = "dll" |
415 | defaults.static_lib_extension = "lib" | ||
412 | defaults.obj_extension = "obj" | 416 | defaults.obj_extension = "obj" |
413 | defaults.external_deps_dirs = { "c:/external/" } | 417 | defaults.external_deps_dirs = { "c:/external/" } |
414 | defaults.variables.LUA_BINDIR = site_config.LUA_BINDIR and site_config.LUA_BINDIR:gsub("\\", "/") or "c:/lua"..cfg.lua_version.."/bin" | 418 | defaults.variables.LUA_BINDIR = site_config.LUA_BINDIR and site_config.LUA_BINDIR:gsub("\\", "/") or "c:/lua"..cfg.lua_version.."/bin" |
@@ -422,6 +426,7 @@ if cfg.platforms.windows then | |||
422 | defaults.variables.WRAPPER = full_prefix.."\\rclauncher.c" | 426 | defaults.variables.WRAPPER = full_prefix.."\\rclauncher.c" |
423 | defaults.variables.LD = "link" | 427 | defaults.variables.LD = "link" |
424 | defaults.variables.MT = "mt" | 428 | defaults.variables.MT = "mt" |
429 | defaults.variables.AR = "lib" | ||
425 | defaults.variables.LUALIB = "lua"..cfg.lua_version..".lib" | 430 | defaults.variables.LUALIB = "lua"..cfg.lua_version..".lib" |
426 | defaults.variables.CFLAGS = "/nologo /MD /O2" | 431 | defaults.variables.CFLAGS = "/nologo /MD /O2" |
427 | defaults.variables.LIBFLAG = "/nologo /dll" | 432 | defaults.variables.LIBFLAG = "/nologo /dll" |
@@ -461,11 +466,14 @@ end | |||
461 | 466 | ||
462 | if cfg.platforms.mingw32 then | 467 | if cfg.platforms.mingw32 then |
463 | defaults.obj_extension = "o" | 468 | defaults.obj_extension = "o" |
469 | defaults.static_lib_extension = "a" | ||
464 | defaults.cmake_generator = "MinGW Makefiles" | 470 | defaults.cmake_generator = "MinGW Makefiles" |
465 | defaults.variables.MAKE = "mingw32-make" | 471 | defaults.variables.MAKE = "mingw32-make" |
466 | defaults.variables.CC = "mingw32-gcc" | 472 | defaults.variables.CC = "mingw32-gcc" |
467 | defaults.variables.RC = "windres" | 473 | defaults.variables.RC = "windres" |
468 | defaults.variables.LD = "mingw32-gcc" | 474 | defaults.variables.LD = "mingw32-gcc" |
475 | defaults.variables.AR = "ar" | ||
476 | defaults.variables.RANLIB = "ranlib" | ||
469 | defaults.variables.CFLAGS = "-O2" | 477 | defaults.variables.CFLAGS = "-O2" |
470 | defaults.variables.LIBFLAG = "-shared" | 478 | defaults.variables.LIBFLAG = "-shared" |
471 | defaults.makefile = "Makefile" | 479 | defaults.makefile = "Makefile" |
@@ -486,6 +494,7 @@ end | |||
486 | 494 | ||
487 | if cfg.platforms.unix then | 495 | if cfg.platforms.unix then |
488 | defaults.lib_extension = "so" | 496 | defaults.lib_extension = "so" |
497 | defaults.static_lib_extension = "a" | ||
489 | defaults.external_lib_extension = "so" | 498 | defaults.external_lib_extension = "so" |
490 | defaults.obj_extension = "o" | 499 | defaults.obj_extension = "o" |
491 | defaults.external_deps_dirs = { "/usr/local", "/usr" } | 500 | defaults.external_deps_dirs = { "/usr/local", "/usr" } |
@@ -558,7 +567,7 @@ if cfg.platforms.macosx then | |||
558 | defaults.variables.LIBFLAG = "-bundle -undefined dynamic_lookup -all_load" | 567 | defaults.variables.LIBFLAG = "-bundle -undefined dynamic_lookup -all_load" |
559 | defaults.variables.STAT = "/usr/bin/stat" | 568 | defaults.variables.STAT = "/usr/bin/stat" |
560 | defaults.variables.STATFLAG = "-f '%A'" | 569 | defaults.variables.STATFLAG = "-f '%A'" |
561 | local version = io.popen("sw_vers -productVersion"):read("*l") | 570 | local version = util.popen_read("sw_vers -productVersion") |
562 | version = tonumber(version and version:match("^[^.]+%.([^.]+)")) or 3 | 571 | version = tonumber(version and version:match("^[^.]+%.([^.]+)")) or 3 |
563 | if version >= 10 then | 572 | if version >= 10 then |
564 | version = 8 | 573 | version = 8 |
@@ -617,8 +626,8 @@ end | |||
617 | if package.loaded.jit then | 626 | if package.loaded.jit then |
618 | -- LuaJIT | 627 | -- LuaJIT |
619 | local lj_version = package.loaded.jit.version:match("LuaJIT (.*)"):gsub("%-","") | 628 | local lj_version = package.loaded.jit.version:match("LuaJIT (.*)"):gsub("%-","") |
620 | --defaults.rocks_provided["luajit"] = lj_version.."-1" | ||
621 | defaults.rocks_provided["luabitop"] = lj_version.."-1" | 629 | defaults.rocks_provided["luabitop"] = lj_version.."-1" |
630 | defaults.rocks_provided_3_0["luajit"] = lj_version.."-1" | ||
622 | end | 631 | end |
623 | 632 | ||
624 | -- Use defaults: | 633 | -- Use defaults: |
@@ -635,6 +644,7 @@ for _, entry in ipairs({"variables", "rocks_provided"}) do | |||
635 | end | 644 | end |
636 | end | 645 | end |
637 | end | 646 | end |
647 | setmetatable(defaults.rocks_provided_3_0, { __index = cfg.rocks_provided }) | ||
638 | 648 | ||
639 | -- For values not set in the config file, use values from the 'defaults' table. | 649 | -- For values not set in the config file, use values from the 'defaults' table. |
640 | local cfg_mt = { | 650 | local cfg_mt = { |
diff --git a/src/luarocks/core/deps.lua b/src/luarocks/core/deps.lua new file mode 100644 index 00000000..6d539eb8 --- /dev/null +++ b/src/luarocks/core/deps.lua | |||
@@ -0,0 +1,192 @@ | |||
1 | |||
2 | local deps = {} | ||
3 | |||
4 | local util = require("luarocks.core.util") | ||
5 | local require = nil | ||
6 | -------------------------------------------------------------------------------- | ||
7 | |||
8 | local deltas = { | ||
9 | scm = 1100, | ||
10 | cvs = 1000, | ||
11 | rc = -1000, | ||
12 | pre = -10000, | ||
13 | beta = -100000, | ||
14 | alpha = -1000000 | ||
15 | } | ||
16 | |||
17 | local version_mt = { | ||
18 | --- Equality comparison for versions. | ||
19 | -- All version numbers must be equal. | ||
20 | -- If both versions have revision numbers, they must be equal; | ||
21 | -- otherwise the revision number is ignored. | ||
22 | -- @param v1 table: version table to compare. | ||
23 | -- @param v2 table: version table to compare. | ||
24 | -- @return boolean: true if they are considered equivalent. | ||
25 | __eq = function(v1, v2) | ||
26 | if #v1 ~= #v2 then | ||
27 | return false | ||
28 | end | ||
29 | for i = 1, #v1 do | ||
30 | if v1[i] ~= v2[i] then | ||
31 | return false | ||
32 | end | ||
33 | end | ||
34 | if v1.revision and v2.revision then | ||
35 | return (v1.revision == v2.revision) | ||
36 | end | ||
37 | return true | ||
38 | end, | ||
39 | --- Size comparison for versions. | ||
40 | -- All version numbers are compared. | ||
41 | -- If both versions have revision numbers, they are compared; | ||
42 | -- otherwise the revision number is ignored. | ||
43 | -- @param v1 table: version table to compare. | ||
44 | -- @param v2 table: version table to compare. | ||
45 | -- @return boolean: true if v1 is considered lower than v2. | ||
46 | __lt = function(v1, v2) | ||
47 | for i = 1, math.max(#v1, #v2) do | ||
48 | local v1i, v2i = v1[i] or 0, v2[i] or 0 | ||
49 | if v1i ~= v2i then | ||
50 | return (v1i < v2i) | ||
51 | end | ||
52 | end | ||
53 | if v1.revision and v2.revision then | ||
54 | return (v1.revision < v2.revision) | ||
55 | end | ||
56 | return false | ||
57 | end | ||
58 | } | ||
59 | |||
60 | local version_cache = {} | ||
61 | setmetatable(version_cache, { | ||
62 | __mode = "kv" | ||
63 | }) | ||
64 | |||
65 | --- Parse a version string, converting to table format. | ||
66 | -- A version table contains all components of the version string | ||
67 | -- converted to numeric format, stored in the array part of the table. | ||
68 | -- If the version contains a revision, it is stored numerically | ||
69 | -- in the 'revision' field. The original string representation of | ||
70 | -- the string is preserved in the 'string' field. | ||
71 | -- Returned version tables use a metatable | ||
72 | -- allowing later comparison through relational operators. | ||
73 | -- @param vstring string: A version number in string format. | ||
74 | -- @return table or nil: A version table or nil | ||
75 | -- if the input string contains invalid characters. | ||
76 | function deps.parse_version(vstring) | ||
77 | if not vstring then return nil end | ||
78 | assert(type(vstring) == "string") | ||
79 | |||
80 | local cached = version_cache[vstring] | ||
81 | if cached then | ||
82 | return cached | ||
83 | end | ||
84 | |||
85 | local version = {} | ||
86 | local i = 1 | ||
87 | |||
88 | local function add_token(number) | ||
89 | version[i] = version[i] and version[i] + number/100000 or number | ||
90 | i = i + 1 | ||
91 | end | ||
92 | |||
93 | -- trim leading and trailing spaces | ||
94 | vstring = vstring:match("^%s*(.*)%s*$") | ||
95 | version.string = vstring | ||
96 | -- store revision separately if any | ||
97 | local main, revision = vstring:match("(.*)%-(%d+)$") | ||
98 | if revision then | ||
99 | vstring = main | ||
100 | version.revision = tonumber(revision) | ||
101 | end | ||
102 | while #vstring > 0 do | ||
103 | -- extract a number | ||
104 | local token, rest = vstring:match("^(%d+)[%.%-%_]*(.*)") | ||
105 | if token then | ||
106 | add_token(tonumber(token)) | ||
107 | else | ||
108 | -- extract a word | ||
109 | token, rest = vstring:match("^(%a+)[%.%-%_]*(.*)") | ||
110 | if not token then | ||
111 | util.printerr("Warning: version number '"..vstring.."' could not be parsed.") | ||
112 | version[i] = 0 | ||
113 | break | ||
114 | end | ||
115 | version[i] = deltas[token] or (token:byte() / 1000) | ||
116 | end | ||
117 | vstring = rest | ||
118 | end | ||
119 | setmetatable(version, version_mt) | ||
120 | version_cache[vstring] = version | ||
121 | return version | ||
122 | end | ||
123 | |||
124 | --- Utility function to compare version numbers given as strings. | ||
125 | -- @param a string: one version. | ||
126 | -- @param b string: another version. | ||
127 | -- @return boolean: True if a > b. | ||
128 | function deps.compare_versions(a, b) | ||
129 | return deps.parse_version(a) > deps.parse_version(b) | ||
130 | end | ||
131 | |||
132 | --- A more lenient check for equivalence between versions. | ||
133 | -- This returns true if the requested components of a version | ||
134 | -- match and ignore the ones that were not given. For example, | ||
135 | -- when requesting "2", then "2", "2.1", "2.3.5-9"... all match. | ||
136 | -- When requesting "2.1", then "2.1", "2.1.3" match, but "2.2" | ||
137 | -- doesn't. | ||
138 | -- @param version string or table: Version to be tested; may be | ||
139 | -- in string format or already parsed into a table. | ||
140 | -- @param requested string or table: Version requested; may be | ||
141 | -- in string format or already parsed into a table. | ||
142 | -- @return boolean: True if the tested version matches the requested | ||
143 | -- version, false otherwise. | ||
144 | local function partial_match(version, requested) | ||
145 | assert(type(version) == "string" or type(version) == "table") | ||
146 | assert(type(requested) == "string" or type(version) == "table") | ||
147 | |||
148 | if type(version) ~= "table" then version = deps.parse_version(version) end | ||
149 | if type(requested) ~= "table" then requested = deps.parse_version(requested) end | ||
150 | if not version or not requested then return false end | ||
151 | |||
152 | for i, ri in ipairs(requested) do | ||
153 | local vi = version[i] or 0 | ||
154 | if ri ~= vi then return false end | ||
155 | end | ||
156 | if requested.revision then | ||
157 | return requested.revision == version.revision | ||
158 | end | ||
159 | return true | ||
160 | end | ||
161 | |||
162 | --- Check if a version satisfies a set of constraints. | ||
163 | -- @param version table: A version in table format | ||
164 | -- @param constraints table: An array of constraints in table format. | ||
165 | -- @return boolean: True if version satisfies all constraints, | ||
166 | -- false otherwise. | ||
167 | function deps.match_constraints(version, constraints) | ||
168 | assert(type(version) == "table") | ||
169 | assert(type(constraints) == "table") | ||
170 | local ok = true | ||
171 | setmetatable(version, version_mt) | ||
172 | for _, constr in pairs(constraints) do | ||
173 | if type(constr.version) == "string" then | ||
174 | constr.version = deps.parse_version(constr.version) | ||
175 | end | ||
176 | local constr_version, constr_op = constr.version, constr.op | ||
177 | setmetatable(constr_version, version_mt) | ||
178 | if constr_op == "==" then ok = version == constr_version | ||
179 | elseif constr_op == "~=" then ok = version ~= constr_version | ||
180 | elseif constr_op == ">" then ok = version > constr_version | ||
181 | elseif constr_op == "<" then ok = version < constr_version | ||
182 | elseif constr_op == ">=" then ok = version >= constr_version | ||
183 | elseif constr_op == "<=" then ok = version <= constr_version | ||
184 | elseif constr_op == "~>" then ok = partial_match(version, constr_version) | ||
185 | end | ||
186 | if not ok then break end | ||
187 | end | ||
188 | return ok | ||
189 | end | ||
190 | |||
191 | return deps | ||
192 | |||
diff --git a/src/luarocks/core/dir.lua b/src/luarocks/core/dir.lua new file mode 100644 index 00000000..240bb38a --- /dev/null +++ b/src/luarocks/core/dir.lua | |||
@@ -0,0 +1,55 @@ | |||
1 | |||
2 | local dir = {} | ||
3 | |||
4 | local require = nil | ||
5 | -------------------------------------------------------------------------------- | ||
6 | |||
7 | dir.separator = "/" | ||
8 | |||
9 | --- Describe a path in a cross-platform way. | ||
10 | -- Use this function to avoid platform-specific directory | ||
11 | -- separators in other modules. Removes trailing slashes from | ||
12 | -- each component given, to avoid repeated separators. | ||
13 | -- Separators inside strings are kept, to handle URLs containing | ||
14 | -- protocols. | ||
15 | -- @param ... strings representing directories | ||
16 | -- @return string: a string with a platform-specific representation | ||
17 | -- of the path. | ||
18 | function dir.path(...) | ||
19 | local t = {...} | ||
20 | while t[1] == "" do | ||
21 | table.remove(t, 1) | ||
22 | end | ||
23 | return (table.concat(t, "/"):gsub("([^:])/+", "%1/"):gsub("^/+", "/"):gsub("/*$", "")) | ||
24 | end | ||
25 | |||
26 | --- Split protocol and path from an URL or local pathname. | ||
27 | -- URLs should be in the "protocol://path" format. | ||
28 | -- For local pathnames, "file" is returned as the protocol. | ||
29 | -- @param url string: an URL or a local pathname. | ||
30 | -- @return string, string: the protocol, and the pathname without the protocol. | ||
31 | function dir.split_url(url) | ||
32 | assert(type(url) == "string") | ||
33 | |||
34 | local protocol, pathname = url:match("^([^:]*)://(.*)") | ||
35 | if not protocol then | ||
36 | protocol = "file" | ||
37 | pathname = url | ||
38 | end | ||
39 | return protocol, pathname | ||
40 | end | ||
41 | |||
42 | --- Normalize a url or local path. | ||
43 | -- URLs should be in the "protocol://path" format. System independent | ||
44 | -- forward slashes are used, removing trailing and double slashes | ||
45 | -- @param url string: an URL or a local pathname. | ||
46 | -- @return string: Normalized result. | ||
47 | function dir.normalize(name) | ||
48 | local protocol, pathname = dir.split_url(name) | ||
49 | pathname = pathname:gsub("\\", "/"):gsub("(.)/*$", "%1"):gsub("//", "/") | ||
50 | if protocol ~= "file" then pathname = protocol .."://"..pathname end | ||
51 | return pathname | ||
52 | end | ||
53 | |||
54 | return dir | ||
55 | |||
diff --git a/src/luarocks/manif_core.lua b/src/luarocks/core/manif.lua index 5c8928d4..f0912bfd 100644 --- a/src/luarocks/manif_core.lua +++ b/src/luarocks/core/manif.lua | |||
@@ -1,15 +1,15 @@ | |||
1 | 1 | ||
2 | --- Core functions for querying manifest files. | 2 | --- Core functions for querying manifest files. |
3 | -- This module requires no specific 'fs' functionality. | 3 | local manif = {} |
4 | local manif_core = {} | ||
5 | package.loaded["luarocks.manif_core"] = manif_core | ||
6 | 4 | ||
7 | local persist = require("luarocks.persist") | 5 | local persist = require("luarocks.core.persist") |
8 | local type_check = require("luarocks.type_check") | 6 | local type_check = require("luarocks.core.type_check") |
9 | local cfg = require("luarocks.cfg") | 7 | local cfg = require("luarocks.core.cfg") |
10 | local dir = require("luarocks.dir") | 8 | local dir = require("luarocks.core.dir") |
11 | local util = require("luarocks.util") | 9 | local util = require("luarocks.core.util") |
12 | local path = require("luarocks.path") | 10 | local path = require("luarocks.core.path") |
11 | local require = nil | ||
12 | -------------------------------------------------------------------------------- | ||
13 | 13 | ||
14 | -- Table with repository identifiers as keys and tables mapping | 14 | -- Table with repository identifiers as keys and tables mapping |
15 | -- Lua versions to cached loaded manifests as values. | 15 | -- Lua versions to cached loaded manifests as values. |
@@ -19,7 +19,7 @@ local manifest_cache = {} | |||
19 | -- @param repo_url string: The repository identifier. | 19 | -- @param repo_url string: The repository identifier. |
20 | -- @param lua_version string: Lua version in "5.x" format, defaults to installed version. | 20 | -- @param lua_version string: Lua version in "5.x" format, defaults to installed version. |
21 | -- @param manifest table: the manifest to be cached. | 21 | -- @param manifest table: the manifest to be cached. |
22 | function manif_core.cache_manifest(repo_url, lua_version, manifest) | 22 | function manif.cache_manifest(repo_url, lua_version, manifest) |
23 | lua_version = lua_version or cfg.lua_version | 23 | lua_version = lua_version or cfg.lua_version |
24 | manifest_cache[repo_url] = manifest_cache[repo_url] or {} | 24 | manifest_cache[repo_url] = manifest_cache[repo_url] or {} |
25 | manifest_cache[repo_url][lua_version] = manifest | 25 | manifest_cache[repo_url][lua_version] = manifest |
@@ -29,7 +29,7 @@ end | |||
29 | -- @param repo_url string: The repository identifier. | 29 | -- @param repo_url string: The repository identifier. |
30 | -- @param lua_version string: Lua version in "5.x" format, defaults to installed version. | 30 | -- @param lua_version string: Lua version in "5.x" format, defaults to installed version. |
31 | -- @return table or nil: loaded manifest or nil if cache is empty. | 31 | -- @return table or nil: loaded manifest or nil if cache is empty. |
32 | function manif_core.get_cached_manifest(repo_url, lua_version) | 32 | function manif.get_cached_manifest(repo_url, lua_version) |
33 | lua_version = lua_version or cfg.lua_version | 33 | lua_version = lua_version or cfg.lua_version |
34 | return manifest_cache[repo_url] and manifest_cache[repo_url][lua_version] | 34 | return manifest_cache[repo_url] and manifest_cache[repo_url][lua_version] |
35 | end | 35 | end |
@@ -42,7 +42,7 @@ end | |||
42 | -- @param quick boolean: If given, skips type checking. | 42 | -- @param quick boolean: If given, skips type checking. |
43 | -- @return table or (nil, string, string): the manifest or nil, | 43 | -- @return table or (nil, string, string): the manifest or nil, |
44 | -- error message and error code ("open", "load", "run" or "type"). | 44 | -- error message and error code ("open", "load", "run" or "type"). |
45 | function manif_core.manifest_loader(file, repo_url, lua_version, quick) | 45 | function manif.manifest_loader(file, repo_url, lua_version, quick) |
46 | local manifest, err, errcode = persist.load_into_table(file) | 46 | local manifest, err, errcode = persist.load_into_table(file) |
47 | if not manifest then | 47 | if not manifest then |
48 | return nil, "Failed loading manifest for "..repo_url..": "..err, errcode | 48 | return nil, "Failed loading manifest for "..repo_url..": "..err, errcode |
@@ -55,7 +55,7 @@ function manif_core.manifest_loader(file, repo_url, lua_version, quick) | |||
55 | end | 55 | end |
56 | end | 56 | end |
57 | 57 | ||
58 | manif_core.cache_manifest(repo_url, lua_version, manifest) | 58 | manif.cache_manifest(repo_url, lua_version, manifest) |
59 | return manifest | 59 | return manifest |
60 | end | 60 | end |
61 | 61 | ||
@@ -65,16 +65,16 @@ end | |||
65 | -- @param repo_url string: URL or pathname for the repository. | 65 | -- @param repo_url string: URL or pathname for the repository. |
66 | -- @return table or (nil, string, string): A table representing the manifest, | 66 | -- @return table or (nil, string, string): A table representing the manifest, |
67 | -- or nil followed by an error message and an error code, see manifest_loader. | 67 | -- or nil followed by an error message and an error code, see manifest_loader. |
68 | function manif_core.load_local_manifest(repo_url) | 68 | function manif.load_local_manifest(repo_url) |
69 | assert(type(repo_url) == "string") | 69 | assert(type(repo_url) == "string") |
70 | 70 | ||
71 | local cached_manifest = manif_core.get_cached_manifest(repo_url) | 71 | local cached_manifest = manif.get_cached_manifest(repo_url) |
72 | if cached_manifest then | 72 | if cached_manifest then |
73 | return cached_manifest | 73 | return cached_manifest |
74 | end | 74 | end |
75 | 75 | ||
76 | local pathname = dir.path(repo_url, "manifest") | 76 | local pathname = dir.path(repo_url, "manifest") |
77 | return manif_core.manifest_loader(pathname, repo_url, nil, true) | 77 | return manif.manifest_loader(pathname, repo_url, nil, true) |
78 | end | 78 | end |
79 | 79 | ||
80 | --- Get all versions of a package listed in a manifest file. | 80 | --- Get all versions of a package listed in a manifest file. |
@@ -85,13 +85,13 @@ end | |||
85 | -- or "all", to use all trees. | 85 | -- or "all", to use all trees. |
86 | -- @return table: An array of strings listing installed | 86 | -- @return table: An array of strings listing installed |
87 | -- versions of a package. | 87 | -- versions of a package. |
88 | function manif_core.get_versions(name, deps_mode) | 88 | function manif.get_versions(name, deps_mode) |
89 | assert(type(name) == "string") | 89 | assert(type(name) == "string") |
90 | assert(type(deps_mode) == "string") | 90 | assert(type(deps_mode) == "string") |
91 | 91 | ||
92 | local manifest = {} | 92 | local manifest = {} |
93 | path.map_trees(deps_mode, function(tree) | 93 | path.map_trees(deps_mode, function(tree) |
94 | local loaded = manif_core.load_local_manifest(path.rocks_dir(tree)) | 94 | local loaded = manif.load_local_manifest(path.rocks_dir(tree)) |
95 | if loaded then | 95 | if loaded then |
96 | util.deep_merge(manifest, loaded) | 96 | util.deep_merge(manifest, loaded) |
97 | end | 97 | end |
@@ -104,4 +104,4 @@ function manif_core.get_versions(name, deps_mode) | |||
104 | return {} | 104 | return {} |
105 | end | 105 | end |
106 | 106 | ||
107 | return manif_core | 107 | return manif |
diff --git a/src/luarocks/core/path.lua b/src/luarocks/core/path.lua new file mode 100644 index 00000000..ffde2c68 --- /dev/null +++ b/src/luarocks/core/path.lua | |||
@@ -0,0 +1,149 @@ | |||
1 | |||
2 | --- Core LuaRocks-specific path handling functions. | ||
3 | local path = {} | ||
4 | |||
5 | local cfg = require("luarocks.core.cfg") | ||
6 | local dir = require("luarocks.core.dir") | ||
7 | local require = nil | ||
8 | -------------------------------------------------------------------------------- | ||
9 | |||
10 | function path.rocks_dir(tree) | ||
11 | if type(tree) == "string" then | ||
12 | return dir.path(tree, cfg.rocks_subdir) | ||
13 | else | ||
14 | assert(type(tree) == "table") | ||
15 | return tree.rocks_dir or dir.path(tree.root, cfg.rocks_subdir) | ||
16 | end | ||
17 | end | ||
18 | |||
19 | --- Produce a versioned version of a filename. | ||
20 | -- @param file string: filename (must start with prefix) | ||
21 | -- @param prefix string: Path prefix for file | ||
22 | -- @param name string: Rock name | ||
23 | -- @param version string: Rock version | ||
24 | -- @return string: a pathname with the same directory parts and a versioned basename. | ||
25 | function path.versioned_name(file, prefix, name, version) | ||
26 | assert(type(file) == "string") | ||
27 | assert(type(name) == "string") | ||
28 | assert(type(version) == "string") | ||
29 | |||
30 | local rest = file:sub(#prefix+1):gsub("^/*", "") | ||
31 | local name_version = (name.."_"..version):gsub("%-", "_"):gsub("%.", "_") | ||
32 | return dir.path(prefix, name_version.."-"..rest) | ||
33 | end | ||
34 | |||
35 | --- Convert a pathname to a module identifier. | ||
36 | -- In Unix, for example, a path "foo/bar/baz.lua" is converted to | ||
37 | -- "foo.bar.baz"; "bla/init.lua" returns "bla"; "foo.so" returns "foo". | ||
38 | -- @param file string: Pathname of module | ||
39 | -- @return string: The module identifier, or nil if given path is | ||
40 | -- not a conformant module path (the function does not check if the | ||
41 | -- path actually exists). | ||
42 | function path.path_to_module(file) | ||
43 | assert(type(file) == "string") | ||
44 | |||
45 | local name = file:match("(.*)%."..cfg.lua_extension.."$") | ||
46 | if name then | ||
47 | name = name:gsub(dir.separator, ".") | ||
48 | local init = name:match("(.*)%.init$") | ||
49 | if init then | ||
50 | name = init | ||
51 | end | ||
52 | else | ||
53 | name = file:match("(.*)%."..cfg.lib_extension.."$") | ||
54 | if name then | ||
55 | name = name:gsub(dir.separator, ".") | ||
56 | else | ||
57 | name = file:match("(.*)%."..cfg.static_lib_extension.."$") | ||
58 | if name then | ||
59 | name = name:gsub(dir.separator, ".") | ||
60 | end | ||
61 | end | ||
62 | end | ||
63 | if not name then name = file end | ||
64 | name = name:gsub("^%.+", ""):gsub("%.+$", "") | ||
65 | return name | ||
66 | end | ||
67 | |||
68 | function path.deploy_lua_dir(tree) | ||
69 | if type(tree) == "string" then | ||
70 | return dir.path(tree, cfg.lua_modules_path) | ||
71 | else | ||
72 | assert(type(tree) == "table") | ||
73 | return tree.lua_dir or dir.path(tree.root, cfg.lua_modules_path) | ||
74 | end | ||
75 | end | ||
76 | |||
77 | function path.deploy_lib_dir(tree) | ||
78 | if type(tree) == "string" then | ||
79 | return dir.path(tree, cfg.lib_modules_path) | ||
80 | else | ||
81 | assert(type(tree) == "table") | ||
82 | return tree.lib_dir or dir.path(tree.root, cfg.lib_modules_path) | ||
83 | end | ||
84 | end | ||
85 | |||
86 | local is_src_extension = { [".lua"] = true, [".tl"] = true, [".tld"] = true, [".moon"] = true } | ||
87 | |||
88 | --- Return the pathname of the file that would be loaded for a module, indexed. | ||
89 | -- @param file_name string: module file name as in manifest (eg. "socket/core.so") | ||
90 | -- @param name string: name of the package (eg. "luasocket") | ||
91 | -- @param version string: version number (eg. "2.0.2-1") | ||
92 | -- @param tree string: repository path (eg. "/usr/local") | ||
93 | -- @param i number: the index, 1 if version is the current default, > 1 otherwise. | ||
94 | -- This is done this way for use by select_module in luarocks.loader. | ||
95 | -- @return string: filename of the module (eg. "/usr/local/lib/lua/5.1/socket/core.so") | ||
96 | function path.which_i(file_name, name, version, tree, i) | ||
97 | local deploy_dir | ||
98 | local extension = file_name:match("%.[a-z]+$") | ||
99 | if is_src_extension[extension] then | ||
100 | deploy_dir = path.deploy_lua_dir(tree) | ||
101 | file_name = dir.path(deploy_dir, file_name) | ||
102 | else | ||
103 | deploy_dir = path.deploy_lib_dir(tree) | ||
104 | file_name = dir.path(deploy_dir, file_name) | ||
105 | end | ||
106 | if i > 1 then | ||
107 | file_name = path.versioned_name(file_name, deploy_dir, name, version) | ||
108 | end | ||
109 | return file_name | ||
110 | end | ||
111 | |||
112 | function path.rocks_tree_to_string(tree) | ||
113 | if type(tree) == "string" then | ||
114 | return tree | ||
115 | else | ||
116 | assert(type(tree) == "table") | ||
117 | return tree.root | ||
118 | end | ||
119 | end | ||
120 | |||
121 | --- Apply a given function to the active rocks trees based on chosen dependency mode. | ||
122 | -- @param deps_mode string: Dependency mode: "one" for the current default tree, | ||
123 | -- "all" for all trees, "order" for all trees with priority >= the current default, | ||
124 | -- "none" for no trees (this function becomes a nop). | ||
125 | -- @param fn function: function to be applied, with the tree dir (string) as the first | ||
126 | -- argument and the remaining varargs of map_trees as the following arguments. | ||
127 | -- @return a table with all results of invocations of fn collected. | ||
128 | function path.map_trees(deps_mode, fn, ...) | ||
129 | local result = {} | ||
130 | if deps_mode == "one" then | ||
131 | table.insert(result, (fn(cfg.root_dir, ...)) or 0) | ||
132 | elseif deps_mode == "all" or deps_mode == "order" then | ||
133 | local use = false | ||
134 | if deps_mode == "all" then | ||
135 | use = true | ||
136 | end | ||
137 | for _, tree in ipairs(cfg.rocks_trees) do | ||
138 | if dir.normalize(path.rocks_tree_to_string(tree)) == dir.normalize(path.rocks_tree_to_string(cfg.root_dir)) then | ||
139 | use = true | ||
140 | end | ||
141 | if use then | ||
142 | table.insert(result, (fn(tree, ...)) or 0) | ||
143 | end | ||
144 | end | ||
145 | end | ||
146 | return result | ||
147 | end | ||
148 | |||
149 | return path | ||
diff --git a/src/luarocks/core/persist.lua b/src/luarocks/core/persist.lua new file mode 100644 index 00000000..48979184 --- /dev/null +++ b/src/luarocks/core/persist.lua | |||
@@ -0,0 +1,81 @@ | |||
1 | |||
2 | local persist = {} | ||
3 | |||
4 | local require = nil | ||
5 | -------------------------------------------------------------------------------- | ||
6 | |||
7 | --- Load and run a Lua file in an environment. | ||
8 | -- @param filename string: the name of the file. | ||
9 | -- @param env table: the environment table. | ||
10 | -- @return (true, any) or (nil, string, string): true and the return value | ||
11 | -- of the file, or nil, an error message and an error code ("open", "load" | ||
12 | -- or "run") in case of errors. | ||
13 | local function run_file(filename, env) | ||
14 | local fd, err = io.open(filename) | ||
15 | if not fd then | ||
16 | return nil, err, "open" | ||
17 | end | ||
18 | local str, err = fd:read("*a") | ||
19 | fd:close() | ||
20 | if not str then | ||
21 | return nil, err, "open" | ||
22 | end | ||
23 | str = str:gsub("^#![^\n]*\n", "") | ||
24 | local chunk, ran | ||
25 | if _VERSION == "Lua 5.1" then -- Lua 5.1 | ||
26 | chunk, err = loadstring(str, filename) | ||
27 | if chunk then | ||
28 | setfenv(chunk, env) | ||
29 | ran, err = pcall(chunk) | ||
30 | end | ||
31 | else -- Lua 5.2 | ||
32 | chunk, err = load(str, filename, "t", env) | ||
33 | if chunk then | ||
34 | ran, err = pcall(chunk) | ||
35 | end | ||
36 | end | ||
37 | if not chunk then | ||
38 | return nil, "Error loading file: "..err, "load" | ||
39 | end | ||
40 | if not ran then | ||
41 | return nil, "Error running file: "..err, "run" | ||
42 | end | ||
43 | return true, err | ||
44 | end | ||
45 | |||
46 | --- Load a Lua file containing assignments, storing them in a table. | ||
47 | -- The global environment is not propagated to the loaded file. | ||
48 | -- @param filename string: the name of the file. | ||
49 | -- @param tbl table or nil: if given, this table is used to store | ||
50 | -- loaded values. | ||
51 | -- @return (table, table) or (nil, string, string): a table with the file's assignments | ||
52 | -- as fields and set of undefined globals accessed in file, | ||
53 | -- or nil, an error message and an error code ("open"; couldn't open the file, | ||
54 | -- "load"; compile-time error, or "run"; run-time error) | ||
55 | -- in case of errors. | ||
56 | function persist.load_into_table(filename, tbl) | ||
57 | assert(type(filename) == "string") | ||
58 | assert(type(tbl) == "table" or not tbl) | ||
59 | |||
60 | local result = tbl or {} | ||
61 | local globals = {} | ||
62 | local globals_mt = { | ||
63 | __index = function(t, k) | ||
64 | globals[k] = true | ||
65 | end | ||
66 | } | ||
67 | local save_mt = getmetatable(result) | ||
68 | setmetatable(result, globals_mt) | ||
69 | |||
70 | local ok, err, errcode = run_file(filename, result) | ||
71 | |||
72 | setmetatable(result, save_mt) | ||
73 | |||
74 | if not ok then | ||
75 | return nil, err, errcode | ||
76 | end | ||
77 | return result, globals | ||
78 | end | ||
79 | |||
80 | return persist | ||
81 | |||
diff --git a/src/luarocks/core/type_check.lua b/src/luarocks/core/type_check.lua new file mode 100644 index 00000000..1822312b --- /dev/null +++ b/src/luarocks/core/type_check.lua | |||
@@ -0,0 +1,229 @@ | |||
1 | |||
2 | local type_check = {} | ||
3 | |||
4 | local cfg = require("luarocks.core.cfg") | ||
5 | local deps = require("luarocks.core.deps") | ||
6 | local require = nil | ||
7 | -------------------------------------------------------------------------------- | ||
8 | |||
9 | type_check.string_1 = { _type = "string" } | ||
10 | type_check.number_1 = { _type = "number" } | ||
11 | type_check.mandatory_string_1 = { _type = "string", _mandatory = true } | ||
12 | |||
13 | local number_1 = type_check.number_1 | ||
14 | local string_1 = type_check.string_1 | ||
15 | local mandatory_string_1 = type_check.mandatory_string_1 | ||
16 | |||
17 | local manifest_types = { | ||
18 | repository = { | ||
19 | _mandatory = true, | ||
20 | -- packages | ||
21 | _any = { | ||
22 | -- versions | ||
23 | _any = { | ||
24 | -- items | ||
25 | _any = { | ||
26 | arch = mandatory_string_1, | ||
27 | modules = { _any = string_1 }, | ||
28 | commands = { _any = string_1 }, | ||
29 | dependencies = { _any = string_1 }, | ||
30 | -- TODO: to be extended with more metadata. | ||
31 | } | ||
32 | } | ||
33 | } | ||
34 | }, | ||
35 | modules = { | ||
36 | _mandatory = true, | ||
37 | -- modules | ||
38 | _any = { | ||
39 | -- providers | ||
40 | _any = string_1 | ||
41 | } | ||
42 | }, | ||
43 | commands = { | ||
44 | _mandatory = true, | ||
45 | -- modules | ||
46 | _any = { | ||
47 | -- commands | ||
48 | _any = string_1 | ||
49 | } | ||
50 | }, | ||
51 | dependencies = { | ||
52 | -- each module | ||
53 | _any = { | ||
54 | -- each version | ||
55 | _any = { | ||
56 | -- each dependency | ||
57 | _any = { | ||
58 | name = string_1, | ||
59 | constraints = { | ||
60 | _any = { | ||
61 | no_upgrade = { _type = "boolean" }, | ||
62 | op = string_1, | ||
63 | version = { | ||
64 | string = string_1, | ||
65 | _any = number_1, | ||
66 | } | ||
67 | } | ||
68 | } | ||
69 | } | ||
70 | } | ||
71 | } | ||
72 | } | ||
73 | } | ||
74 | |||
75 | local function check_version(version, typetbl, context) | ||
76 | local typetbl_version = typetbl._version or "1.0" | ||
77 | if deps.compare_versions(typetbl_version, version) then | ||
78 | if context == "" then | ||
79 | return nil, "Invalid rockspec_format version number in rockspec? Please fix rockspec accordingly." | ||
80 | else | ||
81 | return nil, context.." is not supported in rockspec format "..version.." (requires version "..typetbl_version.."), please fix the rockspec_format field accordingly." | ||
82 | end | ||
83 | end | ||
84 | return true | ||
85 | end | ||
86 | |||
87 | --- Type check an object. | ||
88 | -- The object is compared against an archetypical value | ||
89 | -- matching the expected type -- the actual values don't matter, | ||
90 | -- only their types. Tables are type checked recursively. | ||
91 | -- @param version string: The version of the item. | ||
92 | -- @param item any: The object being checked. | ||
93 | -- @param typetbl any: The type-checking table for the object. | ||
94 | -- @param context string: A string indicating the "context" where the | ||
95 | -- error occurred (the full table path), for error messages. | ||
96 | -- @return boolean or (nil, string): true if type checking | ||
97 | -- succeeded, or nil and an error message if it failed. | ||
98 | -- @see type_check_table | ||
99 | local function type_check_item(version, item, typetbl, context) | ||
100 | assert(type(version) == "string") | ||
101 | |||
102 | local ok, err = check_version(version, typetbl, context) | ||
103 | if not ok then | ||
104 | return nil, err | ||
105 | end | ||
106 | |||
107 | local item_type = type(item) or "nil" | ||
108 | local expected_type = typetbl._type or "table" | ||
109 | |||
110 | if expected_type == "number" then | ||
111 | if not tonumber(item) then | ||
112 | return nil, "Type mismatch on field "..context..": expected a number" | ||
113 | end | ||
114 | elseif expected_type == "string" then | ||
115 | if item_type ~= "string" then | ||
116 | return nil, "Type mismatch on field "..context..": expected a string, got "..item_type | ||
117 | end | ||
118 | if typetbl._pattern then | ||
119 | if not item:match("^"..typetbl._pattern.."$") then | ||
120 | return nil, "Type mismatch on field "..context..": invalid value "..item.." does not match '"..typetbl._pattern.."'" | ||
121 | end | ||
122 | end | ||
123 | elseif expected_type == "table" then | ||
124 | if item_type ~= expected_type then | ||
125 | return nil, "Type mismatch on field "..context..": expected a table" | ||
126 | else | ||
127 | return type_check.type_check_table(version, item, typetbl, context) | ||
128 | end | ||
129 | elseif item_type ~= expected_type then | ||
130 | return nil, "Type mismatch on field "..context..": expected "..expected_type | ||
131 | end | ||
132 | return true | ||
133 | end | ||
134 | |||
135 | local function mkfield(context, field) | ||
136 | if context == "" then | ||
137 | return tostring(field) | ||
138 | elseif type(field) == "string" then | ||
139 | return context.."."..field | ||
140 | else | ||
141 | return context.."["..tostring(field).."]" | ||
142 | end | ||
143 | end | ||
144 | |||
145 | --- Type check the contents of a table. | ||
146 | -- The table's contents are compared against a reference table, | ||
147 | -- which contains the recognized fields, with archetypical values | ||
148 | -- matching the expected types -- the actual values of items in the | ||
149 | -- reference table don't matter, only their types (ie, for field x | ||
150 | -- in tbl that is correctly typed, type(tbl.x) == type(types.x)). | ||
151 | -- If the reference table contains a field called MORE, then | ||
152 | -- unknown fields in the checked table are accepted. | ||
153 | -- If it contains a field called ANY, then its type will be | ||
154 | -- used to check any unknown fields. If a field is prefixed | ||
155 | -- with MUST_, it is mandatory; its absence from the table is | ||
156 | -- a type error. | ||
157 | -- Tables are type checked recursively. | ||
158 | -- @param version string: The version of tbl. | ||
159 | -- @param tbl table: The table to be type checked. | ||
160 | -- @param typetbl table: The type-checking table, containing | ||
161 | -- values for recognized fields in the checked table. | ||
162 | -- @param context string: A string indicating the "context" where the | ||
163 | -- error occurred (such as the name of the table the item is a part of), | ||
164 | -- to be used by error messages. | ||
165 | -- @return boolean or (nil, string): true if type checking | ||
166 | -- succeeded, or nil and an error message if it failed. | ||
167 | function type_check.type_check_table(version, tbl, typetbl, context) | ||
168 | assert(type(version) == "string") | ||
169 | assert(type(tbl) == "table") | ||
170 | assert(type(typetbl) == "table") | ||
171 | |||
172 | local ok, err = check_version(version, typetbl, context) | ||
173 | if not ok then | ||
174 | return nil, err | ||
175 | end | ||
176 | |||
177 | for k, v in pairs(tbl) do | ||
178 | local t = typetbl[k] or typetbl._any | ||
179 | if t then | ||
180 | local ok, err = type_check_item(version, v, t, mkfield(context, k)) | ||
181 | if not ok then return nil, err end | ||
182 | elseif typetbl._more then | ||
183 | -- Accept unknown field | ||
184 | else | ||
185 | if not cfg.accept_unknown_fields then | ||
186 | return nil, "Unknown field "..k | ||
187 | end | ||
188 | end | ||
189 | end | ||
190 | for k, v in pairs(typetbl) do | ||
191 | if k:sub(1,1) ~= "_" and v._mandatory then | ||
192 | if not tbl[k] then | ||
193 | return nil, "Mandatory field "..mkfield(context, k).." is missing." | ||
194 | end | ||
195 | end | ||
196 | end | ||
197 | return true | ||
198 | end | ||
199 | |||
200 | function type_check.check_undeclared_globals(globals, typetbl) | ||
201 | local undeclared = {} | ||
202 | for glob, _ in pairs(globals) do | ||
203 | if not (typetbl[glob] or typetbl["MUST_"..glob]) then | ||
204 | table.insert(undeclared, glob) | ||
205 | end | ||
206 | end | ||
207 | if #undeclared == 1 then | ||
208 | return nil, "Unknown variable: "..undeclared[1] | ||
209 | elseif #undeclared > 1 then | ||
210 | return nil, "Unknown variables: "..table.concat(undeclared, ", ") | ||
211 | end | ||
212 | return true | ||
213 | end | ||
214 | |||
215 | --- Type check a manifest table. | ||
216 | -- Verify the correctness of elements from a | ||
217 | -- manifest table, reporting on unknown fields and type | ||
218 | -- mismatches. | ||
219 | -- @return boolean or (nil, string): true if type checking | ||
220 | -- succeeded, or nil and an error message if it failed. | ||
221 | function type_check.type_check_manifest(manifest, globals) | ||
222 | assert(type(manifest) == "table") | ||
223 | local ok, err = type_check.check_undeclared_globals(globals, manifest_types) | ||
224 | if not ok then return nil, err end | ||
225 | return type_check.type_check_table("1.0", manifest, manifest_types, "") | ||
226 | end | ||
227 | |||
228 | return type_check | ||
229 | |||
diff --git a/src/luarocks/core/util.lua b/src/luarocks/core/util.lua new file mode 100644 index 00000000..85b59af6 --- /dev/null +++ b/src/luarocks/core/util.lua | |||
@@ -0,0 +1,272 @@ | |||
1 | |||
2 | local util = {} | ||
3 | |||
4 | local require = nil | ||
5 | -------------------------------------------------------------------------------- | ||
6 | |||
7 | --- Run a process and read a its output. | ||
8 | -- Equivalent to io.popen(cmd):read("*l"), except that it | ||
9 | -- closes the fd right away. | ||
10 | -- @param cmd string: The command to execute | ||
11 | -- @param spec string: "*l" by default, to read a single line. | ||
12 | -- May be used to read more, passing, for instance, "*a". | ||
13 | -- @return string: the output of the program. | ||
14 | function util.popen_read(cmd, spec) | ||
15 | local fd = io.popen(cmd) | ||
16 | local out = fd:read(spec or "*l") | ||
17 | fd:close() | ||
18 | return out | ||
19 | end | ||
20 | |||
21 | --- Create a new shallow copy of a table: a new table with | ||
22 | -- the same keys and values. Keys point to the same objects as | ||
23 | -- the original table (ie, does not copy recursively). | ||
24 | -- @param tbl table: the input table | ||
25 | -- @return table: a new table with the same contents. | ||
26 | function util.make_shallow_copy(tbl) | ||
27 | local copy = {} | ||
28 | for k,v in pairs(tbl) do | ||
29 | copy[k] = v | ||
30 | end | ||
31 | return copy | ||
32 | end | ||
33 | |||
34 | --- | ||
35 | -- Formats tables with cycles recursively to any depth. | ||
36 | -- References to other tables are shown as values. | ||
37 | -- Self references are indicated. | ||
38 | -- The string returned is "Lua code", which can be procesed | ||
39 | -- (in the case in which indent is composed by spaces or "--"). | ||
40 | -- Userdata and function keys and values are shown as strings, | ||
41 | -- which logically are exactly not equivalent to the original code. | ||
42 | -- This routine can serve for pretty formating tables with | ||
43 | -- proper indentations, apart from printing them: | ||
44 | -- io.write(table.show(t, "t")) -- a typical use | ||
45 | -- Written by Julio Manuel Fernandez-Diaz, | ||
46 | -- Heavily based on "Saving tables with cycles", PIL2, p. 113. | ||
47 | -- @param t table: is the table. | ||
48 | -- @param name string: is the name of the table (optional) | ||
49 | -- @param indent string: is a first indentation (optional). | ||
50 | -- @return string: the pretty-printed table | ||
51 | function util.show_table(t, name, indent) | ||
52 | local cart -- a container | ||
53 | local autoref -- for self references | ||
54 | |||
55 | local function is_empty_table(t) return next(t) == nil end | ||
56 | |||
57 | local function basic_serialize (o) | ||
58 | local so = tostring(o) | ||
59 | if type(o) == "function" then | ||
60 | local info = debug.getinfo(o, "S") | ||
61 | -- info.name is nil because o is not a calling level | ||
62 | if info.what == "C" then | ||
63 | return ("%q"):format(so .. ", C function") | ||
64 | else | ||
65 | -- the information is defined through lines | ||
66 | return ("%q"):format(so .. ", defined in (" .. info.linedefined .. "-" .. info.lastlinedefined .. ")" .. info.source) | ||
67 | end | ||
68 | elseif type(o) == "number" then | ||
69 | return so | ||
70 | else | ||
71 | return ("%q"):format(so) | ||
72 | end | ||
73 | end | ||
74 | |||
75 | local function add_to_cart (value, name, indent, saved, field) | ||
76 | indent = indent or "" | ||
77 | saved = saved or {} | ||
78 | field = field or name | ||
79 | |||
80 | cart = cart .. indent .. field | ||
81 | |||
82 | if type(value) ~= "table" then | ||
83 | cart = cart .. " = " .. basic_serialize(value) .. ";\n" | ||
84 | else | ||
85 | if saved[value] then | ||
86 | cart = cart .. " = {}; -- " .. saved[value] .. " (self reference)\n" | ||
87 | autoref = autoref .. name .. " = " .. saved[value] .. ";\n" | ||
88 | else | ||
89 | saved[value] = name | ||
90 | --if tablecount(value) == 0 then | ||
91 | if is_empty_table(value) then | ||
92 | cart = cart .. " = {};\n" | ||
93 | else | ||
94 | cart = cart .. " = {\n" | ||
95 | for k, v in pairs(value) do | ||
96 | k = basic_serialize(k) | ||
97 | local fname = ("%s[%s]"):format(name, k) | ||
98 | field = ("[%s]"):format(k) | ||
99 | -- three spaces between levels | ||
100 | add_to_cart(v, fname, indent .. " ", saved, field) | ||
101 | end | ||
102 | cart = cart .. indent .. "};\n" | ||
103 | end | ||
104 | end | ||
105 | end | ||
106 | end | ||
107 | |||
108 | name = name or "__unnamed__" | ||
109 | if type(t) ~= "table" then | ||
110 | return name .. " = " .. basic_serialize(t) | ||
111 | end | ||
112 | cart, autoref = "", "" | ||
113 | add_to_cart(t, name, indent) | ||
114 | return cart .. autoref | ||
115 | end | ||
116 | |||
117 | --- Merges contents of src on top of dst's contents. | ||
118 | -- @param dst Destination table, which will receive src's contents. | ||
119 | -- @param src Table which provides new contents to dst. | ||
120 | -- @see platform_overrides | ||
121 | function util.deep_merge(dst, src) | ||
122 | for k, v in pairs(src) do | ||
123 | if type(v) == "table" then | ||
124 | if not dst[k] then | ||
125 | dst[k] = {} | ||
126 | end | ||
127 | if type(dst[k]) == "table" then | ||
128 | util.deep_merge(dst[k], v) | ||
129 | else | ||
130 | dst[k] = v | ||
131 | end | ||
132 | else | ||
133 | dst[k] = v | ||
134 | end | ||
135 | end | ||
136 | end | ||
137 | |||
138 | --- Remove repeated entries from a path-style string. | ||
139 | -- Example: given ("a;b;c;a;b;d", ";"), returns "a;b;c;d". | ||
140 | -- @param list string: A path string (from $PATH or package.path) | ||
141 | -- @param sep string: The separator | ||
142 | function util.remove_path_dupes(list, sep) | ||
143 | assert(type(list) == "string") | ||
144 | assert(type(sep) == "string") | ||
145 | local parts = util.split_string(list, sep) | ||
146 | local final, entries = {}, {} | ||
147 | for _, part in ipairs(parts) do | ||
148 | part = part:gsub("//", "/") | ||
149 | if not entries[part] then | ||
150 | table.insert(final, part) | ||
151 | entries[part] = true | ||
152 | end | ||
153 | end | ||
154 | return table.concat(final, sep) | ||
155 | end | ||
156 | |||
157 | -- from http://lua-users.org/wiki/SplitJoin | ||
158 | -- by Philippe Lhoste | ||
159 | function util.split_string(str, delim, maxNb) | ||
160 | -- Eliminate bad cases... | ||
161 | if string.find(str, delim) == nil then | ||
162 | return { str } | ||
163 | end | ||
164 | if maxNb == nil or maxNb < 1 then | ||
165 | maxNb = 0 -- No limit | ||
166 | end | ||
167 | local result = {} | ||
168 | local pat = "(.-)" .. delim .. "()" | ||
169 | local nb = 0 | ||
170 | local lastPos | ||
171 | for part, pos in string.gmatch(str, pat) do | ||
172 | nb = nb + 1 | ||
173 | result[nb] = part | ||
174 | lastPos = pos | ||
175 | if nb == maxNb then break end | ||
176 | end | ||
177 | -- Handle the last field | ||
178 | if nb ~= maxNb then | ||
179 | result[nb + 1] = string.sub(str, lastPos) | ||
180 | end | ||
181 | return result | ||
182 | end | ||
183 | |||
184 | --- Return an array of keys of a table. | ||
185 | -- @param tbl table: The input table. | ||
186 | -- @return table: The array of keys. | ||
187 | function util.keys(tbl) | ||
188 | local ks = {} | ||
189 | for k,_ in pairs(tbl) do | ||
190 | table.insert(ks, k) | ||
191 | end | ||
192 | return ks | ||
193 | end | ||
194 | |||
195 | --- Print a line to standard error | ||
196 | function util.printerr(...) | ||
197 | io.stderr:write(table.concat({...},"\t")) | ||
198 | io.stderr:write("\n") | ||
199 | end | ||
200 | |||
201 | --- Simple sort function used as a default for util.sortedpairs. | ||
202 | local function default_sort(a, b) | ||
203 | local ta = type(a) | ||
204 | local tb = type(b) | ||
205 | if ta == "number" and tb == "number" then | ||
206 | return a < b | ||
207 | elseif ta == "number" then | ||
208 | return true | ||
209 | elseif tb == "number" then | ||
210 | return false | ||
211 | else | ||
212 | return tostring(a) < tostring(b) | ||
213 | end | ||
214 | end | ||
215 | |||
216 | --- A table iterator generator that returns elements sorted by key, | ||
217 | -- to be used in "for" loops. | ||
218 | -- @param tbl table: The table to be iterated. | ||
219 | -- @param sort_function function or table or nil: An optional comparison function | ||
220 | -- to be used by table.sort when sorting keys, or an array listing an explicit order | ||
221 | -- for keys. If a value itself is an array, it is taken so that the first element | ||
222 | -- is a string representing the field name, and the second element is a priority table | ||
223 | -- for that key, which is returned by the iterator as the third value after the key | ||
224 | -- and the value. | ||
225 | -- @return function: the iterator function. | ||
226 | function util.sortedpairs(tbl, sort_function) | ||
227 | sort_function = sort_function or default_sort | ||
228 | local keys = util.keys(tbl) | ||
229 | local sub_orders = {} | ||
230 | |||
231 | if type(sort_function) == "function" then | ||
232 | table.sort(keys, sort_function) | ||
233 | else | ||
234 | local order = sort_function | ||
235 | local ordered_keys = {} | ||
236 | local all_keys = keys | ||
237 | keys = {} | ||
238 | |||
239 | for _, order_entry in ipairs(order) do | ||
240 | local key, sub_order | ||
241 | if type(order_entry) == "table" then | ||
242 | key = order_entry[1] | ||
243 | sub_order = order_entry[2] | ||
244 | else | ||
245 | key = order_entry | ||
246 | end | ||
247 | |||
248 | if tbl[key] then | ||
249 | ordered_keys[key] = true | ||
250 | sub_orders[key] = sub_order | ||
251 | table.insert(keys, key) | ||
252 | end | ||
253 | end | ||
254 | |||
255 | table.sort(all_keys, default_sort) | ||
256 | for _, key in ipairs(all_keys) do | ||
257 | if not ordered_keys[key] then | ||
258 | table.insert(keys, key) | ||
259 | end | ||
260 | end | ||
261 | end | ||
262 | |||
263 | local i = 1 | ||
264 | return function() | ||
265 | local key = keys[i] | ||
266 | i = i + 1 | ||
267 | return key, tbl[key], sub_orders[key] | ||
268 | end | ||
269 | end | ||
270 | |||
271 | return util | ||
272 | |||
diff --git a/src/luarocks/deps.lua b/src/luarocks/deps.lua index f8deff3c..acbf1dd6 100644 --- a/src/luarocks/deps.lua +++ b/src/luarocks/deps.lua | |||
@@ -12,14 +12,23 @@ | |||
12 | -- test/test_deps.lua file included with LuaRocks provides some | 12 | -- test/test_deps.lua file included with LuaRocks provides some |
13 | -- insights on what these criteria are. | 13 | -- insights on what these criteria are. |
14 | local deps = {} | 14 | local deps = {} |
15 | package.loaded["luarocks.deps"] = deps | 15 | setmetatable(deps, { __index = require("luarocks.core.deps") }) |
16 | 16 | ||
17 | local cfg = require("luarocks.cfg") | 17 | local cfg = require("luarocks.core.cfg") |
18 | local manif_core = require("luarocks.manif_core") | 18 | local manif = require("luarocks.core.manif") |
19 | local path = require("luarocks.path") | 19 | local path = require("luarocks.path") |
20 | local dir = require("luarocks.dir") | 20 | local dir = require("luarocks.dir") |
21 | local util = require("luarocks.util") | 21 | local util = require("luarocks.util") |
22 | 22 | ||
23 | --- Check if rockspec format version satisfies version requirement. | ||
24 | -- @param rockspec table: The rockspec table. | ||
25 | -- @param version string: required version. | ||
26 | -- @return boolean: true if rockspec format matches version or is newer, false otherwise. | ||
27 | function deps.format_is_at_least(rockspec, version) | ||
28 | local rockspec_format = rockspec.rockspec_format or "1.0" | ||
29 | return deps.parse_version(rockspec_format) >= deps.parse_version(version) | ||
30 | end | ||
31 | |||
23 | local operators = { | 32 | local operators = { |
24 | ["=="] = "==", | 33 | ["=="] = "==", |
25 | ["~="] = "~=", | 34 | ["~="] = "~=", |
@@ -34,130 +43,6 @@ local operators = { | |||
34 | ["!="] = "~=" | 43 | ["!="] = "~=" |
35 | } | 44 | } |
36 | 45 | ||
37 | local deltas = { | ||
38 | scm = 1100, | ||
39 | cvs = 1000, | ||
40 | rc = -1000, | ||
41 | pre = -10000, | ||
42 | beta = -100000, | ||
43 | alpha = -1000000 | ||
44 | } | ||
45 | |||
46 | local version_mt = { | ||
47 | --- Equality comparison for versions. | ||
48 | -- All version numbers must be equal. | ||
49 | -- If both versions have revision numbers, they must be equal; | ||
50 | -- otherwise the revision number is ignored. | ||
51 | -- @param v1 table: version table to compare. | ||
52 | -- @param v2 table: version table to compare. | ||
53 | -- @return boolean: true if they are considered equivalent. | ||
54 | __eq = function(v1, v2) | ||
55 | if #v1 ~= #v2 then | ||
56 | return false | ||
57 | end | ||
58 | for i = 1, #v1 do | ||
59 | if v1[i] ~= v2[i] then | ||
60 | return false | ||
61 | end | ||
62 | end | ||
63 | if v1.revision and v2.revision then | ||
64 | return (v1.revision == v2.revision) | ||
65 | end | ||
66 | return true | ||
67 | end, | ||
68 | --- Size comparison for versions. | ||
69 | -- All version numbers are compared. | ||
70 | -- If both versions have revision numbers, they are compared; | ||
71 | -- otherwise the revision number is ignored. | ||
72 | -- @param v1 table: version table to compare. | ||
73 | -- @param v2 table: version table to compare. | ||
74 | -- @return boolean: true if v1 is considered lower than v2. | ||
75 | __lt = function(v1, v2) | ||
76 | for i = 1, math.max(#v1, #v2) do | ||
77 | local v1i, v2i = v1[i] or 0, v2[i] or 0 | ||
78 | if v1i ~= v2i then | ||
79 | return (v1i < v2i) | ||
80 | end | ||
81 | end | ||
82 | if v1.revision and v2.revision then | ||
83 | return (v1.revision < v2.revision) | ||
84 | end | ||
85 | return false | ||
86 | end | ||
87 | } | ||
88 | |||
89 | local version_cache = {} | ||
90 | setmetatable(version_cache, { | ||
91 | __mode = "kv" | ||
92 | }) | ||
93 | |||
94 | --- Parse a version string, converting to table format. | ||
95 | -- A version table contains all components of the version string | ||
96 | -- converted to numeric format, stored in the array part of the table. | ||
97 | -- If the version contains a revision, it is stored numerically | ||
98 | -- in the 'revision' field. The original string representation of | ||
99 | -- the string is preserved in the 'string' field. | ||
100 | -- Returned version tables use a metatable | ||
101 | -- allowing later comparison through relational operators. | ||
102 | -- @param vstring string: A version number in string format. | ||
103 | -- @return table or nil: A version table or nil | ||
104 | -- if the input string contains invalid characters. | ||
105 | function deps.parse_version(vstring) | ||
106 | if not vstring then return nil end | ||
107 | assert(type(vstring) == "string") | ||
108 | |||
109 | local cached = version_cache[vstring] | ||
110 | if cached then | ||
111 | return cached | ||
112 | end | ||
113 | |||
114 | local version = {} | ||
115 | local i = 1 | ||
116 | |||
117 | local function add_token(number) | ||
118 | version[i] = version[i] and version[i] + number/100000 or number | ||
119 | i = i + 1 | ||
120 | end | ||
121 | |||
122 | -- trim leading and trailing spaces | ||
123 | vstring = vstring:match("^%s*(.*)%s*$") | ||
124 | version.string = vstring | ||
125 | -- store revision separately if any | ||
126 | local main, revision = vstring:match("(.*)%-(%d+)$") | ||
127 | if revision then | ||
128 | vstring = main | ||
129 | version.revision = tonumber(revision) | ||
130 | end | ||
131 | while #vstring > 0 do | ||
132 | -- extract a number | ||
133 | local token, rest = vstring:match("^(%d+)[%.%-%_]*(.*)") | ||
134 | if token then | ||
135 | add_token(tonumber(token)) | ||
136 | else | ||
137 | -- extract a word | ||
138 | token, rest = vstring:match("^(%a+)[%.%-%_]*(.*)") | ||
139 | if not token then | ||
140 | util.printerr("Warning: version number '"..vstring.."' could not be parsed.") | ||
141 | version[i] = 0 | ||
142 | break | ||
143 | end | ||
144 | version[i] = deltas[token] or (token:byte() / 1000) | ||
145 | end | ||
146 | vstring = rest | ||
147 | end | ||
148 | setmetatable(version, version_mt) | ||
149 | version_cache[vstring] = version | ||
150 | return version | ||
151 | end | ||
152 | |||
153 | --- Utility function to compare version numbers given as strings. | ||
154 | -- @param a string: one version. | ||
155 | -- @param b string: another version. | ||
156 | -- @return boolean: True if a > b. | ||
157 | function deps.compare_versions(a, b) | ||
158 | return deps.parse_version(a) > deps.parse_version(b) | ||
159 | end | ||
160 | |||
161 | --- Consumes a constraint from a string, converting it to table format. | 46 | --- Consumes a constraint from a string, converting it to table format. |
162 | -- For example, a string ">= 1.0, > 2.0" is converted to a table in the | 47 | -- For example, a string ">= 1.0, > 2.0" is converted to a table in the |
163 | -- format {op = ">=", version={1,0}} and the rest, "> 2.0", is returned | 48 | -- format {op = ">=", version={1,0}} and the rest, "> 2.0", is returned |
@@ -192,7 +77,7 @@ end | |||
192 | function deps.parse_constraints(input) | 77 | function deps.parse_constraints(input) |
193 | assert(type(input) == "string") | 78 | assert(type(input) == "string") |
194 | 79 | ||
195 | local constraints, constraint, oinput = {}, nil, input | 80 | local constraints, oinput, constraint = {}, input |
196 | while #input > 0 do | 81 | while #input > 0 do |
197 | constraint, input = parse_constraint(input) | 82 | constraint, input = parse_constraint(input) |
198 | if constraint then | 83 | if constraint then |
@@ -258,80 +143,25 @@ function deps.show_dep(dep, internal) | |||
258 | end | 143 | end |
259 | end | 144 | end |
260 | 145 | ||
261 | --- A more lenient check for equivalence between versions. | ||
262 | -- This returns true if the requested components of a version | ||
263 | -- match and ignore the ones that were not given. For example, | ||
264 | -- when requesting "2", then "2", "2.1", "2.3.5-9"... all match. | ||
265 | -- When requesting "2.1", then "2.1", "2.1.3" match, but "2.2" | ||
266 | -- doesn't. | ||
267 | -- @param version string or table: Version to be tested; may be | ||
268 | -- in string format or already parsed into a table. | ||
269 | -- @param requested string or table: Version requested; may be | ||
270 | -- in string format or already parsed into a table. | ||
271 | -- @return boolean: True if the tested version matches the requested | ||
272 | -- version, false otherwise. | ||
273 | local function partial_match(version, requested) | ||
274 | assert(type(version) == "string" or type(version) == "table") | ||
275 | assert(type(requested) == "string" or type(version) == "table") | ||
276 | |||
277 | if type(version) ~= "table" then version = deps.parse_version(version) end | ||
278 | if type(requested) ~= "table" then requested = deps.parse_version(requested) end | ||
279 | if not version or not requested then return false end | ||
280 | |||
281 | for i, ri in ipairs(requested) do | ||
282 | local vi = version[i] or 0 | ||
283 | if ri ~= vi then return false end | ||
284 | end | ||
285 | if requested.revision then | ||
286 | return requested.revision == version.revision | ||
287 | end | ||
288 | return true | ||
289 | end | ||
290 | |||
291 | --- Check if a version satisfies a set of constraints. | ||
292 | -- @param version table: A version in table format | ||
293 | -- @param constraints table: An array of constraints in table format. | ||
294 | -- @return boolean: True if version satisfies all constraints, | ||
295 | -- false otherwise. | ||
296 | function deps.match_constraints(version, constraints) | ||
297 | assert(type(version) == "table") | ||
298 | assert(type(constraints) == "table") | ||
299 | local ok = true | ||
300 | setmetatable(version, version_mt) | ||
301 | for _, constr in pairs(constraints) do | ||
302 | if type(constr.version) == "string" then | ||
303 | constr.version = deps.parse_version(constr.version) | ||
304 | end | ||
305 | local constr_version, constr_op = constr.version, constr.op | ||
306 | setmetatable(constr_version, version_mt) | ||
307 | if constr_op == "==" then ok = version == constr_version | ||
308 | elseif constr_op == "~=" then ok = version ~= constr_version | ||
309 | elseif constr_op == ">" then ok = version > constr_version | ||
310 | elseif constr_op == "<" then ok = version < constr_version | ||
311 | elseif constr_op == ">=" then ok = version >= constr_version | ||
312 | elseif constr_op == "<=" then ok = version <= constr_version | ||
313 | elseif constr_op == "~>" then ok = partial_match(version, constr_version) | ||
314 | end | ||
315 | if not ok then break end | ||
316 | end | ||
317 | return ok | ||
318 | end | ||
319 | |||
320 | --- Attempt to match a dependency to an installed rock. | 146 | --- Attempt to match a dependency to an installed rock. |
321 | -- @param dep table: A dependency parsed in table format. | 147 | -- @param dep table: A dependency parsed in table format. |
322 | -- @param blacklist table: Versions that can't be accepted. Table where keys | 148 | -- @param blacklist table: Versions that can't be accepted. Table where keys |
323 | -- are program versions and values are 'true'. | 149 | -- are program versions and values are 'true'. |
150 | -- @param provided table: A table of auto-dependencies provided | ||
151 | -- by this Lua implementation for the given dependency. | ||
324 | -- @return string or nil: latest installed version of the rock matching the dependency | 152 | -- @return string or nil: latest installed version of the rock matching the dependency |
325 | -- or nil if it could not be matched. | 153 | -- or nil if it could not be matched. |
326 | local function match_dep(dep, blacklist, deps_mode) | 154 | local function match_dep(dep, blacklist, deps_mode, rocks_provided) |
327 | assert(type(dep) == "table") | 155 | assert(type(dep) == "table") |
328 | 156 | assert(type(rocks_provided) == "table") | |
157 | |||
329 | local versions | 158 | local versions |
330 | if cfg.rocks_provided[dep.name] then | 159 | local provided = rocks_provided[dep.name] |
331 | -- provided rocks have higher priority than manifest's rocks | 160 | if provided then |
332 | versions = { cfg.rocks_provided[dep.name] } | 161 | -- Provided rocks have higher priority than manifest's rocks. |
162 | versions = { provided } | ||
333 | else | 163 | else |
334 | versions = manif_core.get_versions(dep.name, deps_mode) | 164 | versions = manif.get_versions(dep.name, deps_mode) |
335 | end | 165 | end |
336 | 166 | ||
337 | local latest_version | 167 | local latest_version |
@@ -366,9 +196,9 @@ function deps.match_deps(rockspec, blacklist, deps_mode) | |||
366 | local matched, missing, no_upgrade = {}, {}, {} | 196 | local matched, missing, no_upgrade = {}, {}, {} |
367 | 197 | ||
368 | for _, dep in ipairs(rockspec.dependencies) do | 198 | for _, dep in ipairs(rockspec.dependencies) do |
369 | local found = match_dep(dep, blacklist and blacklist[dep.name] or nil, deps_mode) | 199 | local found = match_dep(dep, blacklist and blacklist[dep.name] or nil, deps_mode, rockspec.rocks_provided) |
370 | if found then | 200 | if found then |
371 | if not cfg.rocks_provided[dep.name] then | 201 | if not rockspec.rocks_provided[dep.name] then |
372 | matched[dep] = {name = dep.name, version = found} | 202 | matched[dep] = {name = dep.name, version = found} |
373 | end | 203 | end |
374 | else | 204 | else |
@@ -393,10 +223,10 @@ local function values_set(tbl) | |||
393 | return set | 223 | return set |
394 | end | 224 | end |
395 | 225 | ||
396 | local function rock_status(name, deps_mode) | 226 | local function rock_status(name, deps_mode, rocks_provided) |
397 | local search = require("luarocks.search") | 227 | local search = require("luarocks.search") |
398 | local installed = match_dep(search.make_query(name), nil, deps_mode) | 228 | local installed = match_dep(search.make_query(name), nil, deps_mode, rocks_provided) |
399 | local installation_type = cfg.rocks_provided[name] and "provided by VM" or "installed" | 229 | local installation_type = rocks_provided[name] and "provided by VM" or "installed" |
400 | return installed and installed.." "..installation_type or "not installed" | 230 | return installed and installed.." "..installation_type or "not installed" |
401 | end | 231 | end |
402 | 232 | ||
@@ -418,7 +248,8 @@ function deps.fulfill_dependencies(rockspec, deps_mode) | |||
418 | end | 248 | end |
419 | local supported = nil | 249 | local supported = nil |
420 | for _, plat in pairs(rockspec.supported_platforms) do | 250 | for _, plat in pairs(rockspec.supported_platforms) do |
421 | local neg, plat = plat:match("^(!?)(.*)") | 251 | local neg |
252 | neg, plat = plat:match("^(!?)(.*)") | ||
422 | if neg == "!" then | 253 | if neg == "!" then |
423 | if deps.platforms_set[plat] then | 254 | if deps.platforms_set[plat] then |
424 | return nil, "This rockspec for "..rockspec.package.." does not support "..plat.." platforms." | 255 | return nil, "This rockspec for "..rockspec.package.." does not support "..plat.." platforms." |
@@ -442,27 +273,27 @@ function deps.fulfill_dependencies(rockspec, deps_mode) | |||
442 | local first_missing_dep = true | 273 | local first_missing_dep = true |
443 | 274 | ||
444 | for _, dep in ipairs(rockspec.dependencies) do | 275 | for _, dep in ipairs(rockspec.dependencies) do |
445 | if not match_dep(dep, nil, deps_mode) then | 276 | if not match_dep(dep, nil, deps_mode, rockspec.rocks_provided) then |
446 | if first_missing_dep then | 277 | if first_missing_dep then |
447 | util.printout(("Missing dependencies for %s %s:"):format(rockspec.name, rockspec.version)) | 278 | util.printout(("Missing dependencies for %s %s:"):format(rockspec.name, rockspec.version)) |
448 | first_missing_dep = false | 279 | first_missing_dep = false |
449 | end | 280 | end |
450 | 281 | ||
451 | util.printout((" %s (%s)"):format(deps.show_dep(dep), rock_status(dep.name, deps_mode))) | 282 | util.printout((" %s (%s)"):format(deps.show_dep(dep), rock_status(dep.name, deps_mode, rockspec.rocks_provided))) |
452 | end | 283 | end |
453 | end | 284 | end |
454 | 285 | ||
455 | first_missing_dep = true | 286 | first_missing_dep = true |
456 | 287 | ||
457 | for _, dep in ipairs(rockspec.dependencies) do | 288 | for _, dep in ipairs(rockspec.dependencies) do |
458 | if not match_dep(dep, nil, deps_mode) then | 289 | if not match_dep(dep, nil, deps_mode, rockspec.rocks_provided) then |
459 | if first_missing_dep then | 290 | if first_missing_dep then |
460 | util.printout() | 291 | util.printout() |
461 | first_missing_dep = false | 292 | first_missing_dep = false |
462 | end | 293 | end |
463 | 294 | ||
464 | util.printout(("%s %s depends on %s (%s)"):format( | 295 | util.printout(("%s %s depends on %s (%s)"):format( |
465 | rockspec.name, rockspec.version, deps.show_dep(dep), rock_status(dep.name, deps_mode))) | 296 | rockspec.name, rockspec.version, deps.show_dep(dep), rock_status(dep.name, deps_mode, rockspec.rocks_provided))) |
466 | 297 | ||
467 | if dep.constraints[1] and dep.constraints[1].no_upgrade then | 298 | if dep.constraints[1] and dep.constraints[1].no_upgrade then |
468 | util.printerr("This version of "..rockspec.name.." is designed for use with") | 299 | util.printerr("This version of "..rockspec.name.." is designed for use with") |
@@ -691,7 +522,6 @@ function deps.scan_deps(results, missing, manifest, name, version, deps_mode) | |||
691 | 522 | ||
692 | local fetch = require("luarocks.fetch") | 523 | local fetch = require("luarocks.fetch") |
693 | 524 | ||
694 | local err | ||
695 | if results[name] then | 525 | if results[name] then |
696 | return results, missing | 526 | return results, missing |
697 | end | 527 | end |
@@ -709,7 +539,10 @@ function deps.scan_deps(results, missing, manifest, name, version, deps_mode) | |||
709 | end | 539 | end |
710 | dependencies_name[version] = rockspec.dependencies | 540 | dependencies_name[version] = rockspec.dependencies |
711 | else | 541 | else |
712 | rockspec = { dependencies = deplist } | 542 | rockspec = { |
543 | dependencies = deplist, | ||
544 | rocks_provided = setmetatable({}, { __index = cfg.rocks_provided_3_0 }) | ||
545 | } | ||
713 | end | 546 | end |
714 | local matched, failures = deps.match_deps(rockspec, nil, deps_mode) | 547 | local matched, failures = deps.match_deps(rockspec, nil, deps_mode) |
715 | results[name] = results | 548 | results[name] = results |
@@ -744,8 +577,4 @@ function deps.get_deps_mode(flags) | |||
744 | end | 577 | end |
745 | end | 578 | end |
746 | 579 | ||
747 | function deps.deps_mode_to_flag(deps_mode) | ||
748 | return "--deps-mode="..deps_mode | ||
749 | end | ||
750 | |||
751 | return deps | 580 | return deps |
diff --git a/src/luarocks/dir.lua b/src/luarocks/dir.lua index f72ebd6c..ad7fb870 100644 --- a/src/luarocks/dir.lua +++ b/src/luarocks/dir.lua | |||
@@ -1,9 +1,7 @@ | |||
1 | 1 | ||
2 | --- Generic utilities for handling pathnames. | 2 | --- Generic utilities for handling pathnames. |
3 | local dir = {} | 3 | local dir = {} |
4 | package.loaded["luarocks.dir"] = dir | 4 | setmetatable(dir, { __index = require("luarocks.core.dir") }) |
5 | |||
6 | dir.separator = "/" | ||
7 | 5 | ||
8 | --- Strip the path off a path+filename. | 6 | --- Strip the path off a path+filename. |
9 | -- @param pathname string: A path+name, such as "/a/b/c" | 7 | -- @param pathname string: A path+name, such as "/a/b/c" |
@@ -26,49 +24,4 @@ function dir.dir_name(pathname) | |||
26 | return (pathname:gsub("/*$", ""):match("(.*)[/]+[^/]*")) or "" | 24 | return (pathname:gsub("/*$", ""):match("(.*)[/]+[^/]*")) or "" |
27 | end | 25 | end |
28 | 26 | ||
29 | --- Describe a path in a cross-platform way. | ||
30 | -- Use this function to avoid platform-specific directory | ||
31 | -- separators in other modules. Removes trailing slashes from | ||
32 | -- each component given, to avoid repeated separators. | ||
33 | -- Separators inside strings are kept, to handle URLs containing | ||
34 | -- protocols. | ||
35 | -- @param ... strings representing directories | ||
36 | -- @return string: a string with a platform-specific representation | ||
37 | -- of the path. | ||
38 | function dir.path(...) | ||
39 | local t = {...} | ||
40 | while t[1] == "" do | ||
41 | table.remove(t, 1) | ||
42 | end | ||
43 | return (table.concat(t, "/"):gsub("([^:])/+", "%1/"):gsub("^/+", "/"):gsub("/*$", "")) | ||
44 | end | ||
45 | |||
46 | --- Split protocol and path from an URL or local pathname. | ||
47 | -- URLs should be in the "protocol://path" format. | ||
48 | -- For local pathnames, "file" is returned as the protocol. | ||
49 | -- @param url string: an URL or a local pathname. | ||
50 | -- @return string, string: the protocol, and the pathname without the protocol. | ||
51 | function dir.split_url(url) | ||
52 | assert(type(url) == "string") | ||
53 | |||
54 | local protocol, pathname = url:match("^([^:]*)://(.*)") | ||
55 | if not protocol then | ||
56 | protocol = "file" | ||
57 | pathname = url | ||
58 | end | ||
59 | return protocol, pathname | ||
60 | end | ||
61 | |||
62 | --- Normalize a url or local path. | ||
63 | -- URLs should be in the "protocol://path" format. System independent | ||
64 | -- forward slashes are used, removing trailing and double slashes | ||
65 | -- @param url string: an URL or a local pathname. | ||
66 | -- @return string: Normalized result. | ||
67 | function dir.normalize(name) | ||
68 | local protocol, pathname = dir.split_url(name) | ||
69 | pathname = pathname:gsub("\\", "/"):gsub("(.)/*$", "%1"):gsub("//", "/") | ||
70 | if protocol ~= "file" then pathname = protocol .."://"..pathname end | ||
71 | return pathname | ||
72 | end | ||
73 | |||
74 | return dir | 27 | return dir |
diff --git a/src/luarocks/doc.lua b/src/luarocks/doc.lua index 15460b31..63c4c4e3 100644 --- a/src/luarocks/doc.lua +++ b/src/luarocks/doc.lua | |||
@@ -2,7 +2,6 @@ | |||
2 | --- Module implementing the LuaRocks "doc" command. | 2 | --- Module implementing the LuaRocks "doc" command. |
3 | -- Shows documentation for an installed rock. | 3 | -- Shows documentation for an installed rock. |
4 | local doc = {} | 4 | local doc = {} |
5 | package.loaded["luarocks.doc"] = doc | ||
6 | 5 | ||
7 | local util = require("luarocks.util") | 6 | local util = require("luarocks.util") |
8 | local search = require("luarocks.search") | 7 | local search = require("luarocks.search") |
@@ -12,7 +11,6 @@ local fetch = require("luarocks.fetch") | |||
12 | local fs = require("luarocks.fs") | 11 | local fs = require("luarocks.fs") |
13 | local download = require("luarocks.download") | 12 | local download = require("luarocks.download") |
14 | 13 | ||
15 | util.add_run_function(doc) | ||
16 | doc.help_summary = "Show documentation for an installed rock." | 14 | doc.help_summary = "Show documentation for an installed rock." |
17 | 15 | ||
18 | doc.help = [[ | 16 | doc.help = [[ |
diff --git a/src/luarocks/download.lua b/src/luarocks/download.lua index 18573ae4..c81958de 100644 --- a/src/luarocks/download.lua +++ b/src/luarocks/download.lua | |||
@@ -2,7 +2,6 @@ | |||
2 | --- Module implementing the luarocks "download" command. | 2 | --- Module implementing the luarocks "download" command. |
3 | -- Download a rock from the repository. | 3 | -- Download a rock from the repository. |
4 | local download = {} | 4 | local download = {} |
5 | package.loaded["luarocks.download"] = download | ||
6 | 5 | ||
7 | local util = require("luarocks.util") | 6 | local util = require("luarocks.util") |
8 | local path = require("luarocks.path") | 7 | local path = require("luarocks.path") |
@@ -10,9 +9,8 @@ local fetch = require("luarocks.fetch") | |||
10 | local search = require("luarocks.search") | 9 | local search = require("luarocks.search") |
11 | local fs = require("luarocks.fs") | 10 | local fs = require("luarocks.fs") |
12 | local dir = require("luarocks.dir") | 11 | local dir = require("luarocks.dir") |
13 | local cfg = require("luarocks.cfg") | 12 | local cfg = require("luarocks.core.cfg") |
14 | 13 | ||
15 | util.add_run_function(download) | ||
16 | download.help_summary = "Download a specific rock file from a rocks server." | 14 | download.help_summary = "Download a specific rock file from a rocks server." |
17 | download.help_arguments = "[--all] [--arch=<arch> | --source | --rockspec] [<name> [<version>]]" | 15 | download.help_arguments = "[--all] [--arch=<arch> | --source | --rockspec] [<name> [<version>]]" |
18 | 16 | ||
diff --git a/src/luarocks/fetch.lua b/src/luarocks/fetch.lua index 824a3731..37db78f4 100644 --- a/src/luarocks/fetch.lua +++ b/src/luarocks/fetch.lua | |||
@@ -1,7 +1,6 @@ | |||
1 | 1 | ||
2 | --- Functions related to fetching and loading local and remote files. | 2 | --- Functions related to fetching and loading local and remote files. |
3 | local fetch = {} | 3 | local fetch = {} |
4 | package.loaded["luarocks.fetch"] = fetch | ||
5 | 4 | ||
6 | local fs = require("luarocks.fs") | 5 | local fs = require("luarocks.fs") |
7 | local dir = require("luarocks.dir") | 6 | local dir = require("luarocks.dir") |
@@ -10,7 +9,7 @@ local path = require("luarocks.path") | |||
10 | local deps = require("luarocks.deps") | 9 | local deps = require("luarocks.deps") |
11 | local persist = require("luarocks.persist") | 10 | local persist = require("luarocks.persist") |
12 | local util = require("luarocks.util") | 11 | local util = require("luarocks.util") |
13 | local cfg = require("luarocks.cfg") | 12 | local cfg = require("luarocks.core.cfg") |
14 | 13 | ||
15 | function fetch.is_basic_protocol(protocol, remote) | 14 | function fetch.is_basic_protocol(protocol, remote) |
16 | return protocol == "http" or protocol == "https" or protocol == "ftp" or (not remote and protocol == "file") | 15 | return protocol == "http" or protocol == "https" or protocol == "ftp" or (not remote and protocol == "file") |
@@ -209,6 +208,7 @@ function fetch.load_local_rockspec(filename, quick) | |||
209 | return nil, "Rockspec format "..rockspec.rockspec_format.." is not supported, please upgrade LuaRocks." | 208 | return nil, "Rockspec format "..rockspec.rockspec_format.." is not supported, please upgrade LuaRocks." |
210 | end | 209 | end |
211 | end | 210 | end |
211 | rockspec.format_is_at_least = deps.format_is_at_least | ||
212 | 212 | ||
213 | util.platform_overrides(rockspec.build) | 213 | util.platform_overrides(rockspec.build) |
214 | util.platform_overrides(rockspec.dependencies) | 214 | util.platform_overrides(rockspec.dependencies) |
@@ -244,10 +244,19 @@ function fetch.load_local_rockspec(filename, quick) | |||
244 | rockspec.local_filename = filename | 244 | rockspec.local_filename = filename |
245 | local filebase = rockspec.source.file or rockspec.source.url | 245 | local filebase = rockspec.source.file or rockspec.source.url |
246 | local base = fetch.url_to_base_dir(filebase) | 246 | local base = fetch.url_to_base_dir(filebase) |
247 | rockspec.source.dir_set = rockspec.source.dir ~= nil | ||
247 | rockspec.source.dir = rockspec.source.dir | 248 | rockspec.source.dir = rockspec.source.dir |
248 | or rockspec.source.module | 249 | or rockspec.source.module |
249 | or ((filebase:match("%.lua$") or filebase:match("%.c$")) and ".") | 250 | or ( (filebase:match("%.lua$") or filebase:match("%.c$")) |
251 | and (rockspec:format_is_at_least("3.0") | ||
252 | and (fetch.is_basic_protocol(protocol) and "." or base) | ||
253 | or ".") ) | ||
250 | or base | 254 | or base |
255 | |||
256 | rockspec.rocks_provided = (rockspec:format_is_at_least("3.0") | ||
257 | and cfg.rocks_provided_3_0 | ||
258 | or cfg.rocks_provided) | ||
259 | |||
251 | if rockspec.dependencies then | 260 | if rockspec.dependencies then |
252 | for i = 1, #rockspec.dependencies do | 261 | for i = 1, #rockspec.dependencies do |
253 | local parsed, err = deps.parse_dep(rockspec.dependencies[i]) | 262 | local parsed, err = deps.parse_dep(rockspec.dependencies[i]) |
@@ -347,7 +356,29 @@ function fetch.get_sources(rockspec, extract, dest_dir) | |||
347 | ok, err = fs.unpack_archive(rockspec.source.file) | 356 | ok, err = fs.unpack_archive(rockspec.source.file) |
348 | if not ok then return nil, err end | 357 | if not ok then return nil, err end |
349 | if not fs.exists(rockspec.source.dir) then | 358 | if not fs.exists(rockspec.source.dir) then |
350 | return nil, "Directory "..rockspec.source.dir.." not found inside archive "..rockspec.source.file, "source.dir", source_file, store_dir | 359 | |
360 | -- If rockspec.source.dir can't be found, see if we only have one | ||
361 | -- directory in store_dir. If that's the case, assume it's what | ||
362 | -- we're looking for. | ||
363 | -- We only do this if the rockspec source.dir was not set, and only | ||
364 | -- with rockspecs newer than 3.0. | ||
365 | local dir_count, found_dir = 0 | ||
366 | |||
367 | if not rockspec.source.dir_set and rockspec:format_is_at_least("3.0") then | ||
368 | local files = fs.list_dir() | ||
369 | for _, f in ipairs(files) do | ||
370 | if fs.is_dir(f) then | ||
371 | dir_count = dir_count + 1 | ||
372 | found_dir = f | ||
373 | end | ||
374 | end | ||
375 | end | ||
376 | |||
377 | if dir_count == 1 then | ||
378 | rockspec.source.dir = found_dir | ||
379 | else | ||
380 | return nil, "Directory "..rockspec.source.dir.." not found inside archive "..rockspec.source.file, "source.dir", source_file, store_dir | ||
381 | end | ||
351 | end | 382 | end |
352 | fs.pop_dir() | 383 | fs.pop_dir() |
353 | end | 384 | end |
diff --git a/src/luarocks/fetch/git.lua b/src/luarocks/fetch/git.lua index f61d89e9..72da4974 100644 --- a/src/luarocks/fetch/git.lua +++ b/src/luarocks/fetch/git.lua | |||
@@ -6,20 +6,48 @@ local unpack = unpack or table.unpack | |||
6 | 6 | ||
7 | local fs = require("luarocks.fs") | 7 | local fs = require("luarocks.fs") |
8 | local dir = require("luarocks.dir") | 8 | local dir = require("luarocks.dir") |
9 | local deps = require("luarocks.deps") | ||
9 | local util = require("luarocks.util") | 10 | local util = require("luarocks.util") |
10 | 11 | ||
12 | local cached_git_version | ||
13 | |||
14 | --- Get git version. | ||
15 | -- @param git_cmd string: name of git command. | ||
16 | -- @return table: git version as returned by luarocks.deps.parse_version. | ||
17 | local function git_version(git_cmd) | ||
18 | if not cached_git_version then | ||
19 | local version_line = io.popen(fs.Q(git_cmd)..' --version'):read() | ||
20 | local version_string = version_line:match('%d-%.%d+%.?%d*') | ||
21 | cached_git_version = deps.parse_version(version_string) | ||
22 | end | ||
23 | |||
24 | return cached_git_version | ||
25 | end | ||
26 | |||
27 | --- Check if git satisfies version requirement. | ||
28 | -- @param git_cmd string: name of git command. | ||
29 | -- @param version string: required version. | ||
30 | -- @return boolean: true if git matches version or is newer, false otherwise. | ||
31 | local function git_is_at_least(git_cmd, version) | ||
32 | return git_version(git_cmd) >= deps.parse_version(version) | ||
33 | end | ||
34 | |||
11 | --- Git >= 1.7.10 can clone a branch **or tag**, < 1.7.10 by branch only. We | 35 | --- Git >= 1.7.10 can clone a branch **or tag**, < 1.7.10 by branch only. We |
12 | -- need to know this in order to build the appropriate command; if we can't | 36 | -- need to know this in order to build the appropriate command; if we can't |
13 | -- clone by tag then we'll have to issue a subsequent command to check out the | 37 | -- clone by tag then we'll have to issue a subsequent command to check out the |
14 | -- given tag. | 38 | -- given tag. |
39 | -- @param git_cmd string: name of git command. | ||
15 | -- @return boolean: Whether Git can clone by tag. | 40 | -- @return boolean: Whether Git can clone by tag. |
16 | local function git_can_clone_by_tag(git_cmd) | 41 | local function git_can_clone_by_tag(git_cmd) |
17 | local version_string = io.popen(fs.Q(git_cmd)..' --version'):read() | 42 | return git_is_at_least(git_cmd, "1.7.10") |
18 | local major, minor, tiny = version_string:match('(%d-)%.(%d+)%.?(%d*)') | 43 | end |
19 | major, minor, tiny = tonumber(major), tonumber(minor), tonumber(tiny) or 0 | 44 | |
20 | local value = major > 1 or (major == 1 and (minor > 7 or (minor == 7 and tiny >= 10))) | 45 | --- Git >= 1.8.4 can fetch submodules shallowly, saving bandwidth and time for |
21 | git_can_clone_by_tag = function() return value end | 46 | -- submodules with large history. |
22 | return value | 47 | -- @param git_cmd string: name of git command. |
48 | -- @return boolean: Whether Git can fetch submodules shallowly. | ||
49 | local function git_supports_shallow_submodules(git_cmd) | ||
50 | return git_is_at_least(git_cmd, "1.8.4") | ||
23 | end | 51 | end |
24 | 52 | ||
25 | --- Download sources for building a rock, using git. | 53 | --- Download sources for building a rock, using git. |
@@ -76,12 +104,25 @@ function git.get_sources(rockspec, extract, dest_dir, depth) | |||
76 | ok, err = fs.change_dir(module) | 104 | ok, err = fs.change_dir(module) |
77 | if not ok then return nil, err end | 105 | if not ok then return nil, err end |
78 | if tag_or_branch and not git_can_clone_by_tag() then | 106 | if tag_or_branch and not git_can_clone_by_tag() then |
79 | local checkout_command = {fs.Q(git_cmd), "checkout", tag_or_branch} | 107 | if not fs.execute(fs.Q(git_cmd), "checkout", tag_or_branch) then |
80 | if not fs.execute(unpack(checkout_command)) then | ||
81 | return nil, 'Failed to check out the "' .. tag_or_branch ..'" tag or branch.' | 108 | return nil, 'Failed to check out the "' .. tag_or_branch ..'" tag or branch.' |
82 | end | 109 | end |
83 | end | 110 | end |
84 | 111 | ||
112 | -- Fetching git submodules is supported only when rockspec format is >= 3.0. | ||
113 | if rockspec:format_is_at_least("3.0") then | ||
114 | command = {fs.Q(git_cmd), "submodule", "update", "--init", "--recursive"} | ||
115 | |||
116 | if git_supports_shallow_submodules(git_cmd) then | ||
117 | -- Fetch only the last commit of each submodule. | ||
118 | table.insert(command, 5, "--depth=1") | ||
119 | end | ||
120 | |||
121 | if not fs.execute(unpack(command)) then | ||
122 | return nil, 'Failed to fetch submodules.' | ||
123 | end | ||
124 | end | ||
125 | |||
85 | fs.delete(dir.path(store_dir, module, ".git")) | 126 | fs.delete(dir.path(store_dir, module, ".git")) |
86 | fs.delete(dir.path(store_dir, module, ".gitignore")) | 127 | fs.delete(dir.path(store_dir, module, ".gitignore")) |
87 | fs.pop_dir() | 128 | fs.pop_dir() |
diff --git a/src/luarocks/fs.lua b/src/luarocks/fs.lua index 54cc7d73..7322e552 100644 --- a/src/luarocks/fs.lua +++ b/src/luarocks/fs.lua | |||
@@ -8,9 +8,10 @@ | |||
8 | local pairs = pairs | 8 | local pairs = pairs |
9 | 9 | ||
10 | local fs = {} | 10 | local fs = {} |
11 | -- To avoid a loop when loading the other fs modules. | ||
11 | package.loaded["luarocks.fs"] = fs | 12 | package.loaded["luarocks.fs"] = fs |
12 | 13 | ||
13 | local cfg = require("luarocks.cfg") | 14 | local cfg = require("luarocks.core.cfg") |
14 | 15 | ||
15 | local pack = table.pack or function(...) return { n = select("#", ...), ... } end | 16 | local pack = table.pack or function(...) return { n = select("#", ...), ... } end |
16 | local unpack = table.unpack or unpack | 17 | local unpack = table.unpack or unpack |
diff --git a/src/luarocks/fs/lua.lua b/src/luarocks/fs/lua.lua index a31cbb4e..770da2b7 100644 --- a/src/luarocks/fs/lua.lua +++ b/src/luarocks/fs/lua.lua | |||
@@ -6,7 +6,7 @@ local fs_lua = {} | |||
6 | 6 | ||
7 | local fs = require("luarocks.fs") | 7 | local fs = require("luarocks.fs") |
8 | 8 | ||
9 | local cfg = require("luarocks.cfg") | 9 | local cfg = require("luarocks.core.cfg") |
10 | local dir = require("luarocks.dir") | 10 | local dir = require("luarocks.dir") |
11 | local util = require("luarocks.util") | 11 | local util = require("luarocks.util") |
12 | local path = require("luarocks.path") | 12 | local path = require("luarocks.path") |
diff --git a/src/luarocks/fs/tools.lua b/src/luarocks/fs/tools.lua index ed51b545..d2062ed8 100644 --- a/src/luarocks/fs/tools.lua +++ b/src/luarocks/fs/tools.lua | |||
@@ -4,7 +4,7 @@ local tools = {} | |||
4 | 4 | ||
5 | local fs = require("luarocks.fs") | 5 | local fs = require("luarocks.fs") |
6 | local dir = require("luarocks.dir") | 6 | local dir = require("luarocks.dir") |
7 | local cfg = require("luarocks.cfg") | 7 | local cfg = require("luarocks.core.cfg") |
8 | 8 | ||
9 | local vars = cfg.variables | 9 | local vars = cfg.variables |
10 | 10 | ||
diff --git a/src/luarocks/fs/unix.lua b/src/luarocks/fs/unix.lua index 5c6b542c..df9d256d 100644 --- a/src/luarocks/fs/unix.lua +++ b/src/luarocks/fs/unix.lua | |||
@@ -4,7 +4,7 @@ local unix = {} | |||
4 | 4 | ||
5 | local fs = require("luarocks.fs") | 5 | local fs = require("luarocks.fs") |
6 | 6 | ||
7 | local cfg = require("luarocks.cfg") | 7 | local cfg = require("luarocks.core.cfg") |
8 | local dir = require("luarocks.dir") | 8 | local dir = require("luarocks.dir") |
9 | local util = require("luarocks.util") | 9 | local util = require("luarocks.util") |
10 | 10 | ||
diff --git a/src/luarocks/fs/unix/tools.lua b/src/luarocks/fs/unix/tools.lua index d0802725..d9dc009f 100644 --- a/src/luarocks/fs/unix/tools.lua +++ b/src/luarocks/fs/unix/tools.lua | |||
@@ -4,7 +4,7 @@ local tools = {} | |||
4 | 4 | ||
5 | local fs = require("luarocks.fs") | 5 | local fs = require("luarocks.fs") |
6 | local dir = require("luarocks.dir") | 6 | local dir = require("luarocks.dir") |
7 | local cfg = require("luarocks.cfg") | 7 | local cfg = require("luarocks.core.cfg") |
8 | 8 | ||
9 | local vars = cfg.variables | 9 | local vars = cfg.variables |
10 | 10 | ||
diff --git a/src/luarocks/fs/win32.lua b/src/luarocks/fs/win32.lua index c99cc895..9ce95986 100644 --- a/src/luarocks/fs/win32.lua +++ b/src/luarocks/fs/win32.lua | |||
@@ -5,7 +5,7 @@ local win32 = {} | |||
5 | 5 | ||
6 | local fs = require("luarocks.fs") | 6 | local fs = require("luarocks.fs") |
7 | 7 | ||
8 | local cfg = require("luarocks.cfg") | 8 | local cfg = require("luarocks.core.cfg") |
9 | local dir = require("luarocks.dir") | 9 | local dir = require("luarocks.dir") |
10 | local util = require("luarocks.util") | 10 | local util = require("luarocks.util") |
11 | 11 | ||
diff --git a/src/luarocks/fs/win32/tools.lua b/src/luarocks/fs/win32/tools.lua index 4adc78d1..7f6b853e 100644 --- a/src/luarocks/fs/win32/tools.lua +++ b/src/luarocks/fs/win32/tools.lua | |||
@@ -6,7 +6,7 @@ local tools = {} | |||
6 | 6 | ||
7 | local fs = require("luarocks.fs") | 7 | local fs = require("luarocks.fs") |
8 | local dir = require("luarocks.dir") | 8 | local dir = require("luarocks.dir") |
9 | local cfg = require("luarocks.cfg") | 9 | local cfg = require("luarocks.core.cfg") |
10 | 10 | ||
11 | local vars = cfg.variables | 11 | local vars = cfg.variables |
12 | 12 | ||
diff --git a/src/luarocks/help.lua b/src/luarocks/help.lua index 871e97e9..d27c3a50 100644 --- a/src/luarocks/help.lua +++ b/src/luarocks/help.lua | |||
@@ -7,12 +7,11 @@ | |||
7 | local help = {} | 7 | local help = {} |
8 | 8 | ||
9 | local util = require("luarocks.util") | 9 | local util = require("luarocks.util") |
10 | local cfg = require("luarocks.cfg") | 10 | local cfg = require("luarocks.core.cfg") |
11 | local dir = require("luarocks.dir") | 11 | local dir = require("luarocks.dir") |
12 | 12 | ||
13 | local program = util.this_program("luarocks") | 13 | local program = util.this_program("luarocks") |
14 | 14 | ||
15 | util.add_run_function(help) | ||
16 | help.help_summary = "Help on commands. Type '"..program.." help <command>' for more." | 15 | help.help_summary = "Help on commands. Type '"..program.." help <command>' for more." |
17 | 16 | ||
18 | help.help_arguments = "[<command>]" | 17 | help.help_arguments = "[<command>]" |
diff --git a/src/luarocks/index.lua b/src/luarocks/index.lua index e1f563ef..468f5cf8 100644 --- a/src/luarocks/index.lua +++ b/src/luarocks/index.lua | |||
@@ -1,7 +1,6 @@ | |||
1 | 1 | ||
2 | --- Module which builds the index.html page to be used in rocks servers. | 2 | --- Module which builds the index.html page to be used in rocks servers. |
3 | local index = {} | 3 | local index = {} |
4 | package.loaded["luarocks.index"] = index | ||
5 | 4 | ||
6 | local util = require("luarocks.util") | 5 | local util = require("luarocks.util") |
7 | local fs = require("luarocks.fs") | 6 | local fs = require("luarocks.fs") |
diff --git a/src/luarocks/install.lua b/src/luarocks/install.lua index b5128430..c9b085f5 100644 --- a/src/luarocks/install.lua +++ b/src/luarocks/install.lua | |||
@@ -1,7 +1,6 @@ | |||
1 | --- Module implementing the LuaRocks "install" command. | 1 | --- Module implementing the LuaRocks "install" command. |
2 | -- Installs binary rocks. | 2 | -- Installs binary rocks. |
3 | local install = {} | 3 | local install = {} |
4 | package.loaded["luarocks.install"] = install | ||
5 | 4 | ||
6 | local path = require("luarocks.path") | 5 | local path = require("luarocks.path") |
7 | local repos = require("luarocks.repos") | 6 | local repos = require("luarocks.repos") |
@@ -9,11 +8,10 @@ local fetch = require("luarocks.fetch") | |||
9 | local util = require("luarocks.util") | 8 | local util = require("luarocks.util") |
10 | local fs = require("luarocks.fs") | 9 | local fs = require("luarocks.fs") |
11 | local deps = require("luarocks.deps") | 10 | local deps = require("luarocks.deps") |
12 | local manif = require("luarocks.manif") | 11 | local writer = require("luarocks.manif.writer") |
13 | local remove = require("luarocks.remove") | 12 | local remove = require("luarocks.remove") |
14 | local cfg = require("luarocks.cfg") | 13 | local cfg = require("luarocks.core.cfg") |
15 | 14 | ||
16 | util.add_run_function(install) | ||
17 | install.help_summary = "Install a rock." | 15 | install.help_summary = "Install a rock." |
18 | 16 | ||
19 | install.help_arguments = "{<rock>|<name> [<version>]}" | 17 | install.help_arguments = "{<rock>|<name> [<version>]}" |
@@ -75,7 +73,7 @@ function install.install_binary_rock(rock_file, deps_mode) | |||
75 | 73 | ||
76 | -- For compatibility with .rock files built with LuaRocks 1 | 74 | -- For compatibility with .rock files built with LuaRocks 1 |
77 | if not fs.exists(path.rock_manifest_file(name, version)) then | 75 | if not fs.exists(path.rock_manifest_file(name, version)) then |
78 | ok, err = manif.make_rock_manifest(name, version) | 76 | ok, err = writer.make_rock_manifest(name, version) |
79 | if err then return nil, err end | 77 | if err then return nil, err end |
80 | end | 78 | end |
81 | 79 | ||
diff --git a/src/luarocks/lint.lua b/src/luarocks/lint.lua index d5cc48d0..742f1736 100644 --- a/src/luarocks/lint.lua +++ b/src/luarocks/lint.lua | |||
@@ -2,13 +2,11 @@ | |||
2 | --- Module implementing the LuaRocks "lint" command. | 2 | --- Module implementing the LuaRocks "lint" command. |
3 | -- Utility function that checks syntax of the rockspec. | 3 | -- Utility function that checks syntax of the rockspec. |
4 | local lint = {} | 4 | local lint = {} |
5 | package.loaded["luarocks.lint"] = lint | ||
6 | 5 | ||
7 | local util = require("luarocks.util") | 6 | local util = require("luarocks.util") |
8 | local download = require("luarocks.download") | 7 | local download = require("luarocks.download") |
9 | local fetch = require("luarocks.fetch") | 8 | local fetch = require("luarocks.fetch") |
10 | 9 | ||
11 | util.add_run_function(lint) | ||
12 | lint.help_summary = "Check syntax of a rockspec." | 10 | lint.help_summary = "Check syntax of a rockspec." |
13 | lint.help_arguments = "<rockspec>" | 11 | lint.help_arguments = "<rockspec>" |
14 | lint.help = [[ | 12 | lint.help = [[ |
diff --git a/src/luarocks/list.lua b/src/luarocks/list.lua index c65e058f..45f1a26f 100644 --- a/src/luarocks/list.lua +++ b/src/luarocks/list.lua | |||
@@ -2,15 +2,13 @@ | |||
2 | --- Module implementing the LuaRocks "list" command. | 2 | --- Module implementing the LuaRocks "list" command. |
3 | -- Lists currently installed rocks. | 3 | -- Lists currently installed rocks. |
4 | local list = {} | 4 | local list = {} |
5 | package.loaded["luarocks.list"] = list | ||
6 | 5 | ||
7 | local search = require("luarocks.search") | 6 | local search = require("luarocks.search") |
8 | local deps = require("luarocks.deps") | 7 | local deps = require("luarocks.deps") |
9 | local cfg = require("luarocks.cfg") | 8 | local cfg = require("luarocks.core.cfg") |
10 | local util = require("luarocks.util") | 9 | local util = require("luarocks.util") |
11 | local path = require("luarocks.path") | 10 | local path = require("luarocks.path") |
12 | 11 | ||
13 | util.add_run_function(list) | ||
14 | list.help_summary = "List currently installed rocks." | 12 | list.help_summary = "List currently installed rocks." |
15 | list.help_arguments = "[--porcelain] <filter>" | 13 | list.help_arguments = "[--porcelain] <filter>" |
16 | list.help = [[ | 14 | list.help = [[ |
diff --git a/src/luarocks/loader.lua b/src/luarocks/loader.lua index 874bc10c..b8072110 100644 --- a/src/luarocks/loader.lua +++ b/src/luarocks/loader.lua | |||
@@ -6,21 +6,24 @@ | |||
6 | -- used to load previous modules, so that the loader chooses versions | 6 | -- used to load previous modules, so that the loader chooses versions |
7 | -- that are declared to be compatible with the ones loaded earlier. | 7 | -- that are declared to be compatible with the ones loaded earlier. |
8 | local loaders = package.loaders or package.searchers | 8 | local loaders = package.loaders or package.searchers |
9 | local package, require, ipairs, table, type, next, tostring, error = | 9 | local require, ipairs, table, type, next, tostring, error = |
10 | package, require, ipairs, table, type, next, tostring, error | 10 | require, ipairs, table, type, next, tostring, error |
11 | local unpack = unpack or table.unpack | 11 | local unpack = unpack or table.unpack |
12 | 12 | ||
13 | --module("luarocks.loader") | ||
14 | local loader = {} | 13 | local loader = {} |
15 | package.loaded["luarocks.loader"] = loader | ||
16 | 14 | ||
17 | local cfg = require("luarocks.cfg") | 15 | local is_clean = not package.loaded["luarocks.core.cfg"] |
16 | |||
17 | -- This loader module depends only on core modules. | ||
18 | local cfg = require("luarocks.core.cfg") | ||
18 | cfg.init_package_paths() | 19 | cfg.init_package_paths() |
19 | 20 | ||
20 | local path = require("luarocks.path") | 21 | local path = require("luarocks.core.path") |
21 | local manif_core = require("luarocks.manif_core") | 22 | local manif = require("luarocks.core.manif") |
22 | local deps = require("luarocks.deps") | 23 | local deps = require("luarocks.core.deps") |
23 | local util = require("luarocks.util") | 24 | local util = require("luarocks.core.util") |
25 | local require = nil | ||
26 | -------------------------------------------------------------------------------- | ||
24 | 27 | ||
25 | -- Workaround for wrappers produced by older versions of LuaRocks | 28 | -- Workaround for wrappers produced by older versions of LuaRocks |
26 | local temporary_global = false | 29 | local temporary_global = false |
@@ -55,7 +58,7 @@ local function load_rocks_trees() | |||
55 | local any_ok = false | 58 | local any_ok = false |
56 | local trees = {} | 59 | local trees = {} |
57 | for _, tree in ipairs(cfg.rocks_trees) do | 60 | for _, tree in ipairs(cfg.rocks_trees) do |
58 | local manifest, err = manif_core.load_local_manifest(path.rocks_dir(tree)) | 61 | local manifest, err = manif.load_local_manifest(path.rocks_dir(tree)) |
59 | if manifest then | 62 | if manifest then |
60 | any_ok = true | 63 | any_ok = true |
61 | table.insert(trees, {tree=tree, manifest=manifest}) | 64 | table.insert(trees, {tree=tree, manifest=manifest}) |
@@ -246,4 +249,12 @@ end | |||
246 | 249 | ||
247 | table.insert(loaders, 1, loader.luarocks_loader) | 250 | table.insert(loaders, 1, loader.luarocks_loader) |
248 | 251 | ||
252 | if is_clean then | ||
253 | for modname, _ in pairs(package.loaded) do | ||
254 | if modname:match("^luarocks%.") then | ||
255 | package.loaded[modname] = nil | ||
256 | end | ||
257 | end | ||
258 | end | ||
259 | |||
249 | return loader | 260 | return loader |
diff --git a/src/luarocks/make.lua b/src/luarocks/make.lua index 1464def7..eb38bff0 100644 --- a/src/luarocks/make.lua +++ b/src/luarocks/make.lua | |||
@@ -4,18 +4,16 @@ | |||
4 | -- it does not fetch sources, etc., assuming everything is | 4 | -- it does not fetch sources, etc., assuming everything is |
5 | -- available in the current directory. | 5 | -- available in the current directory. |
6 | local make = {} | 6 | local make = {} |
7 | package.loaded["luarocks.make"] = make | ||
8 | 7 | ||
9 | local build = require("luarocks.build") | 8 | local build = require("luarocks.build") |
10 | local fs = require("luarocks.fs") | 9 | local fs = require("luarocks.fs") |
11 | local util = require("luarocks.util") | 10 | local util = require("luarocks.util") |
12 | local cfg = require("luarocks.cfg") | 11 | local cfg = require("luarocks.core.cfg") |
13 | local fetch = require("luarocks.fetch") | 12 | local fetch = require("luarocks.fetch") |
14 | local pack = require("luarocks.pack") | 13 | local pack = require("luarocks.pack") |
15 | local remove = require("luarocks.remove") | 14 | local remove = require("luarocks.remove") |
16 | local deps = require("luarocks.deps") | 15 | local deps = require("luarocks.deps") |
17 | 16 | ||
18 | util.add_run_function(make) | ||
19 | make.help_summary = "Compile package in current directory using a rockspec." | 17 | make.help_summary = "Compile package in current directory using a rockspec." |
20 | make.help_arguments = "[--pack-binary-rock] [<rockspec>]" | 18 | make.help_arguments = "[--pack-binary-rock] [<rockspec>]" |
21 | make.help = [[ | 19 | make.help = [[ |
diff --git a/src/luarocks/manif.lua b/src/luarocks/manif.lua index 1b75452b..40657fdf 100644 --- a/src/luarocks/manif.lua +++ b/src/luarocks/manif.lua | |||
@@ -3,41 +3,18 @@ | |||
3 | -- They are loaded into manifest tables, which are then used for | 3 | -- They are loaded into manifest tables, which are then used for |
4 | -- performing searches, matching dependencies, etc. | 4 | -- performing searches, matching dependencies, etc. |
5 | local manif = {} | 5 | local manif = {} |
6 | package.loaded["luarocks.manif"] = manif | 6 | setmetatable(manif, { __index = require("luarocks.core.manif") }) |
7 | 7 | ||
8 | local manif_core = require("luarocks.manif_core") | ||
9 | local persist = require("luarocks.persist") | 8 | local persist = require("luarocks.persist") |
10 | local fetch = require("luarocks.fetch") | 9 | local fetch = require("luarocks.fetch") |
11 | local dir = require("luarocks.dir") | 10 | local dir = require("luarocks.dir") |
12 | local fs = require("luarocks.fs") | 11 | local fs = require("luarocks.fs") |
13 | local search = require("luarocks.search") | ||
14 | local util = require("luarocks.util") | 12 | local util = require("luarocks.util") |
15 | local cfg = require("luarocks.cfg") | 13 | local cfg = require("luarocks.core.cfg") |
16 | local path = require("luarocks.path") | 14 | local path = require("luarocks.path") |
17 | local repos = require("luarocks.repos") | ||
18 | local deps = require("luarocks.deps") | ||
19 | 15 | ||
20 | manif.rock_manifest_cache = {} | 16 | manif.rock_manifest_cache = {} |
21 | 17 | ||
22 | --- Commit a table to disk in given local path. | ||
23 | -- @param where string: The directory where the table should be saved. | ||
24 | -- @param name string: The filename. | ||
25 | -- @param tbl table: The table to be saved. | ||
26 | -- @return boolean or (nil, string): true if successful, or nil and a | ||
27 | -- message in case of errors. | ||
28 | local function save_table(where, name, tbl) | ||
29 | assert(type(where) == "string") | ||
30 | assert(type(name) == "string") | ||
31 | assert(type(tbl) == "table") | ||
32 | |||
33 | local filename = dir.path(where, name) | ||
34 | local ok, err = persist.save_from_table(filename..".tmp", tbl) | ||
35 | if ok then | ||
36 | ok, err = fs.replace_file(filename, filename..".tmp") | ||
37 | end | ||
38 | return ok, err | ||
39 | end | ||
40 | |||
41 | function manif.load_rock_manifest(name, version, root) | 18 | function manif.load_rock_manifest(name, version, root) |
42 | assert(type(name) == "string") | 19 | assert(type(name) == "string") |
43 | assert(type(version) == "string") | 20 | assert(type(version) == "string") |
@@ -48,43 +25,13 @@ function manif.load_rock_manifest(name, version, root) | |||
48 | end | 25 | end |
49 | local pathname = path.rock_manifest_file(name, version, root) | 26 | local pathname = path.rock_manifest_file(name, version, root) |
50 | local rock_manifest = persist.load_into_table(pathname) | 27 | local rock_manifest = persist.load_into_table(pathname) |
51 | if not rock_manifest then return nil end | 28 | if not rock_manifest then |
29 | return nil, "rock_manifest file not found for "..name.." "..version.." - not a LuaRocks tree?" | ||
30 | end | ||
52 | manif.rock_manifest_cache[name_version] = rock_manifest | 31 | manif.rock_manifest_cache[name_version] = rock_manifest |
53 | return rock_manifest.rock_manifest | 32 | return rock_manifest.rock_manifest |
54 | end | 33 | end |
55 | 34 | ||
56 | function manif.make_rock_manifest(name, version) | ||
57 | local install_dir = path.install_dir(name, version) | ||
58 | local rock_manifest = path.rock_manifest_file(name, version) | ||
59 | local tree = {} | ||
60 | for _, file in ipairs(fs.find(install_dir)) do | ||
61 | local full_path = dir.path(install_dir, file) | ||
62 | local walk = tree | ||
63 | local last | ||
64 | local last_name | ||
65 | for name in file:gmatch("[^/]+") do | ||
66 | local next = walk[name] | ||
67 | if not next then | ||
68 | next = {} | ||
69 | walk[name] = next | ||
70 | end | ||
71 | last = walk | ||
72 | last_name = name | ||
73 | walk = next | ||
74 | end | ||
75 | if fs.is_file(full_path) then | ||
76 | local sum, err = fs.get_md5(full_path) | ||
77 | if not sum then | ||
78 | return nil, "Failed producing checksum: "..tostring(err) | ||
79 | end | ||
80 | last[last_name] = sum | ||
81 | end | ||
82 | end | ||
83 | rock_manifest = { rock_manifest=tree } | ||
84 | manif.rock_manifest_cache[name.."/"..version] = rock_manifest | ||
85 | save_table(install_dir, "rock_manifest", rock_manifest ) | ||
86 | end | ||
87 | |||
88 | local function fetch_manifest_from(repo_url, filename) | 35 | local function fetch_manifest_from(repo_url, filename) |
89 | local url = dir.path(repo_url, filename) | 36 | local url = dir.path(repo_url, filename) |
90 | local name = repo_url:gsub("[/:]","_") | 37 | local name = repo_url:gsub("[/:]","_") |
@@ -112,7 +59,7 @@ function manif.load_manifest(repo_url, lua_version) | |||
112 | assert(type(lua_version) == "string" or not lua_version) | 59 | assert(type(lua_version) == "string" or not lua_version) |
113 | lua_version = lua_version or cfg.lua_version | 60 | lua_version = lua_version or cfg.lua_version |
114 | 61 | ||
115 | local cached_manifest = manif_core.get_cached_manifest(repo_url, lua_version) | 62 | local cached_manifest = manif.get_cached_manifest(repo_url, lua_version) |
116 | if cached_manifest then | 63 | if cached_manifest then |
117 | return cached_manifest | 64 | return cached_manifest |
118 | end | 65 | end |
@@ -159,309 +106,7 @@ function manif.load_manifest(repo_url, lua_version) | |||
159 | end | 106 | end |
160 | pathname = nozip | 107 | pathname = nozip |
161 | end | 108 | end |
162 | return manif_core.manifest_loader(pathname, repo_url, lua_version) | 109 | return manif.manifest_loader(pathname, repo_url, lua_version) |
163 | end | ||
164 | |||
165 | --- Output a table listing items of a package. | ||
166 | -- @param itemsfn function: a function for obtaining items of a package. | ||
167 | -- pkg and version will be passed to it; it should return a table with | ||
168 | -- items as keys. | ||
169 | -- @param pkg string: package name | ||
170 | -- @param version string: package version | ||
171 | -- @param tbl table: the package matching table: keys should be item names | ||
172 | -- and values arrays of strings with packages names in "name/version" format. | ||
173 | local function store_package_items(itemsfn, pkg, version, tbl) | ||
174 | assert(type(itemsfn) == "function") | ||
175 | assert(type(pkg) == "string") | ||
176 | assert(type(version) == "string") | ||
177 | assert(type(tbl) == "table") | ||
178 | |||
179 | local pkg_version = pkg.."/"..version | ||
180 | local result = {} | ||
181 | |||
182 | for item, path in pairs(itemsfn(pkg, version)) do | ||
183 | result[item] = path | ||
184 | if not tbl[item] then | ||
185 | tbl[item] = {} | ||
186 | end | ||
187 | table.insert(tbl[item], pkg_version) | ||
188 | end | ||
189 | return result | ||
190 | end | ||
191 | |||
192 | --- Sort function for ordering rock identifiers in a manifest's | ||
193 | -- modules table. Rocks are ordered alphabetically by name, and then | ||
194 | -- by version which greater first. | ||
195 | -- @param a string: Version to compare. | ||
196 | -- @param b string: Version to compare. | ||
197 | -- @return boolean: The comparison result, according to the | ||
198 | -- rule outlined above. | ||
199 | local function sort_pkgs(a, b) | ||
200 | assert(type(a) == "string") | ||
201 | assert(type(b) == "string") | ||
202 | |||
203 | local na, va = a:match("(.*)/(.*)$") | ||
204 | local nb, vb = b:match("(.*)/(.*)$") | ||
205 | |||
206 | return (na == nb) and deps.compare_versions(va, vb) or na < nb | ||
207 | end | ||
208 | |||
209 | --- Sort items of a package matching table by version number (higher versions first). | ||
210 | -- @param tbl table: the package matching table: keys should be strings | ||
211 | -- and values arrays of strings with packages names in "name/version" format. | ||
212 | local function sort_package_matching_table(tbl) | ||
213 | assert(type(tbl) == "table") | ||
214 | |||
215 | if next(tbl) then | ||
216 | for item, pkgs in pairs(tbl) do | ||
217 | if #pkgs > 1 then | ||
218 | table.sort(pkgs, sort_pkgs) | ||
219 | -- Remove duplicates from the sorted array. | ||
220 | local prev = nil | ||
221 | local i = 1 | ||
222 | while pkgs[i] do | ||
223 | local curr = pkgs[i] | ||
224 | if curr == prev then | ||
225 | table.remove(pkgs, i) | ||
226 | else | ||
227 | prev = curr | ||
228 | i = i + 1 | ||
229 | end | ||
230 | end | ||
231 | end | ||
232 | end | ||
233 | end | ||
234 | end | ||
235 | |||
236 | --- Process the dependencies of a manifest table to determine its dependency | ||
237 | -- chains for loading modules. The manifest dependencies information is filled | ||
238 | -- and any dependency inconsistencies or missing dependencies are reported to | ||
239 | -- standard error. | ||
240 | -- @param manifest table: a manifest table. | ||
241 | -- @param deps_mode string: Dependency mode: "one" for the current default tree, | ||
242 | -- "all" for all trees, "order" for all trees with priority >= the current default, | ||
243 | -- "none" for no trees. | ||
244 | local function update_dependencies(manifest, deps_mode) | ||
245 | assert(type(manifest) == "table") | ||
246 | assert(type(deps_mode) == "string") | ||
247 | |||
248 | for pkg, versions in pairs(manifest.repository) do | ||
249 | for version, repositories in pairs(versions) do | ||
250 | local current = pkg.." "..version | ||
251 | for _, repo in ipairs(repositories) do | ||
252 | if repo.arch == "installed" then | ||
253 | local missing | ||
254 | repo.dependencies, missing = deps.scan_deps({}, {}, manifest, pkg, version, deps_mode) | ||
255 | repo.dependencies[pkg] = nil | ||
256 | if missing then | ||
257 | for miss, err in pairs(missing) do | ||
258 | if miss == current then | ||
259 | util.printerr("Tree inconsistency detected: "..current.." has no rockspec. "..err) | ||
260 | elseif deps_mode ~= "none" then | ||
261 | util.printerr("Missing dependency for "..pkg.." "..version..": "..miss) | ||
262 | end | ||
263 | end | ||
264 | end | ||
265 | end | ||
266 | end | ||
267 | end | ||
268 | end | ||
269 | end | ||
270 | |||
271 | --- Filter manifest table by Lua version, removing rockspecs whose Lua version | ||
272 | -- does not match. | ||
273 | -- @param manifest table: a manifest table. | ||
274 | -- @param lua_version string or nil: filter by Lua version | ||
275 | -- @param repodir string: directory of repository being scanned | ||
276 | -- @param cache table: temporary rockspec cache table | ||
277 | local function filter_by_lua_version(manifest, lua_version, repodir, cache) | ||
278 | assert(type(manifest) == "table") | ||
279 | assert(type(repodir) == "string") | ||
280 | assert((not cache) or type(cache) == "table") | ||
281 | |||
282 | cache = cache or {} | ||
283 | lua_version = deps.parse_version(lua_version) | ||
284 | for pkg, versions in pairs(manifest.repository) do | ||
285 | local to_remove = {} | ||
286 | for version, repositories in pairs(versions) do | ||
287 | for _, repo in ipairs(repositories) do | ||
288 | if repo.arch == "rockspec" then | ||
289 | local pathname = dir.path(repodir, pkg.."-"..version..".rockspec") | ||
290 | local rockspec, err = cache[pathname] | ||
291 | if not rockspec then | ||
292 | rockspec, err = fetch.load_local_rockspec(pathname, true) | ||
293 | end | ||
294 | if rockspec then | ||
295 | cache[pathname] = rockspec | ||
296 | for _, dep in ipairs(rockspec.dependencies) do | ||
297 | if dep.name == "lua" then | ||
298 | if not deps.match_constraints(lua_version, dep.constraints) then | ||
299 | table.insert(to_remove, version) | ||
300 | end | ||
301 | break | ||
302 | end | ||
303 | end | ||
304 | else | ||
305 | util.printerr("Error loading rockspec for "..pkg.." "..version..": "..err) | ||
306 | end | ||
307 | end | ||
308 | end | ||
309 | end | ||
310 | if next(to_remove) then | ||
311 | for _, incompat in ipairs(to_remove) do | ||
312 | versions[incompat] = nil | ||
313 | end | ||
314 | if not next(versions) then | ||
315 | manifest.repository[pkg] = nil | ||
316 | end | ||
317 | end | ||
318 | end | ||
319 | end | ||
320 | |||
321 | --- Store search results in a manifest table. | ||
322 | -- @param results table: The search results as returned by search.disk_search. | ||
323 | -- @param manifest table: A manifest table (must contain repository, modules, commands tables). | ||
324 | -- It will be altered to include the search results. | ||
325 | -- @param dep_handler: dependency handler function | ||
326 | -- @return boolean or (nil, string): true in case of success, or nil followed by an error message. | ||
327 | local function store_results(results, manifest, dep_handler) | ||
328 | assert(type(results) == "table") | ||
329 | assert(type(manifest) == "table") | ||
330 | assert((not dep_handler) or type(dep_handler) == "function") | ||
331 | |||
332 | for name, versions in pairs(results) do | ||
333 | local pkgtable = manifest.repository[name] or {} | ||
334 | for version, entries in pairs(versions) do | ||
335 | local versiontable = {} | ||
336 | for _, entry in ipairs(entries) do | ||
337 | local entrytable = {} | ||
338 | entrytable.arch = entry.arch | ||
339 | if entry.arch == "installed" then | ||
340 | local rock_manifest = manif.load_rock_manifest(name, version) | ||
341 | if not rock_manifest then | ||
342 | return nil, "rock_manifest file not found for "..name.." "..version.." - not a LuaRocks 2 tree?" | ||
343 | end | ||
344 | entrytable.modules = store_package_items(repos.package_modules, name, version, manifest.modules) | ||
345 | entrytable.commands = store_package_items(repos.package_commands, name, version, manifest.commands) | ||
346 | end | ||
347 | table.insert(versiontable, entrytable) | ||
348 | end | ||
349 | pkgtable[version] = versiontable | ||
350 | end | ||
351 | manifest.repository[name] = pkgtable | ||
352 | end | ||
353 | if dep_handler then | ||
354 | dep_handler(manifest) | ||
355 | end | ||
356 | sort_package_matching_table(manifest.modules) | ||
357 | sort_package_matching_table(manifest.commands) | ||
358 | return true | ||
359 | end | ||
360 | |||
361 | --- Scan a LuaRocks repository and output a manifest file. | ||
362 | -- A file called 'manifest' will be written in the root of the given | ||
363 | -- repository directory. | ||
364 | -- @param repo A local repository directory. | ||
365 | -- @param deps_mode string: Dependency mode: "one" for the current default tree, | ||
366 | -- "all" for all trees, "order" for all trees with priority >= the current default, | ||
367 | -- "none" for the default dependency mode from the configuration. | ||
368 | -- @param remote boolean: 'true' if making a manifest for a rocks server. | ||
369 | -- @return boolean or (nil, string): True if manifest was generated, | ||
370 | -- or nil and an error message. | ||
371 | function manif.make_manifest(repo, deps_mode, remote) | ||
372 | assert(type(repo) == "string") | ||
373 | assert(type(deps_mode) == "string") | ||
374 | |||
375 | if deps_mode == "none" then deps_mode = cfg.deps_mode end | ||
376 | |||
377 | if not fs.is_dir(repo) then | ||
378 | return nil, "Cannot access repository at "..repo | ||
379 | end | ||
380 | |||
381 | local query = search.make_query("") | ||
382 | query.exact_name = false | ||
383 | query.arch = "any" | ||
384 | local results = search.disk_search(repo, query) | ||
385 | local manifest = { repository = {}, modules = {}, commands = {} } | ||
386 | |||
387 | manif_core.cache_manifest(repo, nil, manifest) | ||
388 | |||
389 | local dep_handler = nil | ||
390 | if not remote then | ||
391 | dep_handler = function(manifest) | ||
392 | update_dependencies(manifest, deps_mode) | ||
393 | end | ||
394 | end | ||
395 | local ok, err = store_results(results, manifest, dep_handler) | ||
396 | if not ok then return nil, err end | ||
397 | |||
398 | if remote then | ||
399 | local cache = {} | ||
400 | for luaver in util.lua_versions() do | ||
401 | local vmanifest = { repository = {}, modules = {}, commands = {} } | ||
402 | local dep_handler = function(manifest) | ||
403 | filter_by_lua_version(manifest, luaver, repo, cache) | ||
404 | end | ||
405 | local ok, err = store_results(results, vmanifest, dep_handler) | ||
406 | save_table(repo, "manifest-"..luaver, vmanifest) | ||
407 | end | ||
408 | end | ||
409 | |||
410 | return save_table(repo, "manifest", manifest) | ||
411 | end | ||
412 | |||
413 | --- Load a manifest file from a local repository and add to the repository | ||
414 | -- information with regard to the given name and version. | ||
415 | -- A file called 'manifest' will be written in the root of the given | ||
416 | -- repository directory. | ||
417 | -- @param name string: Name of a package from the repository. | ||
418 | -- @param version string: Version of a package from the repository. | ||
419 | -- @param repo string or nil: Pathname of a local repository. If not given, | ||
420 | -- the default local repository is used. | ||
421 | -- @param deps_mode string: Dependency mode: "one" for the current default tree, | ||
422 | -- "all" for all trees, "order" for all trees with priority >= the current default, | ||
423 | -- "none" for using the default dependency mode from the configuration. | ||
424 | -- @return boolean or (nil, string): True if manifest was generated, | ||
425 | -- or nil and an error message. | ||
426 | function manif.update_manifest(name, version, repo, deps_mode) | ||
427 | assert(type(name) == "string") | ||
428 | assert(type(version) == "string") | ||
429 | repo = path.rocks_dir(repo or cfg.root_dir) | ||
430 | assert(type(deps_mode) == "string") | ||
431 | |||
432 | if deps_mode == "none" then deps_mode = cfg.deps_mode end | ||
433 | |||
434 | local manifest, err = manif.load_manifest(repo) | ||
435 | if not manifest then | ||
436 | util.printerr("No existing manifest. Attempting to rebuild...") | ||
437 | local ok, err = manif.make_manifest(repo, deps_mode) | ||
438 | if not ok then | ||
439 | return nil, err | ||
440 | end | ||
441 | manifest, err = manif.load_manifest(repo) | ||
442 | if not manifest then | ||
443 | return nil, err | ||
444 | end | ||
445 | end | ||
446 | |||
447 | local results = {[name] = {[version] = {{arch = "installed", repo = repo}}}} | ||
448 | |||
449 | local dep_handler = function(manifest) | ||
450 | update_dependencies(manifest, deps_mode) | ||
451 | end | ||
452 | local ok, err = store_results(results, manifest, dep_handler) | ||
453 | if not ok then return nil, err end | ||
454 | |||
455 | return save_table(repo, "manifest", manifest) | ||
456 | end | ||
457 | |||
458 | function manif.zip_manifests() | ||
459 | for ver in util.lua_versions() do | ||
460 | local file = "manifest-"..ver | ||
461 | local zip = file..".zip" | ||
462 | fs.delete(dir.path(fs.current_dir(), zip)) | ||
463 | fs.zip(zip, file) | ||
464 | end | ||
465 | end | 110 | end |
466 | 111 | ||
467 | local function relative_path(from_dir, to_file) | 112 | local function relative_path(from_dir, to_file) |
@@ -489,7 +134,7 @@ local function find_providers(file, root) | |||
489 | assert(type(file) == "string") | 134 | assert(type(file) == "string") |
490 | root = root or cfg.root_dir | 135 | root = root or cfg.root_dir |
491 | 136 | ||
492 | local manifest, err = manif_core.load_local_manifest(path.rocks_dir(root)) | 137 | local manifest, err = manif.load_local_manifest(path.rocks_dir(root)) |
493 | if not manifest then | 138 | if not manifest then |
494 | return nil, "untracked" | 139 | return nil, "untracked" |
495 | end | 140 | end |
@@ -535,7 +180,7 @@ end | |||
535 | function manif.find_conflicting_file(name, version, file, root) | 180 | function manif.find_conflicting_file(name, version, file, root) |
536 | root = root or cfg.root_dir | 181 | root = root or cfg.root_dir |
537 | 182 | ||
538 | local manifest = manif_core.load_local_manifest(path.rocks_dir(root)) | 183 | local manifest = manif.load_local_manifest(path.rocks_dir(root)) |
539 | if not manifest then | 184 | if not manifest then |
540 | return | 185 | return |
541 | end | 186 | end |
diff --git a/src/luarocks/manif/writer.lua b/src/luarocks/manif/writer.lua new file mode 100644 index 00000000..1eb5ee7c --- /dev/null +++ b/src/luarocks/manif/writer.lua | |||
@@ -0,0 +1,358 @@ | |||
1 | |||
2 | local writer = {} | ||
3 | |||
4 | local cfg = require("luarocks.core.cfg") | ||
5 | local search = require("luarocks.search") | ||
6 | local repos = require("luarocks.repos") | ||
7 | local deps = require("luarocks.deps") | ||
8 | local fs = require("luarocks.fs") | ||
9 | local util = require("luarocks.util") | ||
10 | local dir = require("luarocks.dir") | ||
11 | local fetch = require("luarocks.fetch") | ||
12 | local path = require("luarocks.path") | ||
13 | local persist = require("luarocks.persist") | ||
14 | local manif = require("luarocks.manif") | ||
15 | |||
16 | --- Output a table listing items of a package. | ||
17 | -- @param itemsfn function: a function for obtaining items of a package. | ||
18 | -- pkg and version will be passed to it; it should return a table with | ||
19 | -- items as keys. | ||
20 | -- @param pkg string: package name | ||
21 | -- @param version string: package version | ||
22 | -- @param tbl table: the package matching table: keys should be item names | ||
23 | -- and values arrays of strings with packages names in "name/version" format. | ||
24 | local function store_package_items(itemsfn, pkg, version, tbl) | ||
25 | assert(type(itemsfn) == "function") | ||
26 | assert(type(pkg) == "string") | ||
27 | assert(type(version) == "string") | ||
28 | assert(type(tbl) == "table") | ||
29 | |||
30 | local pkg_version = pkg.."/"..version | ||
31 | local result = {} | ||
32 | |||
33 | for item, path in pairs(itemsfn(pkg, version)) do | ||
34 | result[item] = path | ||
35 | if not tbl[item] then | ||
36 | tbl[item] = {} | ||
37 | end | ||
38 | table.insert(tbl[item], pkg_version) | ||
39 | end | ||
40 | return result | ||
41 | end | ||
42 | |||
43 | |||
44 | --- Process the dependencies of a manifest table to determine its dependency | ||
45 | -- chains for loading modules. The manifest dependencies information is filled | ||
46 | -- and any dependency inconsistencies or missing dependencies are reported to | ||
47 | -- standard error. | ||
48 | -- @param manifest table: a manifest table. | ||
49 | -- @param deps_mode string: Dependency mode: "one" for the current default tree, | ||
50 | -- "all" for all trees, "order" for all trees with priority >= the current default, | ||
51 | -- "none" for no trees. | ||
52 | local function update_dependencies(manifest, deps_mode) | ||
53 | assert(type(manifest) == "table") | ||
54 | assert(type(deps_mode) == "string") | ||
55 | |||
56 | for pkg, versions in pairs(manifest.repository) do | ||
57 | for version, repositories in pairs(versions) do | ||
58 | local current = pkg.." "..version | ||
59 | for _, repo in ipairs(repositories) do | ||
60 | if repo.arch == "installed" then | ||
61 | local missing | ||
62 | repo.dependencies, missing = deps.scan_deps({}, {}, manifest, pkg, version, deps_mode) | ||
63 | repo.dependencies[pkg] = nil | ||
64 | if missing then | ||
65 | for miss, err in pairs(missing) do | ||
66 | if miss == current then | ||
67 | util.printerr("Tree inconsistency detected: "..current.." has no rockspec. "..err) | ||
68 | elseif deps_mode ~= "none" then | ||
69 | util.printerr("Missing dependency for "..pkg.." "..version..": "..miss) | ||
70 | end | ||
71 | end | ||
72 | end | ||
73 | end | ||
74 | end | ||
75 | end | ||
76 | end | ||
77 | end | ||
78 | |||
79 | --- Sort function for ordering rock identifiers in a manifest's | ||
80 | -- modules table. Rocks are ordered alphabetically by name, and then | ||
81 | -- by version which greater first. | ||
82 | -- @param a string: Version to compare. | ||
83 | -- @param b string: Version to compare. | ||
84 | -- @return boolean: The comparison result, according to the | ||
85 | -- rule outlined above. | ||
86 | local function sort_pkgs(a, b) | ||
87 | assert(type(a) == "string") | ||
88 | assert(type(b) == "string") | ||
89 | |||
90 | local na, va = a:match("(.*)/(.*)$") | ||
91 | local nb, vb = b:match("(.*)/(.*)$") | ||
92 | |||
93 | return (na == nb) and deps.compare_versions(va, vb) or na < nb | ||
94 | end | ||
95 | |||
96 | --- Sort items of a package matching table by version number (higher versions first). | ||
97 | -- @param tbl table: the package matching table: keys should be strings | ||
98 | -- and values arrays of strings with packages names in "name/version" format. | ||
99 | local function sort_package_matching_table(tbl) | ||
100 | assert(type(tbl) == "table") | ||
101 | |||
102 | if next(tbl) then | ||
103 | for item, pkgs in pairs(tbl) do | ||
104 | if #pkgs > 1 then | ||
105 | table.sort(pkgs, sort_pkgs) | ||
106 | -- Remove duplicates from the sorted array. | ||
107 | local prev = nil | ||
108 | local i = 1 | ||
109 | while pkgs[i] do | ||
110 | local curr = pkgs[i] | ||
111 | if curr == prev then | ||
112 | table.remove(pkgs, i) | ||
113 | else | ||
114 | prev = curr | ||
115 | i = i + 1 | ||
116 | end | ||
117 | end | ||
118 | end | ||
119 | end | ||
120 | end | ||
121 | end | ||
122 | |||
123 | --- Filter manifest table by Lua version, removing rockspecs whose Lua version | ||
124 | -- does not match. | ||
125 | -- @param manifest table: a manifest table. | ||
126 | -- @param lua_version string or nil: filter by Lua version | ||
127 | -- @param repodir string: directory of repository being scanned | ||
128 | -- @param cache table: temporary rockspec cache table | ||
129 | local function filter_by_lua_version(manifest, lua_version, repodir, cache) | ||
130 | assert(type(manifest) == "table") | ||
131 | assert(type(repodir) == "string") | ||
132 | assert((not cache) or type(cache) == "table") | ||
133 | |||
134 | cache = cache or {} | ||
135 | lua_version = deps.parse_version(lua_version) | ||
136 | for pkg, versions in pairs(manifest.repository) do | ||
137 | local to_remove = {} | ||
138 | for version, repositories in pairs(versions) do | ||
139 | for _, repo in ipairs(repositories) do | ||
140 | if repo.arch == "rockspec" then | ||
141 | local pathname = dir.path(repodir, pkg.."-"..version..".rockspec") | ||
142 | local rockspec, err = cache[pathname] | ||
143 | if not rockspec then | ||
144 | rockspec, err = fetch.load_local_rockspec(pathname, true) | ||
145 | end | ||
146 | if rockspec then | ||
147 | cache[pathname] = rockspec | ||
148 | for _, dep in ipairs(rockspec.dependencies) do | ||
149 | if dep.name == "lua" then | ||
150 | if not deps.match_constraints(lua_version, dep.constraints) then | ||
151 | table.insert(to_remove, version) | ||
152 | end | ||
153 | break | ||
154 | end | ||
155 | end | ||
156 | else | ||
157 | util.printerr("Error loading rockspec for "..pkg.." "..version..": "..err) | ||
158 | end | ||
159 | end | ||
160 | end | ||
161 | end | ||
162 | if next(to_remove) then | ||
163 | for _, incompat in ipairs(to_remove) do | ||
164 | versions[incompat] = nil | ||
165 | end | ||
166 | if not next(versions) then | ||
167 | manifest.repository[pkg] = nil | ||
168 | end | ||
169 | end | ||
170 | end | ||
171 | end | ||
172 | |||
173 | --- Store search results in a manifest table. | ||
174 | -- @param results table: The search results as returned by search.disk_search. | ||
175 | -- @param manifest table: A manifest table (must contain repository, modules, commands tables). | ||
176 | -- It will be altered to include the search results. | ||
177 | -- @param dep_handler: dependency handler function | ||
178 | -- @return boolean or (nil, string): true in case of success, or nil followed by an error message. | ||
179 | local function store_results(results, manifest, dep_handler) | ||
180 | assert(type(results) == "table") | ||
181 | assert(type(manifest) == "table") | ||
182 | assert((not dep_handler) or type(dep_handler) == "function") | ||
183 | |||
184 | for name, versions in pairs(results) do | ||
185 | local pkgtable = manifest.repository[name] or {} | ||
186 | for version, entries in pairs(versions) do | ||
187 | local versiontable = {} | ||
188 | for _, entry in ipairs(entries) do | ||
189 | local entrytable = {} | ||
190 | entrytable.arch = entry.arch | ||
191 | if entry.arch == "installed" then | ||
192 | local rock_manifest, err = manif.load_rock_manifest(name, version) | ||
193 | if not rock_manifest then return nil, err end | ||
194 | entrytable.modules = store_package_items(repos.package_modules, name, version, manifest.modules) | ||
195 | entrytable.commands = store_package_items(repos.package_commands, name, version, manifest.commands) | ||
196 | end | ||
197 | table.insert(versiontable, entrytable) | ||
198 | end | ||
199 | pkgtable[version] = versiontable | ||
200 | end | ||
201 | manifest.repository[name] = pkgtable | ||
202 | end | ||
203 | if dep_handler then | ||
204 | dep_handler(manifest) | ||
205 | end | ||
206 | sort_package_matching_table(manifest.modules) | ||
207 | sort_package_matching_table(manifest.commands) | ||
208 | return true | ||
209 | end | ||
210 | |||
211 | --- Commit a table to disk in given local path. | ||
212 | -- @param where string: The directory where the table should be saved. | ||
213 | -- @param name string: The filename. | ||
214 | -- @param tbl table: The table to be saved. | ||
215 | -- @return boolean or (nil, string): true if successful, or nil and a | ||
216 | -- message in case of errors. | ||
217 | local function save_table(where, name, tbl) | ||
218 | assert(type(where) == "string") | ||
219 | assert(type(name) == "string") | ||
220 | assert(type(tbl) == "table") | ||
221 | |||
222 | local filename = dir.path(where, name) | ||
223 | local ok, err = persist.save_from_table(filename..".tmp", tbl) | ||
224 | if ok then | ||
225 | ok, err = fs.replace_file(filename, filename..".tmp") | ||
226 | end | ||
227 | return ok, err | ||
228 | end | ||
229 | |||
230 | function writer.make_rock_manifest(name, version) | ||
231 | local install_dir = path.install_dir(name, version) | ||
232 | local tree = {} | ||
233 | for _, file in ipairs(fs.find(install_dir)) do | ||
234 | local full_path = dir.path(install_dir, file) | ||
235 | local walk = tree | ||
236 | local last | ||
237 | local last_name | ||
238 | for filename in file:gmatch("[^/]+") do | ||
239 | local next = walk[filename] | ||
240 | if not next then | ||
241 | next = {} | ||
242 | walk[filename] = next | ||
243 | end | ||
244 | last = walk | ||
245 | last_name = filename | ||
246 | walk = next | ||
247 | end | ||
248 | if fs.is_file(full_path) then | ||
249 | local sum, err = fs.get_md5(full_path) | ||
250 | if not sum then | ||
251 | return nil, "Failed producing checksum: "..tostring(err) | ||
252 | end | ||
253 | last[last_name] = sum | ||
254 | end | ||
255 | end | ||
256 | local rock_manifest = { rock_manifest=tree } | ||
257 | manif.rock_manifest_cache[name.."/"..version] = rock_manifest | ||
258 | save_table(install_dir, "rock_manifest", rock_manifest ) | ||
259 | end | ||
260 | |||
261 | --- Scan a LuaRocks repository and output a manifest file. | ||
262 | -- A file called 'manifest' will be written in the root of the given | ||
263 | -- repository directory. | ||
264 | -- @param repo A local repository directory. | ||
265 | -- @param deps_mode string: Dependency mode: "one" for the current default tree, | ||
266 | -- "all" for all trees, "order" for all trees with priority >= the current default, | ||
267 | -- "none" for the default dependency mode from the configuration. | ||
268 | -- @param versioned boolean: if versioned versions of the manifest should be created. | ||
269 | -- @return boolean or (nil, string): True if manifest was generated, | ||
270 | -- or nil and an error message. | ||
271 | function writer.make_manifest(repo, deps_mode, remote) | ||
272 | assert(type(repo) == "string") | ||
273 | assert(type(deps_mode) == "string") | ||
274 | |||
275 | if deps_mode == "none" then deps_mode = cfg.deps_mode end | ||
276 | |||
277 | if not fs.is_dir(repo) then | ||
278 | return nil, "Cannot access repository at "..repo | ||
279 | end | ||
280 | |||
281 | local query = search.make_query("") | ||
282 | query.exact_name = false | ||
283 | query.arch = "any" | ||
284 | local results = search.disk_search(repo, query) | ||
285 | local manifest = { repository = {}, modules = {}, commands = {} } | ||
286 | |||
287 | manif.cache_manifest(repo, nil, manifest) | ||
288 | |||
289 | local dep_handler = nil | ||
290 | if not remote then | ||
291 | dep_handler = function(manifest) | ||
292 | update_dependencies(manifest, deps_mode) | ||
293 | end | ||
294 | end | ||
295 | local ok, err = store_results(results, manifest, dep_handler) | ||
296 | if not ok then return nil, err end | ||
297 | |||
298 | if remote then | ||
299 | local cache = {} | ||
300 | for luaver in util.lua_versions() do | ||
301 | local vmanifest = { repository = {}, modules = {}, commands = {} } | ||
302 | local dep_handler = function(manifest) | ||
303 | filter_by_lua_version(manifest, luaver, repo, cache) | ||
304 | end | ||
305 | store_results(results, vmanifest, dep_handler) | ||
306 | save_table(repo, "manifest-"..luaver, vmanifest) | ||
307 | end | ||
308 | end | ||
309 | |||
310 | return save_table(repo, "manifest", manifest) | ||
311 | end | ||
312 | |||
313 | --- Load a manifest file from a local repository and add to the repository | ||
314 | -- information with regard to the given name and version. | ||
315 | -- A file called 'manifest' will be written in the root of the given | ||
316 | -- repository directory. | ||
317 | -- @param name string: Name of a package from the repository. | ||
318 | -- @param version string: Version of a package from the repository. | ||
319 | -- @param repo string or nil: Pathname of a local repository. If not given, | ||
320 | -- the default local repository is used. | ||
321 | -- @param deps_mode string: Dependency mode: "one" for the current default tree, | ||
322 | -- "all" for all trees, "order" for all trees with priority >= the current default, | ||
323 | -- "none" for using the default dependency mode from the configuration. | ||
324 | -- @return boolean or (nil, string): True if manifest was generated, | ||
325 | -- or nil and an error message. | ||
326 | function writer.update_manifest(name, version, repo, deps_mode) | ||
327 | assert(type(name) == "string") | ||
328 | assert(type(version) == "string") | ||
329 | repo = path.rocks_dir(repo or cfg.root_dir) | ||
330 | assert(type(deps_mode) == "string") | ||
331 | |||
332 | if deps_mode == "none" then deps_mode = cfg.deps_mode end | ||
333 | |||
334 | local manifest, err = manif.load_manifest(repo) | ||
335 | if not manifest then | ||
336 | util.printerr("No existing manifest. Attempting to rebuild...") | ||
337 | local ok, err = writer.make_manifest(repo, deps_mode) | ||
338 | if not ok then | ||
339 | return nil, err | ||
340 | end | ||
341 | manifest, err = manif.load_manifest(repo) | ||
342 | if not manifest then | ||
343 | return nil, err | ||
344 | end | ||
345 | end | ||
346 | |||
347 | local results = {[name] = {[version] = {{arch = "installed", repo = repo}}}} | ||
348 | |||
349 | local dep_handler = function(manifest) | ||
350 | update_dependencies(manifest, deps_mode) | ||
351 | end | ||
352 | local ok, err = store_results(results, manifest, dep_handler) | ||
353 | if not ok then return nil, err end | ||
354 | |||
355 | return save_table(repo, "manifest", manifest) | ||
356 | end | ||
357 | |||
358 | return writer | ||
diff --git a/src/luarocks/new_version.lua b/src/luarocks/new_version.lua index 8fc00a86..000b459b 100644 --- a/src/luarocks/new_version.lua +++ b/src/luarocks/new_version.lua | |||
@@ -10,7 +10,6 @@ local persist = require("luarocks.persist") | |||
10 | local fs = require("luarocks.fs") | 10 | local fs = require("luarocks.fs") |
11 | local type_check = require("luarocks.type_check") | 11 | local type_check = require("luarocks.type_check") |
12 | 12 | ||
13 | util.add_run_function(new_version) | ||
14 | new_version.help_summary = "Auto-write a rockspec for a new version of a rock." | 13 | new_version.help_summary = "Auto-write a rockspec for a new version of a rock." |
15 | new_version.help_arguments = "[--tag=<tag>] [<package>|<rockspec>] [<new_version>] [<new_url>]" | 14 | new_version.help_arguments = "[--tag=<tag>] [<package>|<rockspec>] [<new_version>] [<new_url>]" |
16 | new_version.help = [[ | 15 | new_version.help = [[ |
diff --git a/src/luarocks/pack.lua b/src/luarocks/pack.lua index 932ff0d5..49c68e85 100644 --- a/src/luarocks/pack.lua +++ b/src/luarocks/pack.lua | |||
@@ -2,7 +2,6 @@ | |||
2 | --- Module implementing the LuaRocks "pack" command. | 2 | --- Module implementing the LuaRocks "pack" command. |
3 | -- Creates a rock, packing sources or binaries. | 3 | -- Creates a rock, packing sources or binaries. |
4 | local pack = {} | 4 | local pack = {} |
5 | package.loaded["luarocks.pack"] = pack | ||
6 | 5 | ||
7 | local unpack = unpack or table.unpack | 6 | local unpack = unpack or table.unpack |
8 | 7 | ||
@@ -10,13 +9,12 @@ local path = require("luarocks.path") | |||
10 | local repos = require("luarocks.repos") | 9 | local repos = require("luarocks.repos") |
11 | local fetch = require("luarocks.fetch") | 10 | local fetch = require("luarocks.fetch") |
12 | local fs = require("luarocks.fs") | 11 | local fs = require("luarocks.fs") |
13 | local cfg = require("luarocks.cfg") | 12 | local cfg = require("luarocks.core.cfg") |
14 | local util = require("luarocks.util") | 13 | local util = require("luarocks.util") |
15 | local dir = require("luarocks.dir") | 14 | local dir = require("luarocks.dir") |
16 | local manif = require("luarocks.manif") | 15 | local manif = require("luarocks.manif") |
17 | local search = require("luarocks.search") | 16 | local search = require("luarocks.search") |
18 | 17 | ||
19 | util.add_run_function(pack) | ||
20 | pack.help_summary = "Create a rock, packing sources or binaries." | 18 | pack.help_summary = "Create a rock, packing sources or binaries." |
21 | pack.help_arguments = "{<rockspec>|<name> [<version>]}" | 19 | pack.help_arguments = "{<rockspec>|<name> [<version>]}" |
22 | pack.help = [[ | 20 | pack.help = [[ |
@@ -104,10 +102,8 @@ local function do_pack_binary_rock(name, version, tree) | |||
104 | return nil, "'"..name.." "..version.."' does not seem to be an installed rock." | 102 | return nil, "'"..name.." "..version.."' does not seem to be an installed rock." |
105 | end | 103 | end |
106 | 104 | ||
107 | local rock_manifest = manif.load_rock_manifest(name, version, root) | 105 | local rock_manifest, err = manif.load_rock_manifest(name, version, root) |
108 | if not rock_manifest then | 106 | if not rock_manifest then return nil, err end |
109 | return nil, "rock_manifest file not found for "..name.." "..version.." - not a LuaRocks 2 tree?" | ||
110 | end | ||
111 | 107 | ||
112 | local name_version = name .. "-" .. version | 108 | local name_version = name .. "-" .. version |
113 | local rock_file = fs.absolute_name(name_version .. "."..cfg.arch..".rock") | 109 | local rock_file = fs.absolute_name(name_version .. "."..cfg.arch..".rock") |
diff --git a/src/luarocks/path.lua b/src/luarocks/path.lua index dafc64e7..83e9c295 100644 --- a/src/luarocks/path.lua +++ b/src/luarocks/path.lua | |||
@@ -3,9 +3,10 @@ | |||
3 | -- All paths are configured in this module, making it a single | 3 | -- All paths are configured in this module, making it a single |
4 | -- point where the layout of the local installation is defined in LuaRocks. | 4 | -- point where the layout of the local installation is defined in LuaRocks. |
5 | local path = {} | 5 | local path = {} |
6 | setmetatable(path, { __index = require("luarocks.core.path") }) | ||
6 | 7 | ||
7 | local dir = require("luarocks.dir") | 8 | local dir = require("luarocks.dir") |
8 | local cfg = require("luarocks.cfg") | 9 | local cfg = require("luarocks.core.cfg") |
9 | local util = require("luarocks.util") | 10 | local util = require("luarocks.util") |
10 | 11 | ||
11 | --- Infer rockspec filename from a rock filename. | 12 | --- Infer rockspec filename from a rock filename. |
@@ -17,29 +18,11 @@ function path.rockspec_name_from_rock(rock_name) | |||
17 | return base_name:match("(.*)%.[^.]*.rock") .. ".rockspec" | 18 | return base_name:match("(.*)%.[^.]*.rock") .. ".rockspec" |
18 | end | 19 | end |
19 | 20 | ||
20 | function path.rocks_dir(tree) | ||
21 | if type(tree) == "string" then | ||
22 | return dir.path(tree, cfg.rocks_subdir) | ||
23 | else | ||
24 | assert(type(tree) == "table") | ||
25 | return tree.rocks_dir or dir.path(tree.root, cfg.rocks_subdir) | ||
26 | end | ||
27 | end | ||
28 | |||
29 | function path.root_dir(rocks_dir) | 21 | function path.root_dir(rocks_dir) |
30 | assert(type(rocks_dir) == "string") | 22 | assert(type(rocks_dir) == "string") |
31 | return rocks_dir:match("(.*)" .. util.matchquote(cfg.rocks_subdir) .. ".*$") | 23 | return rocks_dir:match("(.*)" .. util.matchquote(cfg.rocks_subdir) .. ".*$") |
32 | end | 24 | end |
33 | 25 | ||
34 | function path.rocks_tree_to_string(tree) | ||
35 | if type(tree) == "string" then | ||
36 | return tree | ||
37 | else | ||
38 | assert(type(tree) == "table") | ||
39 | return tree.root | ||
40 | end | ||
41 | end | ||
42 | |||
43 | function path.deploy_bin_dir(tree) | 26 | function path.deploy_bin_dir(tree) |
44 | if type(tree) == "string" then | 27 | if type(tree) == "string" then |
45 | return dir.path(tree, "bin") | 28 | return dir.path(tree, "bin") |
@@ -49,24 +32,6 @@ function path.deploy_bin_dir(tree) | |||
49 | end | 32 | end |
50 | end | 33 | end |
51 | 34 | ||
52 | function path.deploy_lua_dir(tree) | ||
53 | if type(tree) == "string" then | ||
54 | return dir.path(tree, cfg.lua_modules_path) | ||
55 | else | ||
56 | assert(type(tree) == "table") | ||
57 | return tree.lua_dir or dir.path(tree.root, cfg.lua_modules_path) | ||
58 | end | ||
59 | end | ||
60 | |||
61 | function path.deploy_lib_dir(tree) | ||
62 | if type(tree) == "string" then | ||
63 | return dir.path(tree, cfg.lib_modules_path) | ||
64 | else | ||
65 | assert(type(tree) == "table") | ||
66 | return tree.lib_dir or dir.path(tree.root, cfg.lib_modules_path) | ||
67 | end | ||
68 | end | ||
69 | |||
70 | function path.manifest_file(tree) | 35 | function path.manifest_file(tree) |
71 | if type(tree) == "string" then | 36 | if type(tree) == "string" then |
72 | return dir.path(tree, cfg.rocks_subdir, "manifest") | 37 | return dir.path(tree, cfg.rocks_subdir, "manifest") |
@@ -229,34 +194,6 @@ function path.make_url(pathname, name, version, arch) | |||
229 | return dir.path(pathname, filename) | 194 | return dir.path(pathname, filename) |
230 | end | 195 | end |
231 | 196 | ||
232 | --- Convert a pathname to a module identifier. | ||
233 | -- In Unix, for example, a path "foo/bar/baz.lua" is converted to | ||
234 | -- "foo.bar.baz"; "bla/init.lua" returns "bla"; "foo.so" returns "foo". | ||
235 | -- @param file string: Pathname of module | ||
236 | -- @return string: The module identifier, or nil if given path is | ||
237 | -- not a conformant module path (the function does not check if the | ||
238 | -- path actually exists). | ||
239 | function path.path_to_module(file) | ||
240 | assert(type(file) == "string") | ||
241 | |||
242 | local name = file:match("(.*)%."..cfg.lua_extension.."$") | ||
243 | if name then | ||
244 | name = name:gsub(dir.separator, ".") | ||
245 | local init = name:match("(.*)%.init$") | ||
246 | if init then | ||
247 | name = init | ||
248 | end | ||
249 | else | ||
250 | name = file:match("(.*)%."..cfg.lib_extension.."$") | ||
251 | if name then | ||
252 | name = name:gsub(dir.separator, ".") | ||
253 | end | ||
254 | end | ||
255 | if not name then name = file end | ||
256 | name = name:gsub("^%.+", ""):gsub("%.+$", "") | ||
257 | return name | ||
258 | end | ||
259 | |||
260 | --- Obtain the directory name where a module should be stored. | 197 | --- Obtain the directory name where a module should be stored. |
261 | -- For example, on Unix, "foo.bar.baz" will return "foo/bar". | 198 | -- For example, on Unix, "foo.bar.baz" will return "foo/bar". |
262 | -- @param mod string: A module name in Lua dot-separated format. | 199 | -- @param mod string: A module name in Lua dot-separated format. |
@@ -286,22 +223,6 @@ function path.configure_paths(rockspec) | |||
286 | rockspec.variables = vars | 223 | rockspec.variables = vars |
287 | end | 224 | end |
288 | 225 | ||
289 | --- Produce a versioned version of a filename. | ||
290 | -- @param file string: filename (must start with prefix) | ||
291 | -- @param prefix string: Path prefix for file | ||
292 | -- @param name string: Rock name | ||
293 | -- @param version string: Rock version | ||
294 | -- @return string: a pathname with the same directory parts and a versioned basename. | ||
295 | function path.versioned_name(file, prefix, name, version) | ||
296 | assert(type(file) == "string") | ||
297 | assert(type(name) == "string") | ||
298 | assert(type(version) == "string") | ||
299 | |||
300 | local rest = file:sub(#prefix+1):gsub("^/*", "") | ||
301 | local name_version = (name.."_"..version):gsub("%-", "_"):gsub("%.", "_") | ||
302 | return dir.path(prefix, name_version.."-"..rest) | ||
303 | end | ||
304 | |||
305 | function path.use_tree(tree) | 226 | function path.use_tree(tree) |
306 | cfg.root_dir = tree | 227 | cfg.root_dir = tree |
307 | cfg.rocks_dir = path.rocks_dir(tree) | 228 | cfg.rocks_dir = path.rocks_dir(tree) |
@@ -310,60 +231,6 @@ function path.use_tree(tree) | |||
310 | cfg.deploy_lib_dir = path.deploy_lib_dir(tree) | 231 | cfg.deploy_lib_dir = path.deploy_lib_dir(tree) |
311 | end | 232 | end |
312 | 233 | ||
313 | --- Apply a given function to the active rocks trees based on chosen dependency mode. | ||
314 | -- @param deps_mode string: Dependency mode: "one" for the current default tree, | ||
315 | -- "all" for all trees, "order" for all trees with priority >= the current default, | ||
316 | -- "none" for no trees (this function becomes a nop). | ||
317 | -- @param fn function: function to be applied, with the tree dir (string) as the first | ||
318 | -- argument and the remaining varargs of map_trees as the following arguments. | ||
319 | -- @return a table with all results of invocations of fn collected. | ||
320 | function path.map_trees(deps_mode, fn, ...) | ||
321 | local result = {} | ||
322 | if deps_mode == "one" then | ||
323 | table.insert(result, (fn(cfg.root_dir, ...)) or 0) | ||
324 | elseif deps_mode == "all" or deps_mode == "order" then | ||
325 | local use = false | ||
326 | if deps_mode == "all" then | ||
327 | use = true | ||
328 | end | ||
329 | for _, tree in ipairs(cfg.rocks_trees) do | ||
330 | if dir.normalize(path.rocks_tree_to_string(tree)) == dir.normalize(path.rocks_tree_to_string(cfg.root_dir)) then | ||
331 | use = true | ||
332 | end | ||
333 | if use then | ||
334 | table.insert(result, (fn(tree, ...)) or 0) | ||
335 | end | ||
336 | end | ||
337 | end | ||
338 | return result | ||
339 | end | ||
340 | |||
341 | local is_src_extension = { [".lua"] = true, [".tl"] = true, [".tld"] = true, [".moon"] = true } | ||
342 | |||
343 | --- Return the pathname of the file that would be loaded for a module, indexed. | ||
344 | -- @param file_name string: module file name as in manifest (eg. "socket/core.so") | ||
345 | -- @param name string: name of the package (eg. "luasocket") | ||
346 | -- @param version string: version number (eg. "2.0.2-1") | ||
347 | -- @param tree string: repository path (eg. "/usr/local") | ||
348 | -- @param i number: the index, 1 if version is the current default, > 1 otherwise. | ||
349 | -- This is done this way for use by select_module in luarocks.loader. | ||
350 | -- @return string: filename of the module (eg. "/usr/local/lib/lua/5.1/socket/core.so") | ||
351 | function path.which_i(file_name, name, version, tree, i) | ||
352 | local deploy_dir | ||
353 | local extension = file_name:match("%.[a-z]+$") | ||
354 | if is_src_extension[extension] then | ||
355 | deploy_dir = path.deploy_lua_dir(tree) | ||
356 | file_name = dir.path(deploy_dir, file_name) | ||
357 | else | ||
358 | deploy_dir = path.deploy_lib_dir(tree) | ||
359 | file_name = dir.path(deploy_dir, file_name) | ||
360 | end | ||
361 | if i > 1 then | ||
362 | file_name = path.versioned_name(file_name, deploy_dir, name, version) | ||
363 | end | ||
364 | return file_name | ||
365 | end | ||
366 | |||
367 | --- Return the pathname of the file that would be loaded for a module, | 234 | --- Return the pathname of the file that would be loaded for a module, |
368 | -- returning the versioned pathname if given version is not the default version | 235 | -- returning the versioned pathname if given version is not the default version |
369 | -- in the given manifest. | 236 | -- in the given manifest. |
diff --git a/src/luarocks/path_cmd.lua b/src/luarocks/path_cmd.lua index 15fb9ca2..1f5bb07f 100644 --- a/src/luarocks/path_cmd.lua +++ b/src/luarocks/path_cmd.lua | |||
@@ -5,9 +5,8 @@ local path_cmd = {} | |||
5 | 5 | ||
6 | local util = require("luarocks.util") | 6 | local util = require("luarocks.util") |
7 | local deps = require("luarocks.deps") | 7 | local deps = require("luarocks.deps") |
8 | local cfg = require("luarocks.cfg") | 8 | local cfg = require("luarocks.core.cfg") |
9 | 9 | ||
10 | util.add_run_function(path_cmd) | ||
11 | path_cmd.help_summary = "Return the currently configured package path." | 10 | path_cmd.help_summary = "Return the currently configured package path." |
12 | path_cmd.help_arguments = "" | 11 | path_cmd.help_arguments = "" |
13 | path_cmd.help = [[ | 12 | path_cmd.help = [[ |
diff --git a/src/luarocks/persist.lua b/src/luarocks/persist.lua index c2adb570..16ff5065 100644 --- a/src/luarocks/persist.lua +++ b/src/luarocks/persist.lua | |||
@@ -4,83 +4,10 @@ | |||
4 | -- Implemented separately to avoid interdependencies, | 4 | -- Implemented separately to avoid interdependencies, |
5 | -- as it is used in the bootstrapping stage of the cfg module. | 5 | -- as it is used in the bootstrapping stage of the cfg module. |
6 | local persist = {} | 6 | local persist = {} |
7 | package.loaded["luarocks.persist"] = persist | 7 | setmetatable(persist, { __index = require("luarocks.core.persist") }) |
8 | 8 | ||
9 | local util = require("luarocks.util") | 9 | local util = require("luarocks.util") |
10 | 10 | ||
11 | --- Load and run a Lua file in an environment. | ||
12 | -- @param filename string: the name of the file. | ||
13 | -- @param env table: the environment table. | ||
14 | -- @return (true, any) or (nil, string, string): true and the return value | ||
15 | -- of the file, or nil, an error message and an error code ("open", "load" | ||
16 | -- or "run") in case of errors. | ||
17 | local function run_file(filename, env) | ||
18 | local fd, err = io.open(filename) | ||
19 | if not fd then | ||
20 | return nil, err, "open" | ||
21 | end | ||
22 | local str, err = fd:read("*a") | ||
23 | fd:close() | ||
24 | if not str then | ||
25 | return nil, err, "open" | ||
26 | end | ||
27 | str = str:gsub("^#![^\n]*\n", "") | ||
28 | local chunk, ran | ||
29 | if _VERSION == "Lua 5.1" then -- Lua 5.1 | ||
30 | chunk, err = loadstring(str, filename) | ||
31 | if chunk then | ||
32 | setfenv(chunk, env) | ||
33 | ran, err = pcall(chunk) | ||
34 | end | ||
35 | else -- Lua 5.2 | ||
36 | chunk, err = load(str, filename, "t", env) | ||
37 | if chunk then | ||
38 | ran, err = pcall(chunk) | ||
39 | end | ||
40 | end | ||
41 | if not chunk then | ||
42 | return nil, "Error loading file: "..err, "load" | ||
43 | end | ||
44 | if not ran then | ||
45 | return nil, "Error running file: "..err, "run" | ||
46 | end | ||
47 | return true, err | ||
48 | end | ||
49 | |||
50 | --- Load a Lua file containing assignments, storing them in a table. | ||
51 | -- The global environment is not propagated to the loaded file. | ||
52 | -- @param filename string: the name of the file. | ||
53 | -- @param tbl table or nil: if given, this table is used to store | ||
54 | -- loaded values. | ||
55 | -- @return (table, table) or (nil, string, string): a table with the file's assignments | ||
56 | -- as fields and set of undefined globals accessed in file, | ||
57 | -- or nil, an error message and an error code ("open"; couldn't open the file, | ||
58 | -- "load"; compile-time error, or "run"; run-time error) | ||
59 | -- in case of errors. | ||
60 | function persist.load_into_table(filename, tbl) | ||
61 | assert(type(filename) == "string") | ||
62 | assert(type(tbl) == "table" or not tbl) | ||
63 | |||
64 | local result = tbl or {} | ||
65 | local globals = {} | ||
66 | local globals_mt = { | ||
67 | __index = function(t, k) | ||
68 | globals[k] = true | ||
69 | end | ||
70 | } | ||
71 | local save_mt = getmetatable(result) | ||
72 | setmetatable(result, globals_mt) | ||
73 | |||
74 | local ok, err, errcode = run_file(filename, result) | ||
75 | |||
76 | setmetatable(result, save_mt) | ||
77 | |||
78 | if not ok then | ||
79 | return nil, err, errcode | ||
80 | end | ||
81 | return result, globals | ||
82 | end | ||
83 | |||
84 | local write_table | 11 | local write_table |
85 | 12 | ||
86 | --- Write a value as Lua code. | 13 | --- Write a value as Lua code. |
diff --git a/src/luarocks/purge.lua b/src/luarocks/purge.lua index 17724e84..50f290c8 100644 --- a/src/luarocks/purge.lua +++ b/src/luarocks/purge.lua | |||
@@ -2,7 +2,6 @@ | |||
2 | --- Module implementing the LuaRocks "purge" command. | 2 | --- Module implementing the LuaRocks "purge" command. |
3 | -- Remove all rocks from a given tree. | 3 | -- Remove all rocks from a given tree. |
4 | local purge = {} | 4 | local purge = {} |
5 | package.loaded["luarocks.purge"] = purge | ||
6 | 5 | ||
7 | local util = require("luarocks.util") | 6 | local util = require("luarocks.util") |
8 | local fs = require("luarocks.fs") | 7 | local fs = require("luarocks.fs") |
@@ -10,11 +9,10 @@ local path = require("luarocks.path") | |||
10 | local search = require("luarocks.search") | 9 | local search = require("luarocks.search") |
11 | local deps = require("luarocks.deps") | 10 | local deps = require("luarocks.deps") |
12 | local repos = require("luarocks.repos") | 11 | local repos = require("luarocks.repos") |
13 | local manif = require("luarocks.manif") | 12 | local writer = require("luarocks.manif.writer") |
14 | local cfg = require("luarocks.cfg") | 13 | local cfg = require("luarocks.core.cfg") |
15 | local remove = require("luarocks.remove") | 14 | local remove = require("luarocks.remove") |
16 | 15 | ||
17 | util.add_run_function(purge) | ||
18 | purge.help_summary = "Remove all installed rocks from a tree." | 16 | purge.help_summary = "Remove all installed rocks from a tree." |
19 | purge.help_arguments = "--tree=<tree> [--old-versions]" | 17 | purge.help_arguments = "--tree=<tree> [--old-versions]" |
20 | purge.help = [[ | 18 | purge.help = [[ |
@@ -56,7 +54,7 @@ function purge.command(flags) | |||
56 | end | 54 | end |
57 | 55 | ||
58 | for package, versions in util.sortedpairs(results) do | 56 | for package, versions in util.sortedpairs(results) do |
59 | for version, repositories in util.sortedpairs(versions, sort) do | 57 | for version, _ in util.sortedpairs(versions, sort) do |
60 | if flags["old-versions"] then | 58 | if flags["old-versions"] then |
61 | util.printout("Keeping "..package.." "..version.."...") | 59 | util.printout("Keeping "..package.." "..version.."...") |
62 | local ok, err = remove.remove_other_versions(package, version, flags["force"], flags["force-fast"]) | 60 | local ok, err = remove.remove_other_versions(package, version, flags["force"], flags["force-fast"]) |
@@ -73,7 +71,7 @@ function purge.command(flags) | |||
73 | end | 71 | end |
74 | end | 72 | end |
75 | end | 73 | end |
76 | return manif.make_manifest(cfg.rocks_dir, "one") | 74 | return writer.make_manifest(cfg.rocks_dir, "one") |
77 | end | 75 | end |
78 | 76 | ||
79 | return purge | 77 | return purge |
diff --git a/src/luarocks/remove.lua b/src/luarocks/remove.lua index 82c9818b..008d2616 100644 --- a/src/luarocks/remove.lua +++ b/src/luarocks/remove.lua | |||
@@ -2,7 +2,6 @@ | |||
2 | --- Module implementing the LuaRocks "remove" command. | 2 | --- Module implementing the LuaRocks "remove" command. |
3 | -- Uninstalls rocks. | 3 | -- Uninstalls rocks. |
4 | local remove = {} | 4 | local remove = {} |
5 | package.loaded["luarocks.remove"] = remove | ||
6 | 5 | ||
7 | local search = require("luarocks.search") | 6 | local search = require("luarocks.search") |
8 | local deps = require("luarocks.deps") | 7 | local deps = require("luarocks.deps") |
@@ -10,11 +9,10 @@ local fetch = require("luarocks.fetch") | |||
10 | local repos = require("luarocks.repos") | 9 | local repos = require("luarocks.repos") |
11 | local path = require("luarocks.path") | 10 | local path = require("luarocks.path") |
12 | local util = require("luarocks.util") | 11 | local util = require("luarocks.util") |
13 | local cfg = require("luarocks.cfg") | 12 | local cfg = require("luarocks.core.cfg") |
14 | local manif = require("luarocks.manif") | 13 | local writer = require("luarocks.manif.writer") |
15 | local fs = require("luarocks.fs") | 14 | local fs = require("luarocks.fs") |
16 | 15 | ||
17 | util.add_run_function(remove) | ||
18 | remove.help_summary = "Uninstall a rock." | 16 | remove.help_summary = "Uninstall a rock." |
19 | remove.help_arguments = "[--force|--force-fast] <name> [<version>]" | 17 | remove.help_arguments = "[--force|--force-fast] <name> [<version>]" |
20 | remove.help = [[ | 18 | remove.help = [[ |
diff --git a/src/luarocks/repos.lua b/src/luarocks/repos.lua index 161cdd8a..6de844d2 100644 --- a/src/luarocks/repos.lua +++ b/src/luarocks/repos.lua | |||
@@ -1,11 +1,10 @@ | |||
1 | 1 | ||
2 | --- Functions for managing the repository on disk. | 2 | --- Functions for managing the repository on disk. |
3 | local repos = {} | 3 | local repos = {} |
4 | package.loaded["luarocks.repos"] = repos | ||
5 | 4 | ||
6 | local fs = require("luarocks.fs") | 5 | local fs = require("luarocks.fs") |
7 | local path = require("luarocks.path") | 6 | local path = require("luarocks.path") |
8 | local cfg = require("luarocks.cfg") | 7 | local cfg = require("luarocks.core.cfg") |
9 | local util = require("luarocks.util") | 8 | local util = require("luarocks.util") |
10 | local dir = require("luarocks.dir") | 9 | local dir = require("luarocks.dir") |
11 | local manif = require("luarocks.manif") | 10 | local manif = require("luarocks.manif") |
@@ -79,6 +78,7 @@ function repos.package_modules(package, version) | |||
79 | 78 | ||
80 | local result = {} | 79 | local result = {} |
81 | local rock_manifest = manif.load_rock_manifest(package, version) | 80 | local rock_manifest = manif.load_rock_manifest(package, version) |
81 | if not rock_manifest then return result end | ||
82 | store_package_data(result, package, rock_manifest.lib) | 82 | store_package_data(result, package, rock_manifest.lib) |
83 | store_package_data(result, package, rock_manifest.lua) | 83 | store_package_data(result, package, rock_manifest.lua) |
84 | return result | 84 | return result |
@@ -98,6 +98,7 @@ function repos.package_commands(package, version) | |||
98 | 98 | ||
99 | local result = {} | 99 | local result = {} |
100 | local rock_manifest = manif.load_rock_manifest(package, version) | 100 | local rock_manifest = manif.load_rock_manifest(package, version) |
101 | if not rock_manifest then return result end | ||
101 | store_package_data(result, package, rock_manifest.bin) | 102 | store_package_data(result, package, rock_manifest.bin) |
102 | return result | 103 | return result |
103 | end | 104 | end |
@@ -113,7 +114,7 @@ function repos.has_binaries(name, version) | |||
113 | assert(type(version) == "string") | 114 | assert(type(version) == "string") |
114 | 115 | ||
115 | local rock_manifest = manif.load_rock_manifest(name, version) | 116 | local rock_manifest = manif.load_rock_manifest(name, version) |
116 | if rock_manifest.bin then | 117 | if rock_manifest and rock_manifest.bin then |
117 | for name, md5 in pairs(rock_manifest.bin) do | 118 | for name, md5 in pairs(rock_manifest.bin) do |
118 | -- TODO verify that it is the same file. If it isn't, find the actual command. | 119 | -- TODO verify that it is the same file. If it isn't, find the actual command. |
119 | if fs.is_actual_binary(dir.path(cfg.deploy_bin_dir, name)) then | 120 | if fs.is_actual_binary(dir.path(cfg.deploy_bin_dir, name)) then |
@@ -241,7 +242,7 @@ function repos.deploy_files(name, version, wrap_bin_scripts, deps_mode) | |||
241 | 242 | ||
242 | local function deploy_file_tree(file_tree, path_fn, deploy_dir, move_fn, suffix) | 243 | local function deploy_file_tree(file_tree, path_fn, deploy_dir, move_fn, suffix) |
243 | local source_dir = path_fn(name, version) | 244 | local source_dir = path_fn(name, version) |
244 | return recurse_rock_manifest_tree(file_tree, | 245 | return recurse_rock_manifest_tree(file_tree, |
245 | function(parent_path, parent_module, file) | 246 | function(parent_path, parent_module, file) |
246 | local source = dir.path(source_dir, parent_path, file) | 247 | local source = dir.path(source_dir, parent_path, file) |
247 | local target = dir.path(deploy_dir, parent_path, file) | 248 | local target = dir.path(deploy_dir, parent_path, file) |
@@ -279,7 +280,8 @@ function repos.deploy_files(name, version, wrap_bin_scripts, deps_mode) | |||
279 | ) | 280 | ) |
280 | end | 281 | end |
281 | 282 | ||
282 | local rock_manifest = manif.load_rock_manifest(name, version) | 283 | local rock_manifest, err = manif.load_rock_manifest(name, version) |
284 | if not rock_manifest then return nil, err end | ||
283 | 285 | ||
284 | local function install_binary(source, target, name, version) | 286 | local function install_binary(source, target, name, version) |
285 | if wrap_bin_scripts and fs.is_lua(source) then | 287 | if wrap_bin_scripts and fs.is_lua(source) then |
@@ -310,7 +312,8 @@ function repos.deploy_files(name, version, wrap_bin_scripts, deps_mode) | |||
310 | return nil, err | 312 | return nil, err |
311 | end | 313 | end |
312 | 314 | ||
313 | return manif.update_manifest(name, version, nil, deps_mode) | 315 | local writer = require("luarocks.manif.writer") |
316 | return writer.update_manifest(name, version, nil, deps_mode) | ||
314 | end | 317 | end |
315 | 318 | ||
316 | --- Delete a package from the local repository. | 319 | --- Delete a package from the local repository. |
@@ -370,12 +373,10 @@ function repos.delete_version(name, version, deps_mode, quick) | |||
370 | ) | 373 | ) |
371 | end | 374 | end |
372 | 375 | ||
373 | local rock_manifest = manif.load_rock_manifest(name, version) | 376 | local rock_manifest, err = manif.load_rock_manifest(name, version) |
374 | if not rock_manifest then | 377 | if not rock_manifest then return nil, err end |
375 | return nil, "rock_manifest file not found for "..name.." "..version.." - not a LuaRocks 2 tree?" | ||
376 | end | ||
377 | 378 | ||
378 | local ok, err = true | 379 | local ok = true |
379 | if rock_manifest.bin then | 380 | if rock_manifest.bin then |
380 | ok, err = delete_deployed_file_tree(rock_manifest.bin, cfg.deploy_bin_dir, cfg.wrapper_suffix) | 381 | ok, err = delete_deployed_file_tree(rock_manifest.bin, cfg.deploy_bin_dir, cfg.wrapper_suffix) |
381 | end | 382 | end |
@@ -396,7 +397,8 @@ function repos.delete_version(name, version, deps_mode, quick) | |||
396 | return true | 397 | return true |
397 | end | 398 | end |
398 | 399 | ||
399 | return manif.make_manifest(cfg.rocks_dir, deps_mode) | 400 | local writer = require("luarocks.manif.writer") |
401 | return writer.make_manifest(cfg.rocks_dir, deps_mode) | ||
400 | end | 402 | end |
401 | 403 | ||
402 | return repos | 404 | return repos |
diff --git a/src/luarocks/search.lua b/src/luarocks/search.lua index d22c2a18..44eff694 100644 --- a/src/luarocks/search.lua +++ b/src/luarocks/search.lua | |||
@@ -2,16 +2,15 @@ | |||
2 | --- Module implementing the LuaRocks "search" command. | 2 | --- Module implementing the LuaRocks "search" command. |
3 | -- Queries LuaRocks servers. | 3 | -- Queries LuaRocks servers. |
4 | local search = {} | 4 | local search = {} |
5 | package.loaded["luarocks.search"] = search | 5 | |
6 | 6 | ||
7 | local dir = require("luarocks.dir") | 7 | local dir = require("luarocks.dir") |
8 | local path = require("luarocks.path") | 8 | local path = require("luarocks.path") |
9 | local manif = require("luarocks.manif") | 9 | local manif = require("luarocks.manif") |
10 | local deps = require("luarocks.deps") | 10 | local deps = require("luarocks.deps") |
11 | local cfg = require("luarocks.cfg") | 11 | local cfg = require("luarocks.core.cfg") |
12 | local util = require("luarocks.util") | 12 | local util = require("luarocks.util") |
13 | 13 | ||
14 | util.add_run_function(search) | ||
15 | search.help_summary = "Query the LuaRocks servers." | 14 | search.help_summary = "Query the LuaRocks servers." |
16 | search.help_arguments = "[--source] [--binary] { <name> [<version>] | --all }" | 15 | search.help_arguments = "[--source] [--binary] { <name> [<version>] | --all }" |
17 | search.help = [[ | 16 | search.help = [[ |
diff --git a/src/luarocks/show.lua b/src/luarocks/show.lua index 88f9512d..53d8ffa5 100644 --- a/src/luarocks/show.lua +++ b/src/luarocks/show.lua | |||
@@ -1,17 +1,15 @@ | |||
1 | --- Module implementing the LuaRocks "show" command. | 1 | --- Module implementing the LuaRocks "show" command. |
2 | -- Shows information about an installed rock. | 2 | -- Shows information about an installed rock. |
3 | local show = {} | 3 | local show = {} |
4 | package.loaded["luarocks.show"] = show | ||
5 | 4 | ||
6 | local search = require("luarocks.search") | 5 | local search = require("luarocks.search") |
7 | local cfg = require("luarocks.cfg") | 6 | local cfg = require("luarocks.core.cfg") |
8 | local util = require("luarocks.util") | 7 | local util = require("luarocks.util") |
9 | local path = require("luarocks.path") | 8 | local path = require("luarocks.path") |
10 | local deps = require("luarocks.deps") | 9 | local deps = require("luarocks.deps") |
11 | local fetch = require("luarocks.fetch") | 10 | local fetch = require("luarocks.fetch") |
12 | local manif = require("luarocks.manif") | 11 | local manif = require("luarocks.manif") |
13 | 12 | ||
14 | util.add_run_function(show) | ||
15 | show.help_summary = "Show information about an installed rock." | 13 | show.help_summary = "Show information about an installed rock." |
16 | 14 | ||
17 | show.help = [[ | 15 | show.help = [[ |
@@ -96,6 +94,8 @@ function show.command(flags, name, version) | |||
96 | if flags["rock-tree"] then util.printout(path.rocks_tree_to_string(repo)) | 94 | if flags["rock-tree"] then util.printout(path.rocks_tree_to_string(repo)) |
97 | elseif flags["rock-dir"] then util.printout(directory) | 95 | elseif flags["rock-dir"] then util.printout(directory) |
98 | elseif flags["home"] then util.printout(descript.homepage) | 96 | elseif flags["home"] then util.printout(descript.homepage) |
97 | elseif flags["issues"] then util.printout(descript.issues_url) | ||
98 | elseif flags["labels"] then util.printout(descript.labels and table.concat(descript.labels, "\n")) | ||
99 | elseif flags["modules"] then util.printout(keys_as_string(minfo.modules, "\n")) | 99 | elseif flags["modules"] then util.printout(keys_as_string(minfo.modules, "\n")) |
100 | elseif flags["deps"] then util.printout(keys_as_string(minfo.dependencies)) | 100 | elseif flags["deps"] then util.printout(keys_as_string(minfo.dependencies)) |
101 | elseif flags["rockspec"] then util.printout(rockspec_file) | 101 | elseif flags["rockspec"] then util.printout(rockspec_file) |
@@ -114,6 +114,12 @@ function show.command(flags, name, version) | |||
114 | if descript.homepage then | 114 | if descript.homepage then |
115 | util.printout("Homepage: ", descript.homepage) | 115 | util.printout("Homepage: ", descript.homepage) |
116 | end | 116 | end |
117 | if descript.issues_url then | ||
118 | util.printout("Issues: ", descript.issues_url) | ||
119 | end | ||
120 | if descript.labels then | ||
121 | util.printout("Labels: ", table.concat(descript.labels, ", ")) | ||
122 | end | ||
117 | util.printout("Installed in: ", path.rocks_tree_to_string(repo)) | 123 | util.printout("Installed in: ", path.rocks_tree_to_string(repo)) |
118 | if next(minfo.modules) then | 124 | if next(minfo.modules) then |
119 | util.printout() | 125 | util.printout() |
diff --git a/src/luarocks/tools/tar.lua b/src/luarocks/tools/tar.lua index 637a6c95..ae5c83e2 100644 --- a/src/luarocks/tools/tar.lua +++ b/src/luarocks/tools/tar.lua | |||
@@ -56,10 +56,11 @@ end | |||
56 | local function read_header_block(block) | 56 | local function read_header_block(block) |
57 | local header = {} | 57 | local header = {} |
58 | header.name = nullterm(block:sub(1,100)) | 58 | header.name = nullterm(block:sub(1,100)) |
59 | header.mode = nullterm(block:sub(101,108)) | 59 | header.mode = nullterm(block:sub(101,108)):gsub(" ", "") |
60 | header.uid = octal_to_number(nullterm(block:sub(109,116))) | 60 | header.uid = octal_to_number(nullterm(block:sub(109,116))) |
61 | header.gid = octal_to_number(nullterm(block:sub(117,124))) | 61 | header.gid = octal_to_number(nullterm(block:sub(117,124))) |
62 | header.size = octal_to_number(nullterm(block:sub(125,136))) | 62 | header.size = octal_to_number(nullterm(block:sub(125,136))) |
63 | print("{"..block:sub(125,136).."}", "{"..nullterm(block:sub(125,136)).."}", "{"..octal_to_number(nullterm(block:sub(125,136))).."}", header.size) | ||
63 | header.mtime = octal_to_number(nullterm(block:sub(137,148))) | 64 | header.mtime = octal_to_number(nullterm(block:sub(137,148))) |
64 | header.chksum = octal_to_number(nullterm(block:sub(149,156))) | 65 | header.chksum = octal_to_number(nullterm(block:sub(149,156))) |
65 | header.typeflag = get_typeflag(block:sub(157,157)) | 66 | header.typeflag = get_typeflag(block:sub(157,157)) |
@@ -93,13 +94,14 @@ function tar.untar(filename, destdir) | |||
93 | local long_name, long_link_name | 94 | local long_name, long_link_name |
94 | while true do | 95 | while true do |
95 | local block | 96 | local block |
96 | repeat | 97 | repeat |
97 | block = tar_handle:read(blocksize) | 98 | block = tar_handle:read(blocksize) |
98 | until (not block) or checksum_header(block) > 256 | 99 | until (not block) or checksum_header(block) > 256 |
99 | if not block then break end | 100 | if not block then break end |
100 | local header, err = read_header_block(block) | 101 | local header, err = read_header_block(block) |
101 | if not header then | 102 | if not header then |
102 | util.printerr(err) | 103 | util.printerr(err) |
104 | return nil, err | ||
103 | end | 105 | end |
104 | 106 | ||
105 | local file_data = tar_handle:read(math.ceil(header.size / blocksize) * blocksize):sub(1,header.size) | 107 | local file_data = tar_handle:read(math.ceil(header.size / blocksize) * blocksize):sub(1,header.size) |
diff --git a/src/luarocks/type_check.lua b/src/luarocks/type_check.lua index 63c59ca2..4b7e68d7 100644 --- a/src/luarocks/type_check.lua +++ b/src/luarocks/type_check.lua | |||
@@ -2,16 +2,15 @@ | |||
2 | -- Functions and definitions for doing a basic lint check on files | 2 | -- Functions and definitions for doing a basic lint check on files |
3 | -- loaded by LuaRocks. | 3 | -- loaded by LuaRocks. |
4 | local type_check = {} | 4 | local type_check = {} |
5 | package.loaded["luarocks.type_check"] = type_check | 5 | setmetatable(type_check, { __index = require("luarocks.core.type_check") }) |
6 | 6 | ||
7 | local cfg = require("luarocks.cfg") | 7 | type_check.rockspec_format = "3.0" |
8 | local deps = require("luarocks.deps") | ||
9 | 8 | ||
10 | type_check.rockspec_format = "1.1" | 9 | local string_1 = type_check.string_1 |
10 | local mandatory_string_1 = type_check.mandatory_string_1 | ||
11 | 11 | ||
12 | local string_1 = { _type = "string" } | 12 | local string_3 = { _type = "string", _version = "3.0" } |
13 | local number_1 = { _type = "number" } | 13 | local list_of_strings_3 = { _any = string_3, _version = "3.0" } |
14 | local mandatory_string_1 = { _type = "string", _mandatory = true } | ||
15 | 14 | ||
16 | -- Syntax for type-checking tables: | 15 | -- Syntax for type-checking tables: |
17 | -- | 16 | -- |
@@ -37,6 +36,8 @@ local rockspec_types = { | |||
37 | homepage = string_1, | 36 | homepage = string_1, |
38 | license = string_1, | 37 | license = string_1, |
39 | maintainer = string_1, | 38 | maintainer = string_1, |
39 | labels = list_of_strings_3, | ||
40 | issues_url = string_3, | ||
40 | }, | 41 | }, |
41 | dependencies = { | 42 | dependencies = { |
42 | platforms = {}, -- recursively defined below | 43 | platforms = {}, -- recursively defined below |
@@ -112,206 +113,6 @@ rockspec_types.external_dependencies.platforms._any = rockspec_types.external_de | |||
112 | rockspec_types.source.platforms._any = rockspec_types.source | 113 | rockspec_types.source.platforms._any = rockspec_types.source |
113 | rockspec_types.hooks.platforms._any = rockspec_types.hooks | 114 | rockspec_types.hooks.platforms._any = rockspec_types.hooks |
114 | 115 | ||
115 | local manifest_types = { | ||
116 | repository = { | ||
117 | _mandatory = true, | ||
118 | -- packages | ||
119 | _any = { | ||
120 | -- versions | ||
121 | _any = { | ||
122 | -- items | ||
123 | _any = { | ||
124 | arch = mandatory_string_1, | ||
125 | modules = { _any = string_1 }, | ||
126 | commands = { _any = string_1 }, | ||
127 | dependencies = { _any = string_1 }, | ||
128 | -- TODO: to be extended with more metadata. | ||
129 | } | ||
130 | } | ||
131 | } | ||
132 | }, | ||
133 | modules = { | ||
134 | _mandatory = true, | ||
135 | -- modules | ||
136 | _any = { | ||
137 | -- providers | ||
138 | _any = string_1 | ||
139 | } | ||
140 | }, | ||
141 | commands = { | ||
142 | _mandatory = true, | ||
143 | -- modules | ||
144 | _any = { | ||
145 | -- commands | ||
146 | _any = string_1 | ||
147 | } | ||
148 | }, | ||
149 | dependencies = { | ||
150 | -- each module | ||
151 | _any = { | ||
152 | -- each version | ||
153 | _any = { | ||
154 | -- each dependency | ||
155 | _any = { | ||
156 | name = string_1, | ||
157 | constraints = { | ||
158 | _any = { | ||
159 | no_upgrade = { _type = "boolean" }, | ||
160 | op = string_1, | ||
161 | version = { | ||
162 | string = string_1, | ||
163 | _any = number_1, | ||
164 | } | ||
165 | } | ||
166 | } | ||
167 | } | ||
168 | } | ||
169 | } | ||
170 | } | ||
171 | } | ||
172 | |||
173 | local function check_version(version, typetbl, context) | ||
174 | local typetbl_version = typetbl._version or "1.0" | ||
175 | if deps.compare_versions(typetbl_version, version) then | ||
176 | if context == "" then | ||
177 | return nil, "Invalid rockspec_format version number in rockspec? Please fix rockspec accordingly." | ||
178 | else | ||
179 | return nil, context.." is not supported in rockspec format "..version.." (requires version "..typetbl_version.."), please fix the rockspec_format field accordingly." | ||
180 | end | ||
181 | end | ||
182 | return true | ||
183 | end | ||
184 | |||
185 | local type_check_table | ||
186 | |||
187 | --- Type check an object. | ||
188 | -- The object is compared against an archetypical value | ||
189 | -- matching the expected type -- the actual values don't matter, | ||
190 | -- only their types. Tables are type checked recursively. | ||
191 | -- @param version string: The version of the item. | ||
192 | -- @param item any: The object being checked. | ||
193 | -- @param typetbl any: The type-checking table for the object. | ||
194 | -- @param context string: A string indicating the "context" where the | ||
195 | -- error occurred (the full table path), for error messages. | ||
196 | -- @return boolean or (nil, string): true if type checking | ||
197 | -- succeeded, or nil and an error message if it failed. | ||
198 | -- @see type_check_table | ||
199 | local function type_check_item(version, item, typetbl, context) | ||
200 | assert(type(version) == "string") | ||
201 | |||
202 | local ok, err = check_version(version, typetbl, context) | ||
203 | if not ok then | ||
204 | return nil, err | ||
205 | end | ||
206 | |||
207 | local item_type = type(item) or "nil" | ||
208 | local expected_type = typetbl._type or "table" | ||
209 | |||
210 | if expected_type == "number" then | ||
211 | if not tonumber(item) then | ||
212 | return nil, "Type mismatch on field "..context..": expected a number" | ||
213 | end | ||
214 | elseif expected_type == "string" then | ||
215 | if item_type ~= "string" then | ||
216 | return nil, "Type mismatch on field "..context..": expected a string, got "..item_type | ||
217 | end | ||
218 | if typetbl._pattern then | ||
219 | if not item:match("^"..typetbl._pattern.."$") then | ||
220 | return nil, "Type mismatch on field "..context..": invalid value "..item.." does not match '"..typetbl._pattern.."'" | ||
221 | end | ||
222 | end | ||
223 | elseif expected_type == "table" then | ||
224 | if item_type ~= expected_type then | ||
225 | return nil, "Type mismatch on field "..context..": expected a table" | ||
226 | else | ||
227 | return type_check_table(version, item, typetbl, context) | ||
228 | end | ||
229 | elseif item_type ~= expected_type then | ||
230 | return nil, "Type mismatch on field "..context..": expected "..expected_type | ||
231 | end | ||
232 | return true | ||
233 | end | ||
234 | |||
235 | local function mkfield(context, field) | ||
236 | if context == "" then | ||
237 | return tostring(field) | ||
238 | elseif type(field) == "string" then | ||
239 | return context.."."..field | ||
240 | else | ||
241 | return context.."["..tostring(field).."]" | ||
242 | end | ||
243 | end | ||
244 | |||
245 | --- Type check the contents of a table. | ||
246 | -- The table's contents are compared against a reference table, | ||
247 | -- which contains the recognized fields, with archetypical values | ||
248 | -- matching the expected types -- the actual values of items in the | ||
249 | -- reference table don't matter, only their types (ie, for field x | ||
250 | -- in tbl that is correctly typed, type(tbl.x) == type(types.x)). | ||
251 | -- If the reference table contains a field called MORE, then | ||
252 | -- unknown fields in the checked table are accepted. | ||
253 | -- If it contains a field called ANY, then its type will be | ||
254 | -- used to check any unknown fields. If a field is prefixed | ||
255 | -- with MUST_, it is mandatory; its absence from the table is | ||
256 | -- a type error. | ||
257 | -- Tables are type checked recursively. | ||
258 | -- @param version string: The version of tbl. | ||
259 | -- @param tbl table: The table to be type checked. | ||
260 | -- @param typetbl table: The type-checking table, containing | ||
261 | -- values for recognized fields in the checked table. | ||
262 | -- @param context string: A string indicating the "context" where the | ||
263 | -- error occurred (such as the name of the table the item is a part of), | ||
264 | -- to be used by error messages. | ||
265 | -- @return boolean or (nil, string): true if type checking | ||
266 | -- succeeded, or nil and an error message if it failed. | ||
267 | type_check_table = function(version, tbl, typetbl, context) | ||
268 | assert(type(version) == "string") | ||
269 | assert(type(tbl) == "table") | ||
270 | assert(type(typetbl) == "table") | ||
271 | |||
272 | local ok, err = check_version(version, typetbl, context) | ||
273 | if not ok then | ||
274 | return nil, err | ||
275 | end | ||
276 | |||
277 | for k, v in pairs(tbl) do | ||
278 | local t = typetbl[k] or typetbl._any | ||
279 | if t then | ||
280 | local ok, err = type_check_item(version, v, t, mkfield(context, k)) | ||
281 | if not ok then return nil, err end | ||
282 | elseif typetbl._more then | ||
283 | -- Accept unknown field | ||
284 | else | ||
285 | if not cfg.accept_unknown_fields then | ||
286 | return nil, "Unknown field "..k | ||
287 | end | ||
288 | end | ||
289 | end | ||
290 | for k, v in pairs(typetbl) do | ||
291 | if k:sub(1,1) ~= "_" and v._mandatory then | ||
292 | if not tbl[k] then | ||
293 | return nil, "Mandatory field "..mkfield(context, k).." is missing." | ||
294 | end | ||
295 | end | ||
296 | end | ||
297 | return true | ||
298 | end | ||
299 | |||
300 | local function check_undeclared_globals(globals, typetbl) | ||
301 | local undeclared = {} | ||
302 | for glob, _ in pairs(globals) do | ||
303 | if not (typetbl[glob] or typetbl["MUST_"..glob]) then | ||
304 | table.insert(undeclared, glob) | ||
305 | end | ||
306 | end | ||
307 | if #undeclared == 1 then | ||
308 | return nil, "Unknown variable: "..undeclared[1] | ||
309 | elseif #undeclared > 1 then | ||
310 | return nil, "Unknown variables: "..table.concat(undeclared, ", ") | ||
311 | end | ||
312 | return true | ||
313 | end | ||
314 | |||
315 | --- Type check a rockspec table. | 116 | --- Type check a rockspec table. |
316 | -- Verify the correctness of elements from a | 117 | -- Verify the correctness of elements from a |
317 | -- rockspec table, reporting on unknown fields and type | 118 | -- rockspec table, reporting on unknown fields and type |
@@ -323,22 +124,9 @@ function type_check.type_check_rockspec(rockspec, globals) | |||
323 | if not rockspec.rockspec_format then | 124 | if not rockspec.rockspec_format then |
324 | rockspec.rockspec_format = "1.0" | 125 | rockspec.rockspec_format = "1.0" |
325 | end | 126 | end |
326 | local ok, err = check_undeclared_globals(globals, rockspec_types) | 127 | local ok, err = type_check.check_undeclared_globals(globals, rockspec_types) |
327 | if not ok then return nil, err end | ||
328 | return type_check_table(rockspec.rockspec_format, rockspec, rockspec_types, "") | ||
329 | end | ||
330 | |||
331 | --- Type check a manifest table. | ||
332 | -- Verify the correctness of elements from a | ||
333 | -- manifest table, reporting on unknown fields and type | ||
334 | -- mismatches. | ||
335 | -- @return boolean or (nil, string): true if type checking | ||
336 | -- succeeded, or nil and an error message if it failed. | ||
337 | function type_check.type_check_manifest(manifest, globals) | ||
338 | assert(type(manifest) == "table") | ||
339 | local ok, err = check_undeclared_globals(globals, manifest_types) | ||
340 | if not ok then return nil, err end | 128 | if not ok then return nil, err end |
341 | return type_check_table("1.0", manifest, manifest_types, "") | 129 | return type_check.type_check_table(rockspec.rockspec_format, rockspec, rockspec_types, "") |
342 | end | 130 | end |
343 | 131 | ||
344 | return type_check | 132 | return type_check |
diff --git a/src/luarocks/unpack.lua b/src/luarocks/unpack.lua index c3dc9a4c..af3398ab 100644 --- a/src/luarocks/unpack.lua +++ b/src/luarocks/unpack.lua | |||
@@ -2,16 +2,14 @@ | |||
2 | --- Module implementing the LuaRocks "unpack" command. | 2 | --- Module implementing the LuaRocks "unpack" command. |
3 | -- Unpack the contents of a rock. | 3 | -- Unpack the contents of a rock. |
4 | local unpack = {} | 4 | local unpack = {} |
5 | package.loaded["luarocks.unpack"] = unpack | ||
6 | 5 | ||
7 | local fetch = require("luarocks.fetch") | 6 | local fetch = require("luarocks.fetch") |
8 | local fs = require("luarocks.fs") | 7 | local fs = require("luarocks.fs") |
9 | local util = require("luarocks.util") | 8 | local util = require("luarocks.util") |
10 | local build = require("luarocks.build") | 9 | local build = require("luarocks.build") |
11 | local dir = require("luarocks.dir") | 10 | local dir = require("luarocks.dir") |
12 | local cfg = require("luarocks.cfg") | 11 | local cfg = require("luarocks.core.cfg") |
13 | 12 | ||
14 | util.add_run_function(unpack) | ||
15 | unpack.help_summary = "Unpack the contents of a rock." | 13 | unpack.help_summary = "Unpack the contents of a rock." |
16 | unpack.help_arguments = "[--force] {<rock>|<name> [<version>]}" | 14 | unpack.help_arguments = "[--force] {<rock>|<name> [<version>]}" |
17 | unpack.help = [[ | 15 | unpack.help = [[ |
diff --git a/src/luarocks/upload.lua b/src/luarocks/upload.lua index 7c0c416c..baee47ab 100644 --- a/src/luarocks/upload.lua +++ b/src/luarocks/upload.lua | |||
@@ -4,10 +4,9 @@ local upload = {} | |||
4 | local util = require("luarocks.util") | 4 | local util = require("luarocks.util") |
5 | local fetch = require("luarocks.fetch") | 5 | local fetch = require("luarocks.fetch") |
6 | local pack = require("luarocks.pack") | 6 | local pack = require("luarocks.pack") |
7 | local cfg = require("luarocks.cfg") | 7 | local cfg = require("luarocks.core.cfg") |
8 | local Api = require("luarocks.upload.api") | 8 | local Api = require("luarocks.upload.api") |
9 | 9 | ||
10 | util.add_run_function(upload) | ||
11 | upload.help_summary = "Upload a rockspec to the public rocks repository." | 10 | upload.help_summary = "Upload a rockspec to the public rocks repository." |
12 | upload.help_arguments = "[--skip-pack] [--api-key=<key>] [--force] <rockspec>" | 11 | upload.help_arguments = "[--skip-pack] [--api-key=<key>] [--force] <rockspec>" |
13 | upload.help = [[ | 12 | upload.help = [[ |
diff --git a/src/luarocks/upload/api.lua b/src/luarocks/upload/api.lua index 6df24569..3e28bbda 100644 --- a/src/luarocks/upload/api.lua +++ b/src/luarocks/upload/api.lua | |||
@@ -1,7 +1,7 @@ | |||
1 | 1 | ||
2 | local api = {} | 2 | local api = {} |
3 | 3 | ||
4 | local cfg = require("luarocks.cfg") | 4 | local cfg = require("luarocks.core.cfg") |
5 | local fs = require("luarocks.fs") | 5 | local fs = require("luarocks.fs") |
6 | local util = require("luarocks.util") | 6 | local util = require("luarocks.util") |
7 | local persist = require("luarocks.persist") | 7 | local persist = require("luarocks.persist") |
diff --git a/src/luarocks/util.lua b/src/luarocks/util.lua index c9fb7d63..b23f4fab 100644 --- a/src/luarocks/util.lua +++ b/src/luarocks/util.lua | |||
@@ -2,9 +2,10 @@ | |||
2 | --- Assorted utilities for managing tables, plus a scheduler for rollback functions. | 2 | --- Assorted utilities for managing tables, plus a scheduler for rollback functions. |
3 | -- Does not requires modules directly (only as locals | 3 | -- Does not requires modules directly (only as locals |
4 | -- inside specific functions) to avoid interdependencies, | 4 | -- inside specific functions) to avoid interdependencies, |
5 | -- as this is used in the bootstrapping stage of luarocks.cfg. | 5 | -- as this is used in the bootstrapping stage of luarocks.core.cfg. |
6 | 6 | ||
7 | local util = {} | 7 | local util = {} |
8 | setmetatable(util, { __index = require("luarocks.core.util") }) | ||
8 | 9 | ||
9 | local unpack = unpack or table.unpack | 10 | local unpack = unpack or table.unpack |
10 | 11 | ||
@@ -86,7 +87,9 @@ local supported_flags = { | |||
86 | ["help"] = true, | 87 | ["help"] = true, |
87 | ["home"] = true, | 88 | ["home"] = true, |
88 | ["homepage"] = "\"<url>\"", | 89 | ["homepage"] = "\"<url>\"", |
90 | ["issues"] = true, | ||
89 | ["keep"] = true, | 91 | ["keep"] = true, |
92 | ["labels"] = true, | ||
90 | ["lib"] = "<library>", | 93 | ["lib"] = "<library>", |
91 | ["license"] = "\"<text>\"", | 94 | ["license"] = "\"<text>\"", |
92 | ["list"] = true, | 95 | ["list"] = true, |
@@ -196,34 +199,6 @@ function util.parse_flags(...) | |||
196 | return flags, unpack(out) | 199 | return flags, unpack(out) |
197 | end | 200 | end |
198 | 201 | ||
199 | -- Adds legacy 'run' function to a command module. | ||
200 | -- @param command table: command module with 'command' function, | ||
201 | -- the added 'run' function calls it after parseing command-line arguments. | ||
202 | function util.add_run_function(command) | ||
203 | command.run = function(...) return command.command(util.parse_flags(...)) end | ||
204 | end | ||
205 | |||
206 | --- Merges contents of src on top of dst's contents. | ||
207 | -- @param dst Destination table, which will receive src's contents. | ||
208 | -- @param src Table which provides new contents to dst. | ||
209 | -- @see platform_overrides | ||
210 | function util.deep_merge(dst, src) | ||
211 | for k, v in pairs(src) do | ||
212 | if type(v) == "table" then | ||
213 | if not dst[k] then | ||
214 | dst[k] = {} | ||
215 | end | ||
216 | if type(dst[k]) == "table" then | ||
217 | util.deep_merge(dst[k], v) | ||
218 | else | ||
219 | dst[k] = v | ||
220 | end | ||
221 | else | ||
222 | dst[k] = v | ||
223 | end | ||
224 | end | ||
225 | end | ||
226 | |||
227 | --- Perform platform-specific overrides on a table. | 202 | --- Perform platform-specific overrides on a table. |
228 | -- Overrides values of table with the contents of the appropriate | 203 | -- Overrides values of table with the contents of the appropriate |
229 | -- subset of its "platforms" field. The "platforms" field should | 204 | -- subset of its "platforms" field. The "platforms" field should |
@@ -240,7 +215,7 @@ end | |||
240 | function util.platform_overrides(tbl) | 215 | function util.platform_overrides(tbl) |
241 | assert(type(tbl) == "table" or not tbl) | 216 | assert(type(tbl) == "table" or not tbl) |
242 | 217 | ||
243 | local cfg = require("luarocks.cfg") | 218 | local cfg = require("luarocks.core.cfg") |
244 | 219 | ||
245 | if not tbl then return end | 220 | if not tbl then return end |
246 | 221 | ||
@@ -257,19 +232,6 @@ end | |||
257 | 232 | ||
258 | local var_format_pattern = "%$%((%a[%a%d_]+)%)" | 233 | local var_format_pattern = "%$%((%a[%a%d_]+)%)" |
259 | 234 | ||
260 | --- Create a new shallow copy of a table: a new table with | ||
261 | -- the same keys and values. Keys point to the same objects as | ||
262 | -- the original table (ie, does not copy recursively). | ||
263 | -- @param tbl table: the input table | ||
264 | -- @return table: a new table with the same contents. | ||
265 | function util.make_shallow_copy(tbl) | ||
266 | local copy = {} | ||
267 | for k,v in pairs(tbl) do | ||
268 | copy[k] = v | ||
269 | end | ||
270 | return copy | ||
271 | end | ||
272 | |||
273 | -- Check if a set of needed variables are referenced | 235 | -- Check if a set of needed variables are referenced |
274 | -- somewhere in a list of definitions, warning the user | 236 | -- somewhere in a list of definitions, warning the user |
275 | -- about any unused ones. Each key in needed_set should | 237 | -- about any unused ones. Each key in needed_set should |
@@ -332,86 +294,6 @@ function util.variable_substitutions(tbl, vars) | |||
332 | end | 294 | end |
333 | end | 295 | end |
334 | 296 | ||
335 | --- Return an array of keys of a table. | ||
336 | -- @param tbl table: The input table. | ||
337 | -- @return table: The array of keys. | ||
338 | function util.keys(tbl) | ||
339 | local ks = {} | ||
340 | for k,_ in pairs(tbl) do | ||
341 | table.insert(ks, k) | ||
342 | end | ||
343 | return ks | ||
344 | end | ||
345 | |||
346 | local function default_sort(a, b) | ||
347 | local ta = type(a) | ||
348 | local tb = type(b) | ||
349 | if ta == "number" and tb == "number" then | ||
350 | return a < b | ||
351 | elseif ta == "number" then | ||
352 | return true | ||
353 | elseif tb == "number" then | ||
354 | return false | ||
355 | else | ||
356 | return tostring(a) < tostring(b) | ||
357 | end | ||
358 | end | ||
359 | |||
360 | --- A table iterator generator that returns elements sorted by key, | ||
361 | -- to be used in "for" loops. | ||
362 | -- @param tbl table: The table to be iterated. | ||
363 | -- @param sort_function function or table or nil: An optional comparison function | ||
364 | -- to be used by table.sort when sorting keys, or an array listing an explicit order | ||
365 | -- for keys. If a value itself is an array, it is taken so that the first element | ||
366 | -- is a string representing the field name, and the second element is a priority table | ||
367 | -- for that key, which is returned by the iterator as the third value after the key | ||
368 | -- and the value. | ||
369 | -- @return function: the iterator function. | ||
370 | function util.sortedpairs(tbl, sort_function) | ||
371 | sort_function = sort_function or default_sort | ||
372 | local keys = util.keys(tbl) | ||
373 | local sub_orders = {} | ||
374 | |||
375 | if type(sort_function) == "function" then | ||
376 | table.sort(keys, sort_function) | ||
377 | else | ||
378 | local order = sort_function | ||
379 | local ordered_keys = {} | ||
380 | local all_keys = keys | ||
381 | keys = {} | ||
382 | |||
383 | for _, order_entry in ipairs(order) do | ||
384 | local key, sub_order | ||
385 | if type(order_entry) == "table" then | ||
386 | key = order_entry[1] | ||
387 | sub_order = order_entry[2] | ||
388 | else | ||
389 | key = order_entry | ||
390 | end | ||
391 | |||
392 | if tbl[key] then | ||
393 | ordered_keys[key] = true | ||
394 | sub_orders[key] = sub_order | ||
395 | table.insert(keys, key) | ||
396 | end | ||
397 | end | ||
398 | |||
399 | table.sort(all_keys, default_sort) | ||
400 | for _, key in ipairs(all_keys) do | ||
401 | if not ordered_keys[key] then | ||
402 | table.insert(keys, key) | ||
403 | end | ||
404 | end | ||
405 | end | ||
406 | |||
407 | local i = 1 | ||
408 | return function() | ||
409 | local key = keys[i] | ||
410 | i = i + 1 | ||
411 | return key, tbl[key], sub_orders[key] | ||
412 | end | ||
413 | end | ||
414 | |||
415 | function util.lua_versions() | 297 | function util.lua_versions() |
416 | local versions = { "5.1", "5.2", "5.3" } | 298 | local versions = { "5.1", "5.2", "5.3" } |
417 | local i = 0 | 299 | local i = 0 |
@@ -431,12 +313,6 @@ function util.printout(...) | |||
431 | io.stdout:write("\n") | 313 | io.stdout:write("\n") |
432 | end | 314 | end |
433 | 315 | ||
434 | --- Print a line to standard error | ||
435 | function util.printerr(...) | ||
436 | io.stderr:write(table.concat({...},"\t")) | ||
437 | io.stderr:write("\n") | ||
438 | end | ||
439 | |||
440 | --- Display a warning message. | 316 | --- Display a warning message. |
441 | -- @param msg string: the warning message | 317 | -- @param msg string: the warning message |
442 | function util.warning(msg) | 318 | function util.warning(msg) |
@@ -465,7 +341,7 @@ function util.this_program(default) | |||
465 | end | 341 | end |
466 | 342 | ||
467 | function util.deps_mode_help(program) | 343 | function util.deps_mode_help(program) |
468 | local cfg = require("luarocks.cfg") | 344 | local cfg = require("luarocks.core.cfg") |
469 | return [[ | 345 | return [[ |
470 | --deps-mode=<mode> How to handle dependencies. Four modes are supported: | 346 | --deps-mode=<mode> How to handle dependencies. Four modes are supported: |
471 | * all - use all trees from the rocks_trees list | 347 | * all - use all trees from the rocks_trees list |
@@ -488,7 +364,7 @@ function util.see_help(command, program) | |||
488 | end | 364 | end |
489 | 365 | ||
490 | function util.announce_install(rockspec) | 366 | function util.announce_install(rockspec) |
491 | local cfg = require("luarocks.cfg") | 367 | local cfg = require("luarocks.core.cfg") |
492 | local path = require("luarocks.path") | 368 | local path = require("luarocks.path") |
493 | 369 | ||
494 | local suffix = "" | 370 | local suffix = "" |
@@ -566,135 +442,6 @@ function util.get_default_rockspec() | |||
566 | end | 442 | end |
567 | end | 443 | end |
568 | 444 | ||
569 | -- from http://lua-users.org/wiki/SplitJoin | ||
570 | -- by PhilippeLhoste | ||
571 | function util.split_string(str, delim, maxNb) | ||
572 | -- Eliminate bad cases... | ||
573 | if string.find(str, delim) == nil then | ||
574 | return { str } | ||
575 | end | ||
576 | if maxNb == nil or maxNb < 1 then | ||
577 | maxNb = 0 -- No limit | ||
578 | end | ||
579 | local result = {} | ||
580 | local pat = "(.-)" .. delim .. "()" | ||
581 | local nb = 0 | ||
582 | local lastPos | ||
583 | for part, pos in string.gmatch(str, pat) do | ||
584 | nb = nb + 1 | ||
585 | result[nb] = part | ||
586 | lastPos = pos | ||
587 | if nb == maxNb then break end | ||
588 | end | ||
589 | -- Handle the last field | ||
590 | if nb ~= maxNb then | ||
591 | result[nb + 1] = string.sub(str, lastPos) | ||
592 | end | ||
593 | return result | ||
594 | end | ||
595 | |||
596 | --- Remove repeated entries from a path-style string. | ||
597 | -- Example: given ("a;b;c;a;b;d", ";"), returns "a;b;c;d". | ||
598 | -- @param list string: A path string (from $PATH or package.path) | ||
599 | -- @param sep string: The separator | ||
600 | function util.remove_path_dupes(list, sep) | ||
601 | assert(type(list) == "string") | ||
602 | assert(type(sep) == "string") | ||
603 | local parts = util.split_string(list, sep) | ||
604 | local final, entries = {}, {} | ||
605 | for _, part in ipairs(parts) do | ||
606 | part = part:gsub("//", "/") | ||
607 | if not entries[part] then | ||
608 | table.insert(final, part) | ||
609 | entries[part] = true | ||
610 | end | ||
611 | end | ||
612 | return table.concat(final, sep) | ||
613 | end | ||
614 | |||
615 | --- | ||
616 | -- Formats tables with cycles recursively to any depth. | ||
617 | -- References to other tables are shown as values. | ||
618 | -- Self references are indicated. | ||
619 | -- The string returned is "Lua code", which can be procesed | ||
620 | -- (in the case in which indent is composed by spaces or "--"). | ||
621 | -- Userdata and function keys and values are shown as strings, | ||
622 | -- which logically are exactly not equivalent to the original code. | ||
623 | -- This routine can serve for pretty formating tables with | ||
624 | -- proper indentations, apart from printing them: | ||
625 | -- io.write(table.show(t, "t")) -- a typical use | ||
626 | -- Written by Julio Manuel Fernandez-Diaz, | ||
627 | -- Heavily based on "Saving tables with cycles", PIL2, p. 113. | ||
628 | -- @param t table: is the table. | ||
629 | -- @param name string: is the name of the table (optional) | ||
630 | -- @param indent string: is a first indentation (optional). | ||
631 | -- @return string: the pretty-printed table | ||
632 | function util.show_table(t, name, indent) | ||
633 | local cart -- a container | ||
634 | local autoref -- for self references | ||
635 | |||
636 | local function isemptytable(t) return next(t) == nil end | ||
637 | |||
638 | local function basicSerialize (o) | ||
639 | local so = tostring(o) | ||
640 | if type(o) == "function" then | ||
641 | local info = debug.getinfo(o, "S") | ||
642 | -- info.name is nil because o is not a calling level | ||
643 | if info.what == "C" then | ||
644 | return ("%q"):format(so .. ", C function") | ||
645 | else | ||
646 | -- the information is defined through lines | ||
647 | return ("%q"):format(so .. ", defined in (" .. info.linedefined .. "-" .. info.lastlinedefined .. ")" .. info.source) | ||
648 | end | ||
649 | elseif type(o) == "number" then | ||
650 | return so | ||
651 | else | ||
652 | return ("%q"):format(so) | ||
653 | end | ||
654 | end | ||
655 | |||
656 | local function addtocart (value, name, indent, saved, field) | ||
657 | indent = indent or "" | ||
658 | saved = saved or {} | ||
659 | field = field or name | ||
660 | |||
661 | cart = cart .. indent .. field | ||
662 | |||
663 | if type(value) ~= "table" then | ||
664 | cart = cart .. " = " .. basicSerialize(value) .. ";\n" | ||
665 | else | ||
666 | if saved[value] then | ||
667 | cart = cart .. " = {}; -- " .. saved[value] .. " (self reference)\n" | ||
668 | autoref = autoref .. name .. " = " .. saved[value] .. ";\n" | ||
669 | else | ||
670 | saved[value] = name | ||
671 | --if tablecount(value) == 0 then | ||
672 | if isemptytable(value) then | ||
673 | cart = cart .. " = {};\n" | ||
674 | else | ||
675 | cart = cart .. " = {\n" | ||
676 | for k, v in pairs(value) do | ||
677 | k = basicSerialize(k) | ||
678 | local fname = ("%s[%s]"):format(name, k) | ||
679 | field = ("[%s]"):format(k) | ||
680 | -- three spaces between levels | ||
681 | addtocart(v, fname, indent .. " ", saved, field) | ||
682 | end | ||
683 | cart = cart .. indent .. "};\n" | ||
684 | end | ||
685 | end | ||
686 | end | ||
687 | end | ||
688 | |||
689 | name = name or "__unnamed__" | ||
690 | if type(t) ~= "table" then | ||
691 | return name .. " = " .. basicSerialize(t) | ||
692 | end | ||
693 | cart, autoref = "", "" | ||
694 | addtocart(t, name, indent) | ||
695 | return cart .. autoref | ||
696 | end | ||
697 | |||
698 | function util.array_contains(tbl, value) | 445 | function util.array_contains(tbl, value) |
699 | for _, v in ipairs(tbl) do | 446 | for _, v in ipairs(tbl) do |
700 | if v == value then | 447 | if v == value then |
diff --git a/src/luarocks/validate.lua b/src/luarocks/validate.lua deleted file mode 100644 index c4570aa4..00000000 --- a/src/luarocks/validate.lua +++ /dev/null | |||
@@ -1,159 +0,0 @@ | |||
1 | |||
2 | --- Sandboxed test of build/install of all packages in a repository (unfinished and disabled). | ||
3 | local validate = {} | ||
4 | package.loaded["luarocks.validate"] = validate | ||
5 | |||
6 | local fs = require("luarocks.fs") | ||
7 | local dir = require("luarocks.dir") | ||
8 | local path = require("luarocks.path") | ||
9 | local cfg = require("luarocks.cfg") | ||
10 | local build = require("luarocks.build") | ||
11 | local install = require("luarocks.install") | ||
12 | local util = require("luarocks.util") | ||
13 | |||
14 | util.add_run_function(validate) | ||
15 | validate.help_summary = "Sandboxed test of build/install of all packages in a repository." | ||
16 | |||
17 | validate.help = [[ | ||
18 | <argument>, if given, is a local repository pathname. | ||
19 | ]] | ||
20 | |||
21 | local function save_settings(repo) | ||
22 | local protocol, path = dir.split_url(repo) | ||
23 | table.insert(cfg.rocks_servers, 1, protocol.."://"..path) | ||
24 | return { | ||
25 | root_dir = cfg.root_dir, | ||
26 | rocks_dir = cfg.rocks_dir, | ||
27 | deploy_bin_dir = cfg.deploy_bin_dir, | ||
28 | deploy_lua_dir = cfg.deploy_lua_dir, | ||
29 | deploy_lib_dir = cfg.deploy_lib_dir, | ||
30 | } | ||
31 | end | ||
32 | |||
33 | local function restore_settings(settings) | ||
34 | cfg.root_dir = settings.root_dir | ||
35 | cfg.rocks_dir = settings.rocks_dir | ||
36 | cfg.deploy_bin_dir = settings.deploy_bin_dir | ||
37 | cfg.deploy_lua_dir = settings.deploy_lua_dir | ||
38 | cfg.deploy_lib_dir = settings.deploy_lib_dir | ||
39 | cfg.variables.ROCKS_TREE = settings.rocks_dir | ||
40 | cfg.variables.SCRIPTS_DIR = settings.deploy_bin_dir | ||
41 | table.remove(cfg.rocks_servers, 1) | ||
42 | end | ||
43 | |||
44 | local function prepare_sandbox(file) | ||
45 | local root_dir = fs.make_temp_dir(file):gsub("/+$", "") | ||
46 | cfg.root_dir = root_dir | ||
47 | cfg.rocks_dir = path.rocks_dir(root_dir) | ||
48 | cfg.deploy_bin_dir = path.deploy_bin_dir(root_dir) | ||
49 | cfg.variables.ROCKS_TREE = cfg.rocks_dir | ||
50 | cfg.variables.SCRIPTS_DIR = cfg.deploy_bin_dir | ||
51 | return root_dir | ||
52 | end | ||
53 | |||
54 | local function validate_rockspec(file) | ||
55 | local ok, err, errcode = build.build_rockspec(file, true, "one") | ||
56 | if not ok then | ||
57 | util.printerr(err) | ||
58 | end | ||
59 | return ok, err, errcode | ||
60 | end | ||
61 | |||
62 | local function validate_src_rock(file) | ||
63 | local ok, err, errcode = build.build_rock(file, false, "one") | ||
64 | if not ok then | ||
65 | util.printerr(err) | ||
66 | end | ||
67 | return ok, err, errcode | ||
68 | end | ||
69 | |||
70 | local function validate_rock(file) | ||
71 | local ok, err, errcode = install.install_binary_rock(file, "one") | ||
72 | if not ok then | ||
73 | util.printerr(err) | ||
74 | end | ||
75 | return ok, err, errcode | ||
76 | end | ||
77 | |||
78 | function validate.command(flags, repo) | ||
79 | repo = repo or cfg.rocks_dir | ||
80 | |||
81 | util.printout("Verifying contents of "..repo) | ||
82 | |||
83 | local results = { | ||
84 | ok = {} | ||
85 | } | ||
86 | local settings = save_settings(repo) | ||
87 | local sandbox | ||
88 | if flags["quick"] then | ||
89 | sandbox = prepare_sandbox("luarocks_validate") | ||
90 | end | ||
91 | if not fs.exists(repo) then | ||
92 | return nil, repo.." is not a local repository." | ||
93 | end | ||
94 | for file in fs.dir(repo) do for _=1,1 do | ||
95 | if file == "manifest" or file == "index.html" then | ||
96 | break -- continue for | ||
97 | end | ||
98 | local pathname = fs.absolute_name(dir.path(repo, file)) | ||
99 | if not flags["quick"] then | ||
100 | sandbox = prepare_sandbox(file) | ||
101 | end | ||
102 | local ok, err, errcode | ||
103 | util.printout() | ||
104 | util.printout("Verifying "..pathname) | ||
105 | if file:match("%.rockspec$") then | ||
106 | ok, err, errcode = validate_rockspec(pathname, "one") | ||
107 | elseif file:match("%.src%.rock$") then | ||
108 | ok, err, errcode = validate_src_rock(pathname) | ||
109 | elseif file:match("%.rock$") then | ||
110 | ok, err, errcode = validate_rock(pathname) | ||
111 | end | ||
112 | if ok then | ||
113 | table.insert(results.ok, {file=file} ) | ||
114 | else | ||
115 | if not errcode then | ||
116 | errcode = "misc" | ||
117 | end | ||
118 | if not results[errcode] then | ||
119 | results[errcode] = {} | ||
120 | end | ||
121 | table.insert(results[errcode], {file=file, err=err} ) | ||
122 | end | ||
123 | util.run_scheduled_functions() | ||
124 | if not flags["quick"] then | ||
125 | fs.delete(sandbox) | ||
126 | end | ||
127 | repeat until not fs.pop_dir() | ||
128 | end end | ||
129 | if flags["quick"] then | ||
130 | fs.delete(sandbox) | ||
131 | end | ||
132 | restore_settings(settings) | ||
133 | util.title("Results:") | ||
134 | util.printout("OK: "..tostring(#results.ok)) | ||
135 | for _, entry in ipairs(results.ok) do | ||
136 | util.printout(entry.file) | ||
137 | end | ||
138 | for errcode, errors in pairs(results) do | ||
139 | if errcode ~= "ok" then | ||
140 | util.printout() | ||
141 | util.printout(errcode.." errors: "..tostring(#errors)) | ||
142 | for _, entry in ipairs(errors) do | ||
143 | util.printout(entry.file, entry.err) | ||
144 | end | ||
145 | end | ||
146 | end | ||
147 | |||
148 | util.title("Summary:") | ||
149 | local total = 0 | ||
150 | for errcode, errors in pairs(results) do | ||
151 | util.printout(errcode..": "..tostring(#errors)) | ||
152 | total = total + #errors | ||
153 | end | ||
154 | util.printout("Total: "..total) | ||
155 | return true | ||
156 | end | ||
157 | |||
158 | |||
159 | return validate | ||
diff --git a/src/luarocks/write_rockspec.lua b/src/luarocks/write_rockspec.lua index 33edeb1b..d6075dbd 100644 --- a/src/luarocks/write_rockspec.lua +++ b/src/luarocks/write_rockspec.lua | |||
@@ -1,8 +1,7 @@ | |||
1 | 1 | ||
2 | local write_rockspec = {} | 2 | local write_rockspec = {} |
3 | package.loaded["luarocks.write_rockspec"] = write_rockspec | ||
4 | 3 | ||
5 | local cfg = require("luarocks.cfg") | 4 | local cfg = require("luarocks.core.cfg") |
6 | local dir = require("luarocks.dir") | 5 | local dir = require("luarocks.dir") |
7 | local fetch = require("luarocks.fetch") | 6 | local fetch = require("luarocks.fetch") |
8 | local fs = require("luarocks.fs") | 7 | local fs = require("luarocks.fs") |
@@ -11,7 +10,6 @@ local persist = require("luarocks.persist") | |||
11 | local type_check = require("luarocks.type_check") | 10 | local type_check = require("luarocks.type_check") |
12 | local util = require("luarocks.util") | 11 | local util = require("luarocks.util") |
13 | 12 | ||
14 | util.add_run_function(write_rockspec) | ||
15 | write_rockspec.help_summary = "Write a template for a rockspec file." | 13 | write_rockspec.help_summary = "Write a template for a rockspec file." |
16 | write_rockspec.help_arguments = "[--output=<file> ...] [<name>] [<version>] [<url>|<path>]" | 14 | write_rockspec.help_arguments = "[--output=<file> ...] [<name>] [<version>] [<url>|<path>]" |
17 | write_rockspec.help = [[ | 15 | write_rockspec.help = [[ |