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

How to setup a pre-commit hook for Elixir / Phoenix projects #6

Open
nelsonic opened this issue Mar 21, 2017 · 13 comments
Open

How to setup a pre-commit hook for Elixir / Phoenix projects #6

nelsonic opened this issue Mar 21, 2017 · 13 comments

Comments

@nelsonic
Copy link
Member

we all know how to setup a pre-commit hook for our Node.js projects ... βœ…
but how is it done for elixir-lang / phoenix projects...? πŸ€”

@Jbarget
Copy link
Member

Jbarget commented Mar 29, 2017

this would be πŸ‘Œ

Did a little poking around, and this looks promising: https://mattvonrocketstein.github.io/heredoc/elixir-boilerplate.html

it uses this: http://pre-commit.com/

@Jbarget
Copy link
Member

Jbarget commented Mar 29, 2017

So i've got it working here πŸŽ‰ - but theres a difference from what we've been used to with the npm package, before the pre-commit hooks run, it stashes your changes that aren't staged.

This kind of makes sense as it should be running the tests on the code you're committing not all the changes you've made but as a side effect to stashing your unstaged changes, it creates a patch and is stored in /Users/{yourUsername}/.pre-commit/ and I don't necessarily like the idea of accumulating 1000s of patches over the course of the year.

Its better than nothing for now so will leave it in until we find a better solution

@Danwhy
Copy link
Member

Danwhy commented Mar 30, 2017

Is there a reason not to use https://www.npmjs.com/package/pre-commit in our Phoenix projects? Do we plan to move away from needing to include node altogether?

@nelsonic
Copy link
Member Author

@Danwhy good question. please elaborate.
were you thinking we can use the NPM pre-commit module which still creates the same git pre-commit hook and will ensure that our tests run on localhost before allowing a commit?
That's actually genius given that Phoenix projects require node.js by default ... let's try it!

@Danwhy
Copy link
Member

Danwhy commented Mar 30, 2017

Yeah, just installing it with npm and running as we would on a Node project, but with mix test instead of tape.

I've just tried it, and it seems to work fine. We just add our Phoenix test script as the test script in the package.json, and add that to pre-commit. If the tests (or coverage) fail, the commit fails:

screen shot 2017-03-30 at 17 46 53
screen shot 2017-03-30 at 17 46 46

@nelsonic
Copy link
Member Author

@Danwhy you sir, are a genius!! πŸ¦„
Could we add a section to the readme to this effect? (please/thanks!)

Danwhy added a commit to Danwhy/learn-pre-commit that referenced this issue Mar 30, 2017
@samhstn
Copy link
Member

samhstn commented Apr 4, 2017

Using https://www.npmjs.com/package/pre-commit is a great solution for Phoenix projects, but for the Elixir Libraries which we build without Phoenix, we won't have node and this solution doesn't really feel right.

@nelsonic
Copy link
Member Author

nelsonic commented Apr 5, 2017

@Shouston3 yeah, for Elixir-only (Non Phoenix) projects we do need an Elixir-specific solution ...
not urgent for the https://github.com/dwyl/amp-elixir project, but a very-nice-to-have. πŸ˜‰

@nelsonic
Copy link
Member Author

@Shouston3 did you discover how to add the pre-commit hook in Elixir projects without package.json ?
there's a "gap" on Hex.pm ... https://hex.pm/packages?_utf8=%E2%9C%93&search=pre-commit
image

@samhstn
Copy link
Member

samhstn commented Sep 11, 2017

No, I never looked into it, as we were happy with the solution @Danwhy suggested.

But, with Phoenix 1.3 changing it's folder structure so that node doesn't pollute the root, the solution may no longer suffice and an elixir pre-commit may be a much better solution.

Do you think it would be good to harness what has already been built in node (or something else), or look at building it entirely in elixir?

@nelsonic
Copy link
Member Author

nelsonic commented Sep 11, 2017

@Shouston3 good question ... good point on the Phoenix 1.3 re-org!
I see us writing 20+ "pure" elixir modules in the next year so we could consider not requiring node ...?

However how many people writing code in 2017 don't have Node.js installed on their localhost already?

The thing I want to avoid is falling into the temptation of using node to run the tests on CI ...

https://travis-ci.org/dwyl/tudo is running the CI tests without touching the package.json ...

I'm going to try adding a package.json file to https://github.com/dwyl/hits-elixir
and see how "dirty" I feel ...

@ZooeyMiller
Copy link

Haven't looked at it in depth yet, but this may be worth checking out: https://hex.pm/packages/pre_commit_hook

@ZooeyMiller
Copy link

The one I linked above isn't configurable, so @finnhodgkin and I made a hex module for configurable pre-commit in elixir!

Let us know any thoughts and please try it out! https://github.com/dwyl/elixir-pre-commit

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

No branches or pull requests

5 participants