Code Monkey home page Code Monkey logo

pre-commit-hooks's Introduction

pre-commit-hooks

Pre-commit hooks for PCDS projects (https://pre-commit.com/)

To install pre-commit on your machine:

On Linux, use $ pip install pre-commit or conda install pre-commit -c conda-forge from your favorite python environment. On Mac, use $ brew install pre-commit, or follow the Linux instructions. On Windows, set up python either your favorite way or by using https://docs.conda.io/projects/conda/en/latest/user-guide/install/windows.html, and then follow the Linux instructions.

To install pre-commit hooks to a local repository:

If .pre-config-config.yaml does not already exist in the repository, copy the appropriate file from this repository to the top-level of your local repository, or add the folowing to an existing .pre-config-config.yaml file and commit the addition.

repos:
-   repo: https://github.com/pre-commit/pre-commit-hooks.git
    rev: v2.5.0
    hooks:
    -   id: no-commit-to-branch
    -   id: trailing-whitespace
        files: \.(TcPOU|TcDUT|TcGVL)$

-   repo: https://github.com/pcdshub/pre-commit-hooks.git
    rev: v1.7.0
    hooks:
    -   id: twincat-leading-tabs-remover
    -   id: twincat-lineids-remover
    -   id: twincat-xml-format
    -   id: check-fixed-library-versions
    -   id: no-product-version
    -   id: twincat-st-newline
    # Check if minimize id changes is selected in the plc project file.
    # See https://www.youtube.com/watch?v=KKpBtaYjfWo&t=935s why to do this.
    -   id: minimize-id-changes
    # Checks if TwinCAT versions match in different tsproj files, or if it matches the targeted one.
    -   id: check-twincat-versions
        # Possible optional arguments
        # --target-version: Set a version that you want the tsproj file to have
        # --fix: Fix the version numbers if a target version is set
        # --reason: Add a reason to the error message in case of a non-matching version.
        # --pinned: Require the TwinCAT version to be pinned. Apply pinning if combined with --fix.
        # --no-pinned: Require the TwinCAT version to not be pinned. Remove pinning if combined with --fix.
        args: [--target-version=3.1.4024.20, --pinned, --fix, --reason="This version has a crucial new feature"]
    # Optional, if you use pytmc to generate EPICS IOCs:
    # -   id: pytmc-pragma-linter

Once the file is there, run the following from inside your repository:

$ pre-commit install          # install for this repo based on the config
$ pre-commit run --all-files  # run on everything
$ pre-commit run              # run on staged
$ git commit -am "test"       # run pre-commit and - if successful - commit

Issues and client integration

If pre-commit is not an available command, you may need to look into platform-specific configuration. Generally, you'll need to be in a shell environment that has access to python and with pre-commit installed as directed in the above sections. Typical issues include a misconfigured PATH variable and not having python available. For specific help on integrating with various clients and on various operation systems, see the sections below. If you solve other client integration problems for your favorite workflow, please expand this section in a pull request.

Shell Integration on Windows

  • Make sure git is set up for normal shell use (a git installation option) if you want to use cmd or powershell
  • I have had success using the Anaconda Powershell Prompt and a conda environment with pre-commit installed

Git Bash Integration on Windows

  • Add the following to your ~/.bash_profile: alias python='winpty python', to allow python to run without hanging.
  • If using conda, run $ conda init bash, using the conda.exe in your ~/miniconda3/scripts folder. You may also want to set up your ~/.bash_profile to conda activate your pre-commit environment.
  • Restart your shell after doing the above.

TwinCAT Integrated Git on Windows

  • Someone needs to investigate this one. I don't have this running locally yet and am happy enough with the shells for now.

VSCode Integration on Windows

  • Someone needs to figure out how to get the git extension to work here.
  • If your shell integration works, you can connect to this in the integrated terminal by setting terminal.integrated.shellArgs.windows to the same arguments as used in the Anaconda Powershell prompt shortcut's properties.

pre-commit-hooks's People

Contributors

zllentz avatar klauer avatar zrylettc avatar aeu485 avatar rruiter87 avatar tacaswell avatar

Stargazers

PhilippLe avatar Mark Lazarides avatar Serhii Tetora avatar Roy Cohen avatar ้ฃž avatar  avatar Harriet O'Brien avatar Phong Pham avatar  avatar

Watchers

James Cloos avatar  avatar  avatar Mark Lazarides avatar

pre-commit-hooks's Issues

Idea: hook to check for mixed and/or pinned TwinCAT versions

We had a discussion here TcOpenGroup/TcOpen#731 about the mixed TwinCAT versions and pinned ones in the tsproj files. Would it be an idea to add another pre-commit to check for this? I can add it.

Something like check-twincat-versions --target-version 4024.22 --pinned

Different cases:

  1. In case of a single project, you can select if it should be pinned and add an optional target version.
  2. For two or more, it can check if all target versions match the targeted one and if they are pinned. If no target version is given, it checks whether more than one version is used.

Optionally, I can also add an --fix option, that would fix --pinned and if a --target-version is given.

Investigate continuous integration

https://pre-commit.com/#usage-in-continuous-integration

My original thought, not necessarily how we should do it, given the above documentation:

matrix:
  include:
    name: pre-commit checks
    python: 3.7
    env:
      PRE_COMMIT=1


script:
    - |
      if [ "$PRE_COMMIT" ]; then
        pip install pre-commit
        pre-commit install
        pre-commit run --all-files  # or whatever it is
        exit $?  #  exit with pre-commits return code
      fi

Likely this should be part of pcds-ci-helpers. x-ref an issue there: pcdshub/pcds-ci-helpers#12

TwinCAT XAE Shell Built-in Git Integration

Investigate the built-in git from the TwinCAT XAE shell and figure out how to configure it to run the pre-commit hooks properly

I started investigating this a little bit using the TwinCAT installed on the programming node. In the TwinCAT docs, the interface appears to have more configuration options than mine does in Tools > Options > Source Control, where I only have the option of changing my source control plug-in. I assume this means that the git plugin doesn't have any configuration options.

I peeked at the pre-commit hook script generated on Windows and the shebang looks like:
#!/usr/bin/env python.exe
I'm not sure if this leads to any clues, or if modifying this default script will be the path forward.

As an aside, there will need to be extra effort to get this working at all on the programming node due to the airgap

TwinCAT POU Pre/Post Newlines Hook

I'm considering making a pre-commit hook that inserts newlines at the start and end of TwinCAT POUs (and some of the other files?). When a TwinCAT POU starts and ends with a newline, it is much easier to review in github. Snippit from a current PR of mine that does not do this:

    <Implementation>
      <ST><![CDATA[fbNCErrorFFO(
    stMotionStage := stMotionStage,
    fbFFHWO := fbFFHWO,
    bReset := bReset,
    sName := sName,
    bTripped => bTripped);]]></ST>
    </Implementation>

With pre and post newlines:

    <Implementation>
      <ST><![CDATA[
fbNCErrorFFO(
    stMotionStage := stMotionStage,
    fbFFHWO := fbFFHWO,
    bReset := bReset,
    sName := sName,
    bTripped => bTripped);
]]></ST>
    </Implementation>

With two pre and two post newlines:

    <Implementation>
      <ST><![CDATA[

fbNCErrorFFO(
    stMotionStage := stMotionStage,
    fbFFHWO := fbFFHWO,
    bReset := bReset,
    sName := sName,
    bTripped => bTripped);

]]></ST>
    </Implementation>

Decide on standard configuration for Python

Current test one is:

$ cat .pre-commit-config.yaml
# See https://pre-commit.com for more information
# See https://pre-commit.com/hooks.html for more hooks
repos:
-   repo: https://github.com/pre-commit/pre-commit-hooks
    rev: v2.5.0
    hooks:
    -   id: trailing-whitespace
    -   id: end-of-file-fixer

-   repo: https://gitlab.com/pycqa/flake8
    rev: 3.7.9
    hooks:
    -   id: flake8

Add:

  • Import sorting via isort?

Maybe add doc8?

Thinking doc8 could be useful for checking our static .rst files. Thoughts?

-   repo: https://github.com/PyCQA/doc8
    rev: 0.8.1rc3
    hooks:
    - id: doc8

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.