comparison share/lua/5.2/luarocks/install.lua @ 1132:d137f631bad5

<GreyKnight> (cd luabuild/luarocks-2.0.12; make install)
author HackBot
date Fri, 14 Dec 2012 22:24:27 +0000
parents
children
comparison
equal deleted inserted replaced
1131:ff65dfb63263 1132:d137f631bad5
1
2 --- Module implementing the LuaRocks "install" command.
3 -- Installs binary rocks.
4 module("luarocks.install", package.seeall)
5
6 local path = require("luarocks.path")
7 local repos = require("luarocks.repos")
8 local fetch = require("luarocks.fetch")
9 local util = require("luarocks.util")
10 local fs = require("luarocks.fs")
11 local deps = require("luarocks.deps")
12 local manif = require("luarocks.manif")
13 local cfg = require("luarocks.cfg")
14
15 help_summary = "Install a rock."
16
17 help_arguments = "{<rock>|<name> [<version>]}"
18
19 help = [[
20 Argument may be the name of a rock to be fetched from a repository
21 or a filename of a locally available rock.
22 ]]
23
24 --- Install a binary rock.
25 -- @param rock_file string: local or remote filename of a rock.
26 -- @param deps_mode: string: Which trees to check dependencies for:
27 -- "none", "one", "order" or "all".
28 -- @return boolean or (nil, string, [string]): True if succeeded or
29 -- nil and an error message and an optional error code.
30 function install_binary_rock(rock_file, deps_mode)
31 assert(type(rock_file) == "string")
32
33 local name, version, arch = path.parse_name(rock_file)
34 if not name then
35 return nil, "Filename "..rock_file.." does not match format 'name-version-revision.arch.rock'."
36 end
37
38 if arch ~= "all" and arch ~= cfg.arch then
39 return nil, "Incompatible architecture "..arch, "arch"
40 end
41 if repos.is_installed(name, version) then
42 repos.delete_version(name, version)
43 end
44
45 local rollback = util.schedule_function(function()
46 fs.delete(path.install_dir(name, version))
47 fs.remove_dir_if_empty(path.versions_dir(name))
48 end)
49
50 local ok, err, errcode = fetch.fetch_and_unpack_rock(rock_file, path.install_dir(name, version))
51 if not ok then return nil, err, errcode end
52
53 local rockspec, err, errcode = fetch.load_rockspec(path.rockspec_file(name, version))
54 if err then
55 return nil, "Failed loading rockspec for installed package: "..err, errcode
56 end
57
58 if deps_mode == "none" then
59 util.printerr("Warning: skipping dependency checks.")
60 else
61 ok, err, errcode = deps.check_external_deps(rockspec, "install")
62 if err then return nil, err, errcode end
63 end
64
65 -- For compatibility with .rock files built with LuaRocks 1
66 if not fs.exists(path.rock_manifest_file(name, version)) then
67 ok, err = manif.make_rock_manifest(name, version)
68 if err then return nil, err end
69 end
70
71 if deps_mode ~= "none" then
72 ok, err, errcode = deps.fulfill_dependencies(rockspec, deps_mode)
73 if err then return nil, err, errcode end
74 end
75
76 local wrap_bin_scripts = true
77 if rockspec.deploy and rockspec.deploy.wrap_bin_scripts == false then
78 wrap_bin_scripts = false
79 end
80
81 ok, err = repos.deploy_files(name, version, repos.should_wrap_bin_scripts(rockspec))
82 if err then return nil, err end
83
84 util.remove_scheduled_function(rollback)
85 rollback = util.schedule_function(function()
86 repos.delete_version(name, version)
87 end)
88
89 ok, err = repos.run_hook(rockspec, "post_install")
90 if err then return nil, err end
91
92 ok, err = manif.update_manifest(name, version, nil, deps_mode)
93 if err then return nil, err end
94
95 local license = ""
96 if rockspec.description.license then
97 license = ("(license: "..rockspec.description.license..")")
98 end
99
100 local root_dir = path.root_dir(cfg.rocks_dir)
101 util.printout()
102 util.printout(name.." "..version.." is now installed in "..root_dir.." "..license)
103
104 util.remove_scheduled_function(rollback)
105 return true
106 end
107
108 --- Driver function for the "install" command.
109 -- @param name string: name of a binary rock. If an URL or pathname
110 -- to a binary rock is given, fetches and installs it. If a rockspec or a
111 -- source rock is given, forwards the request to the "build" command.
112 -- If a package name is given, forwards the request to "search" and,
113 -- if returned a result, installs the matching rock.
114 -- @param version string: When passing a package name, a version number
115 -- may also be given.
116 -- @return boolean or (nil, string): True if installation was
117 -- successful, nil and an error message otherwise.
118 function run(...)
119 local flags, name, version = util.parse_flags(...)
120 if type(name) ~= "string" then
121 return nil, "Argument missing, see help."
122 end
123
124 local ok, err = fs.check_command_permissions(flags)
125 if not ok then return nil, err end
126
127 if name:match("%.rockspec$") or name:match("%.src%.rock$") then
128 util.printout("Using "..name.."... switching to 'build' mode")
129 local build = require("luarocks.build")
130 return build.run(name, deps.get_deps_mode(flags), flags["local"] and "--local")
131 elseif name:match("%.rock$") then
132 return install_binary_rock(name, deps.get_deps_mode(flags))
133 else
134 local search = require("luarocks.search")
135 local results, err = search.find_suitable_rock(search.make_query(name:lower(), version))
136 if err then
137 return nil, err
138 elseif type(results) == "string" then
139 local url = results
140 util.printout("Installing "..url.."...")
141 return run(url)
142 else
143 util.printout()
144 util.printerr("Could not determine which rock to install.")
145 util.title("Search results:")
146 search.print_results(results)
147 return nil, (next(results) and "Please narrow your query." or "No results found.")
148 end
149 end
150 end