aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHisham Muhammad <hisham@gobolinux.org>2019-11-08 18:08:13 -0300
committerHisham Muhammad <hisham@gobolinux.org>2019-11-29 18:36:42 -0300
commitb375f9d930f787c9224b5eaf8940486802098778 (patch)
tree719eeebd7a21c22c6cc8187f556608a46a441efd
parentb4dfc787519e9d6df798812858e70778b7c5cf30 (diff)
downloadluarocks-b375f9d930f787c9224b5eaf8940486802098778.tar.gz
luarocks-b375f9d930f787c9224b5eaf8940486802098778.tar.bz2
luarocks-b375f9d930f787c9224b5eaf8940486802098778.zip
refactor dependency scanning for reuse when building lockfile
-rw-r--r--spec/install_spec.lua2
-rw-r--r--src/luarocks/cmd.lua6
-rw-r--r--src/luarocks/core/dir.lua12
-rw-r--r--src/luarocks/core/manif.lua47
-rw-r--r--src/luarocks/core/path.lua38
-rw-r--r--src/luarocks/dir.lua13
-rw-r--r--src/luarocks/loader.lua67
-rw-r--r--src/luarocks/manif.lua2
-rw-r--r--src/luarocks/path.lua39
9 files changed, 114 insertions, 112 deletions
diff --git a/spec/install_spec.lua b/spec/install_spec.lua
index 9b082ab8..bf50ced7 100644
--- a/spec/install_spec.lua
+++ b/spec/install_spec.lua
@@ -184,7 +184,7 @@ describe("luarocks install #integration", function()
184 it("only-deps of luasocket packed rock", function() 184 it("only-deps of luasocket packed rock", function()
185 assert.is_true(run.luarocks_bool("build --pack-binary-rock luasocket 3.0rc1-2")) 185 assert.is_true(run.luarocks_bool("build --pack-binary-rock luasocket 3.0rc1-2"))
186 local output = run.luarocks("install --only-deps " .. "luasocket-3.0rc1-2." .. test_env.platform .. ".rock") 186 local output = run.luarocks("install --only-deps " .. "luasocket-3.0rc1-2." .. test_env.platform .. ".rock")
187 assert.are.same("Successfully installed dependencies for luasocket 3.0rc1-2", output:gsub("\n", "")) 187 assert.match("Successfully installed dependencies for luasocket 3.0rc1-2", output, 1, true)
188 assert.is_true(os.remove("luasocket-3.0rc1-2." .. test_env.platform .. ".rock")) 188 assert.is_true(os.remove("luasocket-3.0rc1-2." .. test_env.platform .. ".rock"))
189 end) 189 end)
190 190
diff --git a/src/luarocks/cmd.lua b/src/luarocks/cmd.lua
index 36080eb9..eef043e7 100644
--- a/src/luarocks/cmd.lua
+++ b/src/luarocks/cmd.lua
@@ -2,7 +2,7 @@
2--- Functions for command-line scripts. 2--- Functions for command-line scripts.
3local cmd = {} 3local cmd = {}
4 4
5local loader = require("luarocks.loader") 5local manif = require("luarocks.manif")
6local util = require("luarocks.util") 6local util = require("luarocks.util")
7local path = require("luarocks.path") 7local path = require("luarocks.path")
8local cfg = require("luarocks.core.cfg") 8local cfg = require("luarocks.core.cfg")
@@ -94,7 +94,7 @@ do
94 elseif args.project_tree then 94 elseif args.project_tree then
95 local tree = args.project_tree 95 local tree = args.project_tree
96 table.insert(cfg.rocks_trees, 1, { name = "project", root = tree } ) 96 table.insert(cfg.rocks_trees, 1, { name = "project", root = tree } )
97 loader.load_rocks_trees() 97 manif.load_rocks_tree_manifests()
98 path.use_tree(tree) 98 path.use_tree(tree)
99 elseif cfg.local_by_default then 99 elseif cfg.local_by_default then
100 if cfg.home_tree then 100 if cfg.home_tree then
@@ -103,7 +103,7 @@ do
103 elseif project_dir then 103 elseif project_dir then
104 local project_tree = project_dir .. "/lua_modules" 104 local project_tree = project_dir .. "/lua_modules"
105 table.insert(cfg.rocks_trees, 1, { name = "project", root = project_tree } ) 105 table.insert(cfg.rocks_trees, 1, { name = "project", root = project_tree } )
106 loader.load_rocks_trees() 106 manif.load_rocks_tree_manifests()
107 path.use_tree(project_tree) 107 path.use_tree(project_tree)
108 else 108 else
109 local trees = cfg.rocks_trees 109 local trees = cfg.rocks_trees
diff --git a/src/luarocks/core/dir.lua b/src/luarocks/core/dir.lua
index 70a82bd4..59b7749c 100644
--- a/src/luarocks/core/dir.lua
+++ b/src/luarocks/core/dir.lua
@@ -37,5 +37,17 @@ function dir.split_url(url)
37 return protocol, pathname 37 return protocol, pathname
38end 38end
39 39
40--- Normalize a url or local path.
41-- URLs should be in the "protocol://path" format. System independent
42-- forward slashes are used, removing trailing and double slashes
43-- @param url string: an URL or a local pathname.
44-- @return string: Normalized result.
45function dir.normalize(name)
46 local protocol, pathname = dir.split_url(name)
47 pathname = pathname:gsub("\\", "/"):gsub("(.)/*$", "%1"):gsub("//", "/")
48 if protocol ~= "file" then pathname = protocol .."://"..pathname end
49 return pathname
50end
51
40return dir 52return dir
41 53
diff --git a/src/luarocks/core/manif.lua b/src/luarocks/core/manif.lua
index 7a561f8a..4fd35c6c 100644
--- a/src/luarocks/core/manif.lua
+++ b/src/luarocks/core/manif.lua
@@ -5,6 +5,9 @@ local manif = {}
5local persist = require("luarocks.core.persist") 5local persist = require("luarocks.core.persist")
6local cfg = require("luarocks.core.cfg") 6local cfg = require("luarocks.core.cfg")
7local dir = require("luarocks.core.dir") 7local dir = require("luarocks.core.dir")
8local util = require("luarocks.core.util")
9local vers = require("luarocks.core.vers")
10local path = require("luarocks.core.path")
8local require = nil 11local require = nil
9-------------------------------------------------------------------------------- 12--------------------------------------------------------------------------------
10 13
@@ -64,4 +67,48 @@ function manif.fast_load_local_manifest(repo_url)
64 return manif.manifest_loader(pathname, repo_url, nil, true) 67 return manif.manifest_loader(pathname, repo_url, nil, true)
65end 68end
66 69
70function manif.load_rocks_tree_manifests(deps_mode)
71 local trees = {}
72 path.map_trees(deps_mode, function(tree)
73 local manifest, err = manif.fast_load_local_manifest(path.rocks_dir(tree))
74 if manifest then
75 table.insert(trees, {tree=tree, manifest=manifest})
76 end
77 end)
78 return trees
79end
80
81function manif.scan_dependencies(name, version, tree_manifests, dest)
82 if dest[name] then
83 return
84 end
85 dest[name] = version
86
87 for _, tree in ipairs(tree_manifests) do
88 local manifest = tree.manifest
89
90 local pkgdeps
91 if manifest.dependencies and manifest.dependencies[name] then
92 pkgdeps = manifest.dependencies[name][version]
93 end
94 if not pkgdeps then
95 return nil
96 end
97 for _, dep in ipairs(pkgdeps) do
98 local pkg, constraints = dep.name, dep.constraints
99
100 for _, t in ipairs(tree_manifests) do
101 local entries = t.manifest.repository[pkg]
102 if entries then
103 for ver, _ in util.sortedpairs(entries, vers.compare_versions) do
104 if (not constraints) or vers.match_constraints(vers.parse_version(ver), constraints) then
105 manif.scan_dependencies(pkg, version, tree_manifests, dest)
106 end
107 end
108 end
109 end
110 end
111 end
112end
113
67return manif 114return manif
diff --git a/src/luarocks/core/path.lua b/src/luarocks/core/path.lua
index 179d3aaf..bf6bdc2d 100644
--- a/src/luarocks/core/path.lua
+++ b/src/luarocks/core/path.lua
@@ -109,4 +109,42 @@ function path.which_i(file_name, name, version, tree, i)
109 return file_name 109 return file_name
110end 110end
111 111
112function 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
119end
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.
128function path.map_trees(deps_mode, fn, ...)
129 local result = {}
130 local current = cfg.root_dir or cfg.rocks_trees[1]
131 if deps_mode == "one" then
132 table.insert(result, (fn(current, ...)) or 0)
133 else
134 local use = false
135 if deps_mode == "all" then
136 use = true
137 end
138 for _, tree in ipairs(cfg.rocks_trees or {}) do
139 if dir.normalize(path.rocks_tree_to_string(tree)) == dir.normalize(path.rocks_tree_to_string(current)) then
140 use = true
141 end
142 if use then
143 table.insert(result, (fn(tree, ...)) or 0)
144 end
145 end
146 end
147 return result
148end
149
112return path 150return path
diff --git a/src/luarocks/dir.lua b/src/luarocks/dir.lua
index 679e8f0d..74adbea2 100644
--- a/src/luarocks/dir.lua
+++ b/src/luarocks/dir.lua
@@ -6,6 +6,7 @@ local core = require("luarocks.core.dir")
6 6
7dir.path = core.path 7dir.path = core.path
8dir.split_url = core.split_url 8dir.split_url = core.split_url
9dir.normalize = core.normalize
9 10
10--- Strip the path off a path+filename. 11--- Strip the path off a path+filename.
11-- @param pathname string: A path+name, such as "/a/b/c" 12-- @param pathname string: A path+name, such as "/a/b/c"
@@ -28,18 +29,6 @@ function dir.dir_name(pathname)
28 return (pathname:gsub("/*$", ""):match("(.*)[/]+[^/]*")) or "" 29 return (pathname:gsub("/*$", ""):match("(.*)[/]+[^/]*")) or ""
29end 30end
30 31
31--- Normalize a url or local path.
32-- URLs should be in the "protocol://path" format. System independent
33-- forward slashes are used, removing trailing and double slashes
34-- @param url string: an URL or a local pathname.
35-- @return string: Normalized result.
36function dir.normalize(name)
37 local protocol, pathname = dir.split_url(name)
38 pathname = pathname:gsub("\\", "/"):gsub("(.)/*$", "%1"):gsub("//", "/")
39 if protocol ~= "file" then pathname = protocol .."://"..pathname end
40 return pathname
41end
42
43--- Returns true if protocol does not require additional tools. 32--- Returns true if protocol does not require additional tools.
44-- @param protocol The protocol name 33-- @param protocol The protocol name
45function dir.is_basic_protocol(protocol) 34function dir.is_basic_protocol(protocol)
diff --git a/src/luarocks/loader.lua b/src/luarocks/loader.lua
index 780a0371..0cc76027 100644
--- a/src/luarocks/loader.lua
+++ b/src/luarocks/loader.lua
@@ -24,7 +24,6 @@ end
24local path = require("luarocks.core.path") 24local path = require("luarocks.core.path")
25local manif = require("luarocks.core.manif") 25local manif = require("luarocks.core.manif")
26local vers = require("luarocks.core.vers") 26local vers = require("luarocks.core.vers")
27local util = require("luarocks.core.util")
28local require = nil 27local require = nil
29-------------------------------------------------------------------------------- 28--------------------------------------------------------------------------------
30 29
@@ -52,29 +51,6 @@ end
52 51
53loader.context = {} 52loader.context = {}
54 53
55-- Contains a table when rocks trees are loaded,
56-- or 'false' to indicate rocks trees failed to load.
57-- 'nil' indicates rocks trees were not attempted to be loaded yet.
58loader.rocks_trees = nil
59
60function loader.load_rocks_trees()
61 local any_ok = false
62 local trees = {}
63 for _, tree in ipairs(cfg.rocks_trees or {}) do
64 local manifest, err = manif.fast_load_local_manifest(path.rocks_dir(tree))
65 if manifest then
66 any_ok = true
67 table.insert(trees, {tree=tree, manifest=manifest})
68 end
69 end
70 if not any_ok then
71 loader.rocks_trees = false
72 return false
73 end
74 loader.rocks_trees = trees
75 return true
76end
77
78--- Process the dependencies of a package to determine its dependency 54--- Process the dependencies of a package to determine its dependency
79-- chain for loading modules. 55-- chain for loading modules.
80-- @param name string: The name of an installed rock. 56-- @param name string: The name of an installed rock.
@@ -90,40 +66,12 @@ function loader.add_context(name, version)
90 temporary_global = false 66 temporary_global = false
91 end 67 end
92 68
93 if loader.context[name] then 69 local tree_manifests = manif.load_rocks_tree_manifests()
94 return 70 if not tree_manifests then
95 end
96 loader.context[name] = version
97
98 if not loader.rocks_trees and not loader.load_rocks_trees() then
99 return nil 71 return nil
100 end 72 end
101 73
102 for _, tree in ipairs(loader.rocks_trees) do 74 return manif.scan_dependencies(name, version, tree_manifests, loader.context)
103 local manifest = tree.manifest
104
105 local pkgdeps
106 if manifest.dependencies and manifest.dependencies[name] then
107 pkgdeps = manifest.dependencies[name][version]
108 end
109 if not pkgdeps then
110 return nil
111 end
112 for _, dep in ipairs(pkgdeps) do
113 local pkg, constraints = dep.name, dep.constraints
114
115 for _, tree in ipairs(loader.rocks_trees) do
116 local entries = tree.manifest.repository[pkg]
117 if entries then
118 for ver, pkgs in util.sortedpairs(entries, vers.compare_versions) do
119 if (not constraints) or vers.match_constraints(vers.parse_version(ver), constraints) then
120 loader.add_context(pkg, version)
121 end
122 end
123 end
124 end
125 end
126 end
127end 75end
128 76
129--- Internal sorting function. 77--- Internal sorting function.
@@ -186,18 +134,19 @@ end
186-- * name of the rock containing the module (eg. "luasocket") 134-- * name of the rock containing the module (eg. "luasocket")
187-- * version of the rock (eg. "2.0.2-1") 135-- * version of the rock (eg. "2.0.2-1")
188-- * return value of filter_file_name 136-- * return value of filter_file_name
189-- * tree of the module (string or table in `rocks_trees` format) 137-- * tree of the module (string or table in `tree_manifests` format)
190local function select_module(module, filter_file_name) 138local function select_module(module, filter_file_name)
191 --assert(type(module) == "string") 139 --assert(type(module) == "string")
192 --assert(type(filter_module_name) == "function") 140 --assert(type(filter_module_name) == "function")
193 141
194 if not loader.rocks_trees and not loader.load_rocks_trees() then 142 local tree_manifests = manif.load_rocks_tree_manifests()
143 if not tree_manifests then
195 return nil 144 return nil
196 end 145 end
197 146
198 local providers = {} 147 local providers = {}
199 local initmodule 148 local initmodule
200 for _, tree in ipairs(loader.rocks_trees) do 149 for _, tree in ipairs(tree_manifests) do
201 local entries = tree.manifest.modules[module] 150 local entries = tree.manifest.modules[module]
202 if entries then 151 if entries then
203 add_providers(providers, entries, tree, module, filter_file_name) 152 add_providers(providers, entries, tree, module, filter_file_name)
@@ -223,7 +172,7 @@ end
223-- * name of the rock containing the module (eg. "luasocket") 172-- * name of the rock containing the module (eg. "luasocket")
224-- * version of the rock (eg. "2.0.2-1") 173-- * version of the rock (eg. "2.0.2-1")
225-- * name of the module (eg. "socket.core", or "socket.core_2_0_2" if file is stored versioned). 174-- * name of the module (eg. "socket.core", or "socket.core_2_0_2" if file is stored versioned).
226-- * tree of the module (string or table in `rocks_trees` format) 175-- * tree of the module (string or table in `tree_manifests` format)
227local function pick_module(module) 176local function pick_module(module)
228 return 177 return
229 select_module(module, function(file_name, name, version, tree, i) 178 select_module(module, function(file_name, name, version, tree, i)
diff --git a/src/luarocks/manif.lua b/src/luarocks/manif.lua
index 1337cab3..b9eadc5b 100644
--- a/src/luarocks/manif.lua
+++ b/src/luarocks/manif.lua
@@ -16,6 +16,8 @@ local queries = require("luarocks.queries")
16local type_manifest = require("luarocks.type.manifest") 16local type_manifest = require("luarocks.type.manifest")
17 17
18manif.cache_manifest = core.cache_manifest 18manif.cache_manifest = core.cache_manifest
19manif.load_rocks_tree_manifests = core.load_rocks_tree_manifests
20manif.scan_dependencies = core.scan_dependencies
19 21
20manif.rock_manifest_cache = {} 22manif.rock_manifest_cache = {}
21 23
diff --git a/src/luarocks/path.lua b/src/luarocks/path.lua
index c7ab9283..b3b01704 100644
--- a/src/luarocks/path.lua
+++ b/src/luarocks/path.lua
@@ -14,6 +14,8 @@ path.versioned_name = core.versioned_name
14path.path_to_module = core.path_to_module 14path.path_to_module = core.path_to_module
15path.deploy_lua_dir = core.deploy_lua_dir 15path.deploy_lua_dir = core.deploy_lua_dir
16path.deploy_lib_dir = core.deploy_lib_dir 16path.deploy_lib_dir = core.deploy_lib_dir
17path.map_trees = core.map_trees
18path.rocks_tree_to_string = core.rocks_tree_to_string
17 19
18--- Infer rockspec filename from a rock filename. 20--- Infer rockspec filename from a rock filename.
19-- @param rock_name string: Pathname of a rock file. 21-- @param rock_name string: Pathname of a rock file.
@@ -226,43 +228,6 @@ function path.use_tree(tree)
226 -- .. package.cpath 228 -- .. package.cpath
227end 229end
228 230
229function path.rocks_tree_to_string(tree)
230 if type(tree) == "string" then
231 return tree
232 else
233 assert(type(tree) == "table")
234 return tree.root
235 end
236end
237
238--- Apply a given function to the active rocks trees based on chosen dependency mode.
239-- @param deps_mode string: Dependency mode: "one" for the current default tree,
240-- "all" for all trees, "order" for all trees with priority >= the current default,
241-- "none" for no trees (this function becomes a nop).
242-- @param fn function: function to be applied, with the tree dir (string) as the first
243-- argument and the remaining varargs of map_trees as the following arguments.
244-- @return a table with all results of invocations of fn collected.
245function path.map_trees(deps_mode, fn, ...)
246 local result = {}
247 if deps_mode == "one" then
248 table.insert(result, (fn(cfg.root_dir, ...)) or 0)
249 elseif deps_mode == "all" or deps_mode == "order" then
250 local use = false
251 if deps_mode == "all" then
252 use = true
253 end
254 for _, tree in ipairs(cfg.rocks_trees) do
255 if dir.normalize(path.rocks_tree_to_string(tree)) == dir.normalize(path.rocks_tree_to_string(cfg.root_dir)) then
256 use = true
257 end
258 if use then
259 table.insert(result, (fn(tree, ...)) or 0)
260 end
261 end
262 end
263 return result
264end
265
266--- Get the namespace of a locally-installed rock, if any. 231--- Get the namespace of a locally-installed rock, if any.
267-- @param name string: The rock name, without a namespace. 232-- @param name string: The rock name, without a namespace.
268-- @param version string: The rock version. 233-- @param version string: The rock version.