mirror of
https://github.com/SquidDev-CC/CC-Tweaked
synced 2025-01-08 08:20:29 +00:00
Add a pre commit hook to lint code
This uses pre-commit [1] to check patches are well formed and run several linters on them. We currently do some boring things (check files are syntactically valid) as well as some project-specific ones: - Run illuaminate on the Lua files - Run checkstyle on Java [1]: https://pre-commit.com/
This commit is contained in:
parent
74dae4ec17
commit
eb2d617ed8
@ -16,6 +16,3 @@ indent_size = 2
|
|||||||
|
|
||||||
[*.yml]
|
[*.yml]
|
||||||
indent_size = 2
|
indent_size = 2
|
||||||
|
|
||||||
[*.properties]
|
|
||||||
insert_final_newline = false
|
|
||||||
|
30
.github/workflows/main-ci.yml
vendored
30
.github/workflows/main-ci.yml
vendored
@ -23,10 +23,15 @@ jobs:
|
|||||||
restore-keys: |
|
restore-keys: |
|
||||||
${{ runner.os }}-gradle-
|
${{ runner.os }}-gradle-
|
||||||
|
|
||||||
|
- name: Disable Gradle daemon
|
||||||
|
run: |
|
||||||
|
mkdir -p ~/.gradle
|
||||||
|
echo "org.gradle.daemon=false" >> ~/.gradle/gradle.properties
|
||||||
|
|
||||||
- name: Build with Gradle
|
- name: Build with Gradle
|
||||||
run: |
|
run: |
|
||||||
./gradlew assemble --no-daemon || ./gradlew assemble --no-daemon
|
./gradlew assemble || ./gradlew assemble
|
||||||
./gradlew downloadAssets --no-daemon || ./gradlew downloadAssets --no-daemon
|
./gradlew downloadAssets || ./gradlew downloadAssets
|
||||||
./gradlew build
|
./gradlew build
|
||||||
|
|
||||||
- name: Upload Jar
|
- name: Upload Jar
|
||||||
@ -39,15 +44,14 @@ jobs:
|
|||||||
run: bash <(curl -s https://codecov.io/bash)
|
run: bash <(curl -s https://codecov.io/bash)
|
||||||
continue-on-error: true
|
continue-on-error: true
|
||||||
|
|
||||||
- name: Generate Java documentation stubs
|
- name: Cache pre-commit
|
||||||
run: ./gradlew luaJavadoc --no-daemon
|
uses: actions/cache@v2
|
||||||
|
with:
|
||||||
- name: Lint Lua code
|
path: ~/.cache/pre-commit
|
||||||
|
key: ${{ runner.os }}-pre-commit-${{ hashFiles('config/pre-commit/config.yml') }}
|
||||||
|
restore-keys: |
|
||||||
|
${{ runner.os }}-pre-commit-
|
||||||
|
- name: Run linters
|
||||||
run: |
|
run: |
|
||||||
test -d bin || mkdir bin
|
pip install pre-commit
|
||||||
test -f bin/illuaminate || wget -q -Obin/illuaminate https://squiddev.cc/illuaminate/linux-x86-64/illuaminate
|
pre-commit run --config config/pre-commit/config.yml --show-diff-on-failure --all --color=always
|
||||||
chmod +x bin/illuaminate
|
|
||||||
bin/illuaminate lint
|
|
||||||
|
|
||||||
- name: Check whitespace
|
|
||||||
run: python3 tools/check-lines.py
|
|
||||||
|
48
config/pre-commit/config.yml
Normal file
48
config/pre-commit/config.yml
Normal file
@ -0,0 +1,48 @@
|
|||||||
|
# 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: v3.2.0
|
||||||
|
hooks:
|
||||||
|
- id: trailing-whitespace
|
||||||
|
- id: end-of-file-fixer
|
||||||
|
- id: check-merge-conflict
|
||||||
|
|
||||||
|
# Quick syntax checkers
|
||||||
|
- id: check-xml
|
||||||
|
- id: check-yaml
|
||||||
|
- id: check-toml
|
||||||
|
- id: check-json
|
||||||
|
exclude: "tsconfig\\.json$"
|
||||||
|
|
||||||
|
- repo: https://github.com/editorconfig-checker/editorconfig-checker.python
|
||||||
|
rev: 2.3.5
|
||||||
|
hooks:
|
||||||
|
- id: editorconfig-checker
|
||||||
|
args: ['-disable-indentation']
|
||||||
|
exclude: "^(.*\\.(bat)|LICENSE)$"
|
||||||
|
|
||||||
|
- repo: local
|
||||||
|
hooks:
|
||||||
|
- id: checkstyle
|
||||||
|
name: Check Java codestyle
|
||||||
|
files: ".*\\.java$"
|
||||||
|
language: system
|
||||||
|
entry: ./gradlew checkstyleMain checkstyleTest
|
||||||
|
pass_filenames: false
|
||||||
|
require_serial: true
|
||||||
|
- id: illuaminate
|
||||||
|
name: Check Lua code
|
||||||
|
files: ".*\\.(lua|java|md)"
|
||||||
|
language: script
|
||||||
|
entry: config/pre-commit/illuaminate-lint.sh
|
||||||
|
pass_filenames: false
|
||||||
|
require_serial: true
|
||||||
|
|
||||||
|
exclude: |
|
||||||
|
(?x)^(
|
||||||
|
src/generated|
|
||||||
|
src/test/resources/test-rom/data/json-parsing/|
|
||||||
|
src/test/server-files/|
|
||||||
|
config/idea/
|
||||||
|
)
|
9
config/pre-commit/illuaminate-lint.sh
Executable file
9
config/pre-commit/illuaminate-lint.sh
Executable file
@ -0,0 +1,9 @@
|
|||||||
|
#!/usr/bin/env sh
|
||||||
|
set -e
|
||||||
|
|
||||||
|
test -d bin || mkdir bin
|
||||||
|
test -f bin/illuaminate || curl -s -obin/illuaminate https://squiddev.cc/illuaminate/linux-x86-64/illuaminate
|
||||||
|
chmod +x bin/illuaminate
|
||||||
|
|
||||||
|
./gradlew luaJavadoc
|
||||||
|
bin/illuaminate lint
|
@ -1,45 +0,0 @@
|
|||||||
import pathlib, sys
|
|
||||||
|
|
||||||
problems = False
|
|
||||||
|
|
||||||
# Skip images and files without extensions
|
|
||||||
exclude = [
|
|
||||||
"*.png",
|
|
||||||
"**/data/json-parsing/*.json",
|
|
||||||
"**/computers/ids.json",
|
|
||||||
]
|
|
||||||
|
|
||||||
for path in pathlib.Path("src").glob("**/*"):
|
|
||||||
# Ideally we'd use generated as a glob, but .match("generated/**/*.json") doesn't work!
|
|
||||||
if path.is_dir() or path.suffix == "" or any(path.match(x) for x in exclude) or path.parts[1] == "generated":
|
|
||||||
continue
|
|
||||||
|
|
||||||
with path.open(encoding="utf-8") as file:
|
|
||||||
has_dos, has_trailing, first, count = False, False, 0, True
|
|
||||||
for i, line in enumerate(file):
|
|
||||||
if first:
|
|
||||||
first = False
|
|
||||||
if line.strip() == "":
|
|
||||||
print("%s has empty first line" % path)
|
|
||||||
|
|
||||||
if len(line) >= 2 and line[-2] == "\r" and line[-1] == "\n" and not has_dos:
|
|
||||||
print("%s has contains '\\r\\n' on line %d" % (path, i + 1))
|
|
||||||
problems = has_dos = True
|
|
||||||
|
|
||||||
if len(line) >= 2 and line[-2] in " \t" and line[-1] == "\n" and not has_trailing:
|
|
||||||
print("%s has trailing whitespace on line %d" % (path, i + 1))
|
|
||||||
problems = has_trailing = True
|
|
||||||
|
|
||||||
if len(line) == 0 or line[-1] != "\n":
|
|
||||||
count = 0
|
|
||||||
elif line.strip() == "":
|
|
||||||
count += 1
|
|
||||||
else:
|
|
||||||
count = 1
|
|
||||||
|
|
||||||
if count != 1:
|
|
||||||
print("%s should have 1 trailing lines, but has %d" % (path, count))
|
|
||||||
problems = True
|
|
||||||
|
|
||||||
if problems:
|
|
||||||
sys.exit(1)
|
|
Loading…
Reference in New Issue
Block a user