Began swapping out action templates for onsolution(), onproject() callbacks

This commit is contained in:
starkos 2009-08-12 01:16:15 +00:00
parent a0753ec776
commit e4a523bd1b
10 changed files with 244 additions and 84 deletions

View File

@ -27,6 +27,7 @@
"tools/ow.lua",
"base/validate.lua",
"base/help.lua",
"base/premake.lua",
"actions/codeblocks/codeblocks_workspace.lua",
"actions/codeblocks/codeblocks_cbp.lua",
"actions/codeblocks/_codeblocks.lua",

View File

@ -4,6 +4,43 @@
-- Copyright (c) 2002-2009 Jason Perkins and the Premake project
--
premake.clean = { }
--
-- Clean a solution or project specific file. Uses information in the project
-- object to build the target filename.
--
-- @param obj
-- A solution or project object.
-- @param pattern
-- A filename pattern to clean; see premake.project.getfilename() for
-- a description of the format.
--
function premake.clean.file(obj, pattern)
local fname = premake.project.getfilename(obj, pattern)
os.remove(fname)
end
--
-- Clean a solution or project specific file. Uses information in the project
-- object to build the target filename.
--
-- @param obj
-- A solution or project object.
-- @param pattern
-- A filename pattern to clean; see premake.project.getfilename() for
-- a description of the format.
--
function premake.clean.file(obj, pattern)
local fname = premake.project.getfilename(obj, pattern)
os.remove(fname)
end
--
-- Remove files created by an object's templates.
@ -79,11 +116,18 @@
end
end
-- Walk the tree again. Delete templated and toolset-specific files
-- Walk the tree again and let the actions clean up after themselves
for action in premake.action.each() do
for _,sln in ipairs(_SOLUTIONS) do
for _, sln in ipairs(_SOLUTIONS) do
if action.oncleansolution then
action.oncleansolution(sln)
end
cleantemplatefiles(sln, action.solutiontemplates)
for prj in premake.eachproject(sln) do
if action.oncleanproject then
action.oncleanproject(prj)
end
cleantemplatefiles(prj, action.projecttemplates)
end
end

View File

@ -18,18 +18,21 @@
cc = { "gcc", "ow" },
},
solutiontemplates = {
{ ".workspace", premake.codeblocks_workspace },
},
onsolution = function(sln)
premake.generate(sln, "{name}.workspace", premake.codeblocks_workspace)
end,
projecttemplates = {
{ ".cbp", premake.codeblocks_cbp },
},
onclean = function(solutions, projects, targets)
for _,name in ipairs(projects) do
os.remove(name .. ".depend")
os.remove(name .. ".layout")
end
onproject = function(prj)
premake.generate(prj, "{name}.cbp", premake.codeblocks_cbp)
end,
oncleansolution = function(sln)
premake.clean.file(sln, "{name}.workspace")
end,
oncleanproject = function(prj)
premake.clean.file(prj, "{name}.cbp")
premake.clean.file(prj, "{name}.depend")
premake.clean.file(prj, "{name}.layout")
end
}

View File

@ -17,23 +17,24 @@
cc = { "gcc" },
},
solutiontemplates = {
{ ".workspace", premake.codelite_workspace },
},
onsolution = function(sln)
premake.generate(sln, "{name}.workspace", premake.codelite_workspace)
end,
projecttemplates = {
{ ".project", premake.codelite_project },
},
onclean = function(solutions, projects, targets)
for _,name in ipairs(solutions) do
os.remove(name .. "_wsp.mk")
os.remove(name .. ".tags")
end
for _,name in ipairs(projects) do
os.remove(name .. ".mk")
os.remove(name .. ".list")
os.remove(name .. ".out")
end
onproject = function(prj)
premake.generate(prj, "{name}.project", premake.codelite_project)
end,
oncleansolution = function(sln)
premake.clean.file(sln, "{name}.workspace")
premake.clean.file(sln, "{name}_wsp.mk")
premake.clean.file(sln, "{name}.tags")
end,
oncleanproject = function(prj)
premake.clean.file(prj, "{name}.project")
premake.clean.file(prj, "{name}.mk")
premake.clean.file(prj, "{name}.list")
premake.clean.file(prj, "{name}.out")
end
}

View File

@ -115,23 +115,24 @@
dotnet = { "mono", "msnet", "pnet" },
},
solutiontemplates = {
{
function(this) return _MAKE.getmakefilename(this, false) end,
premake.make_solution
},
},
onsolution = function(sln)
premake.generate(sln, _MAKE.getmakefilename(sln, false), premake.make_solution)
end,
projecttemplates = {
{
function(this) return _MAKE.getmakefilename(this, true) end,
premake.make_cpp,
function(this) return this.language == "C" or this.language == "C++" end
},
{
function(this) return _MAKE.getmakefilename(this, true) end,
premake.make_csharp,
function(this) return this.language == "C#" end
},
},
onproject = function(prj)
local makefile = _MAKE.getmakefilename(prj, true)
if premake.isdotnetproject(prj) then
premake.generate(prj, makefile, premake.make_csharp)
else
premake.generate(prj, makefile, premake.make_cpp)
end
end,
oncleansolution = function(sln)
premake.clean.file(sln, _MAKE.getmakefilename(sln, false))
end,
oncleanproject = function(prj)
premake.clean.file(prj, _MAKE.getmakefilename(prj, true))
end
}

36
src/base/premake.lua Normal file
View File

@ -0,0 +1,36 @@
--
-- premake.lua
-- High-level processing functions.
-- Copyright (c) 2002-2009 Jason Perkins and the Premake project
--
--
-- Open a file for output, and call a function to actually do the writing.
-- Used by the actions to generate solution and project files.
--
-- @param obj
-- A solution or project object; will be based to the callback function.
-- @param filename
-- The output filename; see the docs for premake.project.getfilename()
-- for the expected format.
-- @param callback
-- The function responsible for writing the file, should take a solution
-- or project as a parameters.
--
function premake.generate(obj, filename, callback)
-- open the file for output and handle any errors
filename = premake.project.getfilename(obj, filename)
local f, err = io.open(filename, "wb")
if (not f) then
error(err, 0)
end
io.output(f)
-- generate the file
callback(obj)
-- clean up
f:close()
end

View File

@ -256,9 +256,9 @@
--
function premake.project.getfilename(prj, pattern)
pattern = pattern:gsub("{path}", prj.location)
pattern = pattern:gsub("{name}", prj.name)
return path.getrelative(os.getcwd(), pattern)
local fname = pattern:gsub("{name}", prj.name)
fname = path.join(prj.location, fname)
return path.getrelative(os.getcwd(), fname)
end

View File

@ -1,7 +1,7 @@
--
-- tests/test_premake.lua
-- Automated test suite for the Premake support functions.
-- Copyright (c) 2008 Jason Perkins and the Premake project
-- Copyright (c) 2008-2009 Jason Perkins and the Premake project
--
@ -30,4 +30,18 @@
--
-- generate() tests
--
function T.premake.generate_OpensCorrectFile()
prj = { name = "MyProject", location = "MyLocation" }
premake.generate(prj, "{name}.prj", function () end)
test.openedfile("MyLocation/MyProject.prj")
end
function T.premake.generate_ClosesFile()
prj = { name = "MyProject", location = "MyLocation" }
premake.generate(prj, "{path}/{name}.prj", function () end)
test.closedfile(true)
end

View File

@ -34,7 +34,7 @@
function T.project.getfilename_PerformsSubstitutions()
local prj = { name = "project", location = "location" }
local r = _project.getfilename(prj, "{path}/{name}.prj")
local r = _project.getfilename(prj, "{name}.prj")
test.isequal("location/project.prj", r)
end

View File

@ -1,7 +1,7 @@
--
-- tests/testfx.lua
-- Automated test framework for Premake.
-- Copyright (c) 2008 Jason Perkins and the Premake project
-- Copyright (c) 2008-2009 Jason Perkins and the Premake project
--
@ -12,9 +12,6 @@
test = { }
--
--
--
-- Assertion functions
--
@ -37,6 +34,15 @@
etxt = eit()
end
end
function test.closedfile(expected)
if expected and not test.value_closedfile then
test.fail("expected file to be closed")
elseif not expected and test.value_closedfile then
test.fail("expected file to remain open")
end
end
function test.contains(value, expected)
@ -104,15 +110,46 @@
end
end
function test.openedfile(fname)
if fname ~= test.value_openedfilename then
local msg = "expected to open file '" .. fname .. "'"
if test.value_openedfilename then
msg = msg .. ", got '" .. test.value_openedfilename .. "'"
end
test.fail(msg)
end
end
function test.success(fn, ...)
local ok, err = pcall(fn, unpack(arg))
if not ok then
test.fail("call failed: " .. err)
end
end
--
-- Test stubs
--
local function stub_io_open(fname, mode)
test.value_openedfilename = fname
test.value_openedfilemode = mode
return {
close = function()
test.value_closedfile = true
end
}
end
local function stub_io_output(f)
end
--
-- Define a collection for the test suites
--
@ -125,35 +162,58 @@
-- Test execution function
--
local function test_setup(suite, fn)
-- clear out some important globals
_ACTION = "test"
_ARGS = { }
_OPTIONS = { }
_SOLUTIONS = { }
test.value_openedfilename = nil
test.value_openedfilemode = nil
test.value_closedfile = false
if suite.setup then
return pcall(suite.setup)
else
return true
end
end
local function test_run(suite, fn)
return pcall(fn)
end
local function test_teardown(suite, fn)
if suite.teardown then
return pcall(suite.teardown)
else
return true
end
end
function test.runall()
io.open = stub_io_open
io.output = stub_io_output
local numpassed = 0
local numfailed = 0
-- HACK: reset the important global state. I'd love to find a
-- way to do this automatically; maybe later.
local function resetglobals()
_ACTION = "test"
_ARGS = { }
_OPTIONS = { }
_SOLUTIONS = { }
end
for suitename,suitetests in pairs(T) do
for suitename, suitetests in pairs(T) do
for testname, testfunc in pairs(suitetests) do
local setup = suitetests.setup
local teardown = suitetests.teardown
local ok = true
resetglobals()
if (setup) then
ok, err = pcall(setup)
end
if (ok) then
ok, err = pcall(testfunc)
end
if (ok and teardown) then
ok, err = pcall(teardown)
local ok, err = test_setup(suitetests, testfunc)
if ok then
ok, err = test_run(suitetests, testfunc)
end
local tok, terr = test_teardown(suitetests, testfunc)
ok = ok and tok
err = err or tok
if (not ok) then
print(string.format("%s.%s: %s", suitename, testname, err))