2014-09-11 00:07:33 +00:00
Puppet Strings
2016-09-11 21:46:57 +00:00
==============
2016-08-23 19:55:40 +00:00
[![Build Status ](https://travis-ci.org/puppetlabs/puppet-strings.png?branch=master )](https://travis-ci.org/puppetlabs/puppet-strings) [![Gem Version ](https://badge.fury.io/rb/puppet-strings.svg )](https://badge.fury.io/rb/puppet-strings)
2014-05-30 19:13:30 +00:00
2016-09-15 22:45:44 +00:00
A Puppet command built on [YARD ](http://yardoc.org/ ).
2014-05-30 19:13:30 +00:00
2016-09-15 22:45:44 +00:00
Puppet Strings generates HTML documentation for Puppet extensions written in Puppet and Ruby.
This tool will eventually place the existing `puppet doc` command once feature parity has been achieved.
| | |
| -------------- |---------------------------------------------------------------- |
| *Code* | [GitHub][repo] |
| *Issues* | [Puppet JIRA Tracker][JIRA] |
| *License* | [Apache 2.0][LICENSE] |
| *Change log* | [CHANGELOG.md][changelog] |
| *Contributing* | [CONTRIBUTING.md][contributing] and [COMMITTERS.md][committers] |
2015-07-17 17:56:22 +00:00
2016-08-23 19:55:40 +00:00
[repo]: https://github.com/puppetlabs/puppet-strings
2015-07-17 17:56:22 +00:00
[JIRA]: https://tickets.puppetlabs.com/browse/PDOC
2016-08-23 19:55:40 +00:00
[LICENSE]: https://github.com/puppetlabs/puppet-strings/blob/master/LICENSE
[changelog]: https://github.com/puppetlabs/puppet-strings/blob/master/CHANGELOG.md
[contributing]: https://github.com/puppetlabs/puppet-strings/blob/master/CONTRIBUTING.md
[committers]: https://github.com/puppetlabs/puppet-strings/blob/master/COMMITTERS.md
2015-07-17 17:56:22 +00:00
2016-09-15 22:45:44 +00:00
Requirements
2014-05-30 19:13:30 +00:00
------------
2016-09-15 22:45:44 +00:00
2014-10-03 21:59:55 +00:00
In order to run strings you need to have the following software installed:
2014-10-01 21:28:08 +00:00
2014-10-03 21:59:55 +00:00
* Ruby 1.9.3 or newer
* Puppet 3.7 or newer
2016-09-15 22:45:44 +00:00
* The `yard` Ruby gem
2014-06-10 04:20:56 +00:00
2016-04-21 23:56:09 +00:00
Installing the YARD Gem
2015-07-28 00:01:27 +00:00
-----------------------
2015-07-17 17:56:22 +00:00
2016-09-15 22:45:44 +00:00
The easiest way to install the `yard` gem is with Puppet itself:
2015-07-28 00:01:27 +00:00
For Puppet 4.x:
```
$ puppet resource package yard provider=puppet_gem
```
For Puppet 3.x:
2015-07-17 17:56:22 +00:00
```
2015-07-28 00:01:27 +00:00
$ puppet resource package yard provider=gem
2015-07-17 17:56:22 +00:00
```
2016-09-15 22:45:44 +00:00
Installing Puppet Strings
2015-07-28 00:01:27 +00:00
-------------------------
2016-09-15 22:45:44 +00:00
Strings can be installed using the [puppet-strings ](https://rubygems.org/gems/puppet-strings ) gem.
To ensure it is installed in right place, it is best to install it using Puppet:
2016-03-16 06:23:05 +00:00
For Puppet 4.x:
```
$ puppet resource package puppet-strings provider=puppet_gem
```
For Puppet 3.x:
```
$ puppet resource package puppet-strings provider=gem
```
2014-10-01 21:28:08 +00:00
Running Puppet Strings
2015-07-17 17:56:22 +00:00
----------------------
2014-05-30 19:13:30 +00:00
2015-07-17 17:56:22 +00:00
Once strings has been installed you can document a puppet module:
```
2015-09-02 20:11:41 +00:00
$ cd /path/to/module
$ puppet strings
2015-07-17 17:56:22 +00:00
```
2014-05-30 19:13:30 +00:00
2016-09-09 20:44:05 +00:00
This processes `README` and all Puppet and Ruby source files under the `./manifests/` , `./functions/` , and `./lib/`
2016-09-15 22:45:44 +00:00
directories by default and creates HTML documentation under the `./doc/` directory.
2014-05-30 19:13:30 +00:00
2015-07-17 17:56:22 +00:00
To document specific files:
2014-10-01 21:28:08 +00:00
2015-07-17 17:56:22 +00:00
```
2016-09-15 22:45:44 +00:00
$ puppet strings generate first.pp second.pp ...
2016-09-09 20:44:05 +00:00
```
To document specific directories:
```
$ puppet strings 'modules/foo/lib/**/*.rb' 'modules/foo/manifests/**/*.pp' 'modules/foo/functions/**/*.pp' ...
2015-07-17 17:56:22 +00:00
```
2014-05-30 19:13:30 +00:00
2016-09-15 22:45:44 +00:00
Strings can emit JSON documenting the Puppet extensions:
2015-10-08 01:22:29 +00:00
```
2016-09-15 22:45:44 +00:00
$ puppet strings generate --emit-json documentation.json
2015-10-08 01:22:29 +00:00
```
It can also print the JSON to stdout:
```
2016-09-15 22:45:44 +00:00
$ puppet strings generate --emit-json-stdout
2015-10-08 01:22:29 +00:00
```
2016-09-15 22:45:44 +00:00
The schema for the JSON output is [documented here ](https://github.com/puppetlabs/puppet-strings/blob/master/JSON.md ).
2014-05-30 19:13:30 +00:00
2016-04-21 23:56:09 +00:00
In addition to generating a directory full of HTML, you can also serve up documentation for all your modules using the `server` action:
2014-10-01 21:28:08 +00:00
2015-07-17 17:56:22 +00:00
```
2015-09-02 20:11:41 +00:00
$ puppet strings server
2015-07-17 17:56:22 +00:00
```
2014-10-01 21:28:08 +00:00
2016-09-15 22:45:44 +00:00
YARD Options
------------
2014-10-20 17:17:52 +00:00
2016-09-15 22:45:44 +00:00
YARD options (see `yard help doc` ) are supported in a `.yardopts` file in the same directory where `puppet strings` is run.
2015-07-17 17:56:22 +00:00
2016-09-15 22:45:44 +00:00
Puppet Strings automatically sets the `markup` option to `markdown` , allowing your documentation strings to be in Markdown format.
2014-10-20 17:17:52 +00:00
2016-09-15 22:45:44 +00:00
Documenting Puppet Extensions
-----------------------------
2014-10-20 17:17:52 +00:00
2016-09-15 22:45:44 +00:00
### Puppet Classes / Defined Types
2014-10-20 17:17:52 +00:00
2016-09-15 22:45:44 +00:00
To document Puppet classes and defined types, use a YARD docstring before the class or defined type definition:
```puppet
# An example class.
#
# This is an example of how to document a Puppet class
2015-09-02 20:11:41 +00:00
#
2016-09-15 22:45:44 +00:00
# @example Declaring the class
# include example
2015-09-02 20:11:41 +00:00
#
2016-09-15 22:45:44 +00:00
# @param first The first parameter for this class
# @param second The second paramter for this class
class example_class(
String $first = $example::params::first_arg,
Integer $second = $example::params::second_arg,
) inherits example::params {
# ...
}
# An example defined type.
2015-09-02 20:11:41 +00:00
#
2016-09-15 22:45:44 +00:00
# This is an example of how to document a defined type.
# @param ports The array of port numbers to use.
define example_type(
Array[Integer] $ports = []
) {
# ...
}
```
***Note: unlike Ruby, Puppet is a typed language; Puppet Strings will automatically use the parameter type information to
document the parameters. A warning will be emitted if you document parameter types.***
### Resource Types
To document custom resource types and their parameters/properties, use the `desc` method or assign a value to the `doc` attribute:
```ruby
Puppet::Type.newtype(:example) do
desc < < -DESC
An example resource type.
@example Using the type.
example { foo:
param => 'hi'
}
DESC
newparam(:param) do
desc 'An example parameter.'
# ...
2015-09-02 20:11:41 +00:00
end
2016-09-15 22:45:44 +00:00
newproperty(:prop) do
desc 'An example property.'
#...
end
# ...
2015-09-02 20:11:41 +00:00
end
2015-07-17 17:56:22 +00:00
```
2014-10-20 17:17:52 +00:00
2016-09-15 22:45:44 +00:00
Puppet Strings documents this way to preserve backwards compatibility with `puppet doc` and existing resource types.
2015-09-10 15:47:12 +00:00
2016-09-15 22:45:44 +00:00
***Note: Puppet Strings does not evaluate your Ruby code, so only certain static expressions are supported.***
2014-10-20 17:17:52 +00:00
2016-09-15 22:45:44 +00:00
To document parameters and properties that are dynamically created, use the `#@!puppet.type.param` and `#@!puppet.type.property`
directives before the `newtype` call:
```ruby
# @!puppet.type.param [value1, value2, value3] my_param Documentation for a dynamic parameter.
# @!puppet.type.property [foo, bar, baz] my_prop Documentation for a dynamic property.
Puppet::Type.newtype(:example) do
#...
end
```
### Providers
To document providers, use the `desc` method or assign a value to the `doc` attribute:
```ruby
Puppet::Type.type(:example).provide :platform do
desc 'An example provider.'
# ...
end
2015-07-17 17:56:22 +00:00
```
2014-10-20 17:17:52 +00:00
2016-09-15 22:45:44 +00:00
Puppet Strings documents this way to preserve backwards compatibility with `puppet doc` and existing resource types.
***Note: Puppet Strings does not evaluate your Ruby code, so only certain static expressions are supported.***
### Functions
Puppet Strings supports three different ways of defining a function in Puppet: with the Puppet 3.x API, Puppet 4.X API,
and in the Puppet language itself.
2015-09-10 15:47:12 +00:00
2016-09-15 22:45:44 +00:00
## Puppet 3.x API
To document a function in the Puppet 3.x API, use the `doc` option to `newfunction` :
2015-08-11 18:53:01 +00:00
```ruby
2016-09-15 22:45:44 +00:00
Puppet::Parser::Functions.newfunction(:example, doc: < < -DOC
Documentation for an example 3.x function.
@param [String] param1 The first parameter.
@param [Integer] param2 The second parameter.
@return [Undef]
@example Calling the function.
example('hi', 10)
DOC
) do |*args|
#...
end
```
***Note: if parameter types are omitted, a default of the `Any` Puppet type will be used.***
2015-08-11 18:53:01 +00:00
2016-09-15 22:45:44 +00:00
## Puppet 4.x API
2015-08-11 18:53:01 +00:00
2016-09-15 22:45:44 +00:00
To document a function in the Puppet 4.x API, use a YARD docstring before the `create_function` call and any `dispatch`
calls:
2015-08-11 18:53:01 +00:00
2016-09-15 22:45:44 +00:00
```ruby
# An example 4.x function.
Puppet::Functions.create_function(:example) do
# @param first The first parameter.
# @param second The second parameter.
# @return [String] Returns a string.
# @example Calling the function
# example('hi', 10)
dispatch :example do
param 'String', :first
param 'Integer', :second
2015-08-11 18:53:01 +00:00
end
2016-09-15 22:45:44 +00:00
2015-08-11 18:53:01 +00:00
# ...
end
2016-09-15 22:45:44 +00:00
```
2015-08-11 18:53:01 +00:00
2016-09-15 22:45:44 +00:00
***Note: Puppet Strings will automatically use the parameter type information from the `dispatch` block to document
the parameters. Only document your parameter types when the Puppet 4.x function contains no `dispatch` calls.***
2015-08-11 18:53:01 +00:00
2016-09-15 22:45:44 +00:00
If the Puppet 4.x function contains multiple `dispatch` calls, Puppet Strings will automatically create `overload` tags
to describe the function's overloads:
```ruby
# An example 4.x function.
Puppet::Functions.create_function(:example) do
# Overload by string.
# @param first The first parameter.
# @return [String] Returns a string.
# @example Calling the function
# example('hi')
dispatch :example_string do
param 'String', :first
end
# Overload by integer.
# @param first The first parameter.
# @return [Integer] Returns an integer.
# @example Calling the function
# example(10)
dispatch :example_integer do
param 'Integer', :first
end
# ...
```
The resulting HTML for this example function will document both `example(String $first)` and `example(Integer $first)` .
## Puppet Language
To document Puppet functions written in the Puppet language, use a YARD docstring before the function definition:
```puppet
# An example function written in Pupppet.
# @param name The name to say hello to.
# @return [String] Returns a string.
# @example Calling the function
# example('world')
function example(String $name) {
"hello $name"
}
2015-08-11 18:53:01 +00:00
```
2016-09-15 22:45:44 +00:00
***Note: Puppet Strings will automatically use the parameter type information from the function's parameters to document
the parameters.***
Additional Resources
--------------------
2014-10-20 17:17:52 +00:00
Here are a few other good resources for getting started with documentation:
2016-08-23 19:55:40 +00:00
* [Module README Template ](https://docs.puppet.com/puppet/latest/reference/modules_documentation.html )
2014-10-20 17:17:52 +00:00
* [YARD Getting Started Guide ](http://www.rubydoc.info/gems/yard/file/docs/GettingStarted.md )
* [YARD Tags Overview ](http://www.rubydoc.info/gems/yard/file/docs/Tags.md )
2015-09-02 15:38:23 +00:00
Rake Tasks
2016-09-11 21:46:57 +00:00
----------
2015-09-02 15:38:23 +00:00
2016-09-11 21:46:57 +00:00
Puppet Strings comes with two rake tasks: `strings:generate` and `strings:gh_pages:update` available in `puppet-strings/tasks` .
2015-09-02 15:38:23 +00:00
2016-09-11 21:46:57 +00:00
Add the following to your Gemfile to use `puppet-strings` :
2016-03-25 06:32:10 +00:00
2015-09-02 20:09:01 +00:00
```ruby
2016-08-23 19:55:40 +00:00
gem 'puppet-strings', :git => 'https://github.com/puppetlabs/puppet-strings.git'
2015-09-02 20:09:01 +00:00
```
2015-09-02 15:38:23 +00:00
2016-09-11 21:46:57 +00:00
In your `Rakefile` , add the following to use the `puppet-strings` tasks:
2015-09-02 15:38:23 +00:00
2015-09-02 20:09:01 +00:00
```ruby
2016-09-11 21:46:57 +00:00
require 'puppet-strings/tasks'
2015-09-02 20:09:01 +00:00
```
2014-10-01 21:28:08 +00:00
2016-09-11 21:46:57 +00:00
The `strings:generate` task can be used to generate documentation:
2016-05-04 07:44:27 +00:00
2016-09-11 21:46:57 +00:00
```
$ rake strings:generate
```
The task accepts the following parameters:
* `patterns` : the search patterns to use for finding files to document (defaults to `manifests/**/*.pp functions/**/*.pp types/**/*.pp lib/**/*.rb` ).
* `debug` : enables debug output when set to `true` .
* `backtrace` : enables backtraces for errors when set to `true` .
* `markup` : the markup language to use (defaults to `markdown` ).
* `yard_args` : additional arguments to pass to YARD.
An example of passing arguments to the `strings:generate` Rake task:
```
$ rake strings:generate\['**/*.pp ** /*.rb, true, true, markdown, --readme README.md']
2016-05-04 07:44:27 +00:00
```
2016-09-11 21:46:57 +00:00
The `strings:gh_pages:update` task will generate your Puppet Strings documentation to be made available via [GitHub Pages ](https://pages.github.com/ ). It will:
2016-07-05 19:51:39 +00:00
2016-09-11 21:46:57 +00:00
1. Create a `doc` directory in the root of your project
2. Check out the `gh-pages` branch of the current repository in the `doc` directory (it will create a branch if one does not already exist)
3. Generate strings documentation using the `strings:generate` task
4. Commit the changes and push them to the `gh-pages` branch **with the `--force` flag**
2016-07-05 19:51:39 +00:00
2016-09-11 21:46:57 +00:00
This task aims to keep the `gh-pages` branch up to date with the current code and uses the `-f` flag when pushing to the `gh-pages` branch.
***Please note this operation will be destructive if not used properly.***
2016-07-05 19:51:39 +00:00
2014-10-01 21:28:08 +00:00
Developing and Contributing
2016-09-15 22:45:44 +00:00
---------------------------
We love contributions from the community!
2014-10-01 21:28:08 +00:00
2016-09-15 22:45:44 +00:00
If you'd like to contribute to the strings module, check out [CONTRIBUTING.md ](https://github.com/puppetlabs/puppet-strings/blob/master/CONTRIBUTING.md ) to get information on the contribution process.
2014-10-01 21:28:08 +00:00
Running Specs
2016-09-15 22:45:44 +00:00
-------------
2014-10-01 21:28:08 +00:00
2016-09-15 22:45:44 +00:00
If you plan on developing features or fixing bugs in Puppet Strings, it is essential that you run specs before opening a pull request.
To run specs, simply execute the `spec` rake task:
2014-10-01 21:28:08 +00:00
$ bundle install --path .bundle/gems
$ bundle exec rake spec
Support
2016-09-15 22:45:44 +00:00
-------
2014-10-01 21:28:08 +00:00
2016-09-15 22:45:44 +00:00
Please log tickets and issues at our [JIRA tracker][JIRA]. A [mailing list ](https://groups.google.com/forum/?fromgroups#!forum/puppet-users )
is available for asking questions and getting help from others.
2014-10-01 21:28:08 +00:00
2016-09-15 22:45:44 +00:00
There is also an active #puppet channel on the Freenode IRC network.
2014-05-30 19:13:30 +00:00
2016-09-15 22:45:44 +00:00
We use semantic version numbers for our releases, and recommend that users stay as up-to-date as possible by upgrading to
patch releases and minor releases as they become available.
2014-05-30 19:13:30 +00:00
2016-09-15 22:45:44 +00:00
Bug fixes and ongoing development will occur in minor releases for the current major version.
Security fixes will be ported to a previous major version on a best-effort basis, until the previous major version is no longer maintained.