2
2
--- Module implementing the LuaRocks "new_version" command.
3
3
-- Utility function that writes a new rockspec, updating data from a previous one.
4
module("luarocks.new_version", package.seeall)
4
--module("luarocks.new_version", package.seeall)
6
package.loaded["luarocks.new_version"] = new_version
6
8
local util = require("luarocks.util")
7
local cfg = require("luarocks.cfg")
8
9
local download = require("luarocks.download")
9
10
local fetch = require("luarocks.fetch")
10
11
local persist = require("luarocks.persist")
11
local dir = require("luarocks.dir")
12
12
local fs = require("luarocks.fs")
13
local type_check = require("luarocks.type_check")
14
help_summary = "Auto-write a rockspec for a new version of a rock."
15
help_arguments = "{<program>|<rockspec>} [<new_version>] [<new_url>]"
15
new_version.help_summary = "Auto-write a rockspec for a new version of a rock."
16
new_version.help_arguments = "{<package>|<rockspec>} [<new_version>] [<new_url>]"
17
18
This is a utility function that writes a new rockspec, updating data
18
19
from a previous one.
33
34
overwriting the file if it already exists.
36
local order = {"rockspec_format", "package", "version",
37
{ "source", { "url", "tag", "branch", "md5" } },
38
{ "description", {"summary", "detailed", "homepage", "license" } },
39
"supported_platforms", "dependencies", "external_dependencies",
40
{ "build", {"type", "modules", "copy_directories", "platforms"} },
43
37
local function try_replace(tbl, field, old, new)
44
38
if not tbl[field] then
57
51
local function check_url_and_update_md5(out_rs, out_name)
52
local old_md5 = out_rs.source.md5
58
53
out_rs.source.md5 = nil
59
54
local file, temp_dir = fetch.fetch_url_at_temp_dir(out_rs.source.url, "luarocks-new-version-"..out_name)
61
util.printout("File successfully downloaded. Updating MD5 checksum...")
62
out_rs.source.md5 = fs.get_md5(file)
64
56
util.printerr("Warning: invalid URL - "..temp_dir)
59
util.printout("File successfully downloaded. Updating MD5 checksum...")
60
out_rs.source.md5 = fs.get_md5(file)
61
local inferred_dir, found_dir = fetch.find_base_dir(file, temp_dir, out_rs.source.url, out_rs.source.dir)
62
if not inferred_dir then
65
if found_dir and found_dir ~= inferred_dir then
66
out_rs.source.dir = found_dir
68
return out_rs.source.md5 ~= old_md5
71
local function update_source_section(out_rs, out_name, url, old_ver, new_ver)
73
out_rs.source.url = url
74
check_url_and_update_md5(out_rs, out_name)
77
if new_ver == old_ver then
80
if not out_rs.source then
81
return nil, "'source' table is missing. Invalid rockspec?"
83
if out_rs.source.dir then
84
try_replace(out_rs.source, "dir", old_ver, new_ver)
86
if out_rs.source.file then
87
try_replace(out_rs.source, "file", old_ver, new_ver)
89
local ok = try_replace(out_rs.source, "url", old_ver, new_ver)
91
check_url_and_update_md5(out_rs, out_name)
94
ok = try_replace(out_rs.source, "tag", old_ver, new_ver)
96
ok = check_url_and_update_md5(out_rs, out_name)
98
util.printerr("Warning: URL is the same, but MD5 has changed. Old rockspec is broken.")
102
return nil, "Failed to determine the location of the new version."
107
function new_version.run(...)
69
108
local flags, input, version, url = util.parse_flags(...)
71
return nil, "Missing arguments: expected program or rockspec. See help."
110
return nil, "Missing arguments: expected program or rockspec. "..util.see_help("new_version")
73
112
assert(type(input) == "string")
100
139
new_ver = old_ver
101
140
new_rev = tonumber(old_rev) + 1
142
local new_rockver = new_ver:gsub("-", "")
105
144
local out_rs = persist.load_into_table(filename)
106
145
local out_name = out_rs.package:lower()
107
out_rs.version = new_ver.."-"..new_rev
109
out_rs.source.url = url
110
check_url_and_update_md5(out_rs, out_name)
112
if new_ver ~= old_ver then
113
local ok = try_replace(out_rs.source, "url", old_ver, new_ver)
115
check_url_and_update_md5(out_rs, out_name)
117
ok = try_replace(out_rs.source, "tag", old_ver, new_ver)
119
return nil, "Failed to determine the location of the new version."
146
out_rs.version = new_rockver.."-"..new_rev
148
local ok, err = update_source_section(out_rs, out_name, url, old_ver, new_ver)
149
if not ok then return nil, err end
125
151
if out_rs.build and out_rs.build.type == "module" then
126
152
out_rs.build.type = "builtin"
129
local out_filename = out_name.."-"..new_ver.."-"..new_rev..".rockspec"
155
local out_filename = out_name.."-"..new_rockver.."-"..new_rev..".rockspec"
131
persist.save_from_table(out_filename, out_rs, order)
157
persist.save_from_table(out_filename, out_rs, type_check.rockspec_order)
133
159
util.printout("Wrote "..out_filename)