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/AlignParameter



357
358
359
360
361
# File 'lib/jazzy/config.rb', line 357

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.



84
85
86
# File 'lib/jazzy/config.rb', line 84

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.



528
529
530
# File 'lib/jazzy/config.rb', line 528

def self.instance
  @instance ||= new
end

Instance Attribute Details

#base_pathObject

Returns the value of attribute base_path.



87
88
89
# File 'lib/jazzy/config.rb', line 87

def base_path
  @base_path
end

Class Method Details

.config_attr(name, **opts) ⇒ Object

rubocop:enable Style/AccessorMethodName



76
77
78
79
80
81
# File 'lib/jazzy/config.rb', line 76

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



369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
# File 'lib/jazzy/config.rb', line 369

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



89
90
91
# File 'lib/jazzy/config.rb', line 89

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

#locate_config_fileObject



448
449
450
451
452
453
454
455
456
457
# File 'lib/jazzy/config.rb', line 448

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



385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
# File 'lib/jazzy/config.rb', line 385

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



417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
# File 'lib/jazzy/config.rb', line 417

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)

  attrs_by_conf_key, attrs_by_name = %i[config_file_key name].map do |prop|
    self.class.all_config_attrs.group_by(&prop)
  end

  config_file.each do |key, value|
    unless attr = attrs_by_conf_key[key]
      message = "WARNING: Unknown config file attribute #{key.inspect}"
      if matching_name = attrs_by_name[key]
        message << ' (Did you mean '
        message << matching_name.first.config_file_key.inspect
        message << '?)'
      end
      warn message
      next
    end

    attr.first.set_if_unconfigured(self, value)
  end

  self.base_path = nil
end


515
516
517
518
519
520
# File 'lib/jazzy/config.rb', line 515

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


474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
# File 'lib/jazzy/config.rb', line 474

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


492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
# File 'lib/jazzy/config.rb', line 492

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.config_file_key}"
      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



459
460
461
462
463
464
465
466
467
468
469
470
471
472
# File 'lib/jazzy/config.rb', line 459

def read_config_file(file)
  case File.extname(file)
    when '.json'         then JSON.parse(File.read(file))
    when '.yaml', '.yml' then
      if YAML.respond_to?('safe_load') # ruby >= 2.1.0
        YAML.safe_load(File.read(file))
      else
        # rubocop:disable Security/YAMLLoad
        YAML.load(File.read(file))
        # rubocop:enable Security/YAMLLoad
      end
    else raise "Config file must be .yaml or .json, but got #{file.inspect}"
  end
end

#theme_directory=(theme_directory) ⇒ Object



363
364
365
366
# File 'lib/jazzy/config.rb', line 363

def theme_directory=(theme_directory)
  @theme_directory = theme_directory
  Doc.template_path = theme_directory + 'templates'
end