Class: Tap::Env

Inherits:
Object
  • Object
show all
Includes:
Signals
Defined in:
lib/tap/env.rb,
lib/tap/env/path.rb,
lib/tap/env/cache.rb,
lib/tap/env/constant.rb,
lib/tap/env/string_ext.rb

Defined Under Namespace

Modules: StringExt Classes: Cache, Constant, Path

Constant Summary collapse

INLINE_TYPE =

Matches an inline type. After the match:

$1:: The prefix string (ex 'Const' for '::Const::type')
$2:: The inline type (ex 'type' for '::Const::type')
/(.*)::([a-z_]*)\z/

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Signals

#sig, #signal, #signal?, #signals

Methods included from Signals::ModuleMethods

included

Constructor Details

#initialize(options = {}) ⇒ Env

Returns a new instance of Env.



74
75
76
77
78
79
# File 'lib/tap/env.rb', line 74

def initialize(options={})
  @paths = options[:paths] || []
  @paths.collect! {|path| path.kind_of?(Path) ? path : Path.new(*path) }
  @constants = options[:constants] || []
  @constants.collect! {|constant| constant.kind_of?(Constant) ? constant : Constant.new(constant) }
end

Instance Attribute Details

#constantsObject (readonly)

Returns the value of attribute constants.



56
57
58
# File 'lib/tap/env.rb', line 56

def constants
  @constants
end

#pathsObject (readonly)

Returns the value of attribute paths.



55
56
57
# File 'lib/tap/env.rb', line 55

def paths
  @paths
end

Class Method Details

.generate(options = {}) ⇒ Object



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
# File 'lib/tap/env.rb', line 9

def generate(options={})
  options = {
    :register => true, 
    :load_paths => true,
    :set => true
  }.merge(options)
  
  dir = File.expand_path(options[:dir] || Dir.pwd)
  pathfile = options[:pathfile] || File.expand_path(Path::FILE, dir)
  map = options[:map] || Path.load(pathfile)
  lib = options[:lib] || 'lib'
  pattern = options[:pattern] || '**/*.rb'
  
  register = options[:register]
  load_paths = options[:load_paths] 
  set = options[:set]
  
  lines = []
  lines << "register #{Path.escape(dir)}" if register
  
  path = Path.new(dir, map)
  path[lib].each do |lib_dir|
    lines << "loadpath #{Path.escape(lib_dir)}" if load_paths
    
    Constant.scan(lib_dir, pattern).each do |constant|
      require_paths = Path.join(constant.require_paths)
      types = constant.types.to_a.collect {|type| Path.escape(Path.join(type)) }
      lines << "set #{constant.const_name} #{Path.escape require_paths} #{types.join(' ')}"
    end if set
  end
  
  lines.uniq!
  lines.sort!
  lines
end

Instance Method Details

#activate(name, version) ⇒ Object



124
125
126
# File 'lib/tap/env.rb', line 124

def activate(name, version)
  Gem.activate(name, version)
end

#auto(options, log = nil) ⇒ Object



116
117
118
119
120
121
122
# File 'lib/tap/env.rb', line 116

def auto(options, log=nil)
  Env.generate(options).each do |line|
    sig, *args = Utils.shellsplit(line)
    signal(sig).call(args)
  end
  self
end

#constant(const_str, type = nil) ⇒ Object



103
104
105
# File 'lib/tap/env.rb', line 103

def constant(const_str, type=nil)
  const_str.kind_of?(Module) ? const_str : resolve(const_str, type).constantize
end

#loadpath(*paths) ⇒ Object

Expands and prepends the specified paths to $LOAD_PATH, removing any duplicates. Returns $LOAD_PATH.



136
137
138
139
140
141
142
143
# File 'lib/tap/env.rb', line 136

def loadpath(*paths)
  paths.reverse_each do |path|
    $LOAD_PATH.unshift File.expand_path(path)
  end
  
  $LOAD_PATH.uniq!
  $LOAD_PATH
end

#match(const_str, type = nil) ⇒ Object



89
90
91
92
# File 'lib/tap/env.rb', line 89

def match(const_str, type=nil)
  const_str = const_str.to_s
  const_str =~ Constant::CONST_REGEXP ? constants_by_const_name($1) : constants_by_path(const_str, type)
end

#path(type) ⇒ Object



81
82
83
84
85
86
87
# File 'lib/tap/env.rb', line 81

def path(type)
  result = []
  paths.each do |path|
    result.concat path[type]
  end
  result
end

#register(dir, map = {}) ⇒ Object

Registers the directory and path mappings as a Path, into paths. The path is unshifted to paths to provide similar functionality as loadpath. Returns the new path.



110
111
112
113
114
# File 'lib/tap/env.rb', line 110

def register(dir, map={})
  new_path = Path.new(dir, map)
  paths.unshift new_path
  new_path
end

#resolve(const_str, type = nil) ⇒ Object



94
95
96
97
98
99
100
101
# File 'lib/tap/env.rb', line 94

def resolve(const_str, type=nil)
  matches = match(const_str, type)
  case matches.length
  when 0 then raise "unresolvable constant: #{const_str.inspect}"
  when 1 then matches.at(0)
  else raise "multiple matching constants: #{const_str.inspect} (#{matches.join(', ')})"
  end
end

#set(const_name, require_path = nil, *types) ⇒ Object



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/tap/env.rb', line 152

def set(const_name, require_path=nil, *types)
  if const_name =~ INLINE_TYPE
    const_name = $1
    types << $2
  end
  
  constant = constants.find {|c| c.const_name == const_name }
  
  unless constant
    constant = Constant.new(const_name)
    constants << constant
  end
  
  require_paths = require_path ? Path.split(require_path, nil) : []
  if require_paths.empty? && const_name.kind_of?(String)
    require_paths << const_name.underscore
  end
  
  constant.require_paths.concat(require_paths).uniq!
  types.each {|type| constant.register_as(*Path.split(type, nil)) }
  
  constant
end

#unloadpath(*paths) ⇒ Object

Expands and removes the specified paths from $LOAD_PATH. Returns $LOAD_PATH.



147
148
149
150
# File 'lib/tap/env.rb', line 147

def unloadpath(*paths)
  paths.each {|path| $LOAD_PATH.delete File.expand_path(path) }
  $LOAD_PATH
end

#unregister(*dirs) ⇒ Object



128
129
130
131
132
# File 'lib/tap/env.rb', line 128

def unregister(*dirs)
  dirs.collect! {|dir| File.expand_path(dir) }
  paths.delete_if {|path| dirs.include?(path.base) }
  self
end

#unset(*const_names) ⇒ Object



176
177
178
179
180
181
182
183
# File 'lib/tap/env.rb', line 176

def unset(*const_names)
  const_names.each do |const_name|
    constants.delete_if do |constant|
      constant.const_name == const_name
    end
  end
  self
end