Class: YARD::Handlers::Chef::CookbookHandler

Inherits:
Base
  • Object
show all
Defined in:
lib/yard-chef/handlers/cookbook.rb

Overview

Handles specific cookbook information like README, description and version.

Instance Method Summary collapse

Methods inherited from Base

#cookbook, #lwrp

Instance Method Details

#docstring(base_dir) ⇒ YARD::Docstring

Generates docstring from the README file.

Returns:

  • (YARD::Docstring)

    the docstring



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/yard-chef/handlers/cookbook.rb', line 86

def docstring(base_dir)
  type = ''
  string = ''
  readme_path = base_dir + '/README.md'
  if File.exist?(readme_path)
    type = :markdown
    string = IO.read(readme_path)
  else
    readme_path = base_dir + '/README.rdoc'
    if File.exist?(readme_path)
      type = :rdoc
      string = IO.read(readme_path)
    end
  end
  [YARD::DocstringParser.new.parse(string).to_docstring, type]
end

#nameString

Get the name of the method being handled.

Returns:

  • (String)

    the method name



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/yard-chef/handlers/cookbook.rb', line 62

def name
  string = ''
  value = statement.parameters.first
  if value.is_a?(YARD::Parser::Ruby::MethodCallNode)
    # The content is code, so evaluate it in the correct directory
    # This handles ruby code like File.read in metadata.rb
    current_directory = Dir.getwd
    Dir.chdir(File.expand_path(File.dirname(statement.file)))
    string << eval(value.source)
    Dir.chdir current_directory
  else
    # YARD builds an abstract syntax tree (AST) which we need to traverse
    # to obtain the complete docstring
    value.traverse do |child|
      string << child.jump(:string_content).source if child.type == :string_content
    end
  end
  string
end

#processObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/yard-chef/handlers/cookbook.rb', line 32

def process
  return unless statement.file.to_s =~ /metadata.rb/

  # Register the cookbook
  cookbook_obj = cookbook
  cookbook_obj.add_file(statement.file)
  case statement.first.source
  when 'description'
    cookbook_obj.short_desc = name
  when 'version'
    cookbook_obj.version = name
  end

  # Get the README file for the cookbook
  base_dir = File.dirname(statement.file)
  if cookbook_obj.docstring == ''
    cookbook_obj.docstring, cookbook_obj.docstring_type = docstring(base_dir)
  end

  # Get the top-level README for the cookbook repository if it exists
  if CHEF.docstring == ''
    readme_dir = File.expand_path('../..', base_dir)
    CHEF.docstring, CHEF.docstring_type = docstring(readme_dir)
  end
end