Skip to content

Commit

Permalink
Add ability to evaluate ruby code in metadata.rb
Browse files Browse the repository at this point in the history
In cases where the version or other data is handled in metadta.rb like this
  version          IO.read(File.join(File.dirname(__FILE__), 'VERSION'))
this commit will eval the given ruby code in the cookbooks directory and use
the output value.
  • Loading branch information
joerg committed Dec 2, 2015
1 parent 918aea6 commit ca09441
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions lib/yard-chef/handlers/cookbook.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,20 @@ def process
#
def name
string = ""
# YARD builds an abstract syntax tree (AST) which we need to traverse
# to obtain the complete docstring
statement.parameters.first.traverse do |child|
string << child.jump(:string_content).source if child.type == :string_content
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
Expand Down

0 comments on commit ca09441

Please sign in to comment.