In this tutorial we will create a simple hello world plugin.
Hammer plugins are nothing but gems. Details on how to build a gem can be found for example at rubygems.org. In the first part of this tutorial we will briefly guide you through the process of creating a very simple gem. First of all you will need the rubygems package installed on your system.
Create the basic gem structure in a project subdirectory of your choice: <code> $ cd ./my_first_hammer_plugin/ $ touch Gemfile $ touch hammer_cli_hello.gemspec $ mkdir -p lib/hammer_cli_hello $ touch lib/hammer_cli_hello.rb $ touch lib/hammer_cli_hello/version.rb </code>
Example Gemfile
:
```ruby
source "rubygems.org"
gemspec ```
Example hammer_cli_hello.gemspec
file:
```ruby
$:.unshift
File.expand_path("../lib", FILE)
require
"hammer_cli_hello/version"
Gem::Specification.new do |s|
s.name = “hammer_cli_hello” s.authors = ["Me"] s.version = HammerCLIHello.version.dup s.platform = Gem::Platform::RUBY s.summary = %q{Hello world commands for Hammer}
s.files = Dir['lib /*.rb'] s.require_paths = ["lib"]
s.add_dependency 'hammer_cli', '>= 0.0.6' end ``` More details about the gemspec structure is again at rubygems.org.
We'll have to specify the plugin version in
lib/hammer_cli_hello/version.rb
:
<code>ruby
module
HammerCLIHello
def self.version
@version ||= Gem::Version.new
'0.0.1'
end
end
</code>
This should be enough for creating a minimalist gem. Let's build and install it. <code> $ gem build ./hammer_cli_hello.gemspec $ gem install hammer_cli_hello-0.0.1.gem </code>
Place your module's config file into
~/.hammer/cli.modules.d/hello_world.yml
.
<code>yaml
:hello:
:enable_module: true
</code>
Verify the installation by running: <code> $ hammer -v > /dev/null </code>
You should see a message saying that your module was loaded (second line in the sample output). <code> [ INFO 2013-10-16 11:19:06 Init] Initialization of Hammer CLI (0.1.0) has started... [ INFO 2013-10-16 11:19:06 Init] Extension module hammer_cli_hello loaded [ INFO 2013-10-16 11:19:06 Init] Configuration from the file /root/.hammer/cli_config.yml has been loaded [ INFO 2013-10-16 11:19:06 Init] Configuration from the file /root/.hammer/cli.modiles.d/hello.yml has been loaded [ INFO 2013-10-16 11:19:06 HammerCLI::MainCommand] Called with options: {"verbose"=>true} </code>
Done. Your first hammer plugin is installed. Unfortunately it does not contain any commands yet. So let's start adding some to finally enjoy real results.
Optionally you can add a Rakefile and build and install the gem with
rake install
<code>ruby
# ./Rakefile
require
'bundler/gem_tasks'
</code>