From 425590156cb48013edf3df1de39c9133e344e0f0 Mon Sep 17 00:00:00 2001
From: AnimeGitB <AnimeGitB@bigblueball.in>
Date: Sat, 9 Jul 2022 14:20:25 +0930
Subject: [PATCH] Spotless code autoformatting

---
 .github/workflows/build.yml      |  1 -
 .github/workflows/build_lint.yml | 48 ++++++++++++++++++++++++++++++++
 build.gradle                     | 31 +++++++++++++++++++--
 3 files changed, 77 insertions(+), 3 deletions(-)
 create mode 100644 .github/workflows/build_lint.yml

diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml
index 89d029f1..c107c5ca 100644
--- a/.github/workflows/build.yml
+++ b/.github/workflows/build.yml
@@ -6,7 +6,6 @@ on:
       - "**.java"
     branches:
       - "stable"
-      - "development"
   pull_request:
     paths:
       - "**.java"
diff --git a/.github/workflows/build_lint.yml b/.github/workflows/build_lint.yml
new file mode 100644
index 00000000..e71099a7
--- /dev/null
+++ b/.github/workflows/build_lint.yml
@@ -0,0 +1,48 @@
+name: "Lint and Build"
+on:
+  workflow_dispatch: ~
+  push:
+    paths:
+      - "**.java"
+    branches:
+      - "development"
+jobs:
+  Build-Server-Jar:
+    runs-on: ubuntu-latest
+    steps:
+      - name: Checkout LintRatchet
+        uses: actions/checkout@v2
+        with:
+          ref: LintRatchet
+      - name: Checkout branch
+        uses: actions/checkout@v2
+      - name: Setup Java
+        uses: actions/setup-java@v3
+        with:
+          distribution: temurin
+          java-version: '17'
+      - name: Cache gradle files
+        uses: actions/cache@v2
+        with:
+          path: |
+            ~/.gradle/caches
+            ~/.gradle/wrapper
+            ./.gradle/loom-cache
+          key: ${{ runner.os }}-gradle-${{ hashFiles('*.gradle', 'gradle.properties', '**/*.accesswidener') }}
+          restore-keys: |
+            ${{ runner.os }}-gradle-
+      - name: Run Spotless
+        run: ./gradlew spotlessApply
+      - name: Run Gradle
+        run: ./gradlew && ./gradlew jar
+      - name: Upload build
+        uses: actions/upload-artifact@v3
+        with:
+          name: Grasscutter
+          path: grasscutter-*.jar
+      - name: Commit any Spotless changes
+        uses: EndBug/add-and-commit@v9
+        with:
+          add: '-u'
+          default_author: github_actions
+          message: 'Fix whitespace'
diff --git a/build.gradle b/build.gradle
index eacc6f64..520f27ab 100644
--- a/build.gradle
+++ b/build.gradle
@@ -34,6 +34,9 @@ plugins {
     // Maven
     id 'maven-publish'
     id 'signing'
+
+    // Spotless formatter
+    id "com.diffplug.spotless" version "6.8.0"
 }
 
 compileJava.options.encoding = "UTF-8"
@@ -93,8 +96,8 @@ dependencies {
 
     compileOnly 'org.projectlombok:lombok:1.18.24'
     annotationProcessor 'org.projectlombok:lombok:1.18.24'
-	testCompileOnly 'org.projectlombok:lombok:1.18.24'
-	testAnnotationProcessor 'org.projectlombok:lombok:1.18.24'
+    testCompileOnly 'org.projectlombok:lombok:1.18.24'
+    testAnnotationProcessor 'org.projectlombok:lombok:1.18.24'
 }
 
 configurations.all {
@@ -238,6 +241,30 @@ eclipse {
     }
 }
 
+spotless {
+  // optional: limit format enforcement to just the files changed by this feature branch
+  ratchetFrom 'LintRatchet'
+
+  format 'misc', {
+    // define the files to apply `misc` to
+    target '*.gradle', '*.md', '.gitignore'
+
+    // define the steps to apply to those files
+    trimTrailingWhitespace()
+    indentWithSpaces() // or spaces. Takes an integer argument if you don't like 4
+    endWithNewline()
+  }
+  java {
+    // don't need to set target, it is inferred from java
+    // define the steps to apply to those files
+    trimTrailingWhitespace()
+    indentWithSpaces() // or spaces. Takes an integer argument if you don't like 4
+    endWithNewline()
+    replaceRegex('Force one space between if/etc. and ( or {', '(?<=\\b(?:if|for|while|switch|try|else|catch|finally|synchronized)) *(?=[\\(\\{])', ' ')
+    replaceRegex('Force one space between ) and {', '\\) *\\{(?!\\})', ') {')
+  }
+}
+
 signing {
     if(!version.endsWith('-dev')) {
         sign publishing.publications.mavenJava
-- 
GitLab