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.

Instance Method Summary collapse

Constructor Details

#initialize(name = nil) ⇒ Lookup

Returns a new instance of Lookup.



200
201
202
203
# File 'lib/roll/environment.rb', line 200

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

Instance Method Details

#append(path, depth = 3) ⇒ Object



245
246
247
248
249
250
# File 'lib/roll/environment.rb', line 245

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



253
254
255
# File 'lib/roll/environment.rb', line 253

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

#each(&block) ⇒ Object



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

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

#fileObject



211
212
213
# File 'lib/roll/environment.rb', line 211

def file
  @file ||= ::Config.find_config('roll', 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 ‘meta’ or ‘.meta’ directory.



296
297
298
299
300
301
302
# File 'lib/roll/environment.rb', line 296

def find_projects(dir, depth=3)
  depth = Integer(depth || 3)
  depth = (0...depth).map{ |i| (["*"] * i).join('/') }.join(',')
  glob = File.join(dir, "{#{depth}}", "{.meta,meta}")
  meta_locations = Dir[glob]
  meta_locations.map{ |d| d.chomp('/meta').chomp('/.meta') }
end

#indexObject

Generate index from lookup list.



272
273
274
275
276
277
278
279
280
281
282
# File 'lib/roll/environment.rb', line 272

def index
  set = Hash.new{ |h,k| h[k] = [] }
  locate.each do |path|
    name = load_name(path)
    #vers = load_version(path)
    if name #&& vers
      set[name] << path
    end
  end
  set
end

#load_name(path) ⇒ Object

Get library name.



305
306
307
308
309
310
# File 'lib/roll/environment.rb', line 305

def load_name(path)
  file = Dir[File.join(path, '{,.}meta', 'name')].first
  if file
    File.read(file).strip  # TODO: handle YAML
  end
end

#locateObject



285
286
287
288
289
290
291
# File 'lib/roll/environment.rb', line 285

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

#nameObject



206
207
208
# File 'lib/roll/environment.rb', line 206

def name
  @name
end

#reloadObject



216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/roll/environment.rb', line 216

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



258
259
260
261
262
263
264
265
266
267
268
269
# File 'lib/roll/environment.rb', line 258

def save
  file = File.join(::Config::CONFIG_HOME, 'roll', name, 'lookup')
  out = @table.map do |(path, depth)|
    "#{path}   #{depth}"
  end
  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



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

def size
  @table.size
end