Skip to content

Commit

Permalink
Java Autoformatter (#728)
Browse files Browse the repository at this point in the history
* Create java-autoformat.yml

* Setup Java

* Run spotless on repository

* Adding spotless dependency during build

* Adding spotless dependency during build

* Give permissions to run spotless

* Syntax

* Adding Debug Tag

* Check for modified files

Also removed debug tag

* Automated Java code formatting changes

* Test command syntax

* Correctly Identify modified files

* Test autoformatter

* Test autoformatter

* Test autoformatter

* Debugging

* Debugging

* Change method for detecting changed files

* Try building before calling spotless

* Update java-autoformat.yml

* Update java-autoformat.yml

* Update java-autoformat.yml

* Purposely bad formatting

Purposely adding some bad formatting to see if the auto-formatter triggers

* Update build.gradle

Disabling Checkstyle in build so the auto-formatter can trigger

* Update java-autoformat.yml

Have auto-formatter trigger when more commits are added to a pull request

* Debugging

* Adding more awful formatting

* Update java-autoformat.yml

* Changing repo URL

* Going back to checkout v1

* Trying URL change

* Trying out using env

* Trying this now...?

* Introducing more horrible changes

* Spotless formatting using google format v1.19.2

* Manual formatting fix

* Automated Java code formatting changes

* Different format type

Trying to get it to pass checkstyle

* Disable checkstyle temporarily

* Automated Java code formatting changes

* Default google formatting style

* Automated Java code formatting changes

* Comments and reordering styles

* Adding extra newlines

* Automated Java code formatting changes

* Changing tabs from 2 to 4 spaces

Supposedly the only difference that aosp makes is the 2 spaces?
Hopefully it doesn't break anything else

* Remove solo } requirement and reactivate checkstyle

* Automated Java code formatting changes

* Update checkstyle.xml

Removed problematic LeftCurly and RightCurly requirements

* Changing back to tabWidth

* Add newline to test formatter + build

* Automated Java code formatting changes

* Trying some ChatGPT stuff

* Getting rid of problematic experimentation

---------

Co-authored-by: Bram van Heuveln <[email protected]>
Co-authored-by: Charles Tian <[email protected]>
Co-authored-by: charlestian23 <[email protected]>
  • Loading branch information
4 people authored Feb 21, 2024
1 parent 80cb273 commit 2190f7e
Show file tree
Hide file tree
Showing 527 changed files with 14,439 additions and 13,222 deletions.
38 changes: 38 additions & 0 deletions .github/workflows/java-autoformat.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
name: Java Code Auto Format
on: pull_request

jobs:
format:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v1
with:
ref: ${{ github.head_ref }}

- name: Set up JDK 11
uses: actions/setup-java@v1
with:
java-version: 11

- name: Grant execute permission for gradlew
run: chmod +x gradlew

- name: Build with Gradle
run: ./gradlew build -x test

- name: Run spotless
run: ./gradlew :spotlessApply

- name: Check for modified files
id: git-check
run: echo "modified=$(if git diff-index --quiet HEAD --; then echo "false"; else echo "true"; fi)" >> $GITHUB_OUTPUT

- name: Push changes
if: steps.git-check.outputs.modified == 'true'
run: |
git config --global user.name 'Bram van Heuveln'
git config --global user.email '[email protected]'
git remote set-url origin https://x-access-token:${{ secrets.GITHUB_TOKEN }}@github.com/${{ github.repository }}
git add .
git diff --cached --exit-code || git commit -am "Automated Java code formatting changes" && git push
30 changes: 30 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,43 @@ plugins {
id 'java'
id 'edu.sc.seis.launch4j' version '2.5.3'
id 'kr.motd.sphinx' version '2.10.0'
id 'com.diffplug.spotless' version '6.25.0'
}


version '2.0.0'

apply plugin: 'java'
apply plugin: 'application'

spotless{
enforceCheck false

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{
// Use the default importOrder configuration
importOrder()

// Cleanthat will refactor your code, but it may break your style: apply it before your formatter
cleanthat()

googleJavaFormat('1.19.2').aosp()

formatAnnotations()
}
}

apply plugin: 'checkstyle'

mainClassName = 'edu.rpi.legup.Legup'

sourceCompatibility = 11
Expand Down
6 changes: 0 additions & 6 deletions config/checkstyle/checkstyle.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,6 @@
<module name="Checker">
<module name="TreeWalker">
<property name="tabWidth" value="4"/>
<module name="LeftCurly"/>
<module name="RightCurly">
<property name="option" value="alone"/>
<property name="tokens"
value="LITERAL_IF, LITERAL_ELSE, LITERAL_TRY, LITERAL_CATCH, LITERAL_FINALLY, LITERAL_WHILE, LITERAL_DO, LITERAL_FOR, METHOD_DEF"/>
</module>
<module name="NeedBraces">
<property name="allowSingleLineStatement" value="true"/>
</module>
Expand Down
Empty file modified gradlew
100644 → 100755
Empty file.
2 changes: 1 addition & 1 deletion src/main/java/edu/rpi/legup/Legup.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ public static void main(String[] args) {
GameBoardFacade.getInstance();
GameBoardFacade.setupConfig();
}
}
}
3 changes: 1 addition & 2 deletions src/main/java/edu/rpi/legup/ai/Solver.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
package edu.rpi.legup.ai;

public class Solver {
}
public class Solver {}
22 changes: 10 additions & 12 deletions src/main/java/edu/rpi/legup/app/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,18 @@

import java.io.*;
import java.util.*;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class Config {
private final static Logger Logger = LogManager.getLogger(Config.class.getName());
private static final Logger Logger = LogManager.getLogger(Config.class.getName());

private Map<String, String> puzzles;
private Map<String, Boolean> fileCreationDisabledStatuses;
Expand All @@ -43,8 +40,8 @@ public List<String> getPuzzleClassNames() {
}

/**
* Returns a list of the names of the puzzles which can have puzzles created and edited
* within the proof editor.
* Returns a list of the names of the puzzles which can have puzzles created and edited within
* the proof editor.
*
* @return the aforementioned list of Strings
*/
Expand All @@ -59,8 +56,8 @@ public List<String> getFileCreationEnabledPuzzles() {
}

/**
* Converts the class name of the puzzles to their display names. Some examples of the conversion:
* convertClassNameToDisplayName("TreeTent") will return "Tree Tent"
* Converts the class name of the puzzles to their display names. Some examples of the
* conversion: convertClassNameToDisplayName("TreeTent") will return "Tree Tent"
* convertClassNameToDisplayName("Nurikabe") will return "Nurikabe"
*
* @param className the name of the class
Expand Down Expand Up @@ -140,15 +137,16 @@ private void loadConfig(InputStream stream) throws InvalidConfigException {
Element puzzle = (Element) puzzleNodes.item(i);
String name = puzzle.getAttribute("name");
String className = puzzle.getAttribute("qualifiedClassName");
boolean status = Boolean.parseBoolean(puzzle.getAttribute("fileCreationDisabled").toLowerCase());
boolean status =
Boolean.parseBoolean(
puzzle.getAttribute("fileCreationDisabled").toLowerCase());
Logger.debug("Class Name: " + className);
this.puzzles.put(name, className);
this.fileCreationDisabledStatuses.put(name, Boolean.valueOf(status));
}

}
catch (ParserConfigurationException | SAXException | IOException e) {
} catch (ParserConfigurationException | SAXException | IOException e) {
throw new InvalidConfigException(e.getMessage());
}
}
}
}
Loading

0 comments on commit 2190f7e

Please sign in to comment.