knife-cookbook-doc 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /Gemfile.lock
2
+
3
+ # Intermediate package dir
4
+ /pkg
5
+
6
+ # Ignore Intellij artifacts
7
+ /*.iml
8
+ /*.ipr
9
+ /*.iws
data/CHANGELOG.md ADDED
@@ -0,0 +1,17 @@
1
+ v0.2.0 (Apr 1 2013)
2
+ --------------------
3
+
4
+ * Rework plugin to generate documentation for LWRPs.
5
+ * Update the plugin to scan the source file for annotations to add to README.
6
+
7
+ v0.1.1 (Feb 20 2013)
8
+ --------------------
9
+
10
+ * Convert plugin into a gem that can be installed via RubyGems.
11
+ * Update README.
12
+ * Add this CHANGELOG file.
13
+
14
+ v0.1.0 (Dec 5 2012)
15
+ -------------------
16
+
17
+ * First tagged version.
data/CONTRIBUTING.md ADDED
@@ -0,0 +1,26 @@
1
+ # How to Contribute
2
+
3
+ Pull requests are greatly appreciated and are what makes opensource great. Here's a quick guide:
4
+
5
+ * Fork it
6
+ * Create your feature branch (`git checkout -b my-new-feature`)
7
+ * Commit your changes (`git commit -am 'Add some feature'`)
8
+ * Push to the branch (`git push origin my-new-feature`)
9
+ * Create new Pull Request
10
+
11
+ Pester us if we don't get your Pull Requests merged in a timely fashion. :)
12
+
13
+ ## How to speed the merging of pull requests
14
+
15
+ * Describe your changes in the CHANGELOG.
16
+ * Give yourself some credit in the appropriate place (usually the CHANGELOG).
17
+ * Make commits of logical units.
18
+ * Ensure your commit messages help others understand what you are doing and why.
19
+ * Check for unnecessary whitespace with `git diff --check` before committing.
20
+ * Maintain the same code style.
21
+ * Maintain the same level of test coverage or improve it.
22
+
23
+ ## Additional Resources
24
+
25
+ * [General GitHub documentation](http://help.github.com/)
26
+ * [GitHub pull request documentation](http://help.github.com/send-pull-requests/)
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org/'
2
+
3
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,202 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ APPENDIX: How to apply the Apache License to your work.
180
+
181
+ To apply the Apache License to your work, attach the following
182
+ boilerplate notice, with the fields enclosed by brackets "[]"
183
+ replaced with your own identifying information. (Don't include
184
+ the brackets!) The text should be enclosed in the appropriate
185
+ comment syntax for the file format. We also recommend that a
186
+ file or class name and description of purpose be included on the
187
+ same "printed page" as the copyright notice for easier
188
+ identification within third-party archives.
189
+
190
+ Copyright [yyyy] [name of copyright owner]
191
+
192
+ Licensed under the Apache License, Version 2.0 (the "License");
193
+ you may not use this file except in compliance with the License.
194
+ You may obtain a copy of the License at
195
+
196
+ http://www.apache.org/licenses/LICENSE-2.0
197
+
198
+ Unless required by applicable law or agreed to in writing, software
199
+ distributed under the License is distributed on an "AS IS" BASIS,
200
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201
+ See the License for the specific language governing permissions and
202
+ limitations under the License.
data/README.md ADDED
@@ -0,0 +1,130 @@
1
+ # knife-cookbook-doc
2
+
3
+ This is a knife plugin to help create and maintain a README.md for a cookbook.
4
+ As much as possible the plugin makes use of the same metadata as used by chef
5
+ when generating the documentation. The plugin will also scan the source files
6
+ for annotations present in comments. Users can also add fragments of markdown
7
+ into the doc/ directory to merge into the generated README.md file.
8
+
9
+ The goal is to keep the code as the authoritative source of information. The
10
+ hope is that keeping the documentation close to the code will help to maintain
11
+ it's currency.
12
+
13
+ ## Getting Started
14
+
15
+ #### Step 1
16
+
17
+ Populate the `metadata.rb` of your cookbook according to Opscode's
18
+ [documentation](http://docs.opscode.com/config_rb_metadata.html). Particular
19
+ attention should be paid to documenting the recipes, attributes, platform
20
+ compatibility and cookbook requirements (i.e. depends, recommends, suggests etc).
21
+
22
+ #### Step 2
23
+
24
+ At the top of each cookbook, add a detailed documentation section such as;
25
+
26
+ =begin
27
+ #<
28
+ The recipe is awesome. It does thing 1, thing 2 and thing 3!
29
+ #>
30
+ =end
31
+
32
+ #### Step 3
33
+
34
+ In each LWRP, add detailed documentation such as;
35
+
36
+ =begin
37
+ #<
38
+ This creates and destroy the awesome service.
39
+
40
+ @action create Create the awesome service.
41
+ @action destroy Destroy the awesome service.
42
+
43
+ @section Examples
44
+
45
+ # An example of my awesome service
46
+ mycookbook_awesome_service "my_service" do
47
+ port 80
48
+ end
49
+ #>
50
+ =end
51
+
52
+ ...
53
+
54
+ #<> @attribute port The port on which the HTTP service will bind.
55
+ attribute :port, :kind_of => Integer, :default => 8080
56
+
57
+ It should be noted that the documentation of the LWRP requires that the user
58
+ document the actions, using `@action <action> <description>` and the attributes
59
+ using `@attribute <attribute> <description>` so that there is meaningful
60
+ descriptions for the actions and attributes in the generated README.
61
+
62
+ The documentation will be added at the start of the LWRP documentation
63
+ except if marked with `@section <heading>`, in which case it will be added
64
+ to the head of the LWRP documentation.
65
+
66
+ #### Step 4
67
+
68
+ Finally the user should add some documentation fragments into the `doc/` dir.
69
+ Most importantly you should add `doc/overview.md` which will replace the first
70
+ `Description` section of the readme. The remaining fragments will be included
71
+ at the end of the readme in lexicographic order of the filename.
72
+
73
+ ## Installation
74
+
75
+ You can install the plugin via RubyGems:
76
+
77
+ $ gem install knife-cookbook-doc
78
+
79
+ Alternatively, you can install the plugin from source:
80
+
81
+ $ git clone git://github.com/realityforge/knife-cookbook-doc.git
82
+ $ cd knife-cookbook-doc/
83
+ $ bundle install
84
+ $ bundle exec rake install
85
+
86
+ Afterwards, the new knife command `knife cookbook doc DIR` will be available.
87
+
88
+ ## Usage
89
+
90
+ knife cookbook doc COOKBOOK_DIR (options)
91
+
92
+ -o, --output-file FILE Set the output file to render to relative to cookbook dir. Defaults to README.md
93
+ -t, --template FILE Set template file used to render README.md
94
+
95
+ Examples:
96
+
97
+ knife cookbook doc path/to/cookbook
98
+ knife cookbook doc path/to/cookbook --template README.md.erb
99
+
100
+ ## Further Details
101
+
102
+ ### Documentation in comments
103
+
104
+ The documentation stored in comments comes in three forms;
105
+
106
+ #### Single line comments
107
+
108
+ #<> This is some documentation
109
+
110
+ #### Multi-line comments using begin/end
111
+
112
+ =begin
113
+ #<
114
+ This is some documentation
115
+ #>
116
+ =end
117
+
118
+ #### Multi-line comments without using begin/end
119
+
120
+ #<
121
+ # This is some documentation
122
+ #>
123
+
124
+
125
+ ## Credit
126
+
127
+ The plugin was originally written by Mathias Lafeldt as a way to create
128
+ initial README.md from the metadata.rb. It was subsequently rewritten by
129
+ Peter Donald to gather information from the other files within the cookbook.
130
+ All credit to Mathias for his wonderful idea.
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rake/clean'
3
+
4
+ task :default => :build
5
+
6
+ CLEAN.include 'pkg'
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/knife_cookbook_doc/version', __FILE__)
3
+
4
+ Gem::Specification.new do |s|
5
+ s.authors = ['Mathias Lafeldt', 'Peter Donald']
6
+ s.email = %w(mathias.lafeldt@gmail.com peter@realityforge.org)
7
+ s.description = %q{Knife plugin to generate README.md for a cookbook}
8
+ s.summary = s.description
9
+ s.homepage = 'http://realityforge.github.com/knife-cookbook-doc'
10
+ s.license = 'Apache 2.0'
11
+
12
+ s.files = `git ls-files`.split($\)
13
+ s.executables = s.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
14
+ s.test_files = s.files.grep(%r{^(test|spec|features)/})
15
+ s.name = 'knife-cookbook-doc'
16
+ s.require_paths = %w(lib)
17
+ s.version = KnifeCookbookDoc::VERSION
18
+
19
+ s.add_dependency 'chef'
20
+ s.add_dependency 'erubis'
21
+
22
+ s.add_development_dependency 'rake'
23
+ end
@@ -0,0 +1,122 @@
1
+ # Description
2
+
3
+ <% unless fragments['overview'] -%>
4
+ <%= description %>
5
+ <% else -%>
6
+ <%= fragments['overview'] -%>
7
+ <% end -%>
8
+
9
+ # Requirements
10
+
11
+ ## Platform:
12
+
13
+ <% unless platforms.empty? %>
14
+ <% platforms.each do |platform| %>
15
+ * <%= platform %>
16
+ <% end %>
17
+ <% else %>
18
+ *No platforms defined*
19
+ <% end %>
20
+
21
+ ## Cookbooks:
22
+
23
+ <% unless dependencies.empty? && recommendations.empty? && suggestions.empty? && conflicting.empty? %>
24
+ <% dependencies.each do |cookbook| %>
25
+ * <%= cookbook %>
26
+ <% end %>
27
+ <% recommendations.each do |cookbook| %>
28
+ * <%= cookbook %> (Recommended but not required)
29
+ <% end %>
30
+ <% suggestions.each do |cookbook| %>
31
+ * <%= cookbook %> (Suggested but not required)
32
+ <% end %>
33
+ <% conflicting.each do |cookbook| %>
34
+ * Conflicts with <%= cookbook %>
35
+ <% end %>
36
+ <% else %>
37
+ *No dependencies defined*
38
+ <% end %>
39
+
40
+ # Attributes
41
+
42
+ <% unless attributes.empty? %>
43
+ <% attributes.each do |name, description, default| %>
44
+ * `<%= name %>` - <%= description %>. Defaults to `<%= default %>`.
45
+ <% end %>
46
+ <% else %>
47
+ *No attributes defined*
48
+ <% end %>
49
+
50
+ # Recipes
51
+
52
+ <% unless recipes.empty? %>
53
+ <% recipes.each do |recipe| %>
54
+ * <% if recipe.top_level_descriptions.empty? %><%= recipe.name %><% else %>[<%= recipe.name %>](#<%= recipe.name.gsub(':','') %>)<% end %><% if recipe.short_description != '' %> - <%= recipe.short_description %><% end %>
55
+ <% end %>
56
+
57
+ <% recipes.each do |recipe| -%>
58
+ <% unless recipe.top_level_descriptions.empty? -%>
59
+ ## <%= recipe.name %>
60
+
61
+ <% if recipe.top_level_description('main') != '' -%>
62
+ <%= recipe.top_level_description('main') %>
63
+
64
+ <% end -%>
65
+ <% recipe.top_level_descriptions.keys.select{|k| k != 'main'}.each do |key| -%>
66
+ ### <%= key -%>
67
+
68
+ <%= recipe.top_level_description(key) %>
69
+
70
+ <% end -%>
71
+ <% end -%>
72
+ <% end -%>
73
+ <% else -%>
74
+ *No recipes defined*
75
+
76
+ <% end -%>
77
+ <% unless resources.empty? -%>
78
+ # Resources
79
+
80
+ <% resources.each do |resource| %>
81
+ * [<%= resource.name %>](#<%= resource.name %>)<% if resource.short_description %> - <%= resource.short_description %><% end %>
82
+ <% end %>
83
+
84
+ <% resources.each do |resource| -%>
85
+ ## <%= resource.name %>
86
+
87
+ <% if resource.top_level_description('main') != '' -%>
88
+ <%= resource.top_level_description('main') -%>
89
+
90
+ <% end -%>
91
+ <% unless resource.actions.empty? -%>
92
+ ### Actions
93
+
94
+ <% resource.actions.each do |action| -%>
95
+ - <%= action %>: <%= resource.action_description(action) %><% if resource.default_action == action %> Default action.<% end %>
96
+ <% end -%>
97
+ <% end -%>
98
+ <% unless resource.attributes.empty? -%>
99
+
100
+ ### Attribute Parameters
101
+
102
+ <% resource.attributes.each do |attribute| -%>
103
+ - <%= attribute %>: <%= resource.attribute_description(attribute) %><% if resource.attribute_has_default_value?(attribute) %> Defaults to <code><%= resource.attribute_default_value(attribute).inspect %></code>.<% end %>
104
+ <% end -%>
105
+ <% end -%>
106
+ <% resource.top_level_descriptions.keys.select{|k| k != 'main'}.each do |key| -%>
107
+
108
+ ### <%= key -%>
109
+
110
+ <%= resource.top_level_description(key) -%>
111
+ <% end -%>
112
+
113
+ <% end -%>
114
+ <% end -%>
115
+ <% fragments.keys.select {|k|k != 'overview'}.each do |key| -%>
116
+ <%= fragments[key] %>
117
+ <% end -%>
118
+ # License and Maintainer
119
+
120
+ Maintainer:: <%= maintainer %> (<<%= maintainer_email %>>)
121
+
122
+ License:: <%= license %>
@@ -0,0 +1,52 @@
1
+ require 'chef/knife'
2
+ require 'pathname'
3
+
4
+ module KnifeCookbookDoc
5
+ class CookbookDoc < Chef::Knife
6
+ deps do
7
+ require 'chef/cookbook/metadata'
8
+ require 'erubis'
9
+ require 'knife_cookbook_doc/readme_model'
10
+ require 'knife_cookbook_doc/recipe_model'
11
+ require 'knife_cookbook_doc/resource_model'
12
+ end
13
+
14
+ banner 'knife cookbook doc DIR (options)'
15
+
16
+ option :constraints,
17
+ :short => '-c',
18
+ :long => '--constraints',
19
+ :boolean => true,
20
+ :default => true,
21
+ :description => 'Include version constraints for platforms and dependencies'
22
+
23
+ option :output_file,
24
+ :short => '-o',
25
+ :long => '--output-file FILE',
26
+ :default => 'README.md',
27
+ :description => 'Set the output file to render to relative to cookbook dir. Defaults to README.md'
28
+
29
+ option :template_file,
30
+ :short => '-t',
31
+ :long => '--template FILE',
32
+ :default => Pathname.new("#{File.dirname(__FILE__)}/README.md.erb").realpath,
33
+ :description => 'Set template file used to render README.md'
34
+
35
+ def run
36
+ unless (cookbook_dir = name_args.first)
37
+ ui.fatal 'Please provide cookbook directory as an argument'
38
+ exit(1)
39
+ end
40
+
41
+ model = ReadmeModel.new(cookbook_dir, config[:constraints])
42
+
43
+ template = File.read(config[:template_file])
44
+ eruby = Erubis::Eruby.new(template)
45
+ result = eruby.result(model.get_binding)
46
+
47
+ File.open("#{cookbook_dir}/#{config[:output_file]}",'wb') do |f|
48
+ f.write result
49
+ end
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,107 @@
1
+ module KnifeCookbookDoc
2
+ class ReadmeModel
3
+ DEFAULT_CONSTRAINT = ">= 0.0.0".freeze
4
+
5
+ def initialize(cookbook_dir, constraints)
6
+
7
+ @metadata = Chef::Cookbook::Metadata.new
8
+ @metadata.from_file("#{cookbook_dir}/metadata.rb")
9
+
10
+ @resources = []
11
+ Dir["#{cookbook_dir}/resources/*.rb"].sort.each do |resource_filename|
12
+ @resources << ResourceModel.new(@metadata.name, resource_filename)
13
+ end
14
+
15
+ @fragments = {}
16
+ Dir["#{cookbook_dir}/doc/*.md"].sort.each do |resource_filename|
17
+ @fragments[::File.basename(resource_filename,'.md')] = IO.read(resource_filename)
18
+ end
19
+
20
+ @recipes = []
21
+ @metadata.recipes.each do |name, description|
22
+ @recipes << RecipeModel.new(name, description, "#{cookbook_dir}/recipes/#{name.gsub(/^.*\:(.*)$/,'\1')}.rb")
23
+ end
24
+ @metadata = @metadata
25
+ @constraints = constraints
26
+ end
27
+
28
+ def fragments
29
+ @fragments
30
+ end
31
+
32
+ def resources
33
+ @resources
34
+ end
35
+
36
+ def description
37
+ @metadata.description
38
+ end
39
+
40
+ def platforms
41
+ @metadata.platforms.map do |platform, version|
42
+ format_constraint(platform.capitalize, version)
43
+ end
44
+ end
45
+
46
+ def dependencies
47
+ @metadata.dependencies.map do |cookbook, version|
48
+ format_constraint(cookbook, version)
49
+ end
50
+ end
51
+
52
+ def recommendations
53
+ @metadata.recommendations.map do |cookbook, version|
54
+ format_constraint(cookbook, version)
55
+ end
56
+ end
57
+
58
+ def suggestions
59
+ @metadata.suggestions.map do |cookbook, version|
60
+ format_constraint(cookbook, version)
61
+ end
62
+ end
63
+
64
+ def conflicting
65
+ @metadata.conflicting.map do |cookbook, version|
66
+ format_constraint(cookbook, version)
67
+ end
68
+ end
69
+
70
+ def attributes
71
+ @metadata.attributes.map do |attr, options|
72
+ name = "node['#{attr.gsub("/", "']['")}']"
73
+ [name, options['description'], options['default']]
74
+ end
75
+ end
76
+
77
+ def recipes
78
+ @recipes
79
+ end
80
+
81
+ def maintainer
82
+ @metadata.maintainer
83
+ end
84
+
85
+ def maintainer_email
86
+ @metadata.maintainer_email
87
+ end
88
+
89
+ def license
90
+ @metadata.license
91
+ end
92
+
93
+ def get_binding
94
+ binding
95
+ end
96
+
97
+ private
98
+
99
+ def format_constraint(name, version)
100
+ if @constraints && version != DEFAULT_CONSTRAINT
101
+ "#{name} (#{version})"
102
+ else
103
+ name
104
+ end
105
+ end
106
+ end
107
+ end
@@ -0,0 +1,77 @@
1
+ module KnifeCookbookDoc
2
+ class RecipeModel
3
+
4
+ attr_reader :name
5
+ attr_reader :short_description
6
+
7
+ def initialize(name, short_description, filename)
8
+ @name = name
9
+ @short_description = short_description
10
+ @filename = filename
11
+ load_descriptions
12
+ end
13
+
14
+ def top_level_description(section)
15
+ (top_level_descriptions[section.to_s] || []).join("\n").gsub(/\n+$/m,"\n")
16
+ end
17
+
18
+ def top_level_descriptions
19
+ @top_level_descriptions ||= {}
20
+ end
21
+
22
+ private
23
+
24
+ def load_descriptions
25
+ current_section = 'main'
26
+ extract_description.each_line do |line|
27
+ if /^ *\@section (.*)$/ =~ line
28
+ current_section = $1.strip
29
+ else
30
+ lines = (top_level_descriptions[current_section] || [])
31
+ lines << line.gsub("\n",'')
32
+ top_level_descriptions[current_section] = lines
33
+ end
34
+ end
35
+ end
36
+
37
+ include ::Chef::Mixin::ConvertToClassName
38
+
39
+ def extract_description
40
+ description = []
41
+ IO.read(@filename).gsub(/^=begin *\n *\#\<\n(.*?)^ *\#\>\n=end *\n/m) do
42
+ description << $1
43
+ ""
44
+ end.gsub(/^ *\#\<\n(.*?)^ *\#\>\n/m) do
45
+ description << $1.gsub(/^ *\# ?/, '')
46
+ ""
47
+ end.gsub(/^ *\#\<\> (.*?)$/) do
48
+ description << $1
49
+ ""
50
+ end
51
+ description.join("\n")
52
+ end
53
+ end
54
+
55
+ class DocumentingLWRPBase < ::Chef::Resource::LWRPBase
56
+
57
+ class << self
58
+ def attribute_specifications
59
+ @attribute_specifications ||= {}
60
+ end
61
+
62
+ def desc(description)
63
+ @description = "#{@description}#{description}\n"
64
+ end
65
+
66
+ def description
67
+ @description || ""
68
+ end
69
+ end
70
+
71
+ def self.attribute(attr_name, validation_opts={})
72
+ result = super(attr_name, validation_opts)
73
+ attribute_specifications[attr_name] = validation_opts
74
+ result
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,147 @@
1
+ module KnifeCookbookDoc
2
+ class ResourceModel
3
+
4
+ attr_reader :native_resource
5
+
6
+ def initialize(cookbook_name, file)
7
+ @native_resource = build_native_from_file(cookbook_name, file)
8
+ load_descriptions
9
+ end
10
+
11
+ def name
12
+ @native_resource.resource_name
13
+ end
14
+
15
+ # Return the unique set of actions, with the default one first, if there is a default
16
+ def actions
17
+ unless @actions
18
+ @actions = [default_action].compact + @native_resource.actions.sort.uniq.select { |a| a != default_action }
19
+ end
20
+ @actions
21
+ end
22
+
23
+ def default_action
24
+ @native_resource.default_action
25
+ end
26
+
27
+ def action_description(action)
28
+ action_descriptions[action.to_s]
29
+ end
30
+
31
+ def action_descriptions
32
+ @action_descriptions ||= {}
33
+ end
34
+
35
+ def attributes
36
+ @native_resource.attribute_specifications.keys
37
+ end
38
+
39
+ def attribute_description(attribute)
40
+ attribute_descriptions[attribute.to_s]
41
+ end
42
+
43
+ def attribute_has_default_value?(attribute)
44
+ specification = @native_resource.attribute_specifications[attribute]
45
+ specification && specification.has_key?(:default)
46
+ end
47
+
48
+ def attribute_default_value(attribute)
49
+ if attribute_has_default_value?(attribute)
50
+ return @native_resource.attribute_specifications[attribute][:default]
51
+ else
52
+ return nil
53
+ end
54
+ end
55
+
56
+ def attribute_descriptions
57
+ @attribute_descriptions ||= {}
58
+ end
59
+
60
+ def short_description
61
+ unless @short_description
62
+ @short_description = first_sentence(top_level_description('main'))
63
+ end
64
+ @short_description
65
+ end
66
+
67
+ def first_sentence(string)
68
+ string.gsub(/^([^.]*?\.)/m) do |match|
69
+ return $1.gsub("\n",' ').strip
70
+ end
71
+ return nil
72
+ end
73
+
74
+ def top_level_description(section)
75
+ (top_level_descriptions[section.to_s] || []).join("\n").gsub(/\n+$/m,"\n")
76
+ end
77
+
78
+ def top_level_descriptions
79
+ @top_level_descriptions ||= {}
80
+ end
81
+
82
+ private
83
+
84
+ def load_descriptions
85
+ current_section = 'main'
86
+ @native_resource.description.each_line do |line|
87
+ if /^ *\@action *([^ ]*) (.*)$/ =~ line
88
+ action_descriptions[$1] = $2.strip
89
+ elsif /^ *\@attribute *([^ ]*) (.*)$/ =~ line
90
+ attribute_descriptions[$1] = $2.strip
91
+ elsif /^ *\@section (.*)$/ =~ line
92
+ current_section = $1.strip
93
+ else
94
+ lines = (top_level_descriptions[current_section] || [])
95
+ lines << line.gsub("\n",'')
96
+ top_level_descriptions[current_section] = lines
97
+ end
98
+ end
99
+ end
100
+
101
+ include ::Chef::Mixin::ConvertToClassName
102
+
103
+ def build_native_from_file(cookbook_name, filename)
104
+ resource_class = Class.new(DocumentingLWRPBase)
105
+
106
+ resource_class.resource_name = filename_to_qualified_string(cookbook_name, filename)
107
+ resource_class.run_context = nil
108
+ resource_data = IO.read(filename)
109
+ resource_data = resource_data.gsub(/^=begin *\n *\#\<\n(.*?)^ *\#\>\n=end *\n/m) do
110
+ "desc <<DOCO\n#{$1}\nDOCO\n"
111
+ end
112
+ resource_data = resource_data.gsub(/^ *\#\<\n(.*?)^ *\#\>\n/m) do
113
+ "desc <<DOCO\n#{$1.gsub(/^ *\# ?/, '')}\nDOCO\n"
114
+ end
115
+ resource_data = resource_data.gsub(/^ *\#\<\> (.*?)$/) do
116
+ "desc #{$1.inspect}\n"
117
+ end
118
+
119
+ resource_class.class_eval(resource_data, filename, 1)
120
+
121
+ resource_class
122
+ end
123
+ end
124
+
125
+ class DocumentingLWRPBase < ::Chef::Resource::LWRPBase
126
+
127
+ class << self
128
+ def attribute_specifications
129
+ @attribute_specifications ||= {}
130
+ end
131
+
132
+ def desc(description)
133
+ @description = "#{@description}#{description}\n"
134
+ end
135
+
136
+ def description
137
+ @description || ""
138
+ end
139
+ end
140
+
141
+ def self.attribute(attr_name, validation_opts={})
142
+ result = super(attr_name, validation_opts)
143
+ attribute_specifications[attr_name] = validation_opts
144
+ result
145
+ end
146
+ end
147
+ end
@@ -0,0 +1,3 @@
1
+ module KnifeCookbookDoc
2
+ VERSION = '0.2.0'
3
+ end
metadata ADDED
@@ -0,0 +1,110 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: knife-cookbook-doc
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Mathias Lafeldt
9
+ - Peter Donald
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2013-04-01 00:00:00.000000000 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: chef
17
+ requirement: !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ! '>='
21
+ - !ruby/object:Gem::Version
22
+ version: '0'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ none: false
27
+ requirements:
28
+ - - ! '>='
29
+ - !ruby/object:Gem::Version
30
+ version: '0'
31
+ - !ruby/object:Gem::Dependency
32
+ name: erubis
33
+ requirement: !ruby/object:Gem::Requirement
34
+ none: false
35
+ requirements:
36
+ - - ! '>='
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ type: :runtime
40
+ prerelease: false
41
+ version_requirements: !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ! '>='
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ - !ruby/object:Gem::Dependency
48
+ name: rake
49
+ requirement: !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: !ruby/object:Gem::Requirement
58
+ none: false
59
+ requirements:
60
+ - - ! '>='
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ description: Knife plugin to generate README.md for a cookbook
64
+ email:
65
+ - mathias.lafeldt@gmail.com
66
+ - peter@realityforge.org
67
+ executables: []
68
+ extensions: []
69
+ extra_rdoc_files: []
70
+ files:
71
+ - .gitignore
72
+ - CHANGELOG.md
73
+ - CONTRIBUTING.md
74
+ - Gemfile
75
+ - LICENSE
76
+ - README.md
77
+ - Rakefile
78
+ - knife_cookbook_doc.gemspec
79
+ - lib/chef/knife/README.md.erb
80
+ - lib/chef/knife/cookbook_doc.rb
81
+ - lib/knife_cookbook_doc/readme_model.rb
82
+ - lib/knife_cookbook_doc/recipe_model.rb
83
+ - lib/knife_cookbook_doc/resource_model.rb
84
+ - lib/knife_cookbook_doc/version.rb
85
+ homepage: http://realityforge.github.com/knife-cookbook-doc
86
+ licenses:
87
+ - Apache 2.0
88
+ post_install_message:
89
+ rdoc_options: []
90
+ require_paths:
91
+ - lib
92
+ required_ruby_version: !ruby/object:Gem::Requirement
93
+ none: false
94
+ requirements:
95
+ - - ! '>='
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ required_rubygems_version: !ruby/object:Gem::Requirement
99
+ none: false
100
+ requirements:
101
+ - - ! '>='
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ requirements: []
105
+ rubyforge_project:
106
+ rubygems_version: 1.8.23
107
+ signing_key:
108
+ specification_version: 3
109
+ summary: Knife plugin to generate README.md for a cookbook
110
+ test_files: []