aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/luarocks/cmd/new_version.tl (renamed from src/luarocks/cmd/new_version.lua)53
1 files changed, 31 insertions, 22 deletions
diff --git a/src/luarocks/cmd/new_version.lua b/src/luarocks/cmd/new_version.tl
index ccba9335..d5da0e86 100644
--- a/src/luarocks/cmd/new_version.lua
+++ b/src/luarocks/cmd/new_version.tl
@@ -1,7 +1,8 @@
1 1
2--- Module implementing the LuaRocks "new_version" command. 2--- Module implementing the LuaRocks "new_version" command.
3-- Utility function that writes a new rockspec, updating data from a previous one. 3-- Utility function that writes a new rockspec, updating data from a previous one.
4local new_version = {} 4local record new_version
5end
5 6
6local util = require("luarocks.util") 7local util = require("luarocks.util")
7local download = require("luarocks.download") 8local download = require("luarocks.download")
@@ -11,7 +12,15 @@ local fs = require("luarocks.fs")
11local dir = require("luarocks.dir") 12local dir = require("luarocks.dir")
12local type_rockspec = require("luarocks.type.rockspec") 13local type_rockspec = require("luarocks.type.rockspec")
13 14
14function new_version.add_to_parser(parser) 15local type Parser = require("luarocks.vendor.argparse").Parser
16
17local type Args = require("luarocks.core.types.args").Args
18
19local type PersistableTable = require("luarocks.core.types.persist").PersistableTable
20
21local type Rockspec = require("luarocks.core.types.rockspec").Rockspec
22
23function new_version.add_to_parser(parser: Parser)
15 local cmd = parser:command("new_version", [[ 24 local cmd = parser:command("new_version", [[
16This is a utility function that writes a new rockspec, updating data from a 25This is a utility function that writes a new rockspec, updating data from a
17previous one. 26previous one.
@@ -51,7 +60,7 @@ if it already exists.]], util.see_also())
51end 60end
52 61
53 62
54local function try_replace(tbl, field, old, new) 63local function try_replace(tbl: {string: string}, field: string, old: string, new: string): boolean
55 if not tbl[field] then 64 if not tbl[field] then
56 return false 65 return false
57 end 66 end
@@ -69,7 +78,7 @@ end
69-- If it specified MD5, update it. 78-- If it specified MD5, update it.
70-- @return (true, false) if MD5 was not specified or it stayed same, 79-- @return (true, false) if MD5 was not specified or it stayed same,
71-- (true, true) if MD5 changed, (nil, string) on error. 80-- (true, true) if MD5 changed, (nil, string) on error.
72local function check_url_and_update_md5(out_rs, invalid_is_error) 81local function check_url_and_update_md5(out_rs: Rockspec, invalid_is_error?: boolean): boolean, string | boolean --!
73 local file, temp_dir = fetch.fetch_url_at_temp_dir(out_rs.source.url, "luarocks-new-version-"..out_rs.package) 82 local file, temp_dir = fetch.fetch_url_at_temp_dir(out_rs.source.url, "luarocks-new-version-"..out_rs.package)
74 if not file then 83 if not file then
75 if invalid_is_error then 84 if invalid_is_error then
@@ -105,7 +114,7 @@ local function check_url_and_update_md5(out_rs, invalid_is_error)
105 end 114 end
106end 115end
107 116
108local function update_source_section(out_rs, url, tag, old_ver, new_ver) 117local function update_source_section(out_rs: Rockspec, url: string, tag: string, old_ver: string, new_ver: string): boolean, string | boolean
109 if tag then 118 if tag then
110 out_rs.source.tag = tag 119 out_rs.source.tag = tag
111 end 120 end
@@ -117,21 +126,21 @@ local function update_source_section(out_rs, url, tag, old_ver, new_ver)
117 return true 126 return true
118 end 127 end
119 if out_rs.source.dir then 128 if out_rs.source.dir then
120 try_replace(out_rs.source, "dir", old_ver, new_ver) 129 try_replace(out_rs.source as {string: string}, "dir", old_ver, new_ver)
121 end 130 end
122 if out_rs.source.file then 131 if out_rs.source.file then
123 try_replace(out_rs.source, "file", old_ver, new_ver) 132 try_replace(out_rs.source as {string: string}, "file", old_ver, new_ver)
124 end 133 end
125 134
126 local old_url = out_rs.source.url 135 local old_url = out_rs.source.url
127 if try_replace(out_rs.source, "url", old_ver, new_ver) then 136 if try_replace(out_rs.source as {string: string}, "url", old_ver, new_ver) then
128 local ok, md5_changed = check_url_and_update_md5(out_rs, true) 137 local ok, md5_changed = check_url_and_update_md5(out_rs, true)
129 if ok then 138 if ok then
130 return ok, md5_changed 139 return ok, md5_changed
131 end 140 end
132 out_rs.source.url = old_url 141 out_rs.source.url = old_url
133 end 142 end
134 if tag or try_replace(out_rs.source, "tag", old_ver, new_ver) then 143 if tag or try_replace(out_rs.source as {string: string}, "tag", old_ver, new_ver) then
135 return true 144 return true
136 end 145 end
137 -- Couldn't replace anything significant, use the old URL. 146 -- Couldn't replace anything significant, use the old URL.
@@ -145,23 +154,23 @@ local function update_source_section(out_rs, url, tag, old_ver, new_ver)
145 return true 154 return true
146end 155end
147 156
148function new_version.command(args) 157function new_version.command(args: Args): boolean, string | boolean
149 if not args.rock then 158 if not args.rock then
150 local err 159 local err: string
151 args.rock, err = util.get_default_rockspec() 160 args.rock, err = util.get_default_rockspec()
152 if not args.rock then 161 if not args.rock then
153 return nil, err 162 return nil, err
154 end 163 end
155 end 164 end
156 165
157 local filename, err 166 local filename, err: string, string
158 if args.rock:match("rockspec$") then 167 if args.rock:match("rockspec$") then
159 filename, err = fetch.fetch_url(args.rock) 168 filename, err = fetch.fetch_url(args.rock)
160 if not filename then 169 if not filename then
161 return nil, err 170 return nil, err
162 end 171 end
163 else 172 else
164 filename, err = download.download("rockspec", args.rock:lower()) 173 filename, err = download.download_file("rockspec", args.rock:lower())
165 if not filename then 174 if not filename then
166 return nil, err 175 return nil, err
167 end 176 end
@@ -173,33 +182,33 @@ function new_version.command(args)
173 end 182 end
174 183
175 local old_ver, old_rev = valid_rs.version:match("(.*)%-(%d+)$") 184 local old_ver, old_rev = valid_rs.version:match("(.*)%-(%d+)$")
176 local new_ver, new_rev 185 local new_ver, new_rev_str, new_rev: string, string, integer
177 186
178 if args.tag and not args.new_version then 187 if args.tag and not args.new_version then
179 args.new_version = args.tag:gsub("^v", "") 188 args.new_version = args.tag:gsub("^v", "")
180 end 189 end
181 190
182 local out_dir 191 local out_dir: string
183 if args.dir then 192 if args.dir then
184 out_dir = dir.normalize(args.dir) 193 out_dir = dir.normalize(args.dir)
185 end 194 end
186 195
187 if args.new_version then 196 if args.new_version then
188 new_ver, new_rev = args.new_version:match("(.*)%-(%d+)$") 197 new_ver, new_rev_str = args.new_version:match("(.*)%-(%d+)$")
189 new_rev = tonumber(new_rev) 198 new_rev = math.tointeger(new_rev_str)
190 if not new_rev then 199 if not new_rev then
191 new_ver = args.new_version 200 new_ver = args.new_version
192 new_rev = 1 201 new_rev = 1
193 end 202 end
194 else 203 else
195 new_ver = old_ver 204 new_ver = old_ver
196 new_rev = tonumber(old_rev) + 1 205 new_rev = math.tointeger(old_rev) + 1
197 end 206 end
198 local new_rockver = new_ver:gsub("-", "") 207 local new_rockver = new_ver:gsub("-", "")
199 208
200 local out_rs, err = persist.load_into_table(filename) 209 local out_rs, err = persist.load_into_table(filename) as Rockspec, string
201 local out_name = out_rs.package:lower() 210 local out_name = out_rs.package:lower()
202 out_rs.version = new_rockver.."-"..new_rev 211 out_rs.version = new_rockver.."-"..tostring(new_rev)
203 212
204 local ok, err = update_source_section(out_rs, args.new_url, args.tag, old_ver, new_ver) 213 local ok, err = update_source_section(out_rs, args.new_url, args.tag, old_ver, new_ver)
205 if not ok then return nil, err end 214 if not ok then return nil, err end
@@ -208,12 +217,12 @@ function new_version.command(args)
208 out_rs.build.type = "builtin" 217 out_rs.build.type = "builtin"
209 end 218 end
210 219
211 local out_filename = out_name.."-"..new_rockver.."-"..new_rev..".rockspec" 220 local out_filename = out_name.."-"..new_rockver.."-"..tostring(new_rev)..".rockspec"
212 if out_dir then 221 if out_dir then
213 out_filename = dir.path(out_dir, out_filename) 222 out_filename = dir.path(out_dir, out_filename)
214 fs.make_dir(out_dir) 223 fs.make_dir(out_dir)
215 end 224 end
216 persist.save_from_table(out_filename, out_rs, type_rockspec.order) 225 persist.save_from_table(out_filename, out_rs as PersistableTable, type_rockspec.order)
217 226
218 util.printout("Wrote "..out_filename) 227 util.printout("Wrote "..out_filename)
219 228