Class: RC::Configuration

Inherits:
Module
  • Object
show all
Includes:
Enumerable
Defined in:
lib/rc/configuration.rb

Overview

The Configuration class encapsulates a project/library’s tool configuration.

Constant Summary collapse

CONFIG_FILE =

Runtime configuration file glob pattern.

'.ruby{rc,}'

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*files) ⇒ Configuration

Initialize new Configuration object.

Parameters:

  • files (Array<String>)

    Configuration files (optional).



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

def initialize(*files)
  @files = files

  @_config = Hash.new{ |h,k| h[k]=[] }
  #@_onload = Hash.new{ |h,k| h[k]=[] }

  load_files(*files)
end

Class Method Details

.load(options = {}) ⇒ Object

Load configuration file from local project or other gem.

Parameters:

  • options (Hash) (defaults to: {})

    Load options.

Options Hash (options):

  • :from (String)

    Name of gem or library.



27
28
29
30
31
32
33
# File 'lib/rc/configuration.rb', line 27

def load(options={})
  if options[:from]
    load_from(options[:from])
  else
    load_local()
  end
end

.load_from(gem) ⇒ Object

Load configuration from another gem.



38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/rc/configuration.rb', line 38

def load_from(gem)
  files = Find.path(CONFIG_FILE, :from=>gem)
  file  = files.find{ |f| File.file?(f) }
  new(*file)

  #if file
  #  paths = [file]
  #else
  #  #paths = Find.path(CONFIG_DIR + '/**/*', :from=>gem)
  #end
  #files = paths.select{ |path| File.file?(path) }
  #new(*files)
end

.load_localObject

Load configuration for current project.



55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/rc/configuration.rb', line 55

def load_local
  files = lookup(CONFIG_FILE)
  file  = files.find{ |f| File.file?(f) }
  new(*file)

  #if file
  #  paths = [file]
  #else
  #  dir = lookup(CONFIG_DIR).find{ |f| File.directory?(f) }
  #  paths = dir ? Dir.glob(File.join(dir, '**/*')) : []
  #end
  #files = paths.select{ |path| File.file?(path) }
end

.lookup(glob, flags = 0) ⇒ Object (private)

Search upward from working directory.



74
75
76
77
78
79
80
81
82
83
84
# File 'lib/rc/configuration.rb', line 74

def lookup(glob, flags=0)
  pwd  = File.expand_path(Dir.pwd)
  home = File.expand_path('~')
  while pwd != '/' && pwd != home
    paths = Dir.glob(File.join(pwd, glob), flags)
    return paths unless paths.empty?
    break if ROOT_INDICATORS.any?{ |r| File.exist?(File.join(pwd, r)) }
    pwd = File.dirname(pwd)
  end
  return []
end

Instance Method Details

#[](feature) ⇒ Object



242
243
244
# File 'lib/rc/configuration.rb', line 242

def [](feature)
  @_config[feature.to_s]
end

#config(target, options = {}, &block) ⇒ Object

Configure a commandline tool or feature.

Examples:

profile :coverage do
  config :qed, :from=>'qed'
end

Parameters:

  • target (Symbol)

    The name of the command or feature to configure.

  • opts (Hash)

    Configuration options.



199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/rc/configuration.rb', line 199

def config(target, options={}, &block)
  #options[:profile] = (options[:profile] || 'default').to_s
  #options[:command] = command.to_s unless options.key?(:command)
  #options[:feature] = command.to_s unless options.key?(:feature)
  #command = options[:command].to_s

  # IDEA: other import options such as local file?

  configs_from(options).each do |c|
    @_config[target.to_s] << c.copy(options)
  end

  return unless block

  @_config[target.to_s] << Config.new(target, options, &block)
end

#configs_from(options) ⇒ Object (private)



314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
# File 'lib/rc/configuration.rb', line 314

def configs_from(options)
  from = options[:from]
  list = []

  return list unless from

  if Array === from
    from_name, from_opts = *from
  else
    from_name, from_opts = from, {}
  end

  from_config = RC.configuration(from_name)

  from_opts[:feature] = options[:feature] unless from_opts.key?(:feature) if options[:feature]
  from_opts[:command] = options[:command] unless from_opts.key?(:command) if options[:command]
  from_opts[:profile] = options[:profile] unless from_opts.key?(:profile) if options[:profile]

  from_opts[:feature] = from_opts[:feature].to_s if from_opts[:feature]
  from_opts[:command] = from_opts[:command].to_s if from_opts[:command]
  from_opts[:profile] = from_opts[:profile].to_s if from_opts[:profile]

  from_config.each do |ftr, confs|
    confs.each_with_index do |c, i|
      if c.match?(from_opts)
        list << c.copy(options)
      end
    end
  end

  list
end

#each(&block) ⇒ Object

Iterate over each feature config.

Examples:

confgiuration.each do |feature, configs|
  configs.each do |config|
    ...
  end
end


256
257
258
# File 'lib/rc/configuration.rb', line 256

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

#evaluate(*args, &cfg) ⇒ Object

Evaluate configuration code.



171
172
173
174
# File 'lib/rc/configuration.rb', line 171

def evaluate(*args, &cfg)
  dsl = DSL.new(self)
  dsl.instance_eval(*args, &cfg)
end

#import(glob, opts = {}) ⇒ Object

Import other runtime configuration files.

Parameters:

  • glob (String)

    File pattern of configutation files to load.

  • opts (Hash) (defaults to: {})

    Load options.

Options Hash (opts):

  • :from (String)

    Name of gem or library.

  • :first (Boolean)

    Only match a single file.



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/rc/configuration.rb', line 117

def import(glob, opts={})
  paths = []

  glob = glob + '**/*' if glob.end_with?('/')

  if from = opts[:from]
    paths = Find.path(glob, :from=>from)
  else
    if glob.start_with?('/')
      if root = lookup_root
        glob = File.join(root, glob)
      else
        raise "no project root for #{glob}" unless root
      end
    end
    paths = Dir.glob(glob)
  end

  paths = paths.select{ |path| File.file?(path) }
  paths = paths[0..0] if opts[:first]

  load_files(*paths)

  paths.empty? ? nil : paths
end

#load_files(*files) ⇒ Object Also known as: load_file

Load configuration files.

TODO: begin/rescue around instance_eval?

TODO: Does each file need it’s own DSL instance?



150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/rc/configuration.rb', line 150

def load_files(*files)
  dsl = DSL.new(self)
  files.each do |file|
    next unless File.file?(file)  # TODO: warn ?
    #begin
      dsl.instance_eval(File.read(file), file)
    #rescue => e
    #  raise e if $DEBUG
    #  warn e.message
    #end
  end
end

#lookup_rootObject (private)

Search upward from project root directory.



350
351
352
353
354
355
356
357
358
# File 'lib/rc/configuration.rb', line 350

def lookup_root
  pwd  = File.expand_path(Dir.pwd)
  home = File.expand_path('~')
  while pwd != '/' && pwd != home
    return pwd if ROOT_INDICATORS.any?{ |r| File.exist?(File.join(pwd, r)) }
    pwd = File.dirname(pwd)
  end
  return nil
end

#profile_names(command = nil) ⇒ Object

Get a list of defined profiles names for the given command. use the current command if no command is given.



289
290
291
292
293
294
295
296
297
298
299
300
301
# File 'lib/rc/configuration.rb', line 289

def profile_names(command=nil)
  command = command || RC.current_command

  list = []
  @_config.each do |feature, configs|
    configs.each do |c|
      if c.command?(command)
        list << c.profile
      end
    end
  end
  list.uniq
end

#sizeObject

The number of feature configs.



263
264
265
# File 'lib/rc/configuration.rb', line 263

def size
  @_config.size
end

#to_aArray Also known as: configurations

Get a list of the defined configurations.

Returns:

  • (Array)

    List of all defined configurations.



272
273
274
275
276
277
278
# File 'lib/rc/configuration.rb', line 272

def to_a
  list = []
  @_config.each do |feature, configs|
    list.concat(configs)
  end
  list
end