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

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

    format 'misc', {
      target fileTree(projectDir) {
        include '*.gradle'
        include 'gradle/*.gradle'
        include 'src/**/*.html'
        include 'src/**/*.js'
        include 'src/**/*.css'
        include 'src/**/*.xml'
        include 'src/**/*.xsd'
        include 'src/**/*.jsp'
        exclude 'src/**/webapp/lib/*'
        exclude 'modules/**'
      }
      indentWithSpaces(2)
      trimTrailingWhitespace()
      endWithNewline()
    }
    format 'markdown', {
      target '**/*.md'
      indentWithSpaces(2)
      endWithNewline()
    }
    java {
      target fileTree(projectDir) {
        include 'src/*/java/**/*.java'
      }
      googleJavaFormat()
    }
  }

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