-------------------------------------------------------------------------------- -- Title: Bundle.lua -- Description: Like a square peg in a round hole -- Author: Raphaël Szwarc http://alt.textdrive.com/lua/ -- Creation Date: January 30, 2007 -- Legal: Copyright (C) 2007 Raphaël Szwarc -- Under the terms of the MIT License -- http://www.opensource.org/licenses/mit-license.html -------------------------------------------------------------------------------- -- import dependencies local debug = require( 'debug' ) local getmetatable = getmetatable local package = package local setmetatable = setmetatable local tostring = tostring local _G = _G -------------------------------------------------------------------------------- -- Bundle -------------------------------------------------------------------------------- module( 'Bundle' ) _VERSION = '1.0' local self = setmetatable( _M, {} ) local meta = getmetatable( self ) self.separator = package.path:match( '(%p)%?%.' ) or '/' -------------------------------------------------------------------------------- -- Utilities -------------------------------------------------------------------------------- local getinfo = debug.getinfo debug.getinfo = function( aLevel, aType ) local someInfo = getinfo( aLevel, aType ) if someInfo and someInfo.source and not someInfo.source:find( self.separator, 1, true ) and _G.arg[ 0 ] then someInfo.source = ( '@%s' ):format( _G.arg[ 0 ] ) end return someInfo end -------------------------------------------------------------------------------- -- Metamethods -------------------------------------------------------------------------------- function meta:__call( aLevel ) local aLevel = aLevel or 2 local aSource = debug.getinfo( aLevel, 'S' ).source local anIndex = aSource:len() - ( aSource:reverse():find( self.separator, 1, true ) or aSource:len() ) local aPath = aSource:sub( 2, anIndex + 1 ) return aPath end function meta:__concat( aValue ) return tostring( self ) .. tostring( aValue ) end function meta:__tostring() return ( '%s/%s' ):format( self._NAME, self._VERSION ) end