Class: KnifeCookbookDoc::RakeTask

Inherits:
Rake::TaskLib
  • Object
show all
Defined in:
lib/knife_cookbook_doc/rake_task.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name = :knife_cookbook_doc) {|_self| ... } ⇒ RakeTask

Returns a new instance of RakeTask.

Yields:

  • (_self)

Yield Parameters:



18
19
20
21
22
23
# File 'lib/knife_cookbook_doc/rake_task.rb', line 18

def initialize(name = :knife_cookbook_doc)
  @name = name
  @options = {}
  yield self if block_given?
  define
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



16
17
18
# File 'lib/knife_cookbook_doc/rake_task.rb', line 16

def name
  @name
end

#optionsObject

Returns the value of attribute options.



16
17
18
# File 'lib/knife_cookbook_doc/rake_task.rb', line 16

def options
  @options
end

Instance Method Details

#defineObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/knife_cookbook_doc/rake_task.rb', line 25

def define
  last_description = ::Rake::Version::MAJOR.to_i < 12 ? ::Rake.application.last_comment : ::Rake.application.last_description
  desc 'Generate cookbook documentation' unless last_description
  task(name) do
    merged_options = default_options.merge(options)
    cookbook_dir = File.realpath(merged_options[:cookbook_dir])
    model = ReadmeModel.new(cookbook_dir, merged_options)
    template = File.read(merged_options[:template_file])
    eruby = Erubis::Eruby.new(template)
    result = eruby.result(model.get_binding)

    File.open("#{cookbook_dir}/#{merged_options[:output_file]}", 'wb') do |f|
      result.each_line do |line|
        f.write line.gsub(/[ \t\r\n]*$/,'')
        f.write "\n"
      end
    end
  end
end