diff --git a/lua/gpm/importer.lua b/lua/gpm/importer.lua index db1196b4..ef85866c 100644 --- a/lua/gpm/importer.lua +++ b/lua/gpm/importer.lua @@ -13,13 +13,13 @@ for _, source in pairs( gpm.sources ) do sources[ #sources + 1 ] = source end -gpm.AsyncImport = promise.Async( function( filePath, parentPackage ) +gpm.AsyncImport = promise.Async( function( filePath, parentPackage, isAutorun ) ArgAssert( filePath, 1, "string" ) for _, source in ipairs( sources ) do if type( source.CanImport ) ~= "function" then continue end if not source.CanImport( filePath ) then continue end - return source.Import( filePath, parentPackage ) + return source.Import( filePath, parentPackage, isAutorun ) end end ) @@ -27,10 +27,10 @@ do local assert = assert - function gpm.Import( filePath, async, parentPackage ) + function gpm.Import( filePath, async, parentPackage, isAutorun ) assert( async or promise.RunningInAsync(), "import supposed to be running in coroutine/async function (do you running it from package)" ) - local p = gpm.AsyncImport( filePath, parentPackage ) + local p = gpm.AsyncImport( filePath, parentPackage, isAutorun ) if not async then return p:Await() end return p end @@ -43,16 +43,16 @@ do local file_Find = file.Find - gpm.ImportFolder = promise.Async( function( luaPath ) - luaPath = paths.Fix( luaPath ) + gpm.ImportFolder = promise.Async( function( filePath, parentPackage, isAutorun ) + filePath = paths.Fix( filePath ) - local files, folders = file_Find( luaPath .. "/*", "LUA" ) + local files, folders = file_Find( filePath .. "/*", "LUA" ) for _, folderName in ipairs( folders ) do - gpm.AsyncImport( luaPath .. "/" .. folderName ) + gpm.AsyncImport( filePath .. "/" .. folderName, parentPackage, isAutorun ) end for _, fileName in ipairs( files ) do - gpm.AsyncImport( luaPath .. "/" .. fileName ) + gpm.AsyncImport( filePath .. "/" .. fileName, parentPackage, isAutorun ) end end ) @@ -65,8 +65,8 @@ if type( pkgs ) == "table" then end end -gpm.ImportFolder( "gpm/packages" ) -gpm.ImportFolder( "packages" ) +gpm.ImportFolder( "gpm/packages", nil, true ) +gpm.ImportFolder( "packages", nil, true ) if SERVER then diff --git a/lua/gpm/init.lua b/lua/gpm/init.lua index c0ee0b87..8229569f 100644 --- a/lua/gpm/init.lua +++ b/lua/gpm/init.lua @@ -10,7 +10,7 @@ CreateConVar( "gpm_cache_lifetime", "24", FCVAR_ARCHIVE, " - the cache lifetime, module( "gpm" ) -_VERSION = 010200 +_VERSION = 010300 -- Include function function includeShared( filePath ) diff --git a/lua/gpm/packages.lua b/lua/gpm/packages.lua index e6b629ba..0d7a43c4 100644 --- a/lua/gpm/packages.lua +++ b/lua/gpm/packages.lua @@ -54,6 +54,9 @@ function GetMetadata( source ) source.client = source.client ~= false source.server = source.server ~= false + -- Should automatically run the package + source.autorun = source.autorun == true + -- Package isolation & logger source.isolation = source.isolation ~= false source.logger = source.logger ~= false diff --git a/lua/gpm/sources/lua.lua b/lua/gpm/sources/lua.lua index 7408b29d..0587fe14 100644 --- a/lua/gpm/sources/lua.lua +++ b/lua/gpm/sources/lua.lua @@ -12,6 +12,7 @@ local AddCSLuaFile = AddCSLuaFile local setmetatable = setmetatable local CompileFile = CompileFile local luaRealm = gpm.LuaRealm +local logger = gpm.Logger local ipairs = ipairs local rawset = rawset local pcall = pcall @@ -38,7 +39,7 @@ Files = setmetatable( {}, { end } ) -Import = promise.Async( function( filePath, parentPackage ) +Import = promise.Async( function( filePath, parentPackage, isAutorun ) local packagePath = paths.Fix( filePath ) local packageFilePath = packagePath @@ -52,25 +53,33 @@ Import = promise.Async( function( filePath, parentPackage ) local packageFile, metadata = Files[ packageFilePath ], nil if packageFile then - metadata = packages.GetMetaData( packageFile ) - if not metadata then - metadata = packages.GetMetaData( { - ["name"] = packageFilePath, - ["main"] = packageFilePath - } ) + metadata = packages.GetMetadata( packageFile ) - if packagePathIsLuaFile then - return packages.Initialize( metadata, packageFile, Files, parentPackage ) + if not metadata then + if not packagePathIsLuaFile then + return promise.Reject( "package file is empty or does not exist (" .. packageFilePath .. ")" ) end - return promise.Reject( "package file is missing (" .. metadata.name .. "@" .. utils.Version( metadata.version ) .. ")" ) - end - - if SERVER and metadata.client then - AddCSLuaFile( packageFilePath ) + metadata = packages.GetMetadata( { + ["name"] = string.GetFileFromFilename( packageFilePath ), + ["main"] = packageFilePath, + ["autorun"] = true + } ) end else - metadata = packages.GetMetaData( {} ) + metadata = packages.GetMetadata( { + ["name"] = string.GetFileFromFilename( packageFilePath ), + ["autorun"] = true + } ) + end + + if isAutorun and not metadata.autorun then + logger:Debug( "package autorun restricted (%s)", metadata.name .. "@" .. utils.Version( metadata.version ) ) + return + end + + if SERVER and metadata.client then + AddCSLuaFile( packageFilePath ) end if CLIENT and not metadata.client then return end