aboutsummaryrefslogtreecommitdiff
path: root/src/luarocks/download.tl
diff options
context:
space:
mode:
Diffstat (limited to 'src/luarocks/download.tl')
-rw-r--r--src/luarocks/download.tl68
1 files changed, 68 insertions, 0 deletions
diff --git a/src/luarocks/download.tl b/src/luarocks/download.tl
new file mode 100644
index 00000000..07a2a65f
--- /dev/null
+++ b/src/luarocks/download.tl
@@ -0,0 +1,68 @@
1local download = {}
2
3local path = require("luarocks.path")
4local fetch = require("luarocks.fetch")
5local search = require("luarocks.search")
6local queries = require("luarocks.queries")
7local fs = require("luarocks.fs")
8local dir = require("luarocks.dir")
9local util = require("luarocks.util")
10
11local function get_file(filename)
12 local protocol, pathname = dir.split_url(filename)
13 if protocol == "file" then
14 local ok, err = fs.copy(pathname, fs.current_dir(), "read")
15 if ok then
16 return pathname
17 else
18 return nil, err
19 end
20 else
21 -- discard third result
22 local ok, err = fetch.fetch_url(filename)
23 return ok, err
24 end
25end
26
27function download.download(arch, name, namespace, version, all, check_lua_versions)
28 local substring = (all and name == "")
29 local query = queries.new(name, namespace, version, substring, arch)
30 local search_err
31
32 if all then
33 local results = search.search_repos(query)
34 local has_result = false
35 local all_ok = true
36 local any_err = ""
37 for name, result in pairs(results) do -- luacheck: ignore 422
38 for version, items in pairs(result) do -- luacheck: ignore 422
39 for _, item in ipairs(items) do
40 -- Ignore provided rocks.
41 if item.arch ~= "installed" then
42 has_result = true
43 local filename = path.make_url(item.repo, name, version, item.arch)
44 local ok, err = get_file(filename)
45 if not ok then
46 all_ok = false
47 any_err = any_err .. "\n" .. err
48 end
49 end
50 end
51 end
52 end
53
54 if has_result then
55 return all_ok, any_err
56 end
57 else
58 local url
59 url, search_err = search.find_rock_checking_lua_versions(query, check_lua_versions)
60 if url then
61 return get_file(url)
62 end
63 end
64 local rock = util.format_rock_name(name, namespace, version)
65 return nil, "Could not find a result named "..rock..(search_err and ": "..search_err or ".")
66end
67
68return download