Class: Jazzy::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/jazzy/config.rb

Overview

rubocop:disable Metrics/ClassLength

Defined Under Namespace

Modules: Mixin Classes: Attribute

Singleton collapse

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfig

rubocop:enable Style/AlignParameters



256
257
258
259
260
# File 'lib/jazzy/config.rb', line 256

def initialize
  self.class.all_config_attrs.each do |attr|
    attr.set_to_default(self)
  end
end

Class Attribute Details

.all_config_attrsObject (readonly)

Returns the value of attribute all_config_attrs.



67
68
69
# File 'lib/jazzy/config.rb', line 67

def all_config_attrs
  @all_config_attrs
end

.instanceConfig

Returns the current config instance creating one if needed.

Returns:

  • (Config)

    the current config instance creating one if needed.



406
407
408
# File 'lib/jazzy/config.rb', line 406

def self.instance
  @instance ||= new
end

Instance Attribute Details

#base_pathObject

Returns the value of attribute base_path.



70
71
72
# File 'lib/jazzy/config.rb', line 70

def base_path
  @base_path
end

Class Method Details

.config_attr(name, **opts) ⇒ Object

rubocop:enable Style/AccessorMethodName



59
60
61
62
63
64
# File 'lib/jazzy/config.rb', line 59

def self.config_attr(name, **opts)
  attr_accessor name
  attr_accessor "#{name}_configured"
  @all_config_attrs ||= []
  @all_config_attrs << Attribute.new(name, **opts)
end

.parse!Object

rubocop:disable Metrics/MethodLength



268
269
270
271
272
273
274
275
276
277
278
279
280
281
# File 'lib/jazzy/config.rb', line 268

def self.parse!
  config = new
  config.parse_command_line
  config.parse_config_file
  PodspecDocumenter.apply_config_defaults(config.podspec, config)

  if config.root_url
    config.dash_url ||= URI.join(
      config.root_url,
      "docsets/#{config.module_name}.xml")
  end

  config
end

Instance Method Details

#expand_path(path) ⇒ Object



72
73
74
# File 'lib/jazzy/config.rb', line 72

def expand_path(path)
  Pathname(path).expand_path(base_path) # nil means Pathname.pwd
end

#locate_config_fileObject



333
334
335
336
337
338
339
340
341
342
# File 'lib/jazzy/config.rb', line 333

def locate_config_file
  return config_file if config_file

  source_directory.ascend do |dir|
    candidate = dir.join('.jazzy.yaml')
    return candidate if candidate.exist?
  end

  nil
end

#parse_command_lineObject



283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
# File 'lib/jazzy/config.rb', line 283

def parse_command_line
  OptionParser.new do |opt|
    opt.banner = 'Usage: jazzy'
    opt.separator ''
    opt.separator 'Options'

    self.class.all_config_attrs.each do |attr|
      attr.attach_to_option_parser(self, opt)
    end

    opt.on('-v', '--version', 'Print version number') do
      puts 'jazzy version: ' + Jazzy::VERSION
      exit
    end

    opt.on('-h', '--help [TOPIC]', 'Available topics:',
           '  usage   Command line options (this help message)',
           '  config  Configuration file options',
           '...or an option keyword, e.g. "dash"') do |topic|
      case topic
      when 'usage', nil
        puts opt
      when 'config'
        print_config_file_help
      else
        print_option_help(topic)
      end
      exit
    end
  end.parse!
end

#parse_config_fileObject



315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
# File 'lib/jazzy/config.rb', line 315

def parse_config_file
  config_path = locate_config_file
  return unless config_path

  self.base_path = config_path.parent

  puts "Using config file #{config_path}"
  config_file = read_config_file(config_path)
  self.class.all_config_attrs.each do |attr|
    key = attr.name.to_s
    if config_file.key?(key)
      attr.set_if_unconfigured(self, config_file[key])
    end
  end

  self.base_path = nil
end


393
394
395
396
397
398
# File 'lib/jazzy/config.rb', line 393

def print_attr_description(attr)
  attr.description.each { |line| puts "  #{line}" }
  if attr.default && attr.default != ''
    puts "  Default: #{attr.default}"
  end
end


352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
# File 'lib/jazzy/config.rb', line 352

def print_config_file_help
  puts <<-_EOS_

    By default, jazzy looks for a file named ".jazzy.yaml" in the source
    directory and its ancestors. You can override the config file location
    with --config.

    (The source directory is the current working directory by default.
    You can override that with --source-directory.)

    The config file can be in YAML or JSON format. Available options are:

    _EOS_
    .gsub(/^ +/, '')

  print_option_help
end


370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
# File 'lib/jazzy/config.rb', line 370

def print_option_help(topic = '')
  found = false
  self.class.all_config_attrs.each do |attr|
    match = ([attr.name] + attr.command_line).any? do |opt|
      opt.to_s.include?(topic)
    end
    if match
      found = true
      puts
      puts attr.name.to_s.tr('_', ' ').upcase
      puts
      puts "  Config file:   #{attr.name}"
      cmd_line_forms = attr.command_line.select { |opt| opt.is_a?(String) }
      if cmd_line_forms.any?
        puts "  Command line:  #{cmd_line_forms.join(', ')}"
      end
      puts
      print_attr_description(attr)
    end
  end
  warn "Unknown help topic #{topic.inspect}" unless found
end

#read_config_file(file) ⇒ Object



344
345
346
347
348
349
350
# File 'lib/jazzy/config.rb', line 344

def read_config_file(file)
  case File.extname(file)
    when '.json'         then JSON.parse(File.read(file))
    when '.yaml', '.yml' then YAML.load(File.read(file))
    else raise "Config file must be .yaml or .json, but got #{file.inspect}"
  end
end

#template_directory=(template_directory) ⇒ Object



262
263
264
265
# File 'lib/jazzy/config.rb', line 262

def template_directory=(template_directory)
  @template_directory = template_directory
  Doc.template_path = template_directory
end