Skip to content

Commit

Permalink
Packages autorun changed
Browse files Browse the repository at this point in the history
  • Loading branch information
unknown-gd committed Apr 16, 2023
1 parent a478bd7 commit efba356
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 27 deletions.
22 changes: 11 additions & 11 deletions lua/gpm/importer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,24 @@ 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 )

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
Expand All @@ -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 )

Expand All @@ -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

Expand Down
2 changes: 1 addition & 1 deletion lua/gpm/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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 )
Expand Down
3 changes: 3 additions & 0 deletions lua/gpm/packages.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
39 changes: 24 additions & 15 deletions lua/gpm/sources/lua.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down

0 comments on commit efba356

Please sign in to comment.