// This setup seems to work. Let's not touch it.

apply plugin: 'maven-publish'
//apply plugin: 'signing'	

task sourcesJar(type: Jar) {
	archiveClassifier = 'sources'
	from sourceSets.main.allSource
}

task groovydocJar(type: Jar, dependsOn: groovydoc) {
	archiveClassifier = 'groovydoc'
	from groovydoc.destinationDir
}

task javadocJar(type: Jar, dependsOn: javadoc) {
	archiveClassifier = 'javadoc'
	from javadoc.destinationDir
}

artifacts {
	archives sourcesJar
	archives groovydocJar
	archives javadocJar
}

ext {
	resolveRepoName = { Project project ->
		resolvedRepoName = "libs-${resolveVersion(project.version)}-local"
		logger.lifecycle("For project [$project.name] with " +
				"version [$project.version] the resolved Artifactory repo is [$resolvedRepoName]")
		return resolvedRepoName
	}

	isReleaseVersion = version.endsWith("RELEASE")
	isMilestoneVersion = version.matches('[0-9].[0-9].[0-9].M[0-9]+') || version.matches('[0-9].[0-9].[0-9].RC[0-9]+')
	isSnapshotVersion = !(isReleaseVersion || isMilestoneVersion)
	isReleaseToCentral = project.hasProperty('central')

	resolveVersion = { String version ->
		if (isMilestoneVersion) {
			return 'milestone'
		}
		if (isReleaseVersion) {
			return 'release'
		}
		return 'snapshot'
	}

	repoUrl = isReleaseToCentral ?
			"https://oss.sonatype.org/service/local/staging/deploy/maven2/" :
			"https://repo.spring.io/${resolveRepoName(project)}"
}

if (isReleaseToCentral) {
	/*signing {
		sign configurations.archives
	}*/

	apply plugin: 'com.gradle.plugin-publish'

	pluginBundle {
		website = 'https://github.com/spring-cloud/spring-cloud-contract'
		vcsUrl = 'https://github.com/spring-cloud/spring-cloud-contract'

		plugins {
			plugin {
				id = 'org.springframework.cloud.contract'
				displayName = 'spring-cloud-contract'
				description = 'Spring Cloud Contract Gradle Plugin'
				tags = ['spring', 'spring-framework', 'spring-cloud', 'spring-cloud-contract']
			}
		}

		mavenCoordinates {
			groupId = project.group
			artifactId = project.name
		}
	}
}

String getProp(String propName) {
	return hasProperty(propName) ?
			(getProperty(propName) ?: System.properties[propName]) : System.properties[propName] ?:
			System.getenv(propName)
}