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

Adds 'chef generate helpers [COOKBOOK] HELPERS_NAME' #1085

Merged
merged 5 commits into from
May 15, 2017

Conversation

burtlo
Copy link
Contributor

@burtlo burtlo commented Nov 21, 2016

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

# 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 %>
Copy link
Contributor

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.

Copy link
Contributor

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 :(

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)

Copy link
Contributor

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.

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?

Copy link
Contributor

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.

@burtlo
Copy link
Contributor Author

burtlo commented Nov 29, 2016

Updates to the template based on conversation by @coderanger and @kameghamegha.

@burtlo burtlo changed the title Adds 'chef generate helpers' Adds 'chef generate helpers [COOKBOOK] HELPER_FILE_NAME' Nov 29, 2016
@burtlo burtlo changed the title Adds 'chef generate helpers [COOKBOOK] HELPER_FILE_NAME' Adds 'chef generate helpers [COOKBOOK] HELPERS_NAME' Nov 29, 2016
@coderanger
Copy link
Contributor

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.

@burtlo
Copy link
Contributor Author

burtlo commented Nov 29, 2016

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).

@coderanger
Copy link
Contributor

@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 define_resource and also dumps the helper into all resource objects in that recipe. Would give better lexical behavior, but this is better than telling people to figure it out themselves, at least this will mostly fail towards user frustration rather than it-works-but-youll-regret it :D

@tduffield
Copy link
Contributor

@burtlo if you rebase this on latest master, I think we can get this merged.

Franklin Webber added 3 commits March 6, 2017 11:25
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>

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
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>
@burtlo burtlo force-pushed the generator/helpers branch from a778405 to f32c4e1 Compare March 6, 2017 17:25
@burtlo
Copy link
Contributor Author

burtlo commented Mar 6, 2017

I have rebased on master with no conflicts. It seems Github here is not pleased though.

@thommay thommay force-pushed the generator/helpers branch from 3d5da40 to 955852e Compare May 15, 2017 11:06
Signed-off-by: Thom May <thom@chef.io>
@thommay thommay force-pushed the generator/helpers branch from 955852e to ff01675 Compare May 15, 2017 13:34
@thommay thommay merged commit b85525e into chef-boneyard:master May 15, 2017
@chef-boneyard chef-boneyard locked and limited conversation to collaborators Feb 14, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants