diff options
Diffstat (limited to 'src/luarocks/download.tl')
-rw-r--r-- | src/luarocks/download.tl | 68 |
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 @@ | |||
1 | local download = {} | ||
2 | |||
3 | local path = require("luarocks.path") | ||
4 | local fetch = require("luarocks.fetch") | ||
5 | local search = require("luarocks.search") | ||
6 | local queries = require("luarocks.queries") | ||
7 | local fs = require("luarocks.fs") | ||
8 | local dir = require("luarocks.dir") | ||
9 | local util = require("luarocks.util") | ||
10 | |||
11 | local 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 | ||
25 | end | ||
26 | |||
27 | function 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 ".") | ||
66 | end | ||
67 | |||
68 | return download | ||