Class: Magellan::Cli::ReferenceGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/magellan/cli/reference_generator.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ ReferenceGenerator

Returns a new instance of ReferenceGenerator.



9
10
11
12
13
# File 'lib/magellan/cli/reference_generator.rb', line 9

def initialize(options = {})
  @dest = options[:dest] || "doc"
  @subdir = options[:subdir] || "."
  @io = $stdout
end

Instance Method Details

#change_global_var_temporarily(*tuples, &block) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/magellan/cli/reference_generator.rb', line 31

def change_global_var_temporarily(*tuples, &block)
  if tuples.empty?
    block.call if block
  else
    gvar_name, value = *tuples.shift
    backup = eval(gvar_name)
    begin
      eval("#{gvar_name} = value")
      change_global_var_temporarily(*tuples, &block)
    ensure
      eval("#{gvar_name} = backup")
    end
  end
end

#process(klass, filename, name = nil) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/magellan/cli/reference_generator.rb', line 46

def process(klass, filename, name = nil)
  if cmd = klass.all_commands["help"]
    cmd.description = I18n.t(:help, scope: [:base, :cmd])
  end

  rel_path = File.join(@subdir, filename)
  path = File.expand_path(rel_path, @dest)
  FileUtils.mkdir_p(File.dirname(path))
  instance = klass.new
  open(path, "w") do |f|
    f.puts "---"
    f.puts "layout: index"
    version = Magellan::Cli::VERSION.split(/\./)[0,2].join(".")
    f.puts "title: " << [name, "magellan-cli-#{version} (#{I18n.locale})", "Reference"].compact.join(" | ")
    lang_links = I18n.available_locales.map do |locale|
      (locale == I18n.locale) ? locale.to_s :
        begin
          path = rel_path.sub(/\/#{I18n.locale}\//, "/#{locale}/").sub(/\.md\z/, ".html")
          %!<a href="/#{path}">#{locale}</a>!
        end
    end
    f.puts %!breadcrumb: <a href="/">Top</a> / <a href="/reference">Reference</a> / <a href="/reference/magellan-cli/#{I18n.locale}">magellan-cli-#{version}</a> / #{name} #{lang_links.join(' ')}!
    f.puts "---"
    f.puts

    f.puts "## "<< I18n.t(:commands, scope: :reference)
    f.puts
    klass.sorted_commands.each do |cmd|
      rel_path =
        Magellan::Cli::Resources::MAPPING.values.include?(cmd.name) ? "./resources/#{cmd.name}.html" :
          Magellan::Cli::Messaging::MAPPING.values.include?(cmd.name) ? "./messaging/#{cmd.name}.html" : "##{cmd.name}"
      f.puts "- [%s](%s)" % [Thor.send(:banner, cmd, false, false), rel_path]
    end
    f.puts

    f.puts "## " << I18n.t(:global_options, scope: :reference)
    f.puts
    change_global_var_temporarily(["$stdout", f], ["$stderr", f]) do
      f.puts "```text"
      klass.send(:print_options, @shell, klass.class_options.values)
      f.puts "```"
      f.puts
    end
    f.puts

    f.puts "## " << I18n.t(:details, scope: :reference)
    klass.sorted_commands.each do |cmd|
      next if Magellan::Cli::Resources::MAPPING.values.include?(cmd.name)
      next if Magellan::Cli::Messaging::MAPPING.values.include?(cmd.name)
      f.puts "### <a name=\"#{cmd.name}\"></a>#{cmd.name}"
      f.puts
      f.puts "```text"
      f.puts Thor.send(:banner, cmd)
      f.puts "```"
      f.puts
      unless cmd.options.empty?
        change_global_var_temporarily(["$stdout", f], ["$stderr", f]) do
          f.puts "```text"
            klass.send(:print_options, @shell, cmd.options.values)
          f.puts "```"
          f.puts
        end
      end
      if cmd.long_description
        f.puts cmd.long_description
      else
        f.puts cmd.description
      end
      f.puts
    end

  end
end

#runObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/magellan/cli/reference_generator.rb', line 15

def run
  @shell = Thor::Shell::Basic.new
  change_global_var_temporarily(["$PROGRAM_NAME", "magellan-cli"]) do
    k = Magellan::Cli::Command
    process(k, "index.md")
    Magellan::Cli::Resources::MAPPING.each do |class_name, name|
      klass = Magellan::Cli::Resources.const_get(class_name)
      process(klass, "resources/#{name}.md", name)
    end
    Magellan::Cli::Messaging::MAPPING.each do |class_name, name|
      klass = Magellan::Cli::Messaging.const_get(class_name)
      process(klass, "messaging/#{name}.md", name)
    end
  end
end