File: package.lua

package info (click to toggle)
lua-lgi 0.9.2-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,376 kB
  • sloc: ansic: 5,082; makefile: 169; sh: 31
file content (52 lines) | stat: -rw-r--r-- 1,591 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
------------------------------------------------------------------------------
--
--  LGI Support for repository packages (namespaces with classes
--  overriden in lgi)
--
--  Copyright (c) 2012 Pavel Holejsovsky
--  Licensed under the MIT license:
--  http://www.opensource.org/licenses/mit-license.php
--
------------------------------------------------------------------------------

local rawget, setmetatable, assert, error
   = rawget, setmetatable, assert, error

local core = require 'lgi.core'

-- Repo package metatable.
local package = { mt = {} }
package.mt.__index = package.mt

-- There is no lazy-loading, but define _resolve to do nothing to
-- achieve API compatibility with GI-based namespaces.
function package.mt:_resolve(recurse)
   return self
end

-- Defines new class, deriving from existing one.  If the class
-- already exists, does nothing and returns nil, otherwise returns
-- newly created class type.
function package.mt:class(name, parent, ...)
   if self[name] then return nil end
   local class = parent:derive(self._name .. '.' .. name, ...)
   self[name] = class
   return class
end

-- Makes sure that given package exists, creates it if it does not.
function package.ensure(name, version)
   local ns = rawget(core.repo, name)
   if not ns then
      ns = setmetatable({ _name = name, _version = version }, package.mt)
      core.repo[name] = ns
   else
      if version and ns._version and version ~= nv._version then
	 error(("%s-%s: required version %s "):format(
		  ns._name, ns._version, version))
      end
   end
   return ns
end

return package