diff --git a/.github/workflows/spotless.yml b/.github/workflows/spotless.yml new file mode 100644 index 00000000..f1c332cd --- /dev/null +++ b/.github/workflows/spotless.yml @@ -0,0 +1,20 @@ +on: [push] +# A workflow run is made up of one or more jobs that can run sequentially or in parallel +jobs: + spotless: + # The type of runner that the job will run on + runs-on: ubuntu-latest + # Steps represent a sequence of tasks that will be executed as part of the job + steps: + # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it + - uses: actions/checkout@v4 + with: + fetch-depth: 0 + - uses: actions/setup-java@v4 + with: + distribution: 'zulu' + java-version: 17 + # Grant execute permission for gradlew + - name: Grant execute permission for gradlew + run: chmod +x gradlew + - run: ./gradlew spotlessCheck diff --git a/build.gradle b/build.gradle index 5b29be23..a4b93fed 100644 --- a/build.gradle +++ b/build.gradle @@ -1,6 +1,7 @@ plugins { id "java" id "edu.wpi.first.GradleRIO" version "2024.1.1" + id "com.diffplug.spotless" version "6.23.3" } java { @@ -84,7 +85,11 @@ wpi.sim.addDriverstation() // in order to make them all available at runtime. Also adding the manifest so WPILib // knows where to look for our Robot Class. jar { - from { configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) } } + from { + configurations.runtimeClasspath.collect { + it.isDirectory() ? it : zipTree(it) + } + } from sourceSets.main.allSource manifest edu.wpi.first.gradlerio.GradleRIOPlugin.javaManifest(ROBOT_MAIN_CLASS) duplicatesStrategy = DuplicatesStrategy.INCLUDE @@ -99,3 +104,59 @@ wpi.java.configureTestTasks(test) tasks.withType(JavaCompile) { options.compilerArgs.add '-XDstringConcat=inline' } + +// START: Setup spotless + +// Configure the spotless settings for the various file types (java, gradle, xml, etc) +spotless { + java { + target fileTree('.') { + include '**/*.java' + exclude '**/build/**', '**/build-*/**' + } + toggleOffOn() + googleJavaFormat() + removeUnusedImports() + trimTrailingWhitespace() + endWithNewline() + } + groovyGradle { + target fileTree('.') { + include '**/*.gradle' + exclude '**/build/**', '**/build-*/**' + } + greclipse() + indentWithSpaces(4) + trimTrailingWhitespace() + endWithNewline() + } + format 'xml', { + target fileTree('.') { + include '**/*.xml' + exclude '**/build/**', '**/build-*/**' + } + eclipseWtp('xml') + trimTrailingWhitespace() + indentWithSpaces(2) + endWithNewline() + } + format 'misc', { + target fileTree('.') { + include '**/*.md', '**/.gitignore' + exclude '**/build/**', '**/build-*/**' + } + trimTrailingWhitespace() + indentWithSpaces(2) + endWithNewline() + } +} + +// Disable automatics spotless execution during a build +// By default, spotless is configured to run during a build. This is generally undesirable because we don't want formatting errors to cause build failures and hinder developent, debugging, and testing. We generally want to run and apply the spotless changes prior to committing and/or pushing the code to a source code repository (e.g. git) +if (!('spotlessCheck' in gradle.startParameter.taskNames)) { + spotlessJavaCheck.enabled = false + spotlessGroovyGradleCheck.enabled = false + spotlessXmlCheck.enabled = false + spotlessMiscCheck.enabled = false +} +// END: Setup spotless