aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorhisham <hisham@9ca3f7c1-7366-0410-b1a3-b5c78f85698c>2009-07-16 21:04:24 +0000
committerhisham <hisham@9ca3f7c1-7366-0410-b1a3-b5c78f85698c>2009-07-16 21:04:24 +0000
commitba906abeafae6ebc199bb3ed447bc2a31bb9b770 (patch)
treefc6a0064dceca02fd3bcc16356b9ec75305ec71a /src
parent565a4515e628dfb540539b18a2998aa915917409 (diff)
downloadluarocks-ba906abeafae6ebc199bb3ed447bc2a31bb9b770.tar.gz
luarocks-ba906abeafae6ebc199bb3ed447bc2a31bb9b770.tar.bz2
luarocks-ba906abeafae6ebc199bb3ed447bc2a31bb9b770.zip
add wip version of new package loader
git-svn-id: http://luarocks.org/svn/luarocks/trunk@40 9ca3f7c1-7366-0410-b1a3-b5c78f85698c
Diffstat (limited to 'src')
-rw-r--r--src/luarocks2.lua147
1 files changed, 147 insertions, 0 deletions
diff --git a/src/luarocks2.lua b/src/luarocks2.lua
new file mode 100644
index 00000000..0ca2f505
--- /dev/null
+++ b/src/luarocks2.lua
@@ -0,0 +1,147 @@
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("luarocks")
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
24 -- FIXME select correctly file to be fetched
25 local persist = require("luarocks.persist")
26 table.insert(trees, { manifest = persist.load_into_table("manifest2") } )
27 any_ok = true
28
29 if not any_ok then
30 rocks_trees = false
31 return false
32 end
33 rocks_trees = trees
34 return true
35end
36
37--- Process the dependencies of a package to determine its dependency
38-- chain for loading modules.
39-- @parse name string: The name of an installed rock.
40-- @parse version string: The version of the rock, in string format
41-- @parse manifest table: The local manifest table where this rock
42-- is installed.
43local function add_context(name, version)
44 -- assert(type(name) == "string")
45 -- assert(type(version) == "string")
46 -- assert(type(manifest) == "table")
47
48 if context[name] then
49 return
50 end
51 context[name] = version
52 --[[
53
54 local pkgdeps = manifest.dependencies and manifest.dependencies[name][version]
55 if not pkgdeps then
56 return
57 end
58 for _, dep in ipairs(pkgdeps) do
59 local package, constraints = dep.name, dep.constraints
60
61 for _, tree in pairs(rocks_trees) do
62 local entries = tree.manifest.repository[package]
63 if entries then
64 for version, packages in pairs(entries) do
65 if (not constraints) or deps.match_constraints(deps.parse_version(version), constraints) then
66 add_context(package, version, tree.manifest)
67 end
68 end
69 end
70 end
71 end
72 ]]
73end
74
75--- Internal sorting function.
76-- @param a table: A provider table.
77-- @param b table: Another provider table.
78-- @return boolean: True if the version of a is greater than that of b.
79local function sort_versions(a,b)
80 return a.version > b.version
81end
82
83local function call_other_loaders(module, name, version, file)
84
85 local actual_module = file:match("(.*)%.[^.]+$")
86
87 for i, loader in pairs(package.loaders) do
88 if loader ~= luarocks_loader then
89 local results = { loader(actual_module) }
90 if type(results[1]) == "function" then
91 return unpack(results)
92 end
93 end
94 end
95 return nil, "Failed loading module "..module.." in LuaRocks rock "..name.." "..version
96end
97
98local function pick_module(module)
99 --assert(type(module) == "string")
100
101 if not rocks_trees and not load_rocks_trees() then
102 return nil
103 end
104
105 local providers = {}
106 for _, tree in pairs(rocks_trees) do
107 local entries = tree.manifest.modules[module]
108 if entries then
109 for entry, file in pairs(entries) do
110 local name, version = entry:match("^([^/]*)/(.*)$")
111 if context[name] == version then
112 return name, version, file
113 end
114 version = deps.parse_version(version)
115 table.insert(providers, {name = name, version = version, file = file})
116 end
117 end
118 end
119
120 if next(providers) then
121 table.sort(providers, sort_versions)
122 local first = providers[1]
123 return first.name, first.version.string, first.file
124 end
125end
126
127--- Package loader for LuaRocks support.
128-- A module is searched in installed rocks that match the
129-- current LuaRocks context. If module is not part of the
130-- context, or if a context has not yet been set, the module
131-- in the package with the highest version is used.
132-- @param module string: The module name, like in plain require().
133-- @return table: The module table (typically), like in plain
134-- require(). See <a href="http://www.lua.org/manual/5.1/manual.html#pdf-require">require()</a>
135-- in the Lua reference manual for details.
136
137function luarocks_loader(module)
138 local name, version, file = pick_module(module)
139 if not name then
140 return nil, "No LuaRocks module found for "..module
141 else
142 add_context(name, version)
143 return call_other_loaders(module, name, version, file)
144 end
145end
146
147table.insert(global_env.package.loaders, 1, luarocks_loader)