Class: CliMarkdown::Creator

Inherits:
Object
  • Object
show all
Defined in:
lib/cli_markdown/creator.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Creator

cli_class is top-level CLI class.



19
20
21
22
23
# File 'lib/cli_markdown/creator.rb', line 19

def initialize(options={})
  @cli_class = options[:cli_class]
  @cli_name = options[:cli_name]
  @parent_command_name = options[:parent_command_name]
end

Class Method Details

.cleanObject



13
14
15
16
# File 'lib/cli_markdown/creator.rb', line 13

def self.clean
  FileUtils.rm_rf("docs/_reference")
  FileUtils.rm_f("docs/reference.md")
end

.create_all(options = {}) ⇒ Object



8
9
10
11
# File 'lib/cli_markdown/creator.rb', line 8

def self.create_all(options={})
  clean unless options[:parent_command_name]
  new(options).create_all
end

Instance Method Details

#create_allObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/cli_markdown/creator.rb', line 25

def create_all
  create_index unless @parent_command_name

  commands = @cli_class.commands.reject { |command_name, command| command.hidden? }
  commands.keys.each do |command_name|
    page = Page.new(
        cli_class: @cli_class,
        cli_name: @cli_name,
        command_name: command_name,
        parent_command_name: @parent_command_name,
      )
    create_page(page)

    if subcommand?(command_name)
      subcommand_class = subcommand_class(command_name)
      parent_command_name = command_name

      say "Creating subcommands pages for #{parent_command_name}..."
      Creator.create_all(
        cli_class: subcommand_class,
        cli_name: @cli_name,
        parent_command_name: parent_command_name
      )
    end
  end
end

#create_include_referenceObject



66
67
68
69
70
# File 'lib/cli_markdown/creator.rb', line 66

def create_include_reference
  path = "docs/_includes/reference.md"
  FileUtils.mkdir_p(File.dirname(path))
  IO.write(path, "Generic tool description. Please edit #{path} with a description.") unless File.exist?(path)
end

#create_indexObject



58
59
60
61
62
63
64
# File 'lib/cli_markdown/creator.rb', line 58

def create_index
  create_include_reference
  page = Index.new(@cli_class, @cli_name)
  FileUtils.mkdir_p(File.dirname(page.path))
  say "Creating #{page.path}"
  IO.write(page.path, page.doc)
end

#create_page(page) ⇒ Object



52
53
54
55
56
# File 'lib/cli_markdown/creator.rb', line 52

def create_page(page)
  say "Creating #{page.path}..."
  FileUtils.mkdir_p(File.dirname(page.path))
  IO.write(page.path, page.doc)
end

#say(text) ⇒ Object



80
81
82
# File 'lib/cli_markdown/creator.rb', line 80

def say(text)
  puts text unless self.class.mute
end

#subcommand?(command_name) ⇒ Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/cli_markdown/creator.rb', line 72

def subcommand?(command_name)
  @cli_class.subcommands.include?(command_name)
end

#subcommand_class(command_name) ⇒ Object



76
77
78
# File 'lib/cli_markdown/creator.rb', line 76

def subcommand_class(command_name)
  @cli_class.subcommand_classes[command_name]
end