/*
 * Master Gradle initialization script
 */

import aQute.bnd.osgi.Constants

/* Add bnd gradle plugin as a script dependency */
buildscript {
  repositories {
    mavenCentral()
    maven {
      url uri(bnd_repourl)
    }
  }
  dependencies {
    classpath bnd_plugin
  }
  /* Since the files in the repository change with each build, we need to recheck for changes */
  configurations.classpath {
    resolutionStrategy {
      cacheChangingModulesFor 30, 'minutes'
      cacheDynamicVersionsFor 30, 'minutes'
    }
  }
  dependencies {
    components {
      all { ComponentMetadataDetails details ->
        details.changing = true
      }
    }
  }
  /* Add bnd gradle plugin to buildscript classpath of rootProject */
  def bndPlugin = files(configurations.classpath.files)
  gradle.rootProject {
    buildscript {
      dependencies {
        classpath bndPlugin
      }
    }
  }
}

gradle.ext.bndWorkspaceConfigure = { workspace ->
  /*
   * Compute the build time stamp. 
   * If the git workspace is clean, the build time is the time of the head commit.
   * If the git workspace is dirty, the build time is the current time.
   */
  if ('git diff --no-ext-diff --quiet'.execute().waitFor() == 0) {
    workspace.setProperty(Constants.TSTAMP, 'git show --no-patch --format=%ct000'.execute().text.trim())
  } else {
    workspace.setProperty(Constants.TSTAMP, Long.toString(System.currentTimeMillis()))
  }
}

apply plugin: 'biz.aQute.bnd.workspace'
