comparison share/lua/5.2/luarocks/new_version.lua @ 1132:d137f631bad5

<GreyKnight> (cd luabuild/luarocks-2.0.12; make install)
author HackBot
date Fri, 14 Dec 2012 22:24:27 +0000
parents
children
comparison
equal deleted inserted replaced
1131:ff65dfb63263 1132:d137f631bad5
1
2 --- Module implementing the LuaRocks "new_version" command.
3 -- Utility function that writes a new rockspec, updating data from a previous one.
4 module("luarocks.new_version", package.seeall)
5
6 local util = require("luarocks.util")
7 local cfg = require("luarocks.cfg")
8 local download = require("luarocks.download")
9 local fetch = require("luarocks.fetch")
10 local persist = require("luarocks.persist")
11 local dir = require("luarocks.dir")
12 local fs = require("luarocks.fs")
13
14 help_summary = "Auto-write a rockspec for a new version of a rock."
15 help_arguments = "{<package>|<rockspec>} [<new_version>] [<new_url>]"
16 help = [[
17 This is a utility function that writes a new rockspec, updating data
18 from a previous one.
19
20 If a package name is given, it downloads the latest rockspec from the
21 default server. If a rockspec is given, it uses it instead.
22
23 If the version number is not given, it only increments the revision
24 number of the given (or downloaded) rockspec.
25
26 If a URL is given, it replaces the one from the old rockspec with the
27 given URL. If a URL is not given and a new version is given, it tries
28 to guess the new URL by replacing occurrences of the version number
29 in the URL or tag. It also tries to download the new URL to determine
30 the new MD5 checksum.
31
32 WARNING: it writes the new rockspec to the current directory,
33 overwriting the file if it already exists.
34 ]]
35
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"} },
41 "hooks"}
42
43 local function try_replace(tbl, field, old, new)
44 if not tbl[field] then
45 return false
46 end
47 local old_field = tbl[field]
48 local new_field = tbl[field]:gsub(old, new)
49 if new_field ~= old_field then
50 util.printout("Guessing new '"..field.."' field as "..new_field)
51 tbl[field] = new_field
52 return true
53 end
54 return false
55 end
56
57 local function check_url_and_update_md5(out_rs, out_name)
58 out_rs.source.md5 = nil
59 local file, temp_dir = fetch.fetch_url_at_temp_dir(out_rs.source.url, "luarocks-new-version-"..out_name)
60 if file then
61 util.printout("File successfully downloaded. Updating MD5 checksum...")
62 out_rs.source.md5 = fs.get_md5(file)
63 else
64 util.printerr("Warning: invalid URL - "..temp_dir)
65 end
66 end
67
68 function run(...)
69 local flags, input, version, url = util.parse_flags(...)
70 if not input then
71 return nil, "Missing arguments: expected program or rockspec. See help."
72 end
73 assert(type(input) == "string")
74
75 local filename = input
76 if not input:match(".rockspec$") then
77 local err
78 filename, err = download.download("rockspec", input)
79 if not filename then
80 return nil, err
81 end
82 end
83
84 local valid_rs, err = fetch.load_rockspec(filename)
85 if not valid_rs then
86 return nil, err
87 end
88
89 local old_ver, old_rev = valid_rs.version:match("(.*)%-(%d+)$")
90 local new_ver, new_rev
91
92 if version then
93 new_ver, new_rev = version:match("(.*)%-(%d+)$")
94 new_rev = tonumber(new_rev)
95 if not new_rev then
96 new_ver = version
97 new_rev = 1
98 end
99 else
100 new_ver = old_ver
101 new_rev = tonumber(old_rev) + 1
102 end
103
104
105 local out_rs = persist.load_into_table(filename)
106 local out_name = out_rs.package:lower()
107 out_rs.version = new_ver.."-"..new_rev
108 if url then
109 out_rs.source.url = url
110 check_url_and_update_md5(out_rs, out_name)
111 else
112 if new_ver ~= old_ver then
113 local ok = try_replace(out_rs.source, "url", old_ver, new_ver)
114 if ok then
115 check_url_and_update_md5(out_rs, out_name)
116 else
117 ok = try_replace(out_rs.source, "tag", old_ver, new_ver)
118 if not ok then
119 return nil, "Failed to determine the location of the new version."
120 end
121 end
122 end
123 end
124
125 if out_rs.build and out_rs.build.type == "module" then
126 out_rs.build.type = "builtin"
127 end
128
129 local out_filename = out_name.."-"..new_ver.."-"..new_rev..".rockspec"
130
131 persist.save_from_table(out_filename, out_rs, order)
132
133 util.printout("Wrote "..out_filename)
134
135 local valid_out_rs, err = fetch.load_local_rockspec(out_filename)
136 if not valid_out_rs then
137 return nil, "Failed loading generated rockspec: "..err
138 end
139
140 return true
141 end