-
Notifications
You must be signed in to change notification settings - Fork 168
Adds 'chef generate helpers [COOKBOOK] HELPERS_NAME' #1085
Conversation
# The module you have defined above may be included into ALL OF YOUR RECIPES by | ||
# including the following line here or within your recipes. | ||
# | ||
# Chef::Recipe.include <%= @cookbook_class_name %>::<%= @helper_class_name %> |
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.
So two concerns, first that if we do move forward with advising people that helper == global DSL modification we should use the more technically correct form of either injecting the mixin into one of the DSL modules or into Recipe
+Resource
+Provider
.
Second: I really don't like the idea of advising users, especially new users, to make global DSL changes. It's really footgun-y in a way that is hard to unwind later on. I personally advise people use the more lexical-ish extend
pattern, but that's got some sharp edges if you don't know what is going on.
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.
Also really really never run global DSL changes from a recipe, it causes basically no end of subtle bugs :(
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.
I had noticed that something like the following (which is placed in the library file, in this case under the end for module OperatingSystemHelpers
) seems to have been adopted as being the best practice. Are you suggesting something else? (I'm not sure of the syntax for what you are suggesting @coderanger )
Chef::Recipe.send(:include, OperatingSystemHelpers)
Chef::Resource.send(:include, OperatingSystemHelpers)
Chef::Provider.send(:include, OperatingSystemHelpers)
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.
You don't need the send
anymore because include
is a public method in Ruby 2.3, and yes that is one way to implement global DSL changes. But you probably shouldn't be doing that unless you've got a really really good reason.
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.
What is your suggestion then? :) I'm unaware of other ways (because I'm only just learning all of this). And maybe an explanation about why not to do this?
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.
https://coderanger.net/chef-tips/#3 is the pattern I recommend to people currently, working on something better but haven't had the cycles.
Updates to the template based on conversation by @coderanger and @kameghamegha. |
If we want to support this, this is probably as good as it can be. I won't block it if others feel strongly but I'm still concerned about the level of potential confusion. Specifically this code will not work: extend MyCookbook::Helpers
my_resource 'foo' do
something my_helper_method
end because the helper is only available from the resource context. If you know Ruby internals this makes some level of sense, but if you're treating it more like a Rails-style helper DSL it's less obvious. |
I definitely agree @coderanger. I think we could address this by bettering the documentation that is mentioned at the top (https://docs.chef.io/libraries.html). |
@burtlo The other solution is write a better pattern, but it is out of scope for this PR :) It would be possible to make something fancy either within Chef or as a library cookbook (my plan is the latter because that's how I roll but if someone else gets there first ...) which sets up the module so that it hooks |
62795f6
to
a778405
Compare
@burtlo if you rebase this on latest master, I think we can get this merged. |
I wanted to provide support for generating a library helper file in the libraries folder. Signed-off-by: Franklin Webber <franklin@chef.io>
When testing this out I used some helper names that also needed to be camel-cased. I created a method that shares a name with the Rails ActiveSupport method but I don't think there is a collision as that is appended onto a String and this project doesn't use that library. Signed-off-by: Franklin Webber <franklin@chef.io>
Extending a module within an instance will add the helper methods as instance methods to that specific instance and not to all recipes or resources. This isolates the impact of this helper module. Signed-off-by: Franklin Webber <franklin@chef.io>
a778405
to
f32c4e1
Compare
I have rebased on master with no conflicts. It seems Github here is not pleased though. |
3d5da40
to
955852e
Compare
955852e
to
ff01675
Compare
Description
I wanted to provide support for generating a library helper file in the libraries folder.
Issues Resolved
None
Check List
Signed-off-by: Franklin Webber franklin@chef.io