diff options
author | Hisham Muhammad <hisham@gobolinux.org> | 2018-06-19 11:23:05 -0300 |
---|---|---|
committer | Hisham Muhammad <hisham@gobolinux.org> | 2018-07-01 15:51:13 -0300 |
commit | 50cd8ea8941d38678e8e6cf82065108dc3217d91 (patch) | |
tree | 6add8edcaeec6ade0f768db63df116671339c6fa /src | |
parent | 968291927c5e54c190f60ae44f4af2effa6e9cf8 (diff) | |
download | luarocks-50cd8ea8941d38678e8e6cf82065108dc3217d91.tar.gz luarocks-50cd8ea8941d38678e8e6cf82065108dc3217d91.tar.bz2 luarocks-50cd8ea8941d38678e8e6cf82065108dc3217d91.zip |
persist: add save_as_module, for all-in-one binary
Diffstat (limited to 'src')
-rw-r--r-- | src/luarocks/persist.lua | 35 |
1 files changed, 33 insertions, 2 deletions
diff --git a/src/luarocks/persist.lua b/src/luarocks/persist.lua index 1460bd15..6dc8b9ab 100644 --- a/src/luarocks/persist.lua +++ b/src/luarocks/persist.lua | |||
@@ -58,7 +58,7 @@ write_table = function(out, tbl, level, field_order) | |||
58 | for k, v, sub_order in util.sortedpairs(tbl, field_order) do | 58 | for k, v, sub_order in util.sortedpairs(tbl, field_order) do |
59 | out:write(sep) | 59 | out:write(sep) |
60 | if indent then | 60 | if indent then |
61 | for n = 1,level do out:write(indentation) end | 61 | for _ = 1, level do out:write(indentation) end |
62 | end | 62 | end |
63 | 63 | ||
64 | if k == i then | 64 | if k == i then |
@@ -86,7 +86,7 @@ write_table = function(out, tbl, level, field_order) | |||
86 | end | 86 | end |
87 | if sep ~= "\n" then | 87 | if sep ~= "\n" then |
88 | out:write("\n") | 88 | out:write("\n") |
89 | for n = 1,level-1 do out:write(indentation) end | 89 | for _ = 1, level - 1 do out:write(indentation) end |
90 | end | 90 | end |
91 | out:write("}") | 91 | out:write("}") |
92 | end | 92 | end |
@@ -103,6 +103,19 @@ local function write_table_as_assignments(out, tbl, field_order) | |||
103 | end | 103 | end |
104 | end | 104 | end |
105 | 105 | ||
106 | --- Write a table as series of assignments to a writer object. | ||
107 | -- @param out table or userdata: a writer object supporting :write() method. | ||
108 | -- @param tbl table: the table to be written. | ||
109 | local function write_table_as_table(out, tbl) | ||
110 | out:write("return {\n") | ||
111 | for k, v, sub_order in util.sortedpairs(tbl) do | ||
112 | out:write(" " .. k .. " = ") | ||
113 | write_value(out, v, 1, sub_order) | ||
114 | out:write(",\n") | ||
115 | end | ||
116 | out:write("}\n") | ||
117 | end | ||
118 | |||
106 | --- Save the contents of a table to a string. | 119 | --- Save the contents of a table to a string. |
107 | -- Each element of the table is saved as a global assignment. | 120 | -- Each element of the table is saved as a global assignment. |
108 | -- Only numbers, strings and tables (containing numbers, strings | 121 | -- Only numbers, strings and tables (containing numbers, strings |
@@ -136,4 +149,22 @@ function persist.save_from_table(filename, tbl, field_order) | |||
136 | return true | 149 | return true |
137 | end | 150 | end |
138 | 151 | ||
152 | --- Save the contents of a table as a module. | ||
153 | -- Each element of the table is saved as a global assignment. | ||
154 | -- Only numbers, strings and tables (containing numbers, strings | ||
155 | -- or other recursively processed tables) are supported. | ||
156 | -- @param filename string: the output filename | ||
157 | -- @param tbl table: the table containing the data to be written | ||
158 | -- @return boolean or (nil, string): true if successful, or nil and a | ||
159 | -- message in case of errors. | ||
160 | function persist.save_as_module(filename, tbl) | ||
161 | local out = io.open(filename, "w") | ||
162 | if not out then | ||
163 | return nil, "Cannot create file at "..filename | ||
164 | end | ||
165 | write_table_as_table(out, tbl) | ||
166 | out:close() | ||
167 | return true | ||
168 | end | ||
169 | |||
139 | return persist | 170 | return persist |