diff options
author | hisham <hisham@9ca3f7c1-7366-0410-b1a3-b5c78f85698c> | 2009-10-04 01:47:30 +0000 |
---|---|---|
committer | hisham <hisham@9ca3f7c1-7366-0410-b1a3-b5c78f85698c> | 2009-10-04 01:47:30 +0000 |
commit | e1ac67028db826a17126df0d44c1060522a9ec9c (patch) | |
tree | fed718cd281333ed4c6f4f0dfb72f763eb359ee6 | |
parent | f042f6cd3d4587d1c5339f73706d78a714c95030 (diff) | |
download | luarocks-e1ac67028db826a17126df0d44c1060522a9ec9c.tar.gz luarocks-e1ac67028db826a17126df0d44c1060522a9ec9c.tar.bz2 luarocks-e1ac67028db826a17126df0d44c1060522a9ec9c.zip |
Remove luarocks.require
git-svn-id: http://luarocks.org/svn/luarocks/trunk@56 9ca3f7c1-7366-0410-b1a3-b5c78f85698c
-rw-r--r-- | src/luarocks/require.lua | 263 |
1 files changed, 0 insertions, 263 deletions
diff --git a/src/luarocks/require.lua b/src/luarocks/require.lua deleted file mode 100644 index 7d083fca..00000000 --- a/src/luarocks/require.lua +++ /dev/null | |||
@@ -1,263 +0,0 @@ | |||
1 | |||
2 | local global_env = _G | ||
3 | local plain_require = require | ||
4 | local plain_package_path = package.path | ||
5 | local plain_package_cpath = package.cpath | ||
6 | local package, assert, ipairs, pairs, os, print, table, type, next = | ||
7 | package, assert, ipairs, pairs, os, print, table, type, next | ||
8 | |||
9 | --- Application interface with LuaRocks. | ||
10 | -- Load this module to LuaRocks-enable an application: | ||
11 | -- this overrides the require() function, making it able to | ||
12 | -- load modules installed as rocks. | ||
13 | module("luarocks.require") | ||
14 | |||
15 | local path = plain_require("luarocks.path") | ||
16 | local manif_core = plain_require("luarocks.manif_core") | ||
17 | local deps = plain_require("luarocks.deps") | ||
18 | local cfg = plain_require("luarocks.cfg") | ||
19 | |||
20 | context = {} | ||
21 | |||
22 | -- Contains a table when rocks trees are loaded, | ||
23 | -- or 'false' to indicate rocks trees failed to load. | ||
24 | -- 'nil' indicates rocks trees were not attempted to be loaded yet. | ||
25 | rocks_trees = nil | ||
26 | |||
27 | local function load_rocks_trees() | ||
28 | local any_ok = false | ||
29 | local trees = {} | ||
30 | for _, tree in pairs(cfg.rocks_trees) do | ||
31 | local rocks_dir = tree .. "/rocks/" | ||
32 | local manifest, err = manif_core.load_local_manifest(rocks_dir) | ||
33 | if manifest then | ||
34 | any_ok = true | ||
35 | table.insert(trees, {rocks_dir=rocks_dir, manifest=manifest}) | ||
36 | end | ||
37 | end | ||
38 | if not any_ok then | ||
39 | rocks_trees = false | ||
40 | return false | ||
41 | end | ||
42 | rocks_trees = trees | ||
43 | return true | ||
44 | end | ||
45 | |||
46 | --- Process the dependencies of a package to determine its dependency | ||
47 | -- chain for loading modules. | ||
48 | -- @parse name string: The name of an installed rock. | ||
49 | -- @parse version string: The version of the rock, in string format | ||
50 | -- @parse manifest table: The local manifest table where this rock | ||
51 | -- is installed. | ||
52 | local function add_context(name, version, manifest) | ||
53 | -- assert(type(name) == "string") | ||
54 | -- assert(type(version) == "string") | ||
55 | -- assert(type(manifest) == "table") | ||
56 | |||
57 | if context[name] then | ||
58 | return | ||
59 | end | ||
60 | context[name] = version | ||
61 | |||
62 | local pkgdeps = manifest.dependencies and manifest.dependencies[name][version] | ||
63 | if pkgdeps then | ||
64 | for _, dep in ipairs(pkgdeps) do | ||
65 | local package, constraints = dep.name, dep.constraints | ||
66 | |||
67 | for _, tree in pairs(rocks_trees) do | ||
68 | local entries = tree.manifest.repository[package] | ||
69 | if entries then | ||
70 | for version, packages in pairs(entries) do | ||
71 | if (not constraints) or deps.match_constraints(deps.parse_version(version), constraints) then | ||
72 | add_context(package, version, tree.manifest) | ||
73 | end | ||
74 | end | ||
75 | end | ||
76 | end | ||
77 | end | ||
78 | end | ||
79 | end | ||
80 | |||
81 | --- Internal sorting function. | ||
82 | -- @param a table: A provider table. | ||
83 | -- @param b table: Another provider table. | ||
84 | -- @return boolean: True if the version of a is greater than that of b. | ||
85 | local function sort_versions(a,b) | ||
86 | return a.version > b.version | ||
87 | end | ||
88 | |||
89 | --- Specify a dependency chain for LuaRocks. | ||
90 | -- In the presence of multiple versions of packages, it is necessary to, | ||
91 | -- at some point, indicate which dependency chain we're following. | ||
92 | -- set_context does this by allowing one to pick a package to be the | ||
93 | -- root of this dependency chain. Once a dependency chain is picked it's | ||
94 | -- easy to know which modules to load ("I want to use *this* version of | ||
95 | -- A, which requires *that* version of B, which requires etc etc etc"). | ||
96 | -- @param name string: The package name of an installed rock. | ||
97 | -- @param version string or nil: Optionally, a version number | ||
98 | -- When a version is not given, it picks the highest version installed. | ||
99 | -- @return boolean: true if succeeded, false otherwise. | ||
100 | function set_context(name, version) | ||
101 | --assert(type(name) == "string") | ||
102 | --assert(type(version) == "string" or not version) | ||
103 | |||
104 | if rocks_trees == false or (not rocks_trees and not load_rocks_trees()) then | ||
105 | return false | ||
106 | end | ||
107 | |||
108 | local manifest | ||
109 | local vtables = {} | ||
110 | for _, tree in ipairs(rocks_trees) do | ||
111 | if version then | ||
112 | local manif_repo = tree.manifest.repository | ||
113 | if manif_repo[name] and manif_repo[name][version] then | ||
114 | manifest = tree.manifest | ||
115 | break | ||
116 | end | ||
117 | else | ||
118 | local versions = manif_core.get_versions(name, tree.manifest) | ||
119 | for _, version in ipairs(versions) do | ||
120 | table.insert(vtables, {version = deps.parse_version(version), manifest = tree.manifest}) | ||
121 | end | ||
122 | end | ||
123 | end | ||
124 | if not version then | ||
125 | if not next(vtables) then | ||
126 | table.sort(vtables, sort_versions) | ||
127 | local highest = vtables[#vtables] | ||
128 | version = highest.version.string | ||
129 | manifest = highest.manifest | ||
130 | end | ||
131 | end | ||
132 | if not manifest then | ||
133 | return false | ||
134 | end | ||
135 | |||
136 | add_context(name, version, manifest) | ||
137 | -- TODO: platform independence | ||
138 | local lpath, cpath = "", "" | ||
139 | for name, version in pairs(context) do | ||
140 | lpath = lpath .. path.lua_dir(name, version) .. "/?.lua;" | ||
141 | lpath = lpath .. path.lua_dir(name, version) .. "/?/init.lua;" | ||
142 | cpath = cpath .. path.lib_dir(name, version) .."/?."..cfg.lib_extension..";" | ||
143 | end | ||
144 | global_env.package.path = lpath .. plain_package_path | ||
145 | global_env.package.cpath = cpath .. plain_package_cpath | ||
146 | end | ||
147 | |||
148 | --- Call the vanilla require() function using specially constructed | ||
149 | -- package paths so that it finds exactly the version we want it to find. | ||
150 | -- @param name string: The rock name. | ||
151 | -- @param version string: The rock version. | ||
152 | -- @param module string: The module name, in require() notation. | ||
153 | -- @return The result returned by require(). | ||
154 | local function plain_require_on(module, name, version, rocks_dir, ...) | ||
155 | --assert(type(module) == "string") | ||
156 | --assert(type(name) == "string") | ||
157 | --assert(type(version) == "string") | ||
158 | |||
159 | local global_package = global_env.package | ||
160 | local save_path = global_package.path | ||
161 | local save_cpath = global_package.cpath | ||
162 | global_package.path = path.lua_dir(name, version, rocks_dir) .. "/?.lua;" | ||
163 | .. path.lua_dir(name, version, rocks_dir) .. "/?/init.lua;" .. save_path | ||
164 | global_package.cpath = path.lib_dir(name, version, rocks_dir) .. "/?."..cfg.lib_extension..";" .. save_cpath | ||
165 | local result = plain_require(module, ...) | ||
166 | global_package.path = save_path | ||
167 | global_package.cpath = save_cpath | ||
168 | return result | ||
169 | end | ||
170 | |||
171 | local function pick_module(module, constraints) | ||
172 | --assert(type(module) == "string") | ||
173 | --assert(not constraints or type(constraints) == "string") | ||
174 | |||
175 | if not rocks_trees and not load_rocks_trees() then | ||
176 | return nil | ||
177 | end | ||
178 | |||
179 | if constraints then | ||
180 | if type(constraints) == "string" then | ||
181 | constraints = deps.parse_constraints(constraints) | ||
182 | else | ||
183 | constraints = nil | ||
184 | end | ||
185 | end | ||
186 | |||
187 | local providers = {} | ||
188 | for _, tree in pairs(rocks_trees) do | ||
189 | local entries = tree.manifest.modules[module] | ||
190 | if entries then | ||
191 | for _, entry in pairs(entries) do | ||
192 | local name, version = entry:match("^([^/]*)/(.*)$") | ||
193 | if context[name] == version then | ||
194 | return name, version, tree | ||
195 | end | ||
196 | version = deps.parse_version(version) | ||
197 | if (not constraints) or deps.match_constraints(version, constraints) then | ||
198 | table.insert(providers, {name = name, version = version, repo = tree}) | ||
199 | end | ||
200 | end | ||
201 | end | ||
202 | end | ||
203 | |||
204 | if next(providers) then | ||
205 | table.sort(providers, sort_versions) | ||
206 | local first = providers[1] | ||
207 | return first.name, first.version.string, first.repo | ||
208 | end | ||
209 | end | ||
210 | |||
211 | --- Inform which rock LuaRocks would use if require() is called | ||
212 | -- with the given arguments. | ||
213 | -- @param module string: The module name, like in plain require(). | ||
214 | -- @param constraints string or nil: An optional comma-separated | ||
215 | -- list of version constraints. | ||
216 | -- @return (string, string) or nil: Rock name and version if the | ||
217 | -- requested module can be supplied by LuaRocks, or nil if it can't. | ||
218 | function get_rock_from_module(module, constraints) | ||
219 | --assert(type(module) == "string") | ||
220 | --assert(not constraints or type(constraints) == "string") | ||
221 | local name, version = pick_module(module, constraints) | ||
222 | return name, version | ||
223 | end | ||
224 | |||
225 | --- Function that overloads require(), adding LuaRocks support. | ||
226 | -- This function wraps around Lua's standard require() call, | ||
227 | -- allowing it to find modules installed by LuaRocks. | ||
228 | -- A module is searched in installed rocks that match the | ||
229 | -- current LuaRocks context. If module is not part of the | ||
230 | -- context, or if a context has not yet been set, the module | ||
231 | -- in the package with the highest version is used. | ||
232 | -- If a module is not available in any installed rock, plain | ||
233 | -- require() is called, using the original package.path and | ||
234 | -- package.cpath lookup locations. | ||
235 | -- Additionally, version constraints for the matching rock may | ||
236 | -- be given as a second parameter. If version constraints could | ||
237 | -- not be fulfilled, this equates to the rock not being | ||
238 | -- available: as such, plain require() with the default paths | ||
239 | -- is called as a fallback. | ||
240 | -- @param module string: The module name, like in plain require(). | ||
241 | -- @param constraints string or nil: An optional comma-separated | ||
242 | -- list of version constraints. | ||
243 | -- @return table: The module table (typically), like in plain | ||
244 | -- require(). See <a href="http://www.lua.org/manual/5.1/manual.html#pdf-require">require()</a> | ||
245 | -- in the Lua reference manual for details. | ||
246 | function require(module, ...) | ||
247 | --assert(type(module) == "string") | ||
248 | |||
249 | if package.loaded[module] or rocks_trees == false or (not rocks_trees and not load_rocks_trees()) then | ||
250 | return plain_require(module, ...) | ||
251 | end | ||
252 | |||
253 | local name, version, repo = pick_module(module, ...) | ||
254 | |||
255 | if not name then | ||
256 | return plain_require(module, ...) | ||
257 | else | ||
258 | add_context(name, version, repo.manifest) | ||
259 | return plain_require_on(module, name, version, repo.rocks_dir, ...) | ||
260 | end | ||
261 | end | ||
262 | |||
263 | global_env.require = require | ||