buildscript {
  repositories {
    gradlePluginPortal()
  }
  dependencies {
    classpath "com.diffplug.spotless:spotless-plugin-gradle:6.25.0"
  }
}
File spotlessXmlFile = file("${projectDir}/gradle/spotless.xml.prefs")
allprojects { p ->

  // 'modules' directory is shown as a project : need to be excluded
  if (!p.buildFile.exists()) {
    return;
  }

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

  spotless {
    enforceCheck false

    java {
      target fileTree(projectDir) {
        include 'src/*/java/**/*.java'
        exclude '**/build/**'
        exclude '**/bin/**'
        exclude '**/out/**'
      }
      googleJavaFormat()
    }
    format 'misc', {
      target fileTree(projectDir) {
        include 'src/**/*.xml'
        exclude '**/build/**'
        exclude '**/bin/**'
        exclude '**/.project/**'
        exclude '**/.classpath/**'
        exclude '**/.settings/**'
        exclude '**/.metadata/**'
        exclude '**/out/**'
        exclude '**/.idea/**'
      }
      eclipseWtp('xml').configFile(spotlessXmlFile)
      trimTrailingWhitespace()
    }
  }

  if (p.file('src/main/resources/domains').exists()) {
    p.afterEvaluate {
      p.spotlessJava.dependsOn(p.generateCode)
    }
  }

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