Skip to main content

List of pre-commit hooks meant to format your source code.

Project description

Language Formatters Pre Commit Hooks

Github Actions CI Coverage PyPi version Supported Python versions

About

This package provides utilities for ensuring that your code is nicely formatted by using pre-commit hooks

List of pretty-format hooks

  • pretty-format-golang
  • pretty-format-ini
  • pretty-format-java
  • pretty-format-kotlin
  • pretty-format-rust
  • pretty-format-toml
  • pretty-format-yaml

⚠: the list above could be out-of-sync respect the exposed pre-commit hooks.

Please refer to .pre-commit-hooks.yaml for a more updated list.

Example Usage

Add a similar snippet into your .pre-commit-config.yaml file

- repo: https://github.com/macisamuele/language-formatters-pre-commit-hooks
  rev: ${LATEST_SHA_OR_VERSION}
  hooks:
  - id: pretty-format-java
    args: [--autofix]
  - id: pretty-format-kotlin
    args: [--autofix]
  - id: pretty-format-yaml
    args: [--autofix, --indent, '2']

Development

This tool uses tox as main tool to build virtual environments.

To get started will be enough to run make development.

If you have aactivator installed this step will happen automatically.

Contributing

Contributions are always welcome.

  1. Fork the project
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Add your modifications
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

FAQ

How to deal with multiple Java versions?

This might be relevant for pretty-format-java and pretty-format-kotlin hooks. The hooks depends on having java on version 11 or greater installed on your machine.

As you're working with compiled-to-JVM languages, we assume that you have java installed on your system. You might not have the minimum required version installed.

To work-around such scenario you have 2 approaches available:

  1. Have multiple java versions installed on your system and ensure that while running the pre-commit hooks JRE 11+ is available on your PATH variable (ie. PATH=${JRE_11_PATH}:${PATH} pre-commit run).

  2. Work around the issue by using docker. ⚠: This approach has been tested (at the time of writing) on Linux and MacOS.

    The latter approach should be preferred if you cannot install an additional JRE version on your system or if doing is unfeasible (e.g. on a CI system). Please note you need to have docker installed on your system.

    Add the following Dockerfile on your repository root (same directory where .pre-commit-config.yaml is stored)

    FROM python:3.7-alpine
    
    # Install JRE-11 as we will run pre-commit hooks that depends an Java 11+
    RUN apk add --no-cache openjdk11-jre
    
    ENV PRE_COMMIT_HOME /pre-commit-docker-cache
    ENV PRE_COMMIT_LANGUAGE_FORMATTERS_VERSION ${version of the library to install}
    
    RUN set -x \
        && pip install --no-cache-dir language-formatters-pre-commit-hooks==${PRE_COMMIT_LANGUAGE_FORMATTERS_VERSION} \
    
        # Run pre-commit-hook to ensure that jars are downloaded and stored in the docker image
        # Run the hooks that you're planning to run within docker.
        # This reduces premission issues as well has makes all the run fast as the lazy-dependencies are pre-fetched
        && pretty-format-java  \
    
        # Update permissions as hooks will be run as your host-system user (your username) but the image is built as root
        && chmod a+r ${PRE_COMMIT_HOME}/*
    

    and the following hook into your .pre-commit-config.yaml file

    repos:
    - repo: local
      hooks:
      - id: pretty-format-java-in-docker    # Useful to eventually SKIP pre-commit hooks
        name: pretty-format-java-in-docker  # This is required, put something sensible
        language: docker                    # Self explanatory
        entry: pretty-format-java           # Hook that you want to run in docker
        args: [...]                         # Arguments that would would pass to the hook (as if it was local)
        files: ^.*\.java$                   # File filter has to be added ;)
    

    By doing the following, the selected hook (pretty-format-java in the example) will be executed within the docker container.

    Side note: We're not embedding the Dockerfile in the repository as this is more a workaround to support whom cannot of installing a more recent Java version on the library-user system and as such we are not planning to fully support this other than giving possible solutions (Java 11+ was released in September, 2018).

License

language-formatters-pre-commit-hooks is licensed with Apache License version 2.0.

Changelog

2.1.0 (2021-05-28)

  • Bump KTlint to 0.40.0
  • Update GoogleJavaFormatter to 1.10.0

2.0.0 (2021-01-16)

  • Preserve comments in while formatting ini files. PR #45 - @Skylion007 thanks for your contribution
  • Preserve comments in while formatting toml files. PR #46 - @Skylion007 thanks for your contribution
  • ⚠ Drop Python2 support. PR #48
  • Update KTLint to 0.40.0

1.6.1 (2020-10-31)

  • Internal fix of downloaded files path. PR #43

1.6.0 (2020-10-24)

  • Update KTLint to 0.39.0
  • Update GoogleJavaFormatter to 1.9
  • Run pretty-format-java serially to prevent multiple-downloads of the same Java artifact. PR #23 - @ineiti thanks for your contribution
  • Internal update of download logic to reduce race coditions while download big artifacts from network. PR #24
  • Bump min pre-commit supported version. PR #27
  • Allow pretty-format-java to modify the Google Java Formatter to use (--google-java-formatter-version CLI argument). PR #30
  • Allow pretty-format-kotlin to modify the KTLint to use (--ktlint-version CLI argument). PR #30
  • Enhance security in commands execution (prevent shell-injection). PR #38

1.5.0 (2020-06-16)

  • Add --preserve-quotes argument into pretty-format-yaml. PR #16 - @vbisserie thanks for your contribution

1.4.2 (2020-06-09)

  • Update KTLint to 0.37.1

1.4.1 (2020-06-03)

  • Update KTLint to 0.37.0

1.4.0 (2020-05-20)

  • Improve handling of multi-document YAML files. PR #3 - @dan-cohn thanks for your contribution
  • pretty-format-java does default to Google style. Add --aosp argument for Android Open Source Project style. PR #8 - @ChenAndrew thanks for your contribution.
  • Update GoogleJavaFormatter to 1.8

1.3.2 (2020-01-25)

  • Definitive packaging fix

1.3.1 (2020-01-24)

  • Update Packaging informations

:warning: This version broke module retrieval (:disappointed:) while improving quality of PyPi uploaded information. You're recommended to use a more recent version of the library.

1.3.0 (2020-01-24)

  • Update KTLint to 0.36.0
  • Enhange pretty-format-yaml to deal with YAML files containing primitive types only - PR #1 - @dan-cohn thanks for your contribution

1.2.5 (2019-11-22)

  • Update KTLint to 0.35.0

1.2.4 (2019-07-19)

  • Update KTLint to 0.34.0 and fix KTLint GitHub link

1.2.3 (2019-02-14)

  • Update Google Java Formatter to 1.7 and KTlint to 0.30.0

1.2.2 (2018-11-20)

  • pretty-format-rust fails if cargo fmt fails

1.2.1 (2018-11-20)

  • no-diff release

1.2.0 (2018-11-20)

  • Bump KTlint to 0.29.0
  • Remove duplicated filenames from command execution

1.1.3 (2018-09-02)

  • Last fix to cargo invocations to use the environmentally defined toolchain

1.1.2 (2018-09-02)

  • Bump KTlint to 0.27.0

1.1.1 (2018-09-02)

  • Ensure that generated files end with a new line
  • Allow rust toolchain customization via RUST_TOOLCHAIN environment variable

1.1.0 (2018-07-29)

  • Add pretty formatters for INI, Rust and TOML files

1.0.1 (2018-07-20)

  • Improve detection of modified files from kotlin formatter

1.0.0 (2018-07-20)

  • Initial release: added pretty formatters for Golang, Java, Kotlin and YAML

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

Built Distribution

language_formatters_pre_commit_hooks-2.1.0-py2.py3-none-any.whl (22.3 kB view hashes)

Uploaded Python 2 Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page