Stop using io.tmpfile() in script embedding; doesn't always work on Windows

This commit is contained in:
Jason Perkins 2014-05-29 18:04:58 -04:00
parent 8fe9c55a15
commit 27a39db6a6

View File

@ -46,19 +46,11 @@
end
local function writeline(out, s, continues)
out:write("\t\"")
out:write(s)
out:write(iif(continues, "\"\n", "\",\n"))
end
local function writefile(out, fname, contents)
local max = 1024
out:write("\t/* " .. fname .. " */\n")
local function appendfile(result, fname, contents)
table.insert(result, "\t/* " .. fname .. " */\n")
-- break up large strings to fit in Visual Studio's string length limit
local max = 4096
local start = 1
local len = contents:len()
while start <= len do
@ -71,11 +63,13 @@
finish = finish - 1
end
writeline(out, contents:sub(start, finish), finish < len)
local s = contents:sub(start, finish)
table.insert(result, "\t\"" .. s .. iif(finish < len, "\"\n", "\",\n"))
start = finish + 1
end
out:write("\n")
table.insert(result, "\n")
end
@ -95,26 +89,23 @@
-- Main script always goes first
table.insert(scripts, 1, "_premake_main.lua")
-- Embed all the scripts to a temporary file first
local file = io.tmpfile()
file:write("/* Premake's Lua scripts, as static data buffers for release mode builds */\n")
file:write("/* DO NOT EDIT - this file is autogenerated - see BUILD.txt */\n")
file:write("/* To regenerate this file, run: premake5 embed */ \n\n")
file:write("const char* builtin_scripts[] = {\n")
-- Convert all scripts to single in-memory string
local result = {}
table.insert(result, "/* Premake's Lua scripts, as static data buffers for release mode builds */\n")
table.insert(result, "/* DO NOT EDIT - this file is autogenerated - see BUILD.txt */\n")
table.insert(result, "/* To regenerate this file, run: premake5 embed */ \n\n")
table.insert(result, "const char* builtin_scripts[] = {\n")
for i, fn in ipairs(scripts) do
local s = stripfile(path.join(dir, fn))
writefile(file, fn, s)
appendfile(result, fn, s)
end
file:write("\t0\n};\n");
table.insert(result, "\t0\n};\n");
result = table.concat(result)
-- Now read it back in and compare it to the current scripts.c; only
-- write it out if changed.
file:seek("set", 0)
local newVersion = file:read("*a")
file:close()
-- Compare it to the current contents of scripts.c; only write out
-- a new scripts.c if there have been changes
local oldVersion
local scriptsFile = path.join(basedir, "src/host/scripts.c")
@ -125,11 +116,10 @@
file:close()
end
if newVersion ~= oldVersion then
if oldVersion ~= result then
print("Writing scripts.c")
file = io.open(scriptsFile, "w+b")
file:write(newVersion)
file:write(result)
file:close()
end
end