Sha256: 465977bcc8daa74f481bb0a3b525cc3f65b8314da9adce5429fbe58fbd97d55f

Contents?: true

Size: 1.9 KB

Versions: 11

Compression:

Stored size: 1.9 KB

Contents

require 'chef/knife'
require 'pathname'

module KnifeCookbookDoc
  class CookbookDoc < Chef::Knife
    deps do
      require 'chef/cookbook/metadata'
      require 'erubis'
      require 'knife_cookbook_doc/base_model'
      require 'knife_cookbook_doc/documenting_lwrp_base'
      require 'knife_cookbook_doc/definitions_model'
      require 'knife_cookbook_doc/readme_model'
      require 'knife_cookbook_doc/recipe_model'
      require 'knife_cookbook_doc/resource_model'
      require 'knife_cookbook_doc/attributes_model'
    end

    banner 'knife cookbook doc DIR (options)'

    option :constraints,
           :short => '-c',
           :long => '--constraints',
           :boolean => true,
           :default => true,
           :description => 'Include version constraints for platforms and dependencies'

    option :output_file,
           :short => '-o',
           :long => '--output-file FILE',
           :default => 'README.md',
           :description => 'Set the output file to render to relative to cookbook dir. Defaults to README.md'

    option :template_file,
           :short => '-t',
           :long => '--template FILE',
           :default => Pathname.new("#{File.dirname(__FILE__)}/README.md.erb").realpath,
           :description => 'Set template file used to render README.md'

    def run
      unless (cookbook_dir = name_args.first)
        ui.fatal 'Please provide cookbook directory as an argument'
        exit(1)
      end

      cookbook_dir = File.realpath(cookbook_dir)

      model = ReadmeModel.new(cookbook_dir, config[:constraints])

      template = File.read(config[:template_file])
      eruby = Erubis::Eruby.new(template)
      result = eruby.result(model.get_binding)

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

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
knife-cookbook-doc-0.20.0 lib/chef/knife/cookbook_doc.rb
knife-cookbook-doc-0.19.0 lib/chef/knife/cookbook_doc.rb
knife-cookbook-doc-0.18.0 lib/chef/knife/cookbook_doc.rb
knife-cookbook-doc-0.17.0 lib/chef/knife/cookbook_doc.rb
knife-cookbook-doc-0.16.0 lib/chef/knife/cookbook_doc.rb
knife-cookbook-doc-0.15.0 lib/chef/knife/cookbook_doc.rb
knife-cookbook-doc-0.14.0 lib/chef/knife/cookbook_doc.rb
knife-cookbook-doc-0.13.0 lib/chef/knife/cookbook_doc.rb
knife-cookbook-doc-0.12.0 lib/chef/knife/cookbook_doc.rb
knife-cookbook-doc-0.11.0 lib/chef/knife/cookbook_doc.rb
knife-cookbook-doc-0.10.0 lib/chef/knife/cookbook_doc.rb