Skip to content

Commit

Permalink
Ci revival
Browse files Browse the repository at this point in the history
  • Loading branch information
matheus-corregiari committed Nov 8, 2023
1 parent 3e4dac3 commit 4f23d4d
Show file tree
Hide file tree
Showing 10 changed files with 295 additions and 24 deletions.
1 change: 1 addition & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ NAME=Arch Toolkit
DESCRIPTION=A simple group of tools to start develop with a smile in your face.
REPO_URL=https://github.com/matheus-corregiari/arch-toolkit
REPO_GIT_URL=https://github.com/matheus-corregiari/arch-toolkit.git
DEV_URL=https://github.com/matheus-corregiari
LICENCE_NAME=The Apache Software License, Version 2.0
LICENCE_URL=http://www.apache.org/licenses/LICENSE-2.0
LICENCE_DIST=repo
Expand Down
7 changes: 7 additions & 0 deletions plugin/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -83,5 +83,12 @@ gradlePlugin {
description = "Enables and configure group for module"
implementationClass = "com.toolkit.plugin.ToolkitGroupPlugin"
}

create("toolkit-publish") {
id = "toolkit-publish"
displayName = "Toolkit Publish Plugin"
description = "Enables and configure publish for module"
implementationClass = "com.toolkit.plugin.ToolkitPublishPlugin"
}
}
}
18 changes: 17 additions & 1 deletion plugin/src/main/kotlin/com/toolkit/plugin/ToolkitGroupPlugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,32 @@ package com.toolkit.plugin

import org.gradle.api.Plugin
import org.gradle.api.Project
import org.jetbrains.kotlin.com.google.gson.JsonArray

internal class ToolkitGroupPlugin : Plugin<Project> {

override fun apply(target: Project) {
target.applyPlugins("jetbrains-kover")
target.plugins.apply("maven-publish")

// Try to unify coverage reports
target.subprojects.onEach {
target.dependencies.add("kover", it)
}

target.afterEvaluate { project ->
val json = JsonArray()
val file = project.layout.buildDirectory.file("modules.txt").get().asFile
if (file.exists()) {
file.delete()
}

project.subprojects
.filter { it.plugins.hasPlugin("toolkit-publish") }
.map { it.name }
.onEach { json.add(it) }

file.createNewFile()
file.writeText(json.toString())
}
}
}
178 changes: 178 additions & 0 deletions plugin/src/main/kotlin/com/toolkit/plugin/ToolkitPublishPlugin.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
package com.toolkit.plugin

import com.toolkit.plugin.util.versionName
import groovy.util.Node
import org.codehaus.groovy.runtime.DefaultGroovyMethods
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.artifacts.Dependency
import org.gradle.api.artifacts.dsl.RepositoryHandler
import org.gradle.api.publish.maven.MavenPom
import org.gradle.api.publish.maven.MavenPublication
import org.gradle.api.publish.maven.tasks.PublishToMavenRepository
import org.jetbrains.kotlin.konan.file.File

internal class ToolkitPublishPlugin : Plugin<Project> {

private val Project.aar: String?
get() {
val aarPath = "$projectDir/build/outputs/aar/$name-release.aar"
return when {
File(aarPath).exists -> aarPath
else -> null
}
}
private val Project.javadoc: String?
get() {
val aarPath = "$projectDir/build/libs/$name-javadoc.jar"
return when {
File(aarPath).exists -> aarPath
else -> null
}
}
private val Project.sources: String?
get() {
val aarPath = "$projectDir/build/libs/$name-sources.jar"
return when {
File(aarPath).exists -> aarPath
else -> null
}
}

override fun apply(target: Project) {
if (target.plugins.hasPlugin("toolkit-library").not()) {
error("To use sample-compose plugin you must implement toolkit-library plugin")
}
target.plugins.apply("maven-publish")

with(target.publishing) {
repositories { repo ->
repo.createGithubRepository(target)
repo.createLocalPathRepository(target)
}

publications {
it.register("Toolkit", MavenPublication::class.java) { pub ->
pub.groupId = target.properties["GROUP"] as String
pub.artifactId = target.name
pub.version = target.versionName

if (target.aar != null) {
pub.artifact(target.aar)
}
if (target.javadoc != null) {
pub.artifact(target.javadoc) { artifact ->
artifact.classifier = "javadoc"
artifact.extension = "jar"
}
}
if (target.sources != null) {
pub.artifact(target.sources) { artifact ->
artifact.classifier = "sources"
artifact.extension = "jar"
}
}
pub.pom { target.configurePom(it) }
}
}
}

with(target.sign) {
setRequired {
// signing is only required if the artifacts are to be published
target.gradle.taskGraph.allTasks.any { (it is PublishToMavenRepository) }
}
}
}

private fun RepositoryHandler.createGithubRepository(project: Project) = maven { maven ->
maven.name = "Github"
maven.url = project.uri("https://maven.pkg.github.com/matheus-corregiari/arch-toolkit")
maven.credentials { cred ->
cred.username = "user"
cred.password = "token"
}
}

private fun RepositoryHandler.createLocalPathRepository(project: Project) = maven { maven ->
maven.name = "LocalPath"
maven.url = project.uri(project.rootProject.layout.buildDirectory.asFile.get().absolutePath)
}

private fun Project.configurePom(pom: MavenPom) {
// Main Configuration
if (pom.name.orNull.isNullOrBlank() && hasProperty("NAME")) {
pom.name.set(properties["NAME"] as String)
}
if (pom.description.orNull.isNullOrBlank() && hasProperty("DESCRIPTION")) {
pom.description.set(properties["DESCRIPTION"] as String)
}
if (pom.url.orNull.isNullOrBlank() && hasProperty("REPO_URL")) {
pom.url.set(properties["REPO_URL"] as String)
}

// SCM
pom.scm {
it.url.set(properties["REPO_GIT_URL"] as String)
}

// Developer Configuration
pom.developers { developers ->
developers.developer { dev ->
dev.id.set("melete")
dev.name.set("Melete")
dev.email.set("[email protected]")
dev.organization.set("Wonderland")
dev.url.set(properties["DEV_URL"] as String)
}
}

// License Configuration
pom.licenses { licenses ->
licenses.license { license ->
license.name.set(properties["LICENCE_NAME"] as String)
license.url.set(properties["LICENCE_URL"] as String)
license.distribution.set(properties["LICENCE_DIST"] as String)
}
}

val configList = listOf("implementation", "api").mapNotNull(configurations::findByName)
if (configList.isNotEmpty()) {
pom.withXml { xml ->
val dependencyNode: Node = xml.asNode().appendNode("dependencies")
configList.forEach { config ->
config.dependencies.forEach { dependencyNode.addDependency(it) }
}
}
}
}

private fun Node.addDependency(dependency: Dependency) {
val projectDependency =
DefaultGroovyMethods.getProperties(dependency)["dependencyProject"] as? Project

if (projectDependency != null) {
val publishExtension = projectDependency.publishing
publishExtension.publications.filterIsInstance(MavenPublication::class.java)
.onEach { pub ->
val node = appendNode("dependency")
node.appendNode("groupId", pub.groupId)
node.appendNode("artifactId", pub.artifactId)
node.appendNode("version", pub.version)
node.appendNode("scope", "runtime")
}
} else {
val group = dependency.group.takeIf { it.isNullOrBlank().not() }
val name = dependency.name.takeIf { it.isNullOrBlank().not() }
val version = dependency.version.takeIf { it.isNullOrBlank().not() }

if (group == null || name == null || version == null) return

val node = appendNode("dependency")
node.appendNode("groupId", group)
node.appendNode("artifactId", name)
node.appendNode("version", version)
node.appendNode("scope", "runtime")
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@ internal class ToolkitTestPlugin : Plugin<Project> {
defaults { reports ->
reports.mergeWith("debug")
}
androidReports("debug") {

}
}

// Kover configuration
Expand Down
15 changes: 11 additions & 4 deletions plugin/src/main/kotlin/com/toolkit/plugin/_projectExtensions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import kotlinx.kover.gradle.plugin.dsl.KoverReportExtension
import org.gradle.api.Project
import org.gradle.api.artifacts.VersionCatalog
import org.gradle.api.artifacts.VersionCatalogsExtension
import org.gradle.api.publish.PublishingExtension
import org.gradle.plugins.signing.SigningExtension
import org.gradle.testing.jacoco.plugins.JacocoPluginExtension
import org.jlleitschuh.gradle.ktlint.KtlintExtension

Expand Down Expand Up @@ -71,10 +73,15 @@ internal val Project.applicationComponent: ApplicationAndroidComponentsExtension
get() = extensions.findByType(ApplicationAndroidComponentsExtension::class.java)
?: error("Project do not implement android-library neither android-application plugin!")

//internal val Project.javadoc: Javadoc
// @Throws(IllegalStateException::class)
// get() = extensions.findByType(ApplicationAndroidComponentsExtension::class.java)
// ?: error("Project do not implement android-library neither android-application plugin!")
internal val Project.publishing: PublishingExtension
@Throws(IllegalStateException::class)
get() = extensions.findByType(PublishingExtension::class.java)
?: error("Project do not implement maven-publish neither android-application plugin!")

internal val Project.sign: SigningExtension
@Throws(IllegalStateException::class)
get() = extensions.findByType(SigningExtension::class.java)
?: error("Project do not implement android-library neither android-application plugin!")

internal fun Project.applyPlugins(vararg id: String) {
id.forEach {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.toolkit.plugin.androidLibrary
import org.gradle.api.Project
import org.gradle.api.tasks.javadoc.Javadoc
import org.gradle.jvm.tasks.Jar
import org.jetbrains.kotlin.konan.file.File

internal fun Project.setupJavadocAndSources() {
val sourcesJar = setupSources()
Expand All @@ -18,16 +19,17 @@ internal fun Project.setupJavadocAndSources() {
}

private fun Project.setupJavadoc(): Jar {
configurations.maybeCreate("jacocoDeps")

val javadoc = tasks.register("javadoc", Javadoc::class.java) {
val mainSource =
androidLibrary.sourceSets.named("main").get().java.getSourceFiles()
val compileOnly = configurations.named("compileOnly").get()
compileOnly.isCanBeResolved = true
val list = ArrayList<java.io.File>()
androidLibrary.sourceSets.forEach { set -> list.addAll(set.java.srcDirs) }

it.isFailOnError = false
it.source = mainSource
it.classpath += files(androidLibrary.bootClasspath)
it.classpath += compileOnly
it.setExcludes(listOf("**/*.kt", "**/*.java"))
it.source(list)
it.classpath += files(androidLibrary.bootClasspath.joinToString(separator = File.separator))
it.classpath += configurations.named("jacocoDeps").get()
}.get()

return tasks.register("javadocJar", Jar::class.java) {
Expand Down
57 changes: 57 additions & 0 deletions plugin/src/main/kotlin/com/toolkit/plugin/util/_versionName.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.toolkit.plugin.util

import org.apache.tools.ant.taskdefs.condition.Os
import org.gradle.api.Project
import java.io.File

internal val Project.versionName: String
get() = runGitCommand(
fileName = "version-name.txt",
command = "git describe",
default = "0.0.0",
)

private val String.execute: Process get() = Runtime.getRuntime().exec(this)
private val Process.text: String get() = inputStream.bufferedReader().readText().trim()
private val String.executeWithText: String?
get() {
val process = execute
if (process.waitFor() != 0) return null
return process.text
}

private fun Project.runGitCommand(
fileName: String,
command: String,
default: String,
): String {
val file = File("$rootDir/build", fileName)
return when {
file.exists().not() -> {
when {
validateGit() -> {
val output = command.executeWithText
if (output == null) {
default
} else {
file.parentFile.mkdirs()
file.writeText(output)
output
}
}

else -> default
}
}

else -> file.readText().trim()
}
}

private fun validateGit(): Boolean {
if (Os.isFamily(Os.FAMILY_WINDOWS)) return false
return when (val output = "whereis git".executeWithText) {
null -> false
else -> (output.isEmpty() || output == "git:").not()
}
}
11 changes: 7 additions & 4 deletions toolkit/delegate/build.gradle
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
plugins { id "toolkit-library" }
plugins {
id "toolkit-library"
id "toolkit-publish"
}

android.namespace = "br.com.arch.toolkit.delegate"

dependencies {
// Libraries
compileOnly libraries.jetbrains.stdlib.jdk8
compileOnly libraries.androidx.annotation
compileOnly libraries.androidx.appcompat
implementation libraries.jetbrains.stdlib.jdk8
implementation libraries.androidx.annotation
implementation libraries.androidx.appcompat
compileOnly libraries.androidx.recycler
}
Loading

0 comments on commit 4f23d4d

Please sign in to comment.