Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Invoking Guard programatically with groups is nonintuitive #118

Closed
ches opened this Issue Aug 14, 2011 · 6 comments

Comments

Projects
None yet
3 participants
Contributor

ches commented Aug 14, 2011

Hey guys,

I don't know if this is a "bug" per say, but it's a little quirky: I found that when attempting to use Guard programmatically as shown in the examples, it was blowing up if I had any groups defined:

ree-1.8.7-2011.03 :003 > Guard::Dsl.evaluate_guardfile :guardfile => '/tmp/Guardfile' 
Using Guardfile at /tmp/Guardfile.
ERROR: Invalid Guardfile, original error is:
undefined method `empty?' for nil:NilClass

Now it's a little annoying that Guard swallows the backtrace/line number of the error, but it didn't take too long in this case to see that this is coming from the groups evaluation. So I realized that if I wanted to load all groups, I needed to pass an empty array for the group option:

Guard::Dsl.evaluate_guardfile :guardfile => '/tmp/Guardfile', :group => []

That's kinda weird :-) Should just check for nil and empty I suppose.

Slightly different subject here, but also an observation from my same use case: it'd be nice if guards were accessible as a hash keyed by their names, instead of an array. I think it's easiest to show what I mean with the Rake task that I got working:

namespace :assets do
  desc 'Generate CSS from all source LESS files'
  task :less do
    Guard.setup
    Guard::Dsl.evaluate_guardfile(:guardfile => guardfile, :group => ['frontend'])

    # This is ugly:
    less = Guard.guards.select { |g| g.is_a? Guard::Less }.first
    less.run_all
  end
end

I don't know how much of a major internal change would be required to change that, haven't looked closely at the code, but again just an observation! At a glance it looked like guard names were pretty much completely disregarded (other than telling people that guard-ego is no longer needed). I know by attempting to use Guard programmatically at this point I'm probably somewhat of a minority :-)

Guard is great by the way, and I'm happy that aside from these small quirks it was pretty quick and easy to reuse the Guard plugins and configuration for something like this.

@netzpirat netzpirat closed this in 87375c7 Aug 14, 2011

@netzpirat netzpirat added a commit that referenced this issue Aug 14, 2011

@netzpirat netzpirat Merge pull request #119 from ches/guard
---

After all my talking in #118, I thought I should fix it :-)
fd405b3
Contributor

netzpirat commented Aug 14, 2011

Your example is very nice. Can you add this example to the wiki? It may be of use for other people also.

Concerning your second issue about getting a specific guard itself: I would not change the Array to a Hash, because there is an accessor, so the guards array is public api. But perhaps you can just add a little helper method to Guard itself that allows you to get a guard like Guard.get_guard(:less) and send another pull request.

Contributor

ches commented Aug 14, 2011

Sure, I've created a Cookbook page as that seemed like an appropriate idea.

Fair point about the public API for the guards -- I also realized when looking at the tests that guards in different groups can have the same name as well, so we'd probably need some way of namespacing for a helper like you mentioned. I'll give it some thought when I have some time! And thanks for the README update about the global groups :-)

Contributor

netzpirat commented Aug 14, 2011

Very nice. Thank you a lot.

Owner

rymai commented Aug 14, 2011

Awesome @ches! Thanks for this page in the wiki! :)

By the war, something like Guard.get_guard(name, group=nil) seems good to me. What do you think?

Contributor

ches commented Aug 14, 2011

Yup, that would be nice and straightforward. Perhaps using a hash instead of keyword arguments though could make it easier to allow filtering by other attributes in the future, in addition to group? Don't know if there's anything else interesting currently that people would want to use, but maybe you guys have some ideas in mind. Don't want to overdesign though either -- keep it simple, I'm just thinking out loud.

Owner

rymai commented Aug 14, 2011

I've created a specific issue for this: #121

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment