Add optional versions argument to require()

This commit is contained in:
Jason Perkins 2015-06-22 15:35:20 -04:00 committed by Damien Courtois
parent c3c288a139
commit bd2b887367
4 changed files with 45 additions and 1 deletions

View File

@ -4,6 +4,9 @@
---
premake = premake or {}
premake._VERSION = _PREMAKE_VERSION
package.loaded["premake"] = premake
premake.modules = {}
premake.extensions = premake.modules
@ -101,6 +104,10 @@
---
function p.checkVersion(version, checks)
if not version then
return false
end
local function parse(str)
local major, minor, patch, dev = str:match("^(%d+)%.?(%d*)%.?(%d*)(.-)$")
major = tonumber(major) or 0
@ -142,6 +149,9 @@
elseif check:startswith("<") then
func = lt
check = check:sub(2)
elseif check:startswith("=") then
func = eq
check = check:sub(2)
else
func = ge
end

View File

@ -55,3 +55,29 @@
return dofile(fname)
end
end
---
-- Extend require() with a second argument to specify the expected
-- version of the loaded module. Raises an error if the version criteria
-- are not met.
--
-- @param modname
-- The name of the module to load.
-- @param versions
-- An optional version criteria string; see premake.checkVersion()
-- for more information on the format.
-- @return
-- If successful, the loaded module, which is also stored into the
-- global package.loaded table.
---
premake.override(_G, "require", function(base, modname, versions)
local mod = base(modname)
if mod and versions and not premake.checkVersion(mod._VERSION, versions) then
error(string.format("module %s %s does not meet version criteria %s",
modname, mod._VERSION or "(none)", versions), 3)
end
return mod
end)

View File

@ -14,7 +14,7 @@
#endif
#define VERSION "HEAD"
#define VERSION "5.0.0-dev"
#define COPYRIGHT "Copyright (C) 2002-2015 Jason Perkins and the Premake Project"
#define PROJECT_URL "https://github.com/premake/premake-core/wiki"
#define ERROR_MESSAGE "Error: %s\n"

View File

@ -169,3 +169,11 @@
test.isfalse(p.checkVersion("2.2.0.0", ">=1.0 <2.0"))
end
--
-- If there is no version information, fails.
--
function suite.fail_onNoVersion()
test.isfalse(p.checkVersion(nil, "1.0"))
end