This repository has been archived by the owner on Jul 14, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 170
Easy generators #118
Merged
Merged
Easy generators #118
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
attr_accessor(name) | ||
end | ||
end | ||
|
||
def self.reset | ||
|
@@ -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) | ||
|
@@ -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} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 3 additions & 3 deletions
6
lib/chef-dk/skeletons/code_generator/templates/default/metadata.rb.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 😄There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
❤️