Improve include() function: only include files once, and allow including specific files instead of just directories

This commit is contained in:
Jason Perkins 2011-12-04 14:47:59 -05:00
parent 1b827d3847
commit c8e3c0df6a
8 changed files with 74 additions and 4 deletions

View File

@ -0,0 +1 @@
.

View File

@ -4,6 +4,8 @@ Sony NGP (in progress)
* Added debugformat with C7 support for Visual Studio
* The error message for invalid flags now includes the offending value
* The include() function will now include each file only once
* The include() function can now include specific files
-------

View File

@ -114,11 +114,25 @@
--
-- A shortcut for including another "premake4.lua" file, often used for projects.
-- Load and run an external script file, with a bit of extra logic to make
-- including projects easier. if "path" is a directory, will look for
-- path/premake4.lua. And each file is tracked, and loaded only once.
--
function include(fname)
return dofile(fname .. "/premake4.lua")
io._includedFiles = { }
function include(filename)
-- if a directory, load the premake script inside it
if os.isdir(filename) then
filename = path.join(filename, "premake4.lua")
end
-- but only load each file once
filename = path.getabsolute(filename)
if not io._includedFiles[filename] then
io._includedFiles[filename] = true
dofile(filename)
end
end

View File

@ -0,0 +1,49 @@
--
-- tests/base/test_include.lua
-- Test the include() function, for including external scripts
-- Copyright (c) 2011 Jason Perkins and the Premake project
--
T.include = { }
local suite = T.include
--
-- Setup and teardown
--
function suite.teardown()
-- clear the list of included files after each run
io._includedFiles = { }
end
--
-- Tests
--
function suite.include_findsPremakeFile_onFolderNameOnly()
include "folder"
test.isequal("ok\n", io.endcapture())
end
function suite.include_onExactFilename()
include "folder/premake4.lua"
test.isequal("ok\n", io.endcapture())
end
function suite.include_runsOnlyOnce_onMultipleIncludes()
include "folder/premake4.lua"
include "folder/premake4.lua"
test.isequal("ok\n", io.endcapture())
end
function suite.include_runsOnlyOnce_onMultipleIncludesWithDifferentPaths()
include "folder/premake4.lua"
include "../tests/folder/premake4.lua"
test.isequal("ok\n", io.endcapture())
end

View File

@ -1 +0,0 @@
return "ok"

View File

@ -0,0 +1 @@
io.printf("ok")

View File

@ -37,6 +37,7 @@
-- The test suites
--
-- Base API tests
dofile("test_dofile.lua")
dofile("test_string.lua")
dofile("test_premake.lua")
@ -48,6 +49,7 @@
dofile("base/test_api.lua")
dofile("base/test_action.lua")
dofile("base/test_config.lua")
dofile("base/test_include.lua")
dofile("base/test_location.lua")
dofile("base/test_os.lua")
dofile("base/test_path.lua")

View File

@ -30,9 +30,11 @@
function test.capture(expected)
local actual = io.endcapture()
-- create line-by-line iterators for both values
local ait = actual:gfind("(.-)" .. io.eol)
local eit = expected:gfind("(.-)\n")
-- compare each value line by line
local linenum = 1
local atxt = ait()
local etxt = eit()