buildscript {
  repositories repos
  dependencies {
    classpath "com.diffplug.spotless:spotless-plugin-gradle:6.25.0"
  }
}

/////
// Root project (including buildSrc)
/////

apply plugin: com.diffplug.gradle.spotless.SpotlessPlugin

spotless {
  enforceCheck false

  java {
    target fileTree(projectDir) {
      // buildSrc java files
      include 'buildSrc/src/*/java/**/*.java'
    }
    googleJavaFormat()
  }
  format 'misc', {
    target fileTree(projectDir) {
      include '.gitignore'
      include '*.gradle'
      include 'gradle/*.gradle'
      include 'documentation/*.yml'
    }
    indentWithSpaces(2)
    trimTrailingWhitespace()
    endWithNewline()
  }
  format 'markdown', {
    target '**/*.md'
    indentWithSpaces(2)
    endWithNewline()
  }
}

/////
// Sub projects
/////

configure(subprojects - project(':axelor-front')) { p ->

  apply plugin: com.diffplug.gradle.spotless.SpotlessPlugin

  spotless {
    enforceCheck false

    java {
      target fileTree(p.projectDir) {
        include 'src/*/java/**/*.java'
      }
      googleJavaFormat()
    }
    format 'misc', {
      target fileTree(p.projectDir) {
        include '*.gradle'
        include 'src/**/*.html'
        include 'src/**/*.js'
        include 'src/**/*.css'
        include 'src/**/*.xml'
        include 'src/**/*.xsd'
        include 'src/**/*.jsp'
        exclude 'src/**/webapp/lib/*'
        if (p.name == 'axelor-tomcat') {
          // Exclude tomcat configs files
          exclude 'src/main/resources/conf/*'
        }
      }
      indentWithSpaces(2)
      trimTrailingWhitespace()
      endWithNewline()
    }
  }

}

/////
// formatCode task
/////

configure([rootProject] + (subprojects - project(':axelor-front'))) {

  tasks.register('formatCode') {
    description "Add license and format code."
    group "Axelor"
    if (tasks.findByName('licenseFormat')) {
      dependsOn 'licenseFormat'
    }
    dependsOn'spotlessApply'
  }
}
