Skip to content

Commit

Permalink
优化buildSrc
Browse files Browse the repository at this point in the history
  • Loading branch information
kosaka-bun committed Jun 5, 2024
1 parent cb5aaa4 commit 79b0c16
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 40 deletions.
7 changes: 2 additions & 5 deletions aar/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import com.android.build.gradle.internal.api.DefaultAndroidSourceDirectorySet
import de.honoka.gradle.buildsrc.MavenPublish
import de.honoka.gradle.buildsrc.MavenPublish.defineAarSourcesJarTask
import de.honoka.gradle.buildsrc.MavenPublish.setupAarVersionAndPublishing
import de.honoka.gradle.buildsrc.publishing
Expand Down Expand Up @@ -32,9 +31,8 @@ android {

sourceSets["main"].java {
srcDir("/patchSrc/main/java")
if(this is DefaultAndroidSourceDirectorySet) {
MavenPublish.sourceDirSet = srcDirs
}
val sourceDirSet = if(this is DefaultAndroidSourceDirectorySet) srcDirs else setOf()
defineAarSourcesJarTask(sourceDirSet)
}

compileOptions {
Expand Down Expand Up @@ -83,4 +81,3 @@ publishing {
}

setupAarVersionAndPublishing(libs.versions.aar.get())
defineAarSourcesJarTask()
1 change: 1 addition & 0 deletions buildSrc/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ plugins {
`kotlin-dsl`
}

//noinspection UseTomlInstead
dependencies {
implementation("org.jetbrains.kotlin:kotlin-gradle-plugin-api:1.8.0")
}
67 changes: 34 additions & 33 deletions buildSrc/src/main/java/de/honoka/gradle/buildsrc/MavenPublish.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package de.honoka.gradle.buildsrc

import org.gradle.api.Project
import org.gradle.api.artifacts.Dependency
import org.gradle.api.publish.maven.MavenPom
import org.gradle.api.publish.maven.MavenPublication
import org.gradle.api.tasks.bundling.Jar
import org.gradle.kotlin.dsl.create
Expand All @@ -16,8 +17,6 @@ object MavenPublish {

private val projectsWillPublish = ArrayList<Project>()

var sourceDirSet: Set<File> = setOf()

fun Project.setupAarVersionAndPublishing(version: String) {
val project = this
this.version = version
Expand All @@ -34,36 +33,7 @@ object MavenPublish {
groupId = group as String
artifactId = rootProject.name
this.version = version
pom.withXml {
val apiDependencies = ArrayList<String>()
project.configurations["api"].allDependencies.forEach {
apiDependencies.add("${it.group}:${it.name}")
}
asNode().appendNode("dependencies").run {
project.configurations.implementation.configure {
allDependencies.forEach {
val isInvalidDependency = it.group == null ||
it.name.lowercase() == "unspecified" ||
it.version == null
if(isInvalidDependency) return@forEach
val moduleName = "${it.group}:${it.name}"
appendNode("dependency").run {
val subNodes = hashMapOf(
"groupId" to it.group,
"artifactId" to it.name,
"version" to it.version
)
if(!apiDependencies.contains(moduleName)) {
subNodes["scope"] = "runtime"
}
subNodes.forEach { entry ->
appendNode(entry.key, entry.value)
}
}
}
}
}
}
pom.setAndroidAarPomDependencies(project)
afterEvaluate {
val artifacts = listOf(
tasks["bundleReleaseAar"],
Expand All @@ -77,7 +47,38 @@ object MavenPublish {
projectsWillPublish.add(this)
}

fun Project.defineAarSourcesJarTask() {
private fun MavenPom.setAndroidAarPomDependencies(project: Project) = withXml {
val apiDependencies = ArrayList<String>()
project.configurations["api"].allDependencies.forEach {
apiDependencies.add("${it.group}:${it.name}")
}
asNode().appendNode("dependencies").run {
project.configurations.implementation.configure {
allDependencies.forEach {
val isInvalidDependency = it.group == null ||
it.name.lowercase() == "unspecified" ||
it.version == null
if(isInvalidDependency) return@forEach
val moduleName = "${it.group}:${it.name}"
appendNode("dependency").run {
val subNodes = hashMapOf(
"groupId" to it.group,
"artifactId" to it.name,
"version" to it.version
)
if(!apiDependencies.contains(moduleName)) {
subNodes["scope"] = "runtime"
}
subNodes.forEach { entry ->
appendNode(entry.key, entry.value)
}
}
}
}
}
}

fun Project.defineAarSourcesJarTask(sourceDirSet: Set<File>) {
tasks.register("sourcesJar", Jar::class.java) {
group = "build"
destinationDirectory.set(Paths.get(buildDir.absolutePath, "libs").toFile())
Expand Down
4 changes: 2 additions & 2 deletions buildSrc/src/main/java/de/honoka/gradle/buildsrc/Project.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ val Project.rawDependencies: Set<Dependency>
}
val set = HashSet<Dependency>()
configurationContainer.forEach {
it.dependencies.forEach {
set.add(it as Dependency)
it.dependencies.forEach { dep ->
set.add(dep as Dependency)
}
}
return set
Expand Down

0 comments on commit 79b0c16

Please sign in to comment.