Skip to content
This repository has been archived by the owner on Jul 14, 2021. It is now read-only.

Easy generators #118

Merged
merged 1 commit into from
Jul 29, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion lib/chef-dk/command/generator_commands.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ module Command
module SharedGeneratorOptions
include Mixlib::CLI

# You really want these to have default values, as
# they will likely be used all over the place.
option :license,
:short => "-I LICENSE",
:long => "--license LICENSE",
Expand All @@ -46,7 +48,8 @@ module SharedGeneratorOptions
option :email,
:short => "-m EMAIL",
:long => "--email EMAIL",
:description => "Email address of the author"
:description => "Email address of the author - defaults to 'you@example.com'",
:default => 'you@example.com'

option :generator_cookbook,
:short => "-g GENERATOR_COOKBOOK_PATH",
Expand Down
8 changes: 4 additions & 4 deletions lib/chef-dk/command/generator_commands/app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@ def run

def setup_context
super
generator_context.app_root = app_root
generator_context.app_name = app_name
generator_context.cookbook_root ||= cookbook_root
generator_context.cookbook_name ||= cookbook_name
Generator.add_attr_to_context(:app_root, app_root)
Generator.add_attr_to_context(:app_name, app_name)
Generator.add_attr_to_context(:cookbook_root, cookbook_root)
Generator.add_attr_to_context(:cookbook_name, cookbook_name)
end

def recipe
Expand Down
17 changes: 5 additions & 12 deletions lib/chef-dk/command/generator_commands/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ class Base < Command::Base

attr_reader :params


options.merge!(SharedGeneratorOptions.options)

def initialize(params)
Expand All @@ -56,17 +55,11 @@ def generator_cookbook_path

# Sets git related generator_context values.
def setup_context
Generator.context.have_git = have_git?
Generator.context.skip_git_init = false
Generator.context.license = config[:license]
Generator.context.copyright_holder = config[:copyright_holder]
Generator.context.email = config[:email]
end

# Delegates to `Generator.context`, the singleton instance of
# Generator::Context
def generator_context
Generator.context
Generator.add_attr_to_context(:have_git, have_git?)
Generator.add_attr_to_context(:skip_git_init, false)
config.each do |k,v|
Generator.add_attr_to_context(k, v)
end
end

# Checks the `PATH` for the presence of a `git` (or `git.exe`, on
Expand Down
6 changes: 3 additions & 3 deletions lib/chef-dk/command/generator_commands/cookbook.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ def run

def setup_context
super
generator_context.skip_git_init = cookbook_path_in_git_repo?
generator_context.cookbook_root = cookbook_root
generator_context.cookbook_name = cookbook_name
Generator.add_attr_to_context(:skip_git_init, cookbook_path_in_git_repo?)
Generator.add_attr_to_context(:cookbook_root, cookbook_root)
Generator.add_attr_to_context(:cookbook_name, cookbook_name)
end

def recipe
Expand Down
6 changes: 3 additions & 3 deletions lib/chef-dk/command/generator_commands/cookbook_code_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ def run

def setup_context
super
generator_context.cookbook_root = cookbook_root
generator_context.cookbook_name = cookbook_name
generator_context.new_file_basename = new_file_basename
Generator.add_attr_to_context(:cookbook_root, cookbook_root)
Generator.add_attr_to_context(:cookbook_name, cookbook_name)
Generator.add_attr_to_context(:new_file_basename, new_file_basename)
end

def cookbook_root
Expand Down
2 changes: 1 addition & 1 deletion lib/chef-dk/command/generator_commands/cookbook_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def recipe

def setup_context
super
generator_context.content_source = config[:source]
Generator.add_attr_to_context(:content_source, config[:source])
end
end
end
Expand Down
6 changes: 2 additions & 4 deletions lib/chef-dk/command/generator_commands/repo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,8 @@ def run

def setup_context
super
generator_context.repo_root = repo_root
generator_context.repo_name = repo_name
generator_context.cli_config = config
generator_context.policy_only = config[:policy_only]
Generator.add_attr_to_context(:repo_root, repo_root)
Generator.add_attr_to_context(:repo_name, repo_name)
end

def recipe
Expand Down
3 changes: 1 addition & 2 deletions lib/chef-dk/command/generator_commands/template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ module GeneratorCommands
# chef generate template [path/to/cookbook_root] name --source=source_file
class Template < CookbookCodeFile

option :source,
option :content_source,
:short => "-s SOURCE_FILE",
:long => "--source SOURCE_FILE",
:description => "Copy content from SOURCE_FILE"
Expand All @@ -38,7 +38,6 @@ def recipe

def setup_context
super
generator_context.content_source = config[:source]
end
end
end
Expand Down
135 changes: 99 additions & 36 deletions lib/chef-dk/generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,11 @@ module ChefDK

module Generator

# This is here to hold attr_accessor data for Generator context variables
class Context
attr_accessor :have_git
attr_accessor :app_root
attr_accessor :cookbook_root
attr_accessor :app_name
attr_accessor :cookbook_name
attr_accessor :new_file_basename
attr_accessor :content_source
attr_accessor :author_name
attr_accessor :author_email
attr_accessor :license
attr_accessor :license_description
attr_accessor :license_text
attr_accessor :repo_root
attr_accessor :repo_name
attr_accessor :cli_config
attr_accessor :skip_git_init
attr_accessor :copyright_holder
attr_accessor :email
attr_accessor :policy_only
def self.add_attr(name)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 to the cleanliness, but this definitely needs some docs and @example tags for future developers please 😄

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah - my plan is to write docs on how to write generators easily.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❤️

attr_accessor(name)
end
end

def self.reset
Expand All @@ -49,6 +34,13 @@ def self.context
@context ||= Context.new
end

def self.add_attr_to_context(name, value=nil)
sym_name = name.to_sym
ChefDK::Generator::Context.add_attr(sym_name)
ChefDK::Generator::TemplateHelper.delegate_to_app_context(sym_name)
context.public_send("#{sym_name}=", value)
end

module TemplateHelper

def self.delegate_to_app_context(name)
Expand All @@ -61,23 +53,94 @@ def year
Time.now.year
end

# delegate all the attributes of app_config
delegate_to_app_context :app_root
delegate_to_app_context :cookbook_root
delegate_to_app_context :cookbook_name
delegate_to_app_context :new_file_basename
delegate_to_app_context :content_source
delegate_to_app_context :author_name
delegate_to_app_context :author_email
delegate_to_app_context :email
delegate_to_app_context :license
delegate_to_app_context :license_description
delegate_to_app_context :license_text
delegate_to_app_context :repo_root
delegate_to_app_context :repo_name
delegate_to_app_context :copyright_holder
delegate_to_app_context :cli_config
delegate_to_app_context :policy_only
# Prints the short description of the license, suitable for use in a
# preamble to a file. Optionally specify a comment to prepend to each line.
def license_description(comment=nil)
case license
when 'all_rights'
result = "Copyright (c) #{year} #{copyright_holder}, All Rights Reserved."
when 'apache2'
result = <<-EOH
Copyright #{year} #{copyright_holder}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess this is fine, but I would much prefer to move these to erb files and just render them. The inline heredocs make the code difficult to follow.


Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
EOH
when 'mit'
result = <<-EOH
The MIT License (MIT)

Copyright (c) #{year} #{copyright_holder}

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
EOH
when 'gplv2'
result = <<-EOH
Copyright (C) #{year} #{copyright_holder}

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
EOH
when 'gplv3'
result = <<-EOH
Copyright (C) #{year} #{copyright_holder}

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
EOH
end
if comment
result.gsub(/^/m, "#{comment} ")
else
result
end
end
end

end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,4 @@
# Cookbook Name:: <%= cookbook_name %>
# Recipe:: default
#
# Copyright (C) <%= Time.new.strftime("%Y") %> <%= author_name %>
#
# <%= license_description %>
#
<%= license_description('#') %>
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name '<%= cookbook_name %>'
maintainer '<%= author_name %>'
maintainer_email '<%= author_email %>'
license '<%= license_description %>'
maintainer '<%= copyright_holder %>'
maintainer_email '<%= email %>'
license '<%= license %>'
description 'Installs/Configures <%= cookbook_name %>'
long_description 'Installs/Configures <%= cookbook_name %>'
version '0.1.0'
Expand Down
Loading