diff options
Diffstat (limited to '')
| -rw-r--r-- | src/luarocks/install.lua | 115 |
1 files changed, 115 insertions, 0 deletions
diff --git a/src/luarocks/install.lua b/src/luarocks/install.lua new file mode 100644 index 00000000..b115a8a8 --- /dev/null +++ b/src/luarocks/install.lua | |||
| @@ -0,0 +1,115 @@ | |||
| 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 rep = require("luarocks.rep") | ||
| 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 | -- @return boolean or (nil, string): True if succeeded or | ||
| 27 | -- nil and an error message. | ||
| 28 | function install_binary_rock(rock_file) | ||
| 29 | local name, version, arch = path.parse_rock_name(rock_file) | ||
| 30 | if not name then | ||
| 31 | return nil, "Filename "..rock_file.." does not match format 'name-version-revision.arch.rock'." | ||
| 32 | end | ||
| 33 | if arch ~= "all" and arch ~= cfg.arch then | ||
| 34 | return nil, "Incompatible architecture "..arch | ||
| 35 | end | ||
| 36 | if rep.is_installed(name, version) then | ||
| 37 | rep.delete_version(name, version) | ||
| 38 | end | ||
| 39 | local rollback = util.schedule_function(function() | ||
| 40 | fs.delete(path.install_dir(name, version)) | ||
| 41 | fs.remove_dir_if_empty(path.versions_dir(name)) | ||
| 42 | end) | ||
| 43 | local ok, err = fetch.fetch_and_unpack_rock(rock_file, path.install_dir(name, version)) | ||
| 44 | if not ok then return nil, err end | ||
| 45 | ok, err = rep.install_bins(name, version) | ||
| 46 | |||
| 47 | local rockspec, err = fetch.load_rockspec(path.rockspec_file(name, version)) | ||
| 48 | if err then | ||
| 49 | return nil, "Failed loading rockspec for installed package: "..err | ||
| 50 | end | ||
| 51 | |||
| 52 | ok, err = deps.check_external_deps(rockspec, "install") | ||
| 53 | if err then | ||
| 54 | return nil, err | ||
| 55 | end | ||
| 56 | |||
| 57 | ok, err = deps.fulfill_dependencies(rockspec) | ||
| 58 | if err then | ||
| 59 | return nil, err | ||
| 60 | end | ||
| 61 | |||
| 62 | ok, err = rep.run_hook(rockspec, "post_install") | ||
| 63 | if err then | ||
| 64 | return nil, err | ||
| 65 | end | ||
| 66 | |||
| 67 | ok, err = manif.update_manifest(name, version) | ||
| 68 | if err then | ||
| 69 | return nil, err | ||
| 70 | end | ||
| 71 | util.remove_scheduled_function(rollback) | ||
| 72 | return true | ||
| 73 | end | ||
| 74 | |||
| 75 | --- Driver function for the "install" command. | ||
| 76 | -- @param name string: name of a binary rock. If an URL or pathname | ||
| 77 | -- to a binary rock is given, fetches and installs it. If a rockspec or a | ||
| 78 | -- source rock is given, forwards the request to the "build" command. | ||
| 79 | -- If a package name is given, forwards the request to "search" and, | ||
| 80 | -- if returned a result, installs the matching rock. | ||
| 81 | -- @param version string: When passing a package name, a version number | ||
| 82 | -- may also be given. | ||
| 83 | -- @return boolean or (nil, string): True if installation was | ||
| 84 | -- successful, nil and an error message otherwise. | ||
| 85 | function run(...) | ||
| 86 | local flags, name, version = util.parse_flags(...) | ||
| 87 | if type(name) ~= "string" then | ||
| 88 | return nil, "Argument missing, see help." | ||
| 89 | end | ||
| 90 | |||
| 91 | if name:match("%.rockspec$") or name:match("%.src%.rock$") then | ||
| 92 | local build = require("luarocks.build") | ||
| 93 | return build.run(name) | ||
| 94 | elseif name:match("%.rock$") then | ||
| 95 | return install_binary_rock(name) | ||
| 96 | else | ||
| 97 | local search = require("luarocks.search") | ||
| 98 | local results, err = search.find_suitable_rock(search.make_query(name, version)) | ||
| 99 | if err then | ||
| 100 | return nil, err | ||
| 101 | elseif type(results) == "string" then | ||
| 102 | local url = results | ||
| 103 | print("Installing "..url.."...") | ||
| 104 | return run(url) | ||
| 105 | else | ||
| 106 | print() | ||
| 107 | print("Could not determine which rock to install.") | ||
| 108 | print() | ||
| 109 | print("Search results:") | ||
| 110 | print("---------------") | ||
| 111 | search.print_results(results) | ||
| 112 | return nil, (next(results) and "Please narrow your query." or "No results found.") | ||
| 113 | end | ||
| 114 | end | ||
| 115 | end | ||
