Class: Roll::Environment::Lookup

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/roll/environment.rb

Overview

The Lookup class provides a table of paths which make it easy to quickly populate and refresh the environment index.

TODO: Provide a way to specifically exclude a location. Probaby recognize a path with a ‘-’ prefix.

Instance Method Summary collapse

Constructor Details

#initialize(name = nil) ⇒ Lookup

Returns a new instance of Lookup.



229
230
231
232
# File 'lib/roll/environment.rb', line 229

def initialize(name=nil)
  @name = name || Environment.current
  reload
end

Instance Method Details

#append(path, depth = 3) ⇒ Object



274
275
276
277
278
279
# File 'lib/roll/environment.rb', line 274

def append(path, depth=3)
  path  = File.expand_path(path)
  depth = (depth || 3).to_i
  @table = @table.reject{ |(p, d)| path == p }
  @table.push([path, depth])
end

#delete(path) ⇒ Object



282
283
284
# File 'lib/roll/environment.rb', line 282

def delete(path)
  @table.reject!{ |p,d| path == p }
end

#each(&block) ⇒ Object



264
265
266
# File 'lib/roll/environment.rb', line 264

def each(&block)
  @table.each(&block)
end

#fileObject



240
241
242
# File 'lib/roll/environment.rb', line 240

def file
  @file ||= ::Config.find_config('roll', 'environments', name, 'lookup').first
end

#find_projects(dir, depth = 3) ⇒ Object

Search a given directory for projects upto a given depth. Projects directories are determined by containing a lib/*.rb file.



327
328
329
330
331
332
333
# File 'lib/roll/environment.rb', line 327

def find_projects(dir, depth=3)
  depth = Integer(depth || 3)
  depth = (0...depth).map{ |i| (["*"] * i).join('/') }.join(',')
  find = File.join(dir, "{#{depth}}", "lib/*.rb")
  locals = Dir.glob(find)
  locals.map{|d| File.dirname(File.dirname(d)) }.uniq
end

#indexObject

Generate index from lookup list.



301
302
303
304
305
306
307
308
309
310
311
312
313
314
# File 'lib/roll/environment.rb', line 301

def index
  set = Hash.new{|h,k| h[k]=[]}
  locate.each do |path|
    name = libname(path)
    next if name == 'roll' # NEVER INCLUDE ROLL ITSELF!!!
    #vers = load_version(path)
    if name #&& vers
      set[name] << path
    else
      warn "omitting: #{path}"
    end
  end
  set
end

#libname(path) ⇒ Object



342
343
344
# File 'lib/roll/environment.rb', line 342

def libname(path)
  (path).name
end

#locateObject

Locate projects.



317
318
319
320
321
322
323
# File 'lib/roll/environment.rb', line 317

def locate
  locs = []
  each do |dir, depth|
    locs << find_projects(dir, depth)
  end
  locs.flatten
end

#metadata(path) ⇒ Object



336
337
338
339
# File 'lib/roll/environment.rb', line 336

def (path)
  @metadata ||= {}
  @metadata[path] ||= Metadata.new(path)
end

#nameObject



235
236
237
# File 'lib/roll/environment.rb', line 235

def name
  @name
end

#reloadObject



245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
# File 'lib/roll/environment.rb', line 245

def reload
  t = []
  if file && File.exist?(file)
    lines = File.readlines(file)
    lines.each do |line|
      line = line.strip
      path, depth = *line.split(/\s+/)
      next if line =~ /^\s*$/  # blank
      next if line =~ /^\#/    # comment
      dir, depth = *line.split(/\s+/)
      t << [path, (depth || 3).to_i]
    end
  else
    t = []
  end
  @table = t
end

#saveObject



287
288
289
290
291
292
293
294
295
296
297
298
# File 'lib/roll/environment.rb', line 287

def save
  file = File.join(HOME_ENV_DIR, name, 'lookup')
  out = @table.map do |(path, depth)|
    "#{path}   #{depth}"
  end.sort
  dir = File.dirname(file)
  FileUtils.mkdir_p(dir) unless File.exist?(dir)
  File.open(file, 'w') do |f|
    f <<  out.join("\n")
  end
  @file = file
end

#sizeObject



269
270
271
# File 'lib/roll/environment.rb', line 269

def size
  @table.size
end