freshshell/fresh

API to register hooks/callbacks

jasoncodes opened this issue · 7 comments

Defining a function (fresh_after_build) in freshrc means one has to have all post-build actions defined in the same spot. This is problematic for keeping related lines together within a freshrc file. This may also be a problem when we eventually support including other freshrc files.

I propose something like the following:

init_vim() {
  if which vim &> /dev/null && ! [ -d $HOME/.vim/bundle/vundle ]; then
    vim || [ -d $HOME/.vim/bundle/vundle ]
  fi
}
fresh-hook after-build init_vim

fresh can then keep an array of function names it needs to call and call them when needed. It could also opt to store the output of declare -f init_vim (the function definition) should the original function likely fall out of scope. If it’s not too hard, I think we should probably just go for the latter option up front. The API should probably record the file, line number and function name so it can output a useful message should this callback error.

I would like feedback on this idea (including whether these should be called hooks, callbacks, or something else) before I work on an implementation.

I like this idea. :)

Reading this Stack Overflow post makes it sound like we should use callback (I think). But I feel like we could go with either. I think I like the word callback better but I could easily be swayed.

Should we deprecate fresh_after_build for a few months when this is implemented? This also makes me think we should warn people if they're using an old version of fresh.

Is your proposed way of handling this going to work for future callbacks we may want to add? Eg. before build, etc.

Edit: Another Stack Overflow thread: http://stackoverflow.com/questions/467557/what-is-meant-by-the-term-hook-in-programming

I’m largely indifferent on the name so let’s go for callback.

We should absolutely deprecate fresh_after_build when this is implemented. Outputting a _note seems like a good move. We can leave that around for quite a while I think as it should only be a couple of lines of code.

I think we should probably consider changing the name of after-build to something less ambiguous as well. I would also like a callback for after build.new has been built but before it has been moved into place. fresh_after_build is called after the previous build has been replaced by the new build.new.The effective difference being that if it errors, the new build won’t replace the last one. Any thoughts on names for these two callbacks?

Lastly, I think we should pass in the appropriate directory name to both of these callbacks. This would make it easier for freshrc files to make changes at the build.new stage (before it is renamed) without hardcoding directory names.

Perhaps if something like this is documented:

  • build
  • after-build
  • move
  • after-move
  • symlink
  • after-symlink

Not bad. I think build is a good name for while it’s called build.new. I wonder if there’s a better name than move though.

Also, symlinking happens during the build process so that wouldn’t be a callback.

There is almost certainly something better than move. Is it kind of the finalising step? finalise?

Also, symlinking happens during the build process so that wouldn’t be a callback.

True, I was trying to think of other things that happen :(

I like finalize. Let’s go with that. With US spelling, I guess?

Yeah US speeling.