aboutsummaryrefslogtreecommitdiff
path: root/src/luarocks2.lua
diff options
context:
space:
mode:
authorhisham <hisham@9ca3f7c1-7366-0410-b1a3-b5c78f85698c>2009-10-08 18:42:37 +0000
committerhisham <hisham@9ca3f7c1-7366-0410-b1a3-b5c78f85698c>2009-10-08 18:42:37 +0000
commitcf8d8c295380dff46e4a25031762eae0c7ec3b61 (patch)
tree827f7496a56888044a85e30e5aea5ad713dfb73b /src/luarocks2.lua
parent653cf4569aa7c9d3a25c470829765d4672ae8359 (diff)
downloadluarocks-cf8d8c295380dff46e4a25031762eae0c7ec3b61.tar.gz
luarocks-cf8d8c295380dff46e4a25031762eae0c7ec3b61.tar.bz2
luarocks-cf8d8c295380dff46e4a25031762eae0c7ec3b61.zip
Let's use luarocks.loader as the name
git-svn-id: http://luarocks.org/svn/luarocks/trunk@71 9ca3f7c1-7366-0410-b1a3-b5c78f85698c
Diffstat (limited to 'src/luarocks2.lua')
-rw-r--r--src/luarocks2.lua167
1 files changed, 0 insertions, 167 deletions
diff --git a/src/luarocks2.lua b/src/luarocks2.lua
deleted file mode 100644
index f50bddf2..00000000
--- a/src/luarocks2.lua
+++ /dev/null
@@ -1,167 +0,0 @@
1
2local global_env = _G
3local package, require, assert, ipairs, pairs, os, print, table, type, next, unpack =
4 package, require, assert, ipairs, pairs, os, print, table, type, next, unpack
5
6module("luarocks2")
7
8local path = require("luarocks.path")
9local manif_core = require("luarocks.manif_core")
10local deps = require("luarocks.deps")
11local cfg = require("luarocks.cfg")
12
13context = {}
14
15-- Contains a table when rocks trees are loaded,
16-- or 'false' to indicate rocks trees failed to load.
17-- 'nil' indicates rocks trees were not attempted to be loaded yet.
18rocks_trees = nil
19
20local function load_rocks_trees()
21 local any_ok = false
22 local trees = {}
23 for _, tree in pairs(cfg.rocks_trees) do
24 local rocks_dir = path.rocks_dir(tree)
25 local manifest, err = manif_core.load_local_manifest(rocks_dir)
26 if manifest then
27 any_ok = true
28 table.insert(trees, {rocks_dir=rocks_dir, manifest=manifest})
29 end
30 end
31 if not any_ok then
32 rocks_trees = false
33 return false
34 end
35 rocks_trees = trees
36 return true
37end
38
39--- Process the dependencies of a package to determine its dependency
40-- chain for loading modules.
41-- @parse name string: The name of an installed rock.
42-- @parse version string: The version of the rock, in string format
43-- @parse manifest table: The local manifest table where this rock
44-- is installed.
45function add_context(name, version)
46 -- assert(type(name) == "string")
47 -- assert(type(version) == "string")
48 -- assert(type(manifest) == "table")
49
50 if context[name] then
51 return
52 end
53 context[name] = version
54
55 if not rocks_trees and not load_rocks_trees() then
56 return nil
57 end
58
59 local providers = {}
60 for _, tree in pairs(rocks_trees) do
61 local manifest = tree.manifest
62
63 local pkgdeps = manifest.dependencies and manifest.dependencies[name][version]
64 if not pkgdeps then
65 return
66 end
67 for _, dep in ipairs(pkgdeps) do
68 local package, constraints = dep.name, dep.constraints
69
70 for _, tree in pairs(rocks_trees) do
71 local entries = tree.manifest.repository[package]
72 if entries then
73 for version, packages in pairs(entries) do
74 if (not constraints) or deps.match_constraints(deps.parse_version(version), constraints) then
75 add_context(package, version, tree.manifest)
76 end
77 end
78 end
79 end
80 end
81 end
82end
83
84--- Internal sorting function.
85-- @param a table: A provider table.
86-- @param b table: Another provider table.
87-- @return boolean: True if the version of a is greater than that of b.
88local function sort_versions(a,b)
89 return a.version > b.version
90end
91
92local function call_other_loaders(module, name, version, file)
93
94 local actual_module = file:match(".*/(.*)%.[^.]+$")
95 for i, loader in pairs(package.loaders) do
96 if loader ~= luarocks_loader then
97 local results = { loader(actual_module) }
98 if type(results[1]) == "function" then
99 return unpack(results)
100 end
101 end
102 end
103 return nil, "Failed loading module "..module.." in LuaRocks rock "..name.." "..version
104end
105
106local function pick_module(module)
107 --assert(type(module) == "string")
108
109 if not rocks_trees and not load_rocks_trees() then
110 return nil
111 end
112
113 local providers = {}
114 for _, tree in pairs(rocks_trees) do
115 local entries = tree.manifest.modules[module]
116 if entries then
117 for i, entry in ipairs(entries) do
118 local name, version = entry:match("^([^/]*)/(.*)$")
119 local file = tree.manifest.repository[name][version][1].modules[module]
120 local deploy_dir
121 if file:match(cfg.lua_extension.."$") then
122 deploy_dir = cfg.deploy_lua_dir
123 else
124 deploy_dir = cfg.deploy_bin_dir
125 end
126 if i == 1 then
127 file = deploy_dir.."/"..file
128 else
129 file = path.versioned_name(deploy_dir.."/"..file, name, version)
130 end
131 if context[name] == version then
132 return name, version, file
133 end
134 version = deps.parse_version(version)
135 table.insert(providers, {name = name, version = version, file = file})
136 end
137 end
138 end
139
140 if next(providers) then
141 table.sort(providers, sort_versions)
142 local first = providers[1]
143 return first.name, first.version.string, first.file
144 end
145end
146
147--- Package loader for LuaRocks support.
148-- A module is searched in installed rocks that match the
149-- current LuaRocks context. If module is not part of the
150-- context, or if a context has not yet been set, the module
151-- in the package with the highest version is used.
152-- @param module string: The module name, like in plain require().
153-- @return table: The module table (typically), like in plain
154-- require(). See <a href="http://www.lua.org/manual/5.1/manual.html#pdf-require">require()</a>
155-- in the Lua reference manual for details.
156
157function luarocks_loader(module)
158 local name, version, file = pick_module(module)
159 if not name then
160 return nil, "No LuaRocks module found for "..module
161 else
162 add_context(name, version)
163 return call_other_loaders(module, name, version, file)
164 end
165end
166
167table.insert(global_env.package.loaders, 1, luarocks_loader)