Browse Source

feat: Add pre-commit hook for linting and formatting (#1132)

* feat: Add pre-commit hook for linting and formatting

* format with prettier
kylo252 3 years ago
parent
commit
988c74ec56
4 changed files with 57 additions and 19 deletions
  1. 9 13
      .github/workflows/lint.yaml
  2. 30 0
      .pre-commit-config.yaml
  3. 12 2
      CONTRIBUTING.md
  4. 6 4
      utils/bin/jdtls

+ 9 - 13
.github/workflows/lint.yaml

@@ -22,17 +22,13 @@ jobs:
       
       - name: Run luacheck
         run: luacheck *.lua lua/
-    
-  shell-linter:
-    name: "Linting with shellcheck"
-    runs-on: ubuntu-20.04
+
+  shellcheck:
+    name: Shellcheck
+    runs-on: ubuntu-latest
     steps:
-      - uses: actions/checkout@v2
-      
-      - name: Use shellcheck
-        run: sudo apt-get install shellcheck
-      
-      - name: Run shellcheck
-        run:  |
-          pwd
-          shellcheck $(find . -name "*.sh")
+    - uses: actions/checkout@v2
+    - name: Run ShellCheck
+      uses: ludeeus/action-shellcheck@master
+      with:
+       scandir: './utils'

+ 30 - 0
.pre-commit-config.yaml

@@ -0,0 +1,30 @@
+repos:
+  - repo: local
+    hooks:
+      - id: shfmt
+        name: shfmt
+        minimum_pre_commit_version: 2.4.0
+        language: golang
+        additional_dependencies: [mvdan.cc/sh/v3/cmd/shfmt@v3.2.2]
+        entry: shfmt
+        args: [-i=2, -ci, -w]
+        types: [shell]
+      - id: shellcheck
+        name: shellcheck
+        language: system
+        types: [shell]
+        entry: bash
+        args:
+          [-c, "shfmt -f $(git rev-parse --show-toplevel) | xargs shellcheck"]
+      - id: stylua
+        name: StyLua
+        language: rust
+        entry: stylua
+        types: [lua]
+        args: [.]
+      - id: luacheck
+        name: luacheck
+        language: system
+        entry: luacheck
+        types: [lua]
+        args: [.]

+ 12 - 2
CONTRIBUTING.md

@@ -17,9 +17,19 @@ One of the best ways to begin contributing in a meaningful way is by helping fin
 
 ## Setting up development tools
 
-1. Install [stylua](https://github.com/johnnymorganz/stylua#installation)
+### For editing Lua files
 
-2. Install [shfmt](https://github.com/mvdan/sh#shfmt)
+1. Formatter: [stylua](https://github.com/johnnymorganz/stylua#installation).
+2. Linter:  [luacheck](https://github.com/luarocks/luacheck).
+
+### For editing shell scripts
+
+1. Formatter: [shfmt](https://github.com/mvdan/sh#shfmt).
+2. Linter: [shellcheck](https://github.com/koalaman/shellcheck).
+
+### (Optional)
+
+Install [pre-commit](https://github.com/pre-commit/pre-commit) which will run all linters and formatters for you as a pre-commit-hook.
 
 ## Some Guidelines
 

+ 6 - 4
utils/bin/jdtls

@@ -1,4 +1,5 @@
 #!/usr/bin/env bash
+# shellcheck disable=SC2116
 
 # NOTE:
 # This doesn't work as is on Windows. You'll need to create an equivalent `.bat` file instead
@@ -7,7 +8,8 @@
 # If you're not using Linux you'll need to adjust the `-configuration` option
 # to point to the `config_mac' or `config_win` folders depending on your system.
 
-case Darwin in
+OS="$(uname -s)"
+case "$OS" in
   Linux)
     CONFIG="$HOME/.local/share/nvim/lspinstall/java/config_linux"
     ;;
@@ -32,7 +34,7 @@ location of your Java installation."
   fi
 else
   JAVACMD="java"
-  which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
+  command -v java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
 
 Please set the JAVA_HOME variable in your environment to match the
 location of your Java installation."
@@ -46,10 +48,10 @@ GRADLE_HOME=$HOME/gradle "$JAVACMD" \
   -Declipse.product=org.eclipse.jdt.ls.core.product \
   -Dlog.protocol=true \
   -Dlog.level=ALL \
-  -javaagent:$HOME/.local/share/nvim/lspinstall/java/lombok.jar \
+  -javaagent:"$HOME/.local/share/nvim/lspinstall/java/lombok.jar" \
   -Xms1g \
   -Xmx2G \
-  -jar $(echo "$JAR") \
+  -jar "$(echo "$JAR")" \
   -configuration "$CONFIG" \
   -data "${1:-$HOME/workspace}" \
   --add-modules=ALL-SYSTEM \