From 5d98731c656463d0ec49271efc2fc837e8b5ca6d Mon Sep 17 00:00:00 2001 From: Jason Perkins Date: Mon, 22 Jun 2015 15:35:20 -0400 Subject: [PATCH] Add optional `versions` argument to require() --- src/base/_foundation.lua | 10 ++++++++++ src/base/globals.lua | 26 ++++++++++++++++++++++++++ src/host/premake.c | 2 +- tests/base/test_versions.lua | 8 ++++++++ 4 files changed, 45 insertions(+), 1 deletion(-) diff --git a/src/base/_foundation.lua b/src/base/_foundation.lua index 87ca2204..8cf1bfda 100644 --- a/src/base/_foundation.lua +++ b/src/base/_foundation.lua @@ -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 diff --git a/src/base/globals.lua b/src/base/globals.lua index 8118ff68..89911b57 100644 --- a/src/base/globals.lua +++ b/src/base/globals.lua @@ -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) diff --git a/src/host/premake.c b/src/host/premake.c index 422b568d..593e1a71 100644 --- a/src/host/premake.c +++ b/src/host/premake.c @@ -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" diff --git a/tests/base/test_versions.lua b/tests/base/test_versions.lua index a2ef7e4d..c7475bc9 100644 --- a/tests/base/test_versions.lua +++ b/tests/base/test_versions.lua @@ -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