aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/luarocks/persist.lua26
1 files changed, 17 insertions, 9 deletions
diff --git a/src/luarocks/persist.lua b/src/luarocks/persist.lua
index b7b5252d..132c15b7 100644
--- a/src/luarocks/persist.lua
+++ b/src/luarocks/persist.lua
@@ -125,26 +125,34 @@ write_table = function(out, tbl, level, field_order)
125 out:write("}") 125 out:write("}")
126end 126end
127 127
128--- Writes a rockspec table to an io-like object.
129-- @param out userdata: a file object, open for writing.
130-- @param tbl table: the table to be written.
131-- @param field_order table: optional prioritization table
132-- @return userdata The file object originally passed in as the `out` parameter.
133local function write_rockspec(out, tbl, field_order)
134 for k, v, sub_order in util.sortedpairs(tbl, field_order) do
135 out:write(k.." = ")
136 write_value(out, v, 0, sub_order)
137 out:write("\n")
138 end
139 return out
140end
141
128--- Save the contents of a table to a string. 142--- Save the contents of a table to a string.
129-- Each element of the table is saved as a global assignment. 143-- Each element of the table is saved as a global assignment.
130-- Only numbers, strings and tables (containing numbers, strings 144-- Only numbers, strings and tables (containing numbers, strings
131-- or other recursively processed tables) are supported. 145-- or other recursively processed tables) are supported.
132-- @param tbl table: the table containing the data to be written 146-- @param tbl table: the table containing the data to be written
133-- @param field_order table: an optional array indicating the order of top-level fields. 147-- @param field_order table: an optional array indicating the order of top-level fields.
134-- @return string or (nil, string): string if successful, or nil and a 148-- @return string
135-- message in case of errors.
136function save_from_table_to_string(tbl, field_order) 149function save_from_table_to_string(tbl, field_order)
137 local out = {buffer = {}} 150 local out = {buffer = {}}
138 function out:write(data) table.insert(self.buffer, data) end 151 function out:write(data) table.insert(self.buffer, data) end
139 for k, v, sub_order in util.sortedpairs(tbl, field_order) do 152 write_rockspec(out, tbl, field_order)
140 out:write(k.." = ")
141 write_value(out, v, 0, sub_order)
142 out:write("\n")
143 end
144 return table.concat(out.buffer) 153 return table.concat(out.buffer)
145end 154end
146 155
147
148--- Save the contents of a table in a file. 156--- Save the contents of a table in a file.
149-- Each element of the table is saved as a global assignment. 157-- Each element of the table is saved as a global assignment.
150-- Only numbers, strings and tables (containing numbers, strings 158-- Only numbers, strings and tables (containing numbers, strings
@@ -159,7 +167,7 @@ function save_from_table(filename, tbl, field_order)
159 if not out then 167 if not out then
160 return nil, "Cannot create file at "..filename 168 return nil, "Cannot create file at "..filename
161 end 169 end
162 out:write(save_from_table_to_string(tbl, field_order)) 170 write_rockspec(out, tbl, field_order)
163 out:close() 171 out:close()
164 return true 172 return true
165end 173end