diff options
author | V1K1NGbg <victor@ilchev.com> | 2024-08-22 17:48:57 -0300 |
---|---|---|
committer | Hisham Muhammad <hisham@gobolinux.org> | 2024-10-21 13:30:51 -0300 |
commit | a9c43e2efbfe05f5a71b68235d7115bbb06f407a (patch) | |
tree | 3003042dbf4d4463767be8e48796452f9190e82f /src | |
parent | 084bc6dd350e585ed48bd2bc454aaea1e8fc8dd7 (diff) | |
download | luarocks-a9c43e2efbfe05f5a71b68235d7115bbb06f407a.tar.gz luarocks-a9c43e2efbfe05f5a71b68235d7115bbb06f407a.tar.bz2 luarocks-a9c43e2efbfe05f5a71b68235d7115bbb06f407a.zip |
Teal: convert luarocks.fetch
Diffstat (limited to 'src')
-rw-r--r-- | src/luarocks/fetch.tl (renamed from src/luarocks/fetch.lua) | 159 |
1 files changed, 82 insertions, 77 deletions
diff --git a/src/luarocks/fetch.lua b/src/luarocks/fetch.tl index 193e5e39..222b2194 100644 --- a/src/luarocks/fetch.lua +++ b/src/luarocks/fetch.tl | |||
@@ -1,6 +1,19 @@ | |||
1 | 1 | ||
2 | --- Functions related to fetching and loading local and remote files. | 2 | --- Functions related to fetching and loading local and remote files. |
3 | local fetch = {} | 3 | local record fetch |
4 | fetch_caching: function(string, ?string): string, string, string, boolean | ||
5 | fetch_url: function(string, ?string, ?boolean, ?string): string, string, string, boolean | ||
6 | fetch_url_at_temp_dir: function(string, string, ?string, ?boolean): string, string, string | ||
7 | find_base_dir: function(string, string, string, ?string): string, string | ||
8 | fetch_and_unpack_rock: function(string, ?string, ?boolean): string, string, string | ||
9 | load_local_rockspec: function(string, ?boolean): Rockspec, string | ||
10 | load_rockspec: function(string, ?string, ?boolean): Rockspec, string, string | ||
11 | find_rockspec_source_dir: function(Rockspec, string): boolean, string | ||
12 | fetch_sources: function(Rockspec, boolean, ?string): string, string, string, string, string | ||
13 | record Fetch | ||
14 | get_sources: function(Rockspec, boolean, ?string): string, string | ||
15 | end | ||
16 | end | ||
4 | 17 | ||
5 | local fs = require("luarocks.fs") | 18 | local fs = require("luarocks.fs") |
6 | local dir = require("luarocks.dir") | 19 | local dir = require("luarocks.dir") |
@@ -10,6 +23,10 @@ local persist = require("luarocks.persist") | |||
10 | local util = require("luarocks.util") | 23 | local util = require("luarocks.util") |
11 | local cfg = require("luarocks.core.cfg") | 24 | local cfg = require("luarocks.core.cfg") |
12 | 25 | ||
26 | local type Fetch = fetch.Fetch | ||
27 | local type Lock = fs.Lock | ||
28 | local type Rockspec = require("luarocks.core.types.rockspec").Rockspec | ||
29 | |||
13 | 30 | ||
14 | --- Fetch a local or remote file, using a local cache directory. | 31 | --- Fetch a local or remote file, using a local cache directory. |
15 | -- Make a remote or local URL/pathname local, fetching the file if necessary. | 32 | -- Make a remote or local URL/pathname local, fetching the file if necessary. |
@@ -28,7 +45,7 @@ local cfg = require("luarocks.core.cfg") | |||
28 | -- * nil | 45 | -- * nil |
29 | -- * an error message | 46 | -- * an error message |
30 | -- * an optional error code. | 47 | -- * an optional error code. |
31 | function fetch.fetch_caching(url, mirroring) | 48 | function fetch.fetch_caching(url: string, mirroring?: string): string, string, string, boolean |
32 | local repo_url, filename = url:match("^(.*)/([^/]+)$") | 49 | local repo_url, filename = url:match("^(.*)/([^/]+)$") |
33 | local name = repo_url:gsub("[/:]","_") | 50 | local name = repo_url:gsub("[/:]","_") |
34 | local cache_dir = dir.path(cfg.local_cache, name) | 51 | local cache_dir = dir.path(cfg.local_cache, name) |
@@ -43,7 +60,7 @@ function fetch.fetch_caching(url, mirroring) | |||
43 | return cachefile, nil, nil, true | 60 | return cachefile, nil, nil, true |
44 | end | 61 | end |
45 | 62 | ||
46 | local lock, errlock | 63 | local lock, errlock: Lock, string |
47 | if ok then | 64 | if ok then |
48 | lock, errlock = fs.lock_access(cache_dir) | 65 | lock, errlock = fs.lock_access(cache_dir) |
49 | end | 66 | end |
@@ -67,10 +84,10 @@ function fetch.fetch_caching(url, mirroring) | |||
67 | return nil, err or "Failed downloading "..url, errcode | 84 | return nil, err or "Failed downloading "..url, errcode |
68 | end | 85 | end |
69 | 86 | ||
70 | local fd, err = io.open(checkfile, "wb") | 87 | local fd, erropen = io.open(checkfile, "wb") |
71 | if err then | 88 | if erropen then |
72 | fs.unlock_access(lock) | 89 | fs.unlock_access(lock) |
73 | return nil, err | 90 | return nil, erropen |
74 | end | 91 | end |
75 | fd:write("!") | 92 | fd:write("!") |
76 | fd:close() | 93 | fd:close() |
@@ -79,13 +96,13 @@ function fetch.fetch_caching(url, mirroring) | |||
79 | return file, nil, nil, from_cache | 96 | return file, nil, nil, from_cache |
80 | end | 97 | end |
81 | 98 | ||
82 | local function ensure_trailing_slash(url) | 99 | local function ensure_trailing_slash(url: string): string |
83 | return (url:gsub("/*$", "/")) | 100 | return (url:gsub("/*$", "/")) |
84 | end | 101 | end |
85 | 102 | ||
86 | local function is_url_relative_to_rocks_servers(url, servers) | 103 | local function is_url_relative_to_rocks_servers(url: string, servers: {{string} | string}): integer, string, {string} |
87 | for _, item in ipairs(servers) do | 104 | for _, item in ipairs(servers) do |
88 | if type(item) == "table" then | 105 | if item is {string} then |
89 | for i, s in ipairs(item) do | 106 | for i, s in ipairs(item) do |
90 | local base = ensure_trailing_slash(s) | 107 | local base = ensure_trailing_slash(s) |
91 | if string.find(url, base, 1, true) == 1 then | 108 | if string.find(url, base, 1, true) == 1 then |
@@ -96,7 +113,7 @@ local function is_url_relative_to_rocks_servers(url, servers) | |||
96 | end | 113 | end |
97 | end | 114 | end |
98 | 115 | ||
99 | local function download_with_mirrors(url, filename, cache, servers) | 116 | local function download_with_mirrors(url: string, filename: string, cache: boolean, servers: {{string} | string}): string, string, string, boolean |
100 | local idx, rest, mirrors = is_url_relative_to_rocks_servers(url, servers) | 117 | local idx, rest, mirrors = is_url_relative_to_rocks_servers(url, servers) |
101 | 118 | ||
102 | if not idx then | 119 | if not idx then |
@@ -111,9 +128,9 @@ local function download_with_mirrors(url, filename, cache, servers) | |||
111 | if i > idx then | 128 | if i > idx then |
112 | util.warning("Failed downloading. Attempting mirror at " .. try_url) | 129 | util.warning("Failed downloading. Attempting mirror at " .. try_url) |
113 | end | 130 | end |
114 | local ok, name, from_cache = fs.download(try_url, filename, cache) | 131 | local name, _, _, from_cache = fs.download(try_url, filename, cache) |
115 | if ok then | 132 | if name then |
116 | return ok, name, from_cache | 133 | return name, nil, nil, from_cache |
117 | else | 134 | else |
118 | err = err .. name .. "\n" | 135 | err = err .. name .. "\n" |
119 | end | 136 | end |
@@ -145,9 +162,7 @@ end | |||
145 | -- * nil | 162 | -- * nil |
146 | -- * an error message | 163 | -- * an error message |
147 | -- * an optional error code. | 164 | -- * an optional error code. |
148 | function fetch.fetch_url(url, filename, cache, mirroring) | 165 | function fetch.fetch_url(url: string, filename?: string, cache?: boolean, mirroring?: string): string, string, string, boolean |
149 | assert(type(url) == "string") | ||
150 | assert(type(filename) == "string" or not filename) | ||
151 | 166 | ||
152 | local protocol, pathname = dir.split_url(url) | 167 | local protocol, pathname = dir.split_url(url) |
153 | if protocol == "file" then | 168 | if protocol == "file" then |
@@ -160,7 +175,7 @@ function fetch.fetch_url(url, filename, cache, mirroring) | |||
160 | end | 175 | end |
161 | filename = filename or dir.base_name(pathname) | 176 | filename = filename or dir.base_name(pathname) |
162 | local dstname = fs.absolute_name(dir.path(".", filename)) | 177 | local dstname = fs.absolute_name(dir.path(".", filename)) |
163 | local ok, err | 178 | local ok, err: boolean, string |
164 | if fullname == dstname then | 179 | if fullname == dstname then |
165 | ok = true | 180 | ok = true |
166 | else | 181 | else |
@@ -172,16 +187,16 @@ function fetch.fetch_url(url, filename, cache, mirroring) | |||
172 | return nil, "Failed copying local file " .. fullname .. " to " .. dstname .. ": " .. err | 187 | return nil, "Failed copying local file " .. fullname .. " to " .. dstname .. ": " .. err |
173 | end | 188 | end |
174 | elseif dir.is_basic_protocol(protocol) then | 189 | elseif dir.is_basic_protocol(protocol) then |
175 | local ok, name, from_cache | 190 | local name, err, err_code, from_cache: string, string, string, boolean |
176 | if mirroring ~= "no_mirror" then | 191 | if mirroring ~= "no_mirror" then |
177 | ok, name, from_cache = download_with_mirrors(url, filename, cache, cfg.rocks_servers) | 192 | name, err, err_code, from_cache = download_with_mirrors(url, filename, cache, cfg.rocks_servers) |
178 | else | 193 | else |
179 | ok, name, from_cache = fs.download(url, filename, cache) | 194 | name, err, err_code, from_cache = fs.download(url, filename, cache) |
180 | end | 195 | end |
181 | if not ok then | 196 | if not name then |
182 | return nil, "Failed downloading "..url..(name and " - "..name or ""), from_cache | 197 | return nil, "Failed downloading "..url..(err and " - "..err or ""), err_code |
183 | end | 198 | end |
184 | return name, nil, nil, from_cache | 199 | return name, nil, nil, from_cache --! string or boolean |
185 | else | 200 | else |
186 | return nil, "Unsupported protocol "..protocol | 201 | return nil, "Unsupported protocol "..protocol |
187 | end | 202 | end |
@@ -198,10 +213,7 @@ end | |||
198 | -- @return (string, string) or (nil, string, [string]): absolute local pathname of | 213 | -- @return (string, string) or (nil, string, [string]): absolute local pathname of |
199 | -- the fetched file and temporary directory name; or nil and an error message | 214 | -- the fetched file and temporary directory name; or nil and an error message |
200 | -- followed by an optional error code | 215 | -- followed by an optional error code |
201 | function fetch.fetch_url_at_temp_dir(url, tmpname, filename, cache) | 216 | function fetch.fetch_url_at_temp_dir(url: string, tmpname: string, filename?: string, cache?: boolean): string, string, string |
202 | assert(type(url) == "string") | ||
203 | assert(type(tmpname) == "string") | ||
204 | assert(type(filename) == "string" or not filename) | ||
205 | filename = filename or dir.base_name(url) | 217 | filename = filename or dir.base_name(url) |
206 | 218 | ||
207 | local protocol, pathname = dir.split_url(url) | 219 | local protocol, pathname = dir.split_url(url) |
@@ -212,18 +224,18 @@ function fetch.fetch_url_at_temp_dir(url, tmpname, filename, cache) | |||
212 | return nil, "File not found: "..pathname | 224 | return nil, "File not found: "..pathname |
213 | end | 225 | end |
214 | else | 226 | else |
215 | local temp_dir, err = fs.make_temp_dir(tmpname) | 227 | local temp_dir, errmake = fs.make_temp_dir(tmpname) |
216 | if not temp_dir then | 228 | if not temp_dir then |
217 | return nil, "Failed creating temporary directory "..tmpname..": "..err | 229 | return nil, "Failed creating temporary directory "..tmpname..": "..errmake |
218 | end | 230 | end |
219 | util.schedule_function(fs.delete, temp_dir) | 231 | util.schedule_function(fs.delete, temp_dir) |
220 | local ok, err = fs.change_dir(temp_dir) | 232 | local ok, errchange = fs.change_dir(temp_dir) |
221 | if not ok then return nil, err end | 233 | if not ok then return nil, errchange end |
222 | 234 | ||
223 | local file, err, errcode | 235 | local file, err, errcode: string, string, string |
224 | 236 | ||
225 | if cache then | 237 | if cache then |
226 | local cachefile | 238 | local cachefile: string |
227 | cachefile, err, errcode = fetch.fetch_caching(url) | 239 | cachefile, err, errcode = fetch.fetch_caching(url) |
228 | 240 | ||
229 | if cachefile then | 241 | if cachefile then |
@@ -257,7 +269,7 @@ end | |||
257 | -- be nil if not found), or nil followed by an error message. | 269 | -- be nil if not found), or nil followed by an error message. |
258 | -- The inferred dir is returned first to avoid confusion with errors, | 270 | -- The inferred dir is returned first to avoid confusion with errors, |
259 | -- because it is never nil. | 271 | -- because it is never nil. |
260 | function fetch.find_base_dir(file, temp_dir, src_url, src_dir) | 272 | function fetch.find_base_dir(file: string, temp_dir: string, src_url: string, src_dir?: string): string, string |
261 | local ok, err = fs.change_dir(temp_dir) | 273 | local ok, err = fs.change_dir(temp_dir) |
262 | if not ok then return nil, err end | 274 | if not ok then return nil, err end |
263 | fs.unpack_archive(file) | 275 | fs.unpack_archive(file) |
@@ -277,7 +289,7 @@ function fetch.find_base_dir(file, temp_dir, src_url, src_dir) | |||
277 | end | 289 | end |
278 | 290 | ||
279 | local inferred_dir = src_dir or dir.deduce_base_dir(src_url) | 291 | local inferred_dir = src_dir or dir.deduce_base_dir(src_url) |
280 | local found_dir = nil | 292 | local found_dir: string = nil |
281 | if fs.exists(inferred_dir) then | 293 | if fs.exists(inferred_dir) then |
282 | found_dir = inferred_dir | 294 | found_dir = inferred_dir |
283 | else | 295 | else |
@@ -285,7 +297,7 @@ function fetch.find_base_dir(file, temp_dir, src_url, src_dir) | |||
285 | local files = fs.list_dir() | 297 | local files = fs.list_dir() |
286 | if files then | 298 | if files then |
287 | table.sort(files) | 299 | table.sort(files) |
288 | for i,filename in ipairs(files) do | 300 | for _,filename in ipairs(files) do |
289 | if fs.is_dir(filename) then | 301 | if fs.is_dir(filename) then |
290 | util.printerr("Found "..filename) | 302 | util.printerr("Found "..filename) |
291 | found_dir = filename | 303 | found_dir = filename |
@@ -298,11 +310,12 @@ function fetch.find_base_dir(file, temp_dir, src_url, src_dir) | |||
298 | return inferred_dir, found_dir | 310 | return inferred_dir, found_dir |
299 | end | 311 | end |
300 | 312 | ||
301 | local function fetch_and_verify_signature_for(url, filename, tmpdir) | 313 | |
314 | local function fetch_and_verify_signature_for(url: string, filename: string, tmpdir: string): string, string, string | ||
302 | local sig_url = signing.signature_url(url) | 315 | local sig_url = signing.signature_url(url) |
303 | local sig_file, err, errcode = fetch.fetch_url_at_temp_dir(sig_url, tmpdir) | 316 | local sig_file, errfetch, errcode = fetch.fetch_url_at_temp_dir(sig_url, tmpdir) |
304 | if not sig_file then | 317 | if not sig_file then |
305 | return nil, "Could not fetch signature file for verification: " .. err, errcode | 318 | return nil, "Could not fetch signature file for verification: " .. errfetch, errcode |
306 | end | 319 | end |
307 | 320 | ||
308 | local ok, err = signing.verify_signature(filename, sig_file) | 321 | local ok, err = signing.verify_signature(filename, sig_file) |
@@ -322,9 +335,7 @@ end | |||
322 | -- @param verify boolean: if true, download and verify signature for rockspec | 335 | -- @param verify boolean: if true, download and verify signature for rockspec |
323 | -- @return string or (nil, string, [string]): the directory containing the contents | 336 | -- @return string or (nil, string, [string]): the directory containing the contents |
324 | -- of the unpacked rock. | 337 | -- of the unpacked rock. |
325 | function fetch.fetch_and_unpack_rock(url, dest, verify) | 338 | function fetch.fetch_and_unpack_rock(url: string, dest?: string, verify?: boolean): string, string, string |
326 | assert(type(url) == "string") | ||
327 | assert(type(dest) == "string" or not dest) | ||
328 | 339 | ||
329 | local name = dir.base_name(url):match("(.*)%.[^.]*%.rock") | 340 | local name = dir.base_name(url):match("(.*)%.[^.]*%.rock") |
330 | local tmpname = "luarocks-rock-" .. name | 341 | local tmpname = "luarocks-rock-" .. name |
@@ -334,7 +345,7 @@ function fetch.fetch_and_unpack_rock(url, dest, verify) | |||
334 | return nil, "Could not fetch rock file: " .. err, errcode | 345 | return nil, "Could not fetch rock file: " .. err, errcode |
335 | end | 346 | end |
336 | 347 | ||
337 | local sig_file | 348 | local sig_file: string |
338 | if verify then | 349 | if verify then |
339 | sig_file, err = fetch_and_verify_signature_for(url, rock_file, tmpname) | 350 | sig_file, err = fetch_and_verify_signature_for(url, rock_file, tmpname) |
340 | if err then | 351 | if err then |
@@ -344,12 +355,12 @@ function fetch.fetch_and_unpack_rock(url, dest, verify) | |||
344 | 355 | ||
345 | rock_file = fs.absolute_name(rock_file) | 356 | rock_file = fs.absolute_name(rock_file) |
346 | 357 | ||
347 | local unpack_dir | 358 | local unpack_dir: string |
348 | if dest then | 359 | if dest then |
349 | unpack_dir = dest | 360 | unpack_dir = dest |
350 | local ok, err = fs.make_dir(unpack_dir) | 361 | local ok, errmake = fs.make_dir(unpack_dir) |
351 | if not ok then | 362 | if not ok then |
352 | return nil, "Failed unpacking rock file: " .. err | 363 | return nil, "Failed unpacking rock file: " .. errmake |
353 | end | 364 | end |
354 | else | 365 | else |
355 | unpack_dir, err = fs.make_temp_dir(name) | 366 | unpack_dir, err = fs.make_temp_dir(name) |
@@ -360,8 +371,8 @@ function fetch.fetch_and_unpack_rock(url, dest, verify) | |||
360 | if not dest then | 371 | if not dest then |
361 | util.schedule_function(fs.delete, unpack_dir) | 372 | util.schedule_function(fs.delete, unpack_dir) |
362 | end | 373 | end |
363 | local ok, err = fs.change_dir(unpack_dir) | 374 | local ok, errchange = fs.change_dir(unpack_dir) |
364 | if not ok then return nil, err end | 375 | if not ok then return nil, errchange end |
365 | ok, err = fs.unzip(rock_file) | 376 | ok, err = fs.unzip(rock_file) |
366 | if not ok then | 377 | if not ok then |
367 | return nil, "Failed unpacking rock file: " .. rock_file .. ": " .. err | 378 | return nil, "Failed unpacking rock file: " .. rock_file .. ": " .. err |
@@ -383,8 +394,7 @@ end | |||
383 | -- rockspec. | 394 | -- rockspec. |
384 | -- @return table or (nil, string): A table representing the rockspec | 395 | -- @return table or (nil, string): A table representing the rockspec |
385 | -- or nil followed by an error message. | 396 | -- or nil followed by an error message. |
386 | function fetch.load_local_rockspec(rel_filename, quick) | 397 | function fetch.load_local_rockspec(rel_filename: string, quick?: boolean): Rockspec, string |
387 | assert(type(rel_filename) == "string") | ||
388 | local abs_filename = fs.absolute_name(rel_filename) | 398 | local abs_filename = fs.absolute_name(rel_filename) |
389 | 399 | ||
390 | local basename = dir.base_name(abs_filename) | 400 | local basename = dir.base_name(abs_filename) |
@@ -395,13 +405,13 @@ function fetch.load_local_rockspec(rel_filename, quick) | |||
395 | end | 405 | end |
396 | 406 | ||
397 | local tbl, err = persist.load_into_table(abs_filename) | 407 | local tbl, err = persist.load_into_table(abs_filename) |
398 | if not tbl then | 408 | if not tbl and err is string then |
399 | return nil, "Could not load rockspec file "..abs_filename.." ("..err..")" | 409 | return nil, "Could not load rockspec file "..abs_filename.." ("..err..")" |
400 | end | 410 | end |
401 | 411 | ||
402 | local rockspec, err = rockspecs.from_persisted_table(abs_filename, tbl, err, quick) | 412 | local rockspec, errrock = rockspecs.from_persisted_table(abs_filename, tbl as Rockspec, err as {string: any}, quick) |
403 | if not rockspec then | 413 | if not rockspec then |
404 | return nil, abs_filename .. ": " .. err | 414 | return nil, abs_filename .. ": " .. errrock |
405 | end | 415 | end |
406 | 416 | ||
407 | local name_version = rockspec.package:lower() .. "-" .. rockspec.version | 417 | local name_version = rockspec.package:lower() .. "-" .. rockspec.version |
@@ -422,10 +432,9 @@ end | |||
422 | -- @param verify boolean: if true, download and verify signature for rockspec | 432 | -- @param verify boolean: if true, download and verify signature for rockspec |
423 | -- @return table or (nil, string, [string]): A table representing the rockspec | 433 | -- @return table or (nil, string, [string]): A table representing the rockspec |
424 | -- or nil followed by an error message and optional error code. | 434 | -- or nil followed by an error message and optional error code. |
425 | function fetch.load_rockspec(url, location, verify) | 435 | function fetch.load_rockspec(url: string, location?: string, verify?: boolean): Rockspec, string, string |
426 | assert(type(url) == "string") | ||
427 | 436 | ||
428 | local name | 437 | local name: string |
429 | local basename = dir.base_name(url) | 438 | local basename = dir.base_name(url) |
430 | if basename == "rockspec" then | 439 | if basename == "rockspec" then |
431 | name = "rockspec" | 440 | name = "rockspec" |
@@ -437,9 +446,9 @@ function fetch.load_rockspec(url, location, verify) | |||
437 | end | 446 | end |
438 | 447 | ||
439 | local tmpname = "luarocks-rockspec-"..name | 448 | local tmpname = "luarocks-rockspec-"..name |
440 | local filename, err, errcode | 449 | local filename, err, errcode, ok: string, string, string, boolean |
441 | if location then | 450 | if location then |
442 | local ok, err = fs.change_dir(location) | 451 | ok, err = fs.change_dir(location) |
443 | if not ok then return nil, err end | 452 | if not ok then return nil, err end |
444 | filename, err = fetch.fetch_url(url) | 453 | filename, err = fetch.fetch_url(url) |
445 | fs.pop_dir() | 454 | fs.pop_dir() |
@@ -451,9 +460,9 @@ function fetch.load_rockspec(url, location, verify) | |||
451 | end | 460 | end |
452 | 461 | ||
453 | if verify then | 462 | if verify then |
454 | local _, err = fetch_and_verify_signature_for(url, filename, tmpname) | 463 | local _, errfetch = fetch_and_verify_signature_for(url, filename, tmpname) |
455 | if err then | 464 | if err then |
456 | return nil, err | 465 | return nil, errfetch |
457 | end | 466 | end |
458 | end | 467 | end |
459 | 468 | ||
@@ -469,16 +478,13 @@ end | |||
469 | -- @return (string, string) or (nil, string, [string]): The absolute pathname of | 478 | -- @return (string, string) or (nil, string, [string]): The absolute pathname of |
470 | -- the fetched source tarball and the temporary directory created to | 479 | -- the fetched source tarball and the temporary directory created to |
471 | -- store it; or nil and an error message and optional error code. | 480 | -- store it; or nil and an error message and optional error code. |
472 | function fetch.get_sources(rockspec, extract, dest_dir) | 481 | function fetch.get_sources(rockspec: Rockspec, extract: boolean, dest_dir?: string): string, string, string |
473 | assert(rockspec:type() == "rockspec") | ||
474 | assert(type(extract) == "boolean") | ||
475 | assert(type(dest_dir) == "string" or not dest_dir) | ||
476 | 482 | ||
477 | local url = rockspec.source.url | 483 | local url = rockspec.source.url |
478 | local name = rockspec.name.."-"..rockspec.version | 484 | local name = rockspec.name.."-"..rockspec.version |
479 | local filename = rockspec.source.file | 485 | local filename = rockspec.source.file |
480 | local source_file, store_dir | 486 | local source_file, store_dir: string, string |
481 | local ok, err, errcode | 487 | local ok, err, errcode: boolean, string, string |
482 | if dest_dir then | 488 | if dest_dir then |
483 | ok, err = fs.change_dir(dest_dir) | 489 | ok, err = fs.change_dir(dest_dir) |
484 | if not ok then return nil, err, "dest_dir" end | 490 | if not ok then return nil, err, "dest_dir" end |
@@ -497,7 +503,7 @@ function fetch.get_sources(rockspec, extract, dest_dir) | |||
497 | end | 503 | end |
498 | end | 504 | end |
499 | if extract then | 505 | if extract then |
500 | local ok, err = fs.change_dir(store_dir) | 506 | ok, err = fs.change_dir(store_dir) |
501 | if not ok then return nil, err end | 507 | if not ok then return nil, err end |
502 | ok, err = fs.unpack_archive(rockspec.source.file) | 508 | ok, err = fs.unpack_archive(rockspec.source.file) |
503 | if not ok then return nil, err end | 509 | if not ok then return nil, err end |
@@ -508,11 +514,12 @@ function fetch.get_sources(rockspec, extract, dest_dir) | |||
508 | return source_file, store_dir | 514 | return source_file, store_dir |
509 | end | 515 | end |
510 | 516 | ||
511 | function fetch.find_rockspec_source_dir(rockspec, store_dir) | 517 | function fetch.find_rockspec_source_dir(rockspec: Rockspec, store_dir: string): boolean, string |
512 | local ok, err = fs.change_dir(store_dir) | 518 | local ok, err = fs.change_dir(store_dir) |
513 | if not ok then return nil, err end | 519 | if not ok then return nil, err end |
514 | 520 | ||
515 | local file_count, dir_count, found_dir = 0, 0, 0 | 521 | local file_count, dir_count = 0, 0 |
522 | local found_dir: string | ||
516 | 523 | ||
517 | if rockspec.source.dir and fs.exists(rockspec.source.dir) then | 524 | if rockspec.source.dir and fs.exists(rockspec.source.dir) then |
518 | ok, err = true, nil | 525 | ok, err = true, nil |
@@ -564,10 +571,7 @@ end | |||
564 | -- @return (string, string) or (nil, string): The absolute pathname of | 571 | -- @return (string, string) or (nil, string): The absolute pathname of |
565 | -- the fetched source tarball and the temporary directory created to | 572 | -- the fetched source tarball and the temporary directory created to |
566 | -- store it; or nil and an error message. | 573 | -- store it; or nil and an error message. |
567 | function fetch.fetch_sources(rockspec, extract, dest_dir) | 574 | function fetch.fetch_sources(rockspec: Rockspec, extract: boolean, dest_dir?: string): string, string, string, string, string |
568 | assert(rockspec:type() == "rockspec") | ||
569 | assert(type(extract) == "boolean") | ||
570 | assert(type(dest_dir) == "string" or not dest_dir) | ||
571 | 575 | ||
572 | -- auto-convert git://github.com URLs to use git+https | 576 | -- auto-convert git://github.com URLs to use git+https |
573 | -- see https://github.blog/2021-09-01-improving-git-protocol-security-github/ | 577 | -- see https://github.blog/2021-09-01-improving-git-protocol-security-github/ |
@@ -578,11 +582,12 @@ function fetch.fetch_sources(rockspec, extract, dest_dir) | |||
578 | end | 582 | end |
579 | 583 | ||
580 | local protocol = rockspec.source.protocol | 584 | local protocol = rockspec.source.protocol |
581 | local ok, err, proto | 585 | local ok, err, proto: boolean, string, Fetch |
586 | |||
582 | if dir.is_basic_protocol(protocol) then | 587 | if dir.is_basic_protocol(protocol) then |
583 | proto = fetch | 588 | proto = fetch as Fetch |
584 | else | 589 | else |
585 | ok, proto = pcall(require, "luarocks.fetch."..protocol:gsub("[+-]", "_")) | 590 | ok, proto = pcall(require, "luarocks.fetch."..protocol:gsub("[+-]", "_")) as (boolean, Fetch) |
586 | if not ok then | 591 | if not ok then |
587 | return nil, "Unknown protocol "..protocol | 592 | return nil, "Unknown protocol "..protocol |
588 | end | 593 | end |
@@ -598,7 +603,7 @@ function fetch.fetch_sources(rockspec, extract, dest_dir) | |||
598 | end | 603 | end |
599 | end | 604 | end |
600 | 605 | ||
601 | local source_file, store_dir = proto.get_sources(rockspec, extract, dest_dir) | 606 | local source_file, store_dir: string, string = proto.get_sources(rockspec, extract, dest_dir) |
602 | if not source_file then return nil, store_dir end | 607 | if not source_file then return nil, store_dir end |
603 | 608 | ||
604 | ok, err = fetch.find_rockspec_source_dir(rockspec, store_dir) | 609 | ok, err = fetch.find_rockspec_source_dir(rockspec, store_dir) |