~ubuntu-branches/ubuntu/vivid/luarocks/vivid-proposed

« back to all changes in this revision

Viewing changes to src/luarocks/new_version.lua

  • Committer: Package Import Robot
  • Author(s): Enrico Tassi
  • Date: 2014-10-11 15:26:47 UTC
  • mfrom: (1.2.6)
  • Revision ID: package-import@ubuntu.com-20141011152647-bkfciayfdz6elvv3
Tags: 2.2.0+dfsg-1
* New upstream release
* patch 0001-Fixed-detection-of-Debian-paths removed (applied upstream)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
 
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)
 
5
local new_version = {}
 
6
package.loaded["luarocks.new_version"] = new_version
5
7
 
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")
13
14
 
14
 
help_summary = "Auto-write a rockspec for a new version of a rock."
15
 
help_arguments = "{<program>|<rockspec>} [<new_version>] [<new_url>]"
16
 
help = [[
 
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
new_version.help = [[
17
18
This is a utility function that writes a new rockspec, updating data
18
19
from a previous one.
19
20
 
33
34
overwriting the file if it already exists.
34
35
]]
35
36
 
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
37
local function try_replace(tbl, field, old, new)
44
38
   if not tbl[field] then
45
39
      return false
55
49
end
56
50
 
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)
60
 
   if file then
61
 
      util.printout("File successfully downloaded. Updating MD5 checksum...")
62
 
      out_rs.source.md5 = fs.get_md5(file)
63
 
   else
 
55
   if not file then
64
56
      util.printerr("Warning: invalid URL - "..temp_dir)
65
 
   end
66
 
end
67
 
 
68
 
function run(...)
 
57
      return true
 
58
   end
 
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
 
63
      return nil, found_dir
 
64
   end
 
65
   if found_dir and found_dir ~= inferred_dir then
 
66
      out_rs.source.dir = found_dir
 
67
   end
 
68
   return out_rs.source.md5 ~= old_md5
 
69
end
 
70
 
 
71
local function update_source_section(out_rs, out_name, url, old_ver, new_ver)
 
72
   if url then
 
73
      out_rs.source.url = url
 
74
      check_url_and_update_md5(out_rs, out_name)
 
75
      return true
 
76
   end
 
77
   if new_ver == old_ver then
 
78
      return true
 
79
   end
 
80
   if not out_rs.source then
 
81
      return nil, "'source' table is missing. Invalid rockspec?"
 
82
   end
 
83
   if out_rs.source.dir then
 
84
      try_replace(out_rs.source, "dir", old_ver, new_ver)
 
85
   end
 
86
   if out_rs.source.file then
 
87
      try_replace(out_rs.source, "file", old_ver, new_ver)
 
88
   end
 
89
   local ok = try_replace(out_rs.source, "url", old_ver, new_ver)
 
90
   if ok then
 
91
      check_url_and_update_md5(out_rs, out_name)
 
92
      return true
 
93
   end
 
94
   ok = try_replace(out_rs.source, "tag", old_ver, new_ver)
 
95
   if not ok then
 
96
      ok = check_url_and_update_md5(out_rs, out_name)
 
97
      if ok then
 
98
         util.printerr("Warning: URL is the same, but MD5 has changed. Old rockspec is broken.")
 
99
      end
 
100
   end
 
101
   if not ok then
 
102
      return nil, "Failed to determine the location of the new version."
 
103
   end
 
104
   return true
 
105
end
 
106
 
 
107
function new_version.run(...)
69
108
   local flags, input, version, url = util.parse_flags(...)
70
109
   if not input then
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")
72
111
   end
73
112
   assert(type(input) == "string")
74
113
   
76
115
   if not input:match(".rockspec$") then
77
116
      local err
78
117
      filename, err = download.download("rockspec", input)
79
 
      if not input then
 
118
      if not filename then
80
119
         return nil, err
81
120
      end
82
121
   end
100
139
      new_ver = old_ver
101
140
      new_rev = tonumber(old_rev) + 1
102
141
   end
103
 
   
 
142
   local new_rockver = new_ver:gsub("-", "")
104
143
   
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
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
 
   
 
146
   out_rs.version = new_rockver.."-"..new_rev
 
147
 
 
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
 
150
 
125
151
   if out_rs.build and out_rs.build.type == "module" then
126
152
      out_rs.build.type = "builtin"
127
153
   end
128
154
   
129
 
   local out_filename = out_name.."-"..new_ver.."-"..new_rev..".rockspec"
 
155
   local out_filename = out_name.."-"..new_rockver.."-"..new_rev..".rockspec"
130
156
   
131
 
   persist.save_from_table(out_filename, out_rs, order)
 
157
   persist.save_from_table(out_filename, out_rs, type_check.rockspec_order)
132
158
   
133
159
   util.printout("Wrote "..out_filename)
134
160
 
139
165
   
140
166
   return true
141
167
end
 
168
 
 
169
return new_version