
plugins {
  id 'java-library'
  id 'wrapper'
  id 'com.jfrog.bintray' version '1.8.1'
  id 'maven-publish'
  id 'com.github.sherter.google-java-format' version '0.3.2'
  id 'net.ltgt.errorprone' version '0.0.11'
  id 'idea'

  // code coverage support
  id 'net.saliman.cobertura' version '2.5.4'
  id 'com.github.kt3k.coveralls' version '2.8.2'

  // Add license plugin. By default, this plugin only applies to the root
  // project. Since we need license enforcement for benchmarks and the
  // Unicode table generator, "apply false" specifies that the plugin should
  // be downloaded and made available to Gradle, but not applied yet. See the
  // allprojects section below for where this plugin is actually applied.
  id 'com.github.hierynomus.license' version '0.15.0' apply false
}

cobertura.coverageFormats = ['html', 'xml']

// The name of the release we're working on. See RELEASING.md for details.
def versionName = '1.5'

wrapper {
  gradleVersion '4.9'
}

sourceCompatibility = 1.6
targetCompatibility = 1.6

repositories {
  mavenCentral()
}

sourceSets {
  main {
    java {
      srcDir 'java'
      exclude 'com/google/re2j/super/**'
    }
  }

  gwt {
    java {
      srcDir 'java'
      include 'com/google/re2j/super/**'
    }
    resources {
      srcDir 'java'
      include 'com/google/re2j/RE2J.gwt.xml'
    }
  }

  test {
    java {
      srcDir 'javatests'
    }
    resources {
      srcDir 'testdata'
    }
  }
}

idea {
  module {
    // Unfortunately, IntelliJ ignores the `exclude` directive in the sourceSet declaration
    // above. Without this, it complains about duplicated classes that exist both in GWT and non-
    // GWT sources.
    excludeDirs += file('java/com/google/re2j/super')
  }
}

test {
  // Allow parallel test execution.
  maxParallelForks = Runtime.runtime.availableProcessors().intdiv(2) ?: 1
}

dependencies {
  testCompile 'junit:junit:4.12'
  testCompile 'com.google.gwt:gwt-dev:2.8.2'
  testCompile 'com.google.gwt:gwt-user:2.8.2'
  testCompile 'com.google.truth:truth:0.36'

  // Cobertura requires slf4j at runtime
  testRuntime "org.slf4j:slf4j-api:1.7.10"
}

// Use error_prone_core 2.0.5, later versions require Java 8 to build and we'd
// like to support building on Java 7.
configurations.errorprone {
  resolutionStrategy.force 'com.google.errorprone:error_prone_core:2.0.5'
}

task sourceJar(type: Jar) {
    baseName 'sources'

    from sourceSets.main.allJava
    from sourceSets.gwt.allSource
}

task javadocJar(type: Jar, dependsOn: javadoc) {
    baseName 'javadoc'

    from javadoc.destinationDir
}

def appendMavenCentralMetadata(Node node) {
    node.appendNode('name', 'RE2/J')
    node.appendNode('description', 'Linear time regular expressions for Java')
    node.appendNode('url', 'http://github.com/google/re2j')

    def license = node.appendNode('licenses').appendNode('license')
    license.appendNode('name', 'Go License')
    license.appendNode('url', 'https://golang.org/LICENSE')
    license.appendNode('distribution', 'repo')

    node.appendNode('scm').appendNode('url', 'https://github.com/google/re2j.git')

    def developerInfo = node.appendNode('developers').appendNode('developer')
    developerInfo.appendNode('id', 'dev')
    developerInfo.appendNode('name', 'The RE2/J Contributors')
    developerInfo.appendNode('email', 're2j-discuss@googlegroups.com')
}

publishing {
  publications {
    mavenJava(MavenPublication) {
      from components.java

      artifact sourceJar {
        classifier 'sources'
      }

      artifact javadocJar {
        classifier 'javadoc'
      }

      groupId 'com.google.re2j'
      artifactId 're2j'
      version versionName

      pom.withXml {
        appendMavenCentralMetadata(asNode())
      }
    }
  }
}

bintray {
  user = System.getenv('BINTRAY_USER')
  key = System.getenv('BINTRAY_KEY')

  pkg {
    repo = 'maven'
    name = 're2j'
    userOrg = 're2j'
    licenses = ['Go']
    vcsUrl = 'https://github.com/google/re2j.git'

    version {
      name = versionName
      desc = "RE2/J ${versionName}"
      released = new Date()
      vcsTag = "re2j-${versionName}"
    }
  }

  publications = ['mavenJava']
}

// If Java formatter checks fail, tell the user how to fix them.
task printFormatHelperMessage {
  doLast {
    throw new GradleException('google-java-format is required.\n' +
        'Use ./gradlew googleJavaFormat to reformat the broken files')
  }
}
verifyGoogleJavaFormat.finalizedBy printFormatHelperMessage
printFormatHelperMessage.onlyIf { verifyGoogleJavaFormat.state.failure != null }

import com.hierynomus.gradle.license.tasks.LicenseCheck

// By default, this plugin only applies to the root project. We apply it here
// manually to all projects so that even the subprojects receive license
// enforcement.
allprojects {
  apply plugin: "license"

  license {
    header rootProject.file('LICENSE.header')
    ext.year = Calendar.getInstance().get(Calendar.YEAR)
    strictCheck true

    mapping {
      // Without this, the plugin adds javadoc-style comments to the top
      // of each Java source file.
      java = 'SLASHSTAR_STYLE'
    }

    include '**/*.gradle'
    include '**/*.java'
    include '**/*.xml'
    include '**/*.pl'
  }

  // Each license check task gets a new "help message" task to tell the
  // user how to fix license violations.
  tasks.withType(LicenseCheck) { checkTask ->
    def helpMessageTask = task "${checkTask.name}HelpMessage" {
      doLast {
        throw new GradleException(
            'Some files lack an appropriate license header.\n' +
            'Run ./gradlew licenseFormat to add one.')
      }
    }

    checkTask.finalizedBy helpMessageTask
    helpMessageTask.onlyIf { checkTask.state.failure != null }
  }
}
