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

Constant Summary collapse

SWIFT_BUILD_TOOLS =
%w[spm xcodebuild symbolgraph].freeze
SOURCE_HOSTS =
%w[github gitlab bitbucket].freeze
BUILTIN_THEME_DIR =
Pathname(__dir__) + 'themes'
BUILTIN_THEMES =
BUILTIN_THEME_DIR.children(false).map(&:to_s)

Singleton collapse

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfig

rubocop:enable Layout/ArgumentAlignment



481
482
483
484
485
# File 'lib/jazzy/config.rb', line 481

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.



95
96
97
# File 'lib/jazzy/config.rb', line 95

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.



667
668
669
# File 'lib/jazzy/config.rb', line 667

def self.instance
  @instance ||= new
end

Instance Attribute Details

#base_pathObject

Returns the value of attribute base_path.



98
99
100
# File 'lib/jazzy/config.rb', line 98

def base_path
  @base_path
end

Class Method Details

.alias_config_attr(name, forward, **opts) ⇒ Object



86
87
88
89
90
91
92
# File 'lib/jazzy/config.rb', line 86

def self.alias_config_attr(name, forward, **opts)
  alias_method name.to_s, forward.to_s
  alias_method "#{name}=", "#{forward}="
  alias_method "#{name}_configured", "#{forward}_configured"
  alias_method "#{name}_configured=", "#{forward}_configured="
  @all_config_attrs << Attribute.new(name, **opts)
end

.config_attr(name, **opts) ⇒ Object

rubocop:enable Naming/AccessorMethodName



78
79
80
81
82
83
84
# File 'lib/jazzy/config.rb', line 78

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



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

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.validate

  config
end

Instance Method Details

#expand_glob_path(path) ⇒ Object



100
101
102
# File 'lib/jazzy/config.rb', line 100

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

#expand_path(path) ⇒ Object



104
105
106
107
# File 'lib/jazzy/config.rb', line 104

def expand_path(path)
  abs_path = expand_glob_path(path)
  Pathname(Dir[abs_path][0] || abs_path) # Use existing filesystem spelling
end

#hide_objc?Boolean

Returns:

  • (Boolean)


113
114
115
# File 'lib/jazzy/config.rb', line 113

def hide_objc?
  hide_declarations == 'objc'
end

#hide_swift?Boolean

Returns:

  • (Boolean)


109
110
111
# File 'lib/jazzy/config.rb', line 109

def hide_swift?
  hide_declarations == 'swift'
end

#locate_config_fileObject

rubocop:enable Metrics/MethodLength



592
593
594
595
596
597
598
599
600
601
# File 'lib/jazzy/config.rb', line 592

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

rubocop:disable Metrics/MethodLength



515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
# File 'lib/jazzy/config.rb', line 515

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!

  unless ARGV.empty?
    warning "Leftover unused command-line text: #{ARGV}"
  end
end

#parse_config_fileObject



551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
# File 'lib/jazzy/config.rb', line 551

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 = "Unknown config file attribute #{key.inspect}"
      if matching_name = attrs_by_name[key]
        message +=
          " (Did you mean #{matching_name.first.config_file_key.inspect}?)"
      end
      warning message
      next
    end

    attr.first.set_if_unconfigured(self, value)
  end

  self.base_path = nil
end


654
655
656
657
658
659
# File 'lib/jazzy/config.rb', line 654

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


613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
# File 'lib/jazzy/config.rb', line 613

def print_config_file_help
  puts "\n    By default, jazzy looks for a file named \".jazzy.yaml\" in the source\n    directory and its ancestors. You can override the config file location\n    with --config.\n\n    (The source directory is the current working directory by default.\n    You can override that with --source-directory.)\n\n    The config file can be in YAML or JSON format. Available options are:\n\n  _EOS_\n    .gsub(/^ +/, '')\n\n  print_option_help\nend\n"


631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
# File 'lib/jazzy/config.rb', line 631

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



603
604
605
606
607
608
609
610
611
# File 'lib/jazzy/config.rb', line 603

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

#theme_directory=(theme_directory) ⇒ Object



487
488
489
490
# File 'lib/jazzy/config.rb', line 487

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

#validateObject



581
582
583
584
585
586
587
588
# File 'lib/jazzy/config.rb', line 581

def validate
  if source_host_configured &&
     source_host_url.nil? &&
     source_host_files_url.nil?
    warning 'Option `source_host` is set but has no effect without either '\
      '`source_host_url` or `source_host_files_url`.'
  end
end

#warning(message) ⇒ Object



510
511
512
# File 'lib/jazzy/config.rb', line 510

def warning(message)
  warn "WARNING: #{message}"
end