buildscript {
  repositories {
    mavenCentral()
    maven {
      url 'https://plugins.gradle.org/m2/'
    }
  }
  dependencies {
    classpath 'com.cinnober.gradle:semver-git:2.2.1'
    classpath 'com.linkedin:gradle-dustjs-plugin:1.0.0'
    classpath 'de.obqo.gradle:gradle-lesscss-plugin:1.0-1.3.3'
    classpath 'net.ltgt.gradle:gradle-errorprone-plugin:0.0.8'
  }
}

apply plugin: 'com.cinnober.gradle.semver-git'
apply plugin: 'idea'
apply plugin: 'distribution'

allprojects {
  repositories {
    mavenCentral()
    mavenLocal()
  }
}

/**
 * Helper that calls a command and returns the output.
 */
def cmdCaller = { commandln ->
  def stdout = new ByteArrayOutputStream()
  exec {
    commandLine commandln
    standardOutput = stdout
  }
  return stdout.toString().trim()
}

subprojects {
  apply plugin: 'java'
  apply plugin: 'idea'
  apply plugin: 'eclipse'
  apply plugin: 'net.ltgt.errorprone'

  // Set the same version for all sub-projects to root project version
  version = rootProject.version

  configurations.errorprone {
    resolutionStrategy.force 'com.google.errorprone:error_prone_core:2.0.5'
  }

  /**
   * Gets the version name from the latest Git tag
   */
  task createVersionFile() << {
    String gitCommitHash = cmdCaller(['git', 'rev-parse', 'HEAD']);
    String gitRepo = cmdCaller(['git', 'config', '--get', 'remote.origin.url']);
    def date = new Date()
    def formattedDate = date.format('yyyy-MM-dd hh:mm zzz')

    String versionStr = version + '\n' +
        gitCommitHash + '\n' +
        gitRepo + '\n' +
        formattedDate + '\n'

    File versionFile = file('build/package/azkaban.version')
    versionFile.parentFile.mkdirs()
    versionFile.write(versionStr)
  }

  /*
   * Print test execution summary when informational logging is enabled.
   */
  test {
    testLogging {
      afterSuite { desc, result ->
        if (desc.getParent()) {
          logger.info desc.getName()
        } else {
          logger.info "Overall"
        }
        logger.info "  ${result.resultType} (" +
              "${result.testCount} tests, " +
              "${result.successfulTestCount} passed, " +
              "${result.failedTestCount} failed, " +
              "${result.skippedTestCount} skipped)"
      }
    }
  }
}

distributions {
  migration {
    baseName = 'azkaban-migration'
    contents {
      from { project(':azkaban-migration').file('build/package') }
    }
  }

  webserver {
    baseName = 'azkaban-web-server'
    contents {
      from { project(':azkaban-webserver').file('build/package') }
    }
  }

  execserver {
    baseName = 'azkaban-exec-server'
    contents {
      from { project(':azkaban-execserver').file('build/package') }
    }
  }

  soloserver {
    baseName = 'azkaban-solo-server'
    contents {
      from { project(':azkaban-soloserver').files('build/package') }
    }
  }

  sql {
    baseName = 'azkaban-sql'
    contents {
      from { project(':azkaban-sql').file('src/sql') }
      from { project(':azkaban-sql').file('build/sql') }
    }
  }
}

// Set up dependencies for distribution tasks.
//
// N.B. The extension for the Tar tasks is set since the Gradle Distribution
//      plugin uses the .tar file extension for GZipped Tar files by default.
//
// N.B. When the distribution tasks are run, azkaban-execserver,
//      azkaban-webserver, azkaban-migration, and azkaban-soloserver only
//      have a dependency on the azkaban-common build artifacts. As a result,
//      the full :azkaban-common:build task is not run, meaning that the
//      tests are skipped. Thus, the dependency on :azkaban-common:build
//      is set here so that the azkaban-common unit tests are run when running
//      the dist tasks.

migrationDistTar.dependsOn ':azkaban-common:build', ':azkaban-migration:copy'
migrationDistTar.compression = Compression.GZIP
migrationDistTar.extension = 'tar.gz'
migrationDistZip.dependsOn ':azkaban-common:build', ':azkaban-migration:copy'

webserverDistTar.dependsOn ':azkaban-common:build', ':azkaban-webserver:copy'
webserverDistTar.compression = Compression.GZIP
webserverDistTar.extension = 'tar.gz'
webserverDistZip.dependsOn ':azkaban-common:build', ':azkaban-webserver:copy'

execserverDistTar.dependsOn ':azkaban-common:build', ':azkaban-execserver:copy'
execserverDistTar.compression = Compression.GZIP
execserverDistTar.extension = 'tar.gz'
execserverDistZip.dependsOn ':azkaban-common:build', ':azkaban-execserver:copy'

soloserverDistTar.dependsOn ':azkaban-common:build', ':azkaban-soloserver:copy'
soloserverDistTar.compression = Compression.GZIP
soloserverDistTar.extension = 'tar.gz'
soloserverDistZip.dependsOn ':azkaban-common:build', ':azkaban-soloserver:copy'

sqlDistTar.dependsOn ':azkaban-sql:concat'
sqlDistTar.compression = Compression.GZIP
sqlDistTar.extension = 'tar.gz'
sqlDistZip.dependsOn ':azkaban-sql:concat'

distTar.dependsOn migrationDistTar, webserverDistTar, execserverDistTar, soloserverDistTar, sqlDistTar
distZip.dependsOn migrationDistZip, webserverDistZip, execserverDistZip, soloserverDistZip, sqlDistZip

/**
 * Gradle wrapper task.
 */
task wrapper(type: Wrapper) {
  gradleVersion = '2.7'
}
