jguttman94 / pre-commit-gradle

Some custom gradle hooks for pre-commit.
MIT License
16 stars 7 forks source link

Add support for gradlew #2

Closed jamesmirvine closed 4 years ago

jamesmirvine commented 4 years ago

A prominent feature of gradle is the wrapper:

Each Wrapper is tied to a specific version of Gradle, so when you first run one of the commands above for a given Gradle version, it will download the corresponding Gradle distribution and use it to execute the build.

Not only does this mean that you don’t have to manually install Gradle yourself, but you are also sure to use the version of Gradle that the build is designed for. This makes your historical builds more reliable

Currently, there is no support for the wrapper, and the commit fails if gradle isn't installed on the path. Can this be added so that projects can continue to be self contained?

.pre-commit-config.yaml

repos:
  - repo: https://github.com/pre-commit/pre-commit-hooks
    rev: v2.4.0
    hooks:
      - id: trailing-whitespace
      - id: end-of-file-fixer
      - id: check-json
      - id: check-yaml
      - id: check-added-large-files
  - repo: https://github.com/antonbabenko/pre-commit-terraform
    rev: v1.19.0
    hooks:
      - id: terraform_fmt
  - repo: https://github.com/jguttman94/pre-commit-gradle
    rev: v0.1.0
    hooks:
      - id: gradle-check
      - id: gradle-spotless

git commit output:

[WARNING] Unstaged files detected.
[INFO] Stashing unstaged files to C:\Users\jirvine/.cache\pre-commit\patch1579904329.
Trim Trailing Whitespace.................................................Passed
Fix End of Files.........................................................Failed
- hook id: end-of-file-fixer
- exit code: 1
- files were modified by this hook

Fixing build.gradle

Check JSON...........................................(no files to check)Skipped
Check Yaml...............................................................Passed
Check for added large files..............................................Passed
Terraform fmt........................................(no files to check)Skipped
gradle check.............................................................Failed
- hook id: gradle-check
- exit code: 1

Traceback (most recent call last):
  File "c:\python37\Lib\runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "c:\python37\Lib\runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "C:\Users\jirvine\.cache\pre-commit\repoju3qot9q\py_env-python3.7\Scripts\gradle-check.exe\__main__.py", line 7, in <module>
  File "c:\users\jirvine\.cache\pre-commit\repoju3qot9q\py_env-python3.7\lib\site-packages\pre_commit_hooks\gradle_check.py", line 24, in main
    cmd_output(cmd, 'check')
  File "c:\users\jirvine\.cache\pre-commit\repoju3qot9q\py_env-python3.7\lib\site-packages\pre_commit_hooks\util.py", line 29, in cmd_output
    raise CalledProcessError(cmd, retcode, proc.returncode, stdout, stderr)
pre_commit_hooks.util.CalledProcessError: (('gradle', 'check'), 0, 1, '', b"'gradle' is not recognized as an internal or external command,\r\noperable program or batch file.\r\n")

gradle spotless..........................................................Failed
- hook id: gradle-spotless
- exit code: 1

Traceback (most recent call last):
  File "c:\python37\Lib\runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "c:\python37\Lib\runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "C:\Users\jirvine\.cache\pre-commit\repoju3qot9q\py_env-python3.7\Scripts\gradle-spotless.exe\__main__.py", line 7, in <module>
  File "c:\users\jirvine\.cache\pre-commit\repoju3qot9q\py_env-python3.7\lib\site-packages\pre_commit_hooks\gradle_spotless.py", line 25, in main
    cmd_output(cmd, 'spotlessJavaCheck', 'spotlessJavaApply')
  File "c:\users\jirvine\.cache\pre-commit\repoju3qot9q\py_env-python3.7\lib\site-packages\pre_commit_hooks\util.py", line 29, in cmd_output
    raise CalledProcessError(cmd, retcode, proc.returncode, stdout, stderr)
pre_commit_hooks.util.CalledProcessError: (('gradle', 'spotlessJavaCheck', 'spotlessJavaApply'), 0, 1, '', b"'gradle' is not recognized as an internal or external command,\r\noperable program or batch file.\r\n")

[WARNING] Stashed changes conflicted with hook auto-fixes... Rolling back fixes...
[INFO] Restored changes from C:\Users\jirvine/.cache\pre-commit\patch1579904329.
jamesmirvine commented 4 years ago

Went and reread the docs. I totally missed the wrapper support. Closing this.

jguttman94 commented 4 years ago

I'll add more graceful termination if gradle is not installed.