2
2
--- Module implementing the luarocks "download" command.
3
3
-- Download a rock from the repository.
4
module("luarocks.download", package.seeall)
4
--module("luarocks.download", package.seeall)
6
package.loaded["luarocks.download"] = download
6
8
local util = require("luarocks.util")
7
9
local path = require("luarocks.path")
8
10
local fetch = require("luarocks.fetch")
9
11
local search = require("luarocks.search")
11
help_summary = "Download a specific rock file from a rocks server."
12
help_arguments = "[--all] [--arch=<arch> | --source | --rockspec] [<name> [<version>]]"
12
local fs = require("luarocks.fs")
13
local dir = require("luarocks.dir")
15
download.help_summary = "Download a specific rock file from a rocks server."
16
download.help_arguments = "[--all] [--arch=<arch> | --source | --rockspec] [<name> [<version>]]"
15
19
--all Download all files if there are multiple matches.
16
20
--source Download .src.rock if available.
17
21
--rockspec Download .rockspec if available.
18
22
--arch=<arch> Download rock for a specific architecture.
21
function download(arch, name, version, all)
25
local function get_file(filename)
26
local protocol, pathname = dir.split_url(filename)
27
if protocol == "file" then
28
local ok, err = fs.copy(pathname, fs.current_dir())
35
return fetch.fetch_url(filename)
39
function download.download(arch, name, version, all)
23
41
local query = search.make_query(name, version)
24
42
if arch then query.arch = arch end
26
44
if name == "" then query.exact_name = false end
27
results, err = search.search_repos(query)
45
results = search.search_repos(query)
29
47
results, err = search.find_suitable_rock(query)
31
49
if type(results) == "string" then
32
local file = fetch.fetch_url(results)
50
return get_file(results)
34
51
elseif type(results) == "table" and next(results) then
36
53
local all_ok = true
39
56
for version, versions in pairs(result) do
40
57
for _,items in pairs(versions) do
41
58
local filename = path.make_url(items.repo, name, version, items.arch)
42
local ok, err = fetch.fetch_url(filename)
59
local ok, err = get_file(filename)
45
62
any_err = any_err .. "\n" .. err
50
67
return all_ok, any_err
52
69
util.printerr("Multiple search results were returned.")
54
util.printout("Search results:")
55
util.printout("---------------")
70
util.title("Search results:")
56
71
search.print_results(results)
57
72
return nil, "Please narrow your query or use --all."
66
81
-- version may also be passed.
67
82
-- @return boolean or (nil, string): true if successful or nil followed
68
83
-- by an error message.
84
function download.run(...)
70
85
local flags, name, version = util.parse_flags(...)
72
87
assert(type(version) == "string" or not version)
85
100
arch = flags["arch"]
88
local dl, err = download(arch, name, version, flags["all"])
103
local dl, err = download.download(arch, name, version, flags["all"])
89
104
return dl and true, err