import static org.gradle.api.JavaVersion.*

['osgi', 'distribution', 'maven', 'signing'].each { apply plugin: it }
// Jacoco and a coveralls upload plugin needed for publishing coverage results
['jacoco', 'com.github.kt3k.coveralls'].each { apply plugin: it }

// We require building with JDK 8 or later.  We turn off doclint since our
// generated *TokenMakers have horrible documentation (see https://github.com/jflex-de/jflex/issues/182)
assert current().isJava8Compatible()
allprojects {
	tasks.withType(Javadoc) {
		options.addStringOption('Xdoclint:none', '-quiet')
	}
}

archivesBaseName = 'rsyntaxtextarea'

// Add coveralls plugin to this build's classpath
buildscript {
	repositories {
		mavenCentral()
	}
	dependencies {
		classpath 'org.kt3k.gradle.plugin:coveralls-gradle-plugin:2.8.2'
	}
}

jacocoTestReport {
	reports {
		xml.enabled = true // coveralls plugin depends on xml format report
		html.enabled = true
	}
}

ext.sharedManifest = manifest {
	attributes('Specification-Title': 'RSyntaxTextArea',
		'Specification-Version': version,
		'Implementation-Title': 'org.fife.ui',
		'Implementation-Version': version,
		// Not sure why Require-Capability is not being added by the osgi plugin...
		'Require-Capability': 'osgi.ee;filter:="(&(osgi.ee=JavaSE)(version=' + javaVersion + '))"')
}
jar {
	manifest { from sharedManifest }
}

// We use "distributions" to create the zip files uploaded to SourceForge
distributions {
	main {
		baseName = 'rsyntaxtextarea'
		contents {
			from { [ 'build/libs', 'distfiles' ] }
			rename 'RSyntaxTextArea-.*\\.jar', 'rsyntaxtextarea.jar'
		}
	}
	src {
		baseName = 'rsyntaxtextarea'
	}
}
distZip.classifier = null
distZip.dependsOn jar
srcDistZip.classifier = 'src'
srcDistZip {
	from projectDir
	include 'src/**/*'
	include 'build.gradle'
	include '.classpath'
	include '.project'
	include 'gradle.properties'
	include 'gradle/**/*'
	include 'gradlew*'
	include 'README.md'
	include '.settings/**'
}

task buildSourceForgeZips {
    doFirst {
        println 'Building zip files for SourceForge'
    }
}
buildSourceForgeZips.dependsOn clean, jar, distZip, srcDistZip

// Stuff to generate and upload Maven artifacts
task javadocJar (type: Jar, dependsOn: javadoc) {
	manifest { from sharedManifest }
	classifier = 'javadoc'
	from javadoc.destinationDir
}
task sourceJar (type: Jar) {
	manifest { from sharedManifest }
	classifier = 'sources'
	from sourceSets.main.allSource
}
artifacts {
	archives jar, javadocJar, sourceJar
}
signing {
	// Don't require signing for e.g. ./gradlew install
	required { gradle.taskGraph.hasTask("uploadArchives") }
	sign configurations.archives
}
repositories {
	mavenCentral()
}
uploadArchives {
	repositories {
		mavenDeployer {
			beforeDeployment { MavenDeployment deployment -> signing.signPom(deployment) }
			if (project.hasProperty('upload') && Boolean.parseBoolean(upload)) { // gradlew uploadArchives -Pupload=true
				repository(url: 'https://oss.sonatype.org/service/local/staging/deploy/maven2/') {
					authentication(userName: ossrhUsername, password: ossrhPassword)
				}
				snapshotRepository(url: 'https://oss.sonatype.org/content/repositories/snapshots/') {
					authentication(userName: ossrhUsername, password: ossrhPassword)
				}
			}
			else {
				repository(url: 'file:///' + projectDir + '/../localMavenRepo')
			}
			pom {
				groupId = 'com.fifesoft'
				name = 'rsyntaxtextarea'
				project {
					parent {
						groupId 'org.sonatype.oss'
						artifactId 'oss-parent'
						version '7'
						relativePath ''
					}
					
					groupId 'com.fifesoft'
					artifactId 'rsyntaxtextarea'
					packaging 'jar'
					
					name 'rsyntaxtextarea'
					description 'RSyntaxTextArea is the syntax highlighting text editor for Swing applications. ' +
					    'Features include syntax highlighting for 40+ languages, code folding, code completion, ' +
						'regex find and replace, macros, code templates, undo/redo, line numbering and bracket ' +
						'matching.'
					
					inceptionYear '2003'
					url 'http://www.fifesoft.com/rsyntaxtextarea/'
					licenses {
						license {
							name 'Modified BSD License'
							url 'http://fifesoft.com/rsyntaxtextarea/RSyntaxTextArea.License.txt'
							distribution 'repo'
						}
					}
					
					scm {
						url 'https://github.com/bobbylight/RSyntaxTextArea'
						connection 'scm:git:git://github.com/bobbylight/RSyntaxTextArea'
						developerConnection 'scm:git:git@github.com:bobbylight/RSyntaxTextArea'
						if (!project.version.endsWith('-SNAPSHOT')) {
							tag project.version
						}
					}
					
					developers {
						developer {
							name 'Robert Futrell'
							url 'http://www.fifesoft.com'
							organization = 'Fifesoft' /* https://issues.gradle.org/browse/GRADLE-1200 */
							organizationUrl 'http://www.fifesoft.com/'
							roles {
								role 'architect'
								role 'developer'
							}
							timezone '0'
						}
					}
					
					// Ugh, have to do this for the "build" section
					// http://stackoverflow.com/questions/27975786/trouble-injecting-the-build-block-while-exporting-a-maven-pom-xml-file-from-grad
					withXml {
						def pluginNode = asNode().appendNode('build').appendNode('plugins').appendNode('plugin')
						pluginNode.appendNode('artifactId', 'maven-compiler-plugin')
						pluginNode.appendNode('version', '3.5.1')
						def executionNode = pluginNode.appendNode('executions').appendNode('execution')
						executionNode.appendNode('id', 'default-compile')
						executionNode.appendNode('configuration').appendNode('source', javaVersion).
						      parent().appendNode('target', javaVersion)
					}
				}
			}
		}
	}
}
