aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorV1K1NGbg <victor@ilchev.com>2024-08-16 15:15:16 +0300
committerV1K1NGbg <victor@ilchev.com>2024-08-16 15:15:16 +0300
commit2369d7c2830cd3d0a75aaf195e41a56ed3100e0c (patch)
tree6ed82e40d89e14abaaece79de164da71cd694cc4
parent337fe2d921ba84f71a4088ec2d115e8e2432da17 (diff)
downloadluarocks-2369d7c2830cd3d0a75aaf195e41a56ed3100e0c.tar.gz
luarocks-2369d7c2830cd3d0a75aaf195e41a56ed3100e0c.tar.bz2
luarocks-2369d7c2830cd3d0a75aaf195e41a56ed3100e0c.zip
build
-rw-r--r--src/luarocks/core/types/build.d.tl8
-rw-r--r--src/luarocks/download.tl69
-rw-r--r--src/luarocks/fs.d.tl4
-rw-r--r--src/luarocks/pack.tl189
-rw-r--r--src/luarocks/search.tl4
5 files changed, 267 insertions, 7 deletions
diff --git a/src/luarocks/core/types/build.d.tl b/src/luarocks/core/types/build.d.tl
index b67396f7..b643dc92 100644
--- a/src/luarocks/core/types/build.d.tl
+++ b/src/luarocks/core/types/build.d.tl
@@ -2,10 +2,10 @@ local record build
2 2
3 interface Build 3 interface Build
4 record Install 4 record Install
5 lua: {string: string} 5 lua: {(string|integer): string}
6 lib: {string: string} 6 lib: {(string|integer): string}
7 conf: {string: string} 7 conf: {(string|integer): string}
8 bin: {string: string} 8 bin: {(string|integer): string}
9 end 9 end
10 10
11 type: string 11 type: string
diff --git a/src/luarocks/download.tl b/src/luarocks/download.tl
new file mode 100644
index 00000000..43a57d8f
--- /dev/null
+++ b/src/luarocks/download.tl
@@ -0,0 +1,69 @@
1local record download
2end
3
4local path = require("luarocks.path")
5local fetch = require("luarocks.fetch")
6local search = require("luarocks.search")
7local queries = require("luarocks.queries")
8local fs = require("luarocks.fs")
9local dir = require("luarocks.dir")
10local util = require("luarocks.util")
11
12local function get_file(filename: string): string, string
13 local protocol, pathname = dir.split_url(filename)
14 if protocol == "file" then
15 local ok, err = fs.copy(pathname, fs.current_dir(), "read")
16 if ok then
17 return pathname
18 else
19 return nil, err
20 end
21 else
22 -- discard third result
23 local ok, err = fetch.fetch_url(filename)
24 return ok, err
25 end
26end
27
28function download.download(arch: string, name: string, namespace: string, version: string, all, check_lua_versions: boolean): string, string
29 local substring = (all and name == "")
30 local query = queries.new(name, namespace, version, substring, arch)
31 local search_err: string
32
33 if all then
34 local results = search.search_repos(query)
35 local has_result = false
36 local all_ok = true
37 local any_err = ""
38 for name, result in pairs(results) do -- luacheck: ignore 422
39 for version, items in pairs(result) do -- luacheck: ignore 422
40 for _, item in ipairs(items) do
41 -- Ignore provided rocks.
42 if item.arch ~= "installed" then
43 has_result = true
44 local filename = path.make_url(item.repo, name, version, item.arch)
45 local ok, err = get_file(filename)
46 if not ok then
47 all_ok = false
48 any_err = any_err .. "\n" .. err
49 end
50 end
51 end
52 end
53 end
54
55 if has_result then
56 return all_ok, any_err
57 end
58 else
59 local url: string
60 url, search_err = search.find_rock_checking_lua_versions(query, check_lua_versions)
61 if url then
62 return get_file(url)
63 end
64 end
65 local rock = util.format_rock_name(name, namespace, version)
66 return nil, "Could not find a result named "..rock..(search_err and ": "..search_err or ".")
67end
68
69return download
diff --git a/src/luarocks/fs.d.tl b/src/luarocks/fs.d.tl
index f9997db1..1e593c40 100644
--- a/src/luarocks/fs.d.tl
+++ b/src/luarocks/fs.d.tl
@@ -63,8 +63,10 @@ local record fs
63 apply_patch: function(string, string, boolean): boolean, string 63 apply_patch: function(string, string, boolean): boolean, string
64 copy_contents: function(string, string): boolean, string 64 copy_contents: function(string, string): boolean, string
65 remove_dir_if_empty: function(string) 65 remove_dir_if_empty: function(string)
66 --command 66 -- command
67 execute_env: function({any: any}, string, ...:string): boolean 67 execute_env: function({any: any}, string, ...:string): boolean
68 -- pack
69 zip: function(string, string, string): boolean, string
68end 70end
69 71
70return fs 72return fs
diff --git a/src/luarocks/pack.tl b/src/luarocks/pack.tl
new file mode 100644
index 00000000..c9ebae2e
--- /dev/null
+++ b/src/luarocks/pack.tl
@@ -0,0 +1,189 @@
1
2-- Create rock files, packing sources or binaries.
3local record pack
4end
5
6local queries = require("luarocks.queries")
7local path = require("luarocks.path")
8local repos = require("luarocks.repos")
9local fetch = require("luarocks.fetch")
10local fs = require("luarocks.fs")
11local cfg = require("luarocks.core.cfg")
12local util = require("luarocks.util")
13local dir = require("luarocks.dir")
14local manif = require("luarocks.manif")
15local search = require("luarocks.search")
16local signing = require("luarocks.signing")
17
18local type t = require("luarocks.core.types.tree")
19local type Tree = t.Tree
20
21local type q = require("luarocks.core.types.query")
22local type Query = q.Query
23
24
25--- Create a source rock.
26-- Packages a rockspec and its required source files in a rock
27-- file with the .src.rock extension, which can later be built and
28-- installed with the "build" command.
29-- @param rockspec_file string: An URL or pathname for a rockspec file.
30-- @return string or (nil, string): The filename of the resulting
31-- .src.rock file; or nil and an error message.
32function pack.pack_source_rock(rockspec_file: string): string, string
33
34 local rockspec, errload = fetch.load_rockspec(rockspec_file)
35 if errload then
36 return nil, "Error loading rockspec: "..errload
37 end
38 rockspec_file = rockspec.local_abs_filename
39
40 local name_version = rockspec.name .. "-" .. rockspec.version
41 local rock_file = fs.absolute_name(name_version .. ".src.rock")
42
43 local temp_dir, err = fs.make_temp_dir("pack-"..name_version)
44 if not temp_dir then
45 return nil, "Failed creating temporary directory: "..err
46 end
47 util.schedule_function(fs.delete, temp_dir)
48
49 local source_file, source_dir = fetch.fetch_sources(rockspec, true, temp_dir)
50 if not source_file then
51 return nil, source_dir
52 end
53 local ok, errchange = fs.change_dir(source_dir)
54 if not ok then return nil, errchange end
55
56 fs.delete(rock_file)
57 fs.copy(rockspec_file, source_dir, "read")
58 ok, err = fs.zip(rock_file, dir.base_name(rockspec_file), dir.base_name(source_file))
59 if not ok then
60 return nil, "Failed packing "..rock_file.." - "..err
61 end
62 fs.pop_dir()
63
64 return rock_file
65end
66
67local function copy_back_files(name: string, version: string, file_tree: {string: any}, deploy_dir: string, pack_dir: string, perms?: string): boolean, string
68 local ok, err = fs.make_dir(pack_dir)
69 if not ok then return nil, err end
70 for file, sub in pairs(file_tree) do
71 local source = dir.path(deploy_dir, file)
72 local target = dir.path(pack_dir, file)
73 if sub is {string: any} then
74 ok, err = copy_back_files(name, version, sub, source, target)
75 if not ok then return nil, err end
76 else
77 local versioned = path.versioned_name(source, deploy_dir, name, version)
78 if fs.exists(versioned) then
79 fs.copy(versioned, target, perms)
80 else
81 fs.copy(source, target, perms)
82 end
83 end
84 end
85 return true
86end
87
88-- @param name string: Name of package to pack.
89-- @param version string or nil: A version number may also be passed.
90-- @param tree string or nil: An optional tree to pick the package from.
91-- @return string or (nil, string): The filename of the resulting
92-- .src.rock file; or nil and an error message.
93function pack.pack_installed_rock(query: Query, tree: string | Tree)
94
95 local name, version, repo, repo_url = search.pick_installed_rock(query, tree)
96 if not name then
97 return nil, version
98 end
99
100 local root = path.root_from_rocks_dir(repo_url)
101 local prefix = path.install_dir(name, version, root)
102 if not fs.exists(prefix) then
103 return nil, "'"..name.." "..version.."' does not seem to be an installed rock."
104 end
105
106 local rock_manifest, err = manif.load_rock_manifest(name, version, root)
107 if not rock_manifest then return nil, err end
108
109 local name_version = name .. "-" .. version
110 local rock_file = fs.absolute_name(name_version .. "."..cfg.arch..".rock")
111
112 local temp_dir = fs.make_temp_dir("pack")
113 fs.copy_contents(prefix, temp_dir)
114
115 local is_binary = false
116 if rock_manifest.lib then
117 local ok, err = copy_back_files(name, version, rock_manifest.lib, path.deploy_lib_dir(repo), dir.path(temp_dir, "lib"), "exec")
118 if not ok then return nil, "Failed copying back files: " .. err end
119 is_binary = true
120 end
121 if rock_manifest.lua then
122 local ok, err = copy_back_files(name, version, rock_manifest.lua, path.deploy_lua_dir(repo), dir.path(temp_dir, "lua"), "read")
123 if not ok then return nil, "Failed copying back files: " .. err end
124 end
125
126 local ok, err = fs.change_dir(temp_dir)
127 if not ok then return nil, err end
128 if not is_binary and not repos.has_binaries(name, version) then
129 rock_file = rock_file:gsub("%."..cfg.arch:gsub("%-","%%-").."%.", ".all.")
130 end
131 fs.delete(rock_file)
132 ok, err = fs.zip(rock_file, unpack(fs.list_dir()))
133 if not ok then
134 return nil, "Failed packing " .. rock_file .. " - " .. err
135 end
136 fs.pop_dir()
137 fs.delete(temp_dir)
138 return rock_file
139end
140
141function pack.report_and_sign_local_file(file, err, sign)
142 if err then
143 return nil, err
144 end
145 local sigfile
146 if sign then
147 sigfile, err = signing.sign_file(file)
148 util.printout()
149 end
150 util.printout("Packed: "..file)
151 if sigfile then
152 util.printout("Signature stored in: "..sigfile)
153 end
154 if err then
155 return nil, err
156 end
157 return true
158end
159
160function pack.pack_binary_rock(name, namespace, version, sign, cmd)
161
162 -- The --pack-binary-rock option for "luarocks build" basically performs
163 -- "luarocks build" on a temporary tree and then "luarocks pack". The
164 -- alternative would require refactoring parts of luarocks.build and
165 -- luarocks.pack, which would save a few file operations: the idea would be
166 -- to shave off the final deploy steps from the build phase and the initial
167 -- collect steps from the pack phase.
168
169 local temp_dir, err = fs.make_temp_dir("luarocks-build-pack-"..dir.base_name(name))
170 if not temp_dir then
171 return nil, "Failed creating temporary directory: "..err
172 end
173 util.schedule_function(fs.delete, temp_dir)
174
175 path.use_tree(temp_dir)
176 local ok, err = cmd()
177 if not ok then
178 return nil, err
179 end
180 local rname, rversion = path.parse_name(name)
181 if not rname then
182 rname, rversion = name, version
183 end
184 local query = queries.new(rname, namespace, rversion)
185 local file, err = pack.pack_installed_rock(query, temp_dir)
186 return pack.report_and_sign_local_file(file, err, sign)
187end
188
189return pack
diff --git a/src/luarocks/search.tl b/src/luarocks/search.tl
index 5681fa54..3737b26a 100644
--- a/src/luarocks/search.tl
+++ b/src/luarocks/search.tl
@@ -254,7 +254,7 @@ function search.find_suitable_rock(query: Query): string, string, string
254 end 254 end
255end 255end
256 256
257function search.find_rock_checking_lua_versions(query: Query, check_lua_versions): string, string 257function search.find_rock_checking_lua_versions(query: Query, check_lua_versions: boolean): string, string
258 local url, err, errcode = search.find_suitable_rock(query) 258 local url, err, errcode = search.find_suitable_rock(query)
259 if url then 259 if url then
260 return url 260 return url
@@ -304,7 +304,7 @@ function search.find_rock_checking_lua_versions(query: Query, check_lua_versions
304 return nil, err 304 return nil, err
305end 305end
306 306
307function search.find_src_or_rockspec(name: string, namespace: string, version: string, check_lua_versions): string, string 307function search.find_src_or_rockspec(name: string, namespace: string, version: string, check_lua_versions: boolean): string, string
308 local query = queries.new(name, namespace, version, false, "src|rockspec") 308 local query = queries.new(name, namespace, version, false, "src|rockspec")
309 local url, err = search.find_rock_checking_lua_versions(query, check_lua_versions) 309 local url, err = search.find_rock_checking_lua_versions(query, check_lua_versions)
310 if not url then 310 if not url then