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 add support for a language? #926

Closed
katrinleinweber opened this issue Jan 29, 2019 · 9 comments
Closed

How to add support for a language? #926

katrinleinweber opened this issue Jan 29, 2019 · 9 comments
Labels

Comments

@katrinleinweber
Copy link

Hi! Please apologise the definitely honest, but probably noob-y question.

I skimmed through pre-commit.com/#contributing, CONTRIBUTING.md and #564 looking for info on adding support for a new language.

Having found languages/all.py, as well as the PRs 812 & 120, I'm left wondering what level of Python knowledge would be needed? Merely Python modules (like for data analysis), or system-level integration & experience with git-interfacing modules?

Thanks for any hints :-)

PS: Specifically, R would be of interest (see r-lib/styler#467) because of its use in academia.

@asottile
Copy link
Member

Yep! you've found the right files to get started. I'd be happy to help you through the python parts of this if you're interested in getting started! You shouldn't need to deal with git at all as the framework bits handle that. The parts for adding a "language" are just installing $your-language packages and running the executables that get installed.

There's ~roughly a couple flavors of "languages" right now -- either those which pre-commit will bootstrap an entire environment for you (node / ruby), or where pre-commit will assume some amount of system installation (python, because well, you need python to run pre-commit) or where pre-commit assumes you've installed some sort of system tooling (go / rust / docker / etc.).

Depending on how the R ecosystem works (I'm not at all familiar with it) you'd pick to either set up a full R runtime or to depend on R already being installed at the system.

For the apis you have to implement for a language here's what they would do (for instance for R):

ENVIRONMENT_DIR

a short string which will be used for the prefix of where packages will be installed. Probably r_env or something like that. (For python for instance, we use py_env and then packages are referenced from py_env/bin/...)

get_default_version

probably don't need to implement this for a first pass -- right now this is only implemented by the python language which guesses what interpreter version to try and use for hooks.

healthy

probably don't need to implement this for a first pass -- right now this is only implemented by the python language which makes sure that a virtual environment is still dll-linked properly

install_environment

this is the tricky part and where the smart bits come in. probably need to do the following things:

  • install an R runtime (optional: you could just depend on R already being installed globally)
  • install the current repository into the ENVIRONMENT_DIR (for python this is pip install . for instance)
  • install additional_dependencies (also optional, but often useful for plugin-like things)

run_hook

This is usually the easiest to implement, most of them look the same as the python hook implementation:

def run_hook(hook, file_args):
with in_env(hook.prefix, hook.language_version):
return helpers.run_xargs(hook, helpers.to_cmd(hook), file_args)

recent PRs

Probably the best most-recent PR which added a language is this one: #751

Then the last bit is adding a few tests for that -- I can help you out with that :)

@katrinleinweber katrinleinweber changed the title How to support for a language? How to add support for a language? Jan 29, 2019
@lorenzwalthert
Copy link
Contributor

Depending on how the R ecosystem works (I'm not at all familiar with it) you'd pick to either set up a full R runtime or to depend on R already being installed at the system.

I think we can for simplicity depend on the global installation of R.

@asottile
Copy link
Member

Sounds good, is there a mechanism for local package installation in R?

@lorenzwalthert
Copy link
Contributor

Within the R console (again), you can install.packages("styler") to satisfy all requirements needed. So in the console, when assuming R is installed, you can run

$ Rscript -e "install.packages('styler', repos='http://cran.rstudio.com')"

@asottile
Copy link
Member

asottile commented Jun 9, 2019

I've decided to clean this up and add it to the docs #1056 -- thanks for the issue it definitely helped me collect my thoughts on this :)

@asottile asottile closed this as completed Jun 9, 2019
@katrinleinweber
Copy link
Author

Awesome, thank you :-)

@scottgigante-immunai
Copy link

@katrinleinweber did you ever work out how to write a pre-commit hook for styler? I'm interested in doing the same but don't want to reinvent the wheel

@lorenzwalthert
Copy link
Contributor

@scottgigante-immunai you can use the {styler} hook from https://github.com/lorenzwalthert/precommit, hopefully soon with pre-commit.ci support :D

@scottgigante-immunai
Copy link

Amazing! Thank you!

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

No branches or pull requests

4 participants