Skip to content
New issue

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

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add pre-execution and post-execution "hooks" #542

Closed
jakub-g opened this issue Nov 22, 2012 · 10 comments
Closed

Add pre-execution and post-execution "hooks" #542

jakub-g opened this issue Nov 22, 2012 · 10 comments

Comments

@jakub-g
Copy link

jakub-g commented Nov 22, 2012

Hello,

I have an idea of adding some way to define hooks that will be executed before Grunt starts and after it finishes.

Now I can achieve this by creating a task, say gruntBeforeStart and gruntAfterEnd and then use grunt.registerTask('fooHooked', 'gruntBeforeStart foo gruntAfterEnd') but you can see it's not very handy and doesn't scale.

I can think of adding a "magic" tasks -- empty by default -- which will be always executed, regardless of the way Grunt is invoked. The main rationale is to invoke Grunt with any set of tasks I like from the command line, and have the hooks take place as well -- without the need to list them explicitly.

Usage example: timer to execute the whole build time, number of files processed, whatever.

Looking forward to your comments.

@chchrist
Copy link

It will be really useful to have hooks. I am trying to run some code using data I collected in a multitask after the multitask finishes. Right now there is no easy way

@cowboy
Copy link
Member

cowboy commented Dec 12, 2012

We've talked about adding setup/teardown methods for multi tasks, this is most likely something we'll address in 0.5.

@chchrist
Copy link

Are those going to work like say, jasmine specs beforeEach and afterEach() ?

@jakub-g
Copy link
Author

jakub-g commented Dec 13, 2012

@cowboy setup/teardown for multi tasks are also a neat idea. However I still think that "global" hooks before/after all of the targets of a multitask and/or the whole Grunt execution will be useful.

Edge use case : let's say I run some task with targets out of order: baz foo:t1 bar foo:t2 foo:t3 and I want to display some collective stats from all targets of foo (i.e. t1 and t2, t3) at the end (only once, not after each target). It will be good if it worked out of the box also when you run Grunt and pass the tasks & targets from the command line like I wrote.

@chchrist
Copy link

I agree and to add to that it would be cool to be able to pass data to the next task

@cowboy
Copy link
Member

cowboy commented Dec 13, 2012

@jakub-g in your example, who defines the gruntAfterEnd behavior for the foo task? You, in the Gruntfile, or the task author in the task itself?

@cowboy
Copy link
Member

cowboy commented Dec 13, 2012

@chchrist what data would you pass? Would you pass it to the next multi task target, or the next completely separate task? What's your use case?

@chchrist
Copy link

@cowboy To the next completely separate task. That way if for example did in a previous task grunt.file.recurse and I need the same file list in the next task, I don't need to iterate again. To be more specific. I have developed a task that finds if your app has missing jasmine spec files in order to prevent code to be untested. Then I wanted to get that list, create the missing specs based on a template and populate my Specrunner.html. It is realy hard now to do that with a multitask.

@tkellen
Copy link
Member

tkellen commented Dec 31, 2012

In 0.5 we're going to be emitting events to facilitate this sort of thing. If you want to see the direction we're headed, check out:
http://github.com/tkellen/node-task

If you need to persist data across tasks, check out our recommendation (under Storing task files) here:
https://github.com/gruntjs/grunt/wiki/Creating-plugins

@kingback
Copy link

try https://github.com/kingback/grunt-task-hooker

require('grunt-task-hooker')(grunt);

grunt.hookTask('build', 'before build', ['clean:dist']);
grunt.hookTask('build', 'before build', ['clean:dist'], true);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants