-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support autoconfig for docker plugin (#5)
* Refactor sisyphus project plugin * Make ktlint happy * Fix build arguments not working * Upgrade prepare plugin
- Loading branch information
Showing
17 changed files
with
436 additions
and
222 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
239 changes: 21 additions & 218 deletions
239
...adle-plugin/src/main/kotlin/com/bybutter/sisyphus/project/gradle/SisyphusProjectPlugin.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,233 +1,36 @@ | ||
package com.bybutter.sisyphus.project.gradle | ||
|
||
import java.io.File | ||
import java.net.URI | ||
import nebula.plugin.contacts.ContactsExtension | ||
import nebula.plugin.publishing.maven.MavenPublishPlugin | ||
import nebula.plugin.publishing.publications.JavadocJarPlugin | ||
import nebula.plugin.publishing.publications.SourceJarPlugin | ||
import org.gradle.api.Action | ||
import com.bybutter.sisyphus.project.gradle.java.JavaBaseProjectPlugin | ||
import com.bybutter.sisyphus.project.gradle.java.JavaLibraryProjectPlugin | ||
import com.bybutter.sisyphus.project.gradle.java.JavaPlatformProjectPlugin | ||
import com.bybutter.sisyphus.project.gradle.publishing.ProjectContactsPlugin | ||
import com.bybutter.sisyphus.project.gradle.publishing.ProjectLicensePlugin | ||
import com.bybutter.sisyphus.project.gradle.publishing.ProjectPublishingPlugin | ||
import com.bybutter.sisyphus.project.gradle.publishing.ProjectSigningPlugin | ||
import com.bybutter.sisyphus.project.gradle.threepart.SisyphusAntlrKotlinPlugin | ||
import com.bybutter.sisyphus.project.gradle.threepart.SisyphusDockerPlugin | ||
import com.bybutter.sisyphus.project.gradle.threepart.SisyphusKtlintPlugin | ||
import org.gradle.api.Plugin | ||
import org.gradle.api.Project | ||
import org.gradle.api.artifacts.dsl.RepositoryHandler | ||
import org.gradle.api.publish.PublishingExtension | ||
import org.gradle.api.publish.maven.MavenPublication | ||
import org.gradle.api.tasks.SourceSetContainer | ||
import org.gradle.plugins.signing.SigningExtension | ||
import org.gradle.plugins.signing.SigningPlugin | ||
import org.jlleitschuh.gradle.ktlint.KtlintExtension | ||
import org.jlleitschuh.gradle.ktlint.reporter.ReporterType | ||
|
||
class SisyphusProjectPlugin : Plugin<Project> { | ||
override fun apply(target: Project) { | ||
applyBase(target) | ||
tryApplyJavaBase(target) | ||
tryApplyLibrary(target) | ||
tryApplyPlatform(target) | ||
tryApplyPublish(target) | ||
tryApplyKtlint(target) | ||
tryApplyAntlr(target) | ||
|
||
target.pluginManager.apply(JavaBaseProjectPlugin::class.java) | ||
target.pluginManager.apply(JavaLibraryProjectPlugin::class.java) | ||
target.pluginManager.apply(JavaPlatformProjectPlugin::class.java) | ||
target.pluginManager.apply(ProjectContactsPlugin::class.java) | ||
target.pluginManager.apply(ProjectLicensePlugin::class.java) | ||
target.pluginManager.apply(ProjectPublishingPlugin::class.java) | ||
target.pluginManager.apply(ProjectSigningPlugin::class.java) | ||
target.pluginManager.apply(SisyphusAntlrKotlinPlugin::class.java) | ||
target.pluginManager.apply(SisyphusDockerPlugin::class.java) | ||
target.pluginManager.apply(SisyphusKtlintPlugin::class.java) | ||
} | ||
|
||
private fun applyBase(target: Project) { | ||
val extension = target.extensions.create("sisyphus", SisyphusExtension::class.java, target) | ||
target.version = extension.version | ||
} | ||
|
||
private fun tryApplyJavaBase(target: Project) { | ||
if (!target.pluginManager.hasPlugin("java-base")) { | ||
target.pluginManager.withPlugin("java-base") { | ||
tryApplyJavaBase(target) | ||
} | ||
return | ||
} | ||
|
||
val extension = target.extensions.getByType(SisyphusExtension::class.java) | ||
target.repositories.applyFromRepositoryKeys(extension.repositories, extension.dependencyRepositories) | ||
} | ||
|
||
private fun tryApplyLibrary(target: Project) { | ||
if (!target.pluginManager.hasPlugin("java-library")) { | ||
target.pluginManager.withPlugin("java-library") { | ||
tryApplyLibrary(target) | ||
} | ||
return | ||
} | ||
|
||
try { | ||
target.pluginManager.apply(MavenPublishPlugin::class.java) | ||
target.pluginManager.apply(SourceJarPlugin::class.java) | ||
target.pluginManager.apply(JavadocJarPlugin::class.java) | ||
} catch (exception: NoClassDefFoundError) { | ||
target.logger.debug("Skip apply library plugin due to java library plugins not existed.") | ||
return | ||
} | ||
} | ||
|
||
private fun tryApplyPlatform(target: Project) { | ||
if (!target.pluginManager.hasPlugin("java-platform")) { | ||
target.pluginManager.withPlugin("java-platform") { | ||
tryApplyPlatform(target) | ||
} | ||
return | ||
} | ||
|
||
try { | ||
target.pluginManager.apply(MavenPublishPlugin::class.java) | ||
} catch (exception: NoClassDefFoundError) { | ||
target.logger.debug("Skip apply platform plugin due to java platform plugins not existed.") | ||
return | ||
} | ||
|
||
target.afterEvaluate { | ||
val publishing = it.extensions.getByType(PublishingExtension::class.java) | ||
publishing.publications.withType(MavenPublication::class.java) { | ||
it.from(target.components.getByName("javaPlatform")) | ||
} | ||
} | ||
} | ||
|
||
private fun tryApplyPublish(target: Project) { | ||
if (!target.pluginManager.hasPlugin("nebula.maven-base-publish")) { | ||
target.pluginManager.withPlugin("nebula.maven-base-publish") { | ||
tryApplyPublish(target) | ||
} | ||
return | ||
} | ||
|
||
target.tryApplyPluginClass("nebula.plugin.info.InfoPlugin") | ||
if (target.tryApplyPluginClass("nebula.plugin.contacts.ContactsPlugin")) { | ||
if (target != target.rootProject) { | ||
val rootContacts = target.rootProject.extensions.findByType(ContactsExtension::class.java) | ||
if (rootContacts != null) { | ||
val currentContacts = target.extensions.getByType(ContactsExtension::class.java) | ||
|
||
for ((email, person) in rootContacts.people) { | ||
if (!currentContacts.people.containsKey(email)) { | ||
currentContacts.people[email] = person | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
val extension = target.extensions.getByType(SisyphusExtension::class.java) | ||
val publishingExtension = target.extensions.getByType(PublishingExtension::class.java) | ||
|
||
publishingExtension.publications.withType(MavenPublication::class.java) { | ||
it.pom { | ||
it.licenses { | ||
it.license { | ||
it.name.set("MIT License") | ||
it.url.set("https://github.com/ButterCam/sisyphus/blob/master/LICENSE") | ||
it.distribution.set("repo") | ||
} | ||
} | ||
} | ||
} | ||
|
||
if (!extension.signKeyName.isNullOrEmpty()) { | ||
target.pluginManager.apply(SigningPlugin::class.java) | ||
val signing = target.extensions.getByType(SigningExtension::class.java) | ||
signing.useGpgCmd() | ||
target.afterEvaluate { | ||
publishingExtension.publications.all { | ||
signing.sign(it) | ||
} | ||
} | ||
} | ||
|
||
if (extension.isRelease) { | ||
publishingExtension.repositories.applyFromRepositoryKeys(extension.repositories, extension.releaseRepositories) | ||
} | ||
if (extension.isSnapshot) { | ||
publishingExtension.repositories.applyFromRepositoryKeys(extension.repositories, extension.snapshotRepositories) | ||
} | ||
} | ||
|
||
private fun tryApplyKtlint(target: Project) { | ||
if (!target.pluginManager.hasPlugin("org.jlleitschuh.gradle.ktlint")) { | ||
target.pluginManager.withPlugin("org.jlleitschuh.gradle.ktlint") { | ||
tryApplyKtlint(target) | ||
} | ||
return | ||
} | ||
|
||
val extension = target.extensions.getByType(KtlintExtension::class.java) | ||
extension.filter(Action { | ||
val pattern = "${File.separatorChar}generated${File.separatorChar}" | ||
it.exclude { | ||
it.file.path.contains(pattern) | ||
} | ||
}) | ||
extension.reporters(Action<KtlintExtension.ReporterExtension> { | ||
it.reporter(ReporterType.CHECKSTYLE) | ||
}) | ||
} | ||
|
||
private fun tryApplyAntlr(target: Project) { | ||
if (!target.pluginManager.hasPlugin("org.gradle.antlr")) { | ||
target.pluginManager.withPlugin("org.gradle.antlr") { | ||
tryApplyAntlr(target) | ||
} | ||
return | ||
} | ||
|
||
if (!target.pluginManager.hasPlugin("kotlin")) { | ||
target.pluginManager.withPlugin("kotlin") { | ||
tryApplyAntlr(target) | ||
} | ||
return | ||
} | ||
|
||
val sourceSets = target.extensions.getByType(SourceSetContainer::class.java) | ||
for (sourceSet in sourceSets) { | ||
val kotlinTask = target.tasks.findByName(sourceSet.getCompileTaskName("kotlin")) ?: continue | ||
val antlrTask = target.tasks.findByName(sourceSet.getTaskName("generate", "GrammarSource")) ?: continue | ||
kotlinTask.dependsOn(antlrTask) | ||
} | ||
} | ||
|
||
private fun RepositoryHandler.applyFromRepositoryKeys(repositories: Map<String, Repository>, repositoryKeys: Collection<String>) { | ||
for (repositoryKey in repositoryKeys) { | ||
val repository = when (repositoryKey) { | ||
"local" -> repositories[repositoryKey] ?: run { | ||
this.mavenLocal() | ||
null | ||
} | ||
"central" -> repositories[repositoryKey] ?: run { | ||
this.mavenCentral() | ||
null | ||
} | ||
"jcenter" -> repositories[repositoryKey] ?: run { | ||
this.jcenter() | ||
null | ||
} | ||
"portal" -> repositories[repositoryKey] ?: run { | ||
this.gradlePluginPortal() | ||
null | ||
} | ||
else -> repositories[repositoryKey] | ||
} | ||
|
||
repository ?: continue | ||
|
||
this.maven { | ||
it.name = repositoryKey | ||
it.url = URI.create(repository.url) | ||
it.credentials.username = repository.username ?: "" | ||
it.credentials.password = repository.password ?: "" | ||
} | ||
} | ||
} | ||
|
||
private fun Project.tryApplyPluginClass(className: String): Boolean { | ||
return try { | ||
val plugin = Class.forName(className) | ||
this.pluginManager.apply(plugin) | ||
true | ||
} catch (ex: ClassNotFoundException) { | ||
false | ||
} | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
...yphus-project-gradle-plugin/src/main/kotlin/com/bybutter/sisyphus/project/gradle/Utils.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package com.bybutter.sisyphus.project.gradle | ||
|
||
import java.net.URI | ||
import org.gradle.api.Project | ||
import org.gradle.api.artifacts.dsl.RepositoryHandler | ||
|
||
internal inline fun Project.ensurePlugin(id: String, noinline block: (Project) -> Unit, returnBlock: () -> Unit) { | ||
if (!pluginManager.hasPlugin(id)) { | ||
pluginManager.withPlugin(id) { | ||
block(this) | ||
} | ||
returnBlock() | ||
} | ||
} | ||
|
||
internal fun Project.tryApplyPluginClass(className: String, action: () -> Unit = {}): Boolean { | ||
return try { | ||
val plugin = Class.forName(className) | ||
action() | ||
this.pluginManager.apply(plugin) | ||
true | ||
} catch (ex: ClassNotFoundException) { | ||
false | ||
} | ||
} | ||
|
||
internal fun RepositoryHandler.applyFromRepositoryKeys(repositories: Map<String, Repository>, repositoryKeys: Collection<String>) { | ||
for (repositoryKey in repositoryKeys) { | ||
val repository = when (repositoryKey) { | ||
"local" -> repositories[repositoryKey] ?: run { | ||
this.mavenLocal() | ||
null | ||
} | ||
"central" -> repositories[repositoryKey] ?: run { | ||
this.mavenCentral() | ||
null | ||
} | ||
"jcenter" -> repositories[repositoryKey] ?: run { | ||
this.jcenter() | ||
null | ||
} | ||
"portal" -> repositories[repositoryKey] ?: run { | ||
this.gradlePluginPortal() | ||
null | ||
} | ||
else -> repositories[repositoryKey] | ||
} | ||
|
||
repository ?: continue | ||
|
||
this.maven { | ||
it.name = repositoryKey | ||
it.url = URI.create(repository.url) | ||
it.credentials.username = repository.username | ||
it.credentials.password = repository.password | ||
} | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
...plugin/src/main/kotlin/com/bybutter/sisyphus/project/gradle/java/JavaBaseProjectPlugin.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.bybutter.sisyphus.project.gradle.java | ||
|
||
import com.bybutter.sisyphus.project.gradle.SisyphusExtension | ||
import com.bybutter.sisyphus.project.gradle.applyFromRepositoryKeys | ||
import com.bybutter.sisyphus.project.gradle.ensurePlugin | ||
import org.gradle.api.Plugin | ||
import org.gradle.api.Project | ||
|
||
class JavaBaseProjectPlugin : Plugin<Project> { | ||
override fun apply(target: Project) { | ||
target.ensurePlugin("java-base", ::apply) { | ||
return | ||
} | ||
|
||
val extension = target.extensions.getByType(SisyphusExtension::class.java) | ||
target.repositories.applyFromRepositoryKeys(extension.repositories, extension.dependencyRepositories) | ||
} | ||
} |
Oops, something went wrong.