|
1 | 1 | import org.gradle.api.Project |
2 | 2 | import org.gradle.api.distribution.DistributionContainer |
3 | 3 | import org.gradle.api.file.CopySpec |
4 | | -import java.io.File |
| 4 | +import org.gradle.api.file.Directory |
| 5 | +import org.gradle.api.provider.Provider |
5 | 6 |
|
6 | | -private object Consts { |
7 | | - val taskRegex = Regex("(.*)DistZip") |
8 | | -} |
| 7 | +/** |
| 8 | + * Name of the per-project Maven repository the dist zips are assembled from. |
| 9 | + * `distZip` depends on `publishAllPublicationsToDistMavenRepository`. |
| 10 | + */ |
| 11 | +const val DIST_MAVEN_REPO = "distMaven" |
| 12 | + |
| 13 | +val Project.distMavenDir: Provider<Directory> get() = layout.buildDirectory.dir(DIST_MAVEN_REPO) |
9 | 14 |
|
10 | 15 | private fun Project.versionName(): String = |
11 | 16 | providers.gradleProperty("versionName").get() |
12 | 17 |
|
13 | 18 | // configure distZip tasks for multiplatform |
14 | 19 | fun DistributionContainer.configureForMultiplatform(project: Project) { |
15 | | - val sep = File.separator |
16 | | - val version = project.versionName() |
17 | | - val name = project.name |
18 | | - |
19 | | - this.maybeCreate("android").contents { |
20 | | - from("build${sep}publications${sep}androidRelease") { |
21 | | - renameModule(name, "android", version = version) |
22 | | - } |
23 | | - from("build${sep}outputs${sep}aar") { |
24 | | - include("*-release*") |
25 | | - rename { |
26 | | - it.replace("-release", "-android-release") |
27 | | - } |
28 | | - } |
29 | | - from("build${sep}libs") { |
30 | | - include("*android*") |
31 | | - include("*androidRelease-javadoc*") |
32 | | - rename { |
33 | | - it.replace("androidRelease-javadoc", "android") |
34 | | - } |
35 | | - } |
36 | | - } |
37 | | - this.getByName("main").contents { |
38 | | - from("build${sep}publications${sep}kotlinMultiplatform") { |
39 | | - renameModule(name, version = version) |
40 | | - } |
41 | | - from("build${sep}kotlinToolingMetadata") |
42 | | - from("build${sep}libs") { |
43 | | - include("*compose-kotlin*") |
44 | | - include("*compose-metadata*") |
45 | | - rename { |
46 | | - it.replace("-kotlin", "") |
47 | | - .replace("-metadata", "") |
48 | | - .replace("Multiplatform-javadoc", "") |
49 | | - } |
50 | | - } |
51 | | - } |
52 | | - this.maybeCreate("desktop").contents { |
53 | | - // kotlin multiplatform modules |
54 | | - from("build${sep}publications${sep}desktop") { |
55 | | - renameModule(name, "desktop", version = version) |
56 | | - } |
57 | | - from("build${sep}libs") { |
58 | | - include("*desktop*") |
59 | | - include("*desktop-javadoc*") |
60 | | - rename { |
61 | | - it.replace("desktop-javadoc", "desktop") |
62 | | - } |
63 | | - } |
64 | | - } |
| 20 | + getByName("main").contents { fromDistMaven(project, project.name) } |
| 21 | + maybeCreate("android").contents { fromDistMaven(project, "${project.name}-android") } |
| 22 | + maybeCreate("desktop").contents { fromDistMaven(project, "${project.name}-desktop") } |
65 | 23 |
|
66 | 24 | // make other distZip tasks run together with the main distZip |
67 | | - val platformDists = project.tasks.filter { task -> |
68 | | - task.name.matches(Consts.taskRegex) |
69 | | - }.toTypedArray() |
| 25 | + val platformDists = project.tasks.filter { it.name.matches(Regex("(.*)DistZip")) }.toTypedArray() |
70 | 26 | project.tasks.getByName("distZip").finalizedBy(*platformDists) |
71 | 27 | } |
72 | 28 |
|
73 | 29 | fun DistributionContainer.configureForJvm(project: Project) { |
74 | | - val sep = File.separator |
75 | | - val version = project.versionName() |
76 | | - val name = project.name |
77 | | - |
78 | | - this.getByName("main").contents { |
79 | | - // non android modules |
80 | | - from("build${sep}libs") |
81 | | - from("build${sep}publications${sep}maven") { |
82 | | - renameModule(name, version = version) |
83 | | - } |
84 | | - // android modules |
85 | | - from("build${sep}outputs${sep}aar") { |
86 | | - include("*-release*") |
87 | | - } |
88 | | - from("build${sep}publications${sep}release") { |
89 | | - renameModule(name, version = version) |
90 | | - } |
91 | | - from("build${sep}intermediates${sep}java_doc_jar${sep}release") { |
92 | | - include("*javadoc*") |
93 | | - rename { it.replace("release", "$name-$version") } |
94 | | - } |
95 | | - from("build${sep}intermediates${sep}source_jar${sep}release") { |
96 | | - include("*sources*") |
97 | | - rename { it.replace("release", "$name-$version") } |
98 | | - } |
99 | | - } |
| 30 | + getByName("main").contents { fromDistMaven(project, project.name) } |
100 | 31 | } |
101 | 32 |
|
102 | | -private fun CopySpec.renameModule(projectName: String, renameTo: String = "", version: String) { |
103 | | - var target = "" |
104 | | - if (renameTo.isNotEmpty()) { |
105 | | - target = "-$renameTo" |
106 | | - } |
107 | | - rename { |
108 | | - it.replace("module.json", "$projectName$target-$version.module") |
| 33 | +/** |
| 34 | + * Copies one publication out of the project's Maven repository layout. Maven guarantees file names |
| 35 | + * there (`<artifactId>-<version>[-<classifier>].<ext>`) independently of how the publish plugin |
| 36 | + * names its tasks or intermediate outputs, which is exactly what craft expects. Only the few |
| 37 | + * craft-specific names below need renaming. |
| 38 | + */ |
| 39 | +private fun CopySpec.fromDistMaven(project: Project, artifactId: String) { |
| 40 | + val version = project.versionName() |
| 41 | + val groupPath = project.group.toString().replace('.', '/') |
| 42 | + from(project.distMavenDir.map { it.dir("$groupPath/$artifactId/$version") }) { |
| 43 | + exclude("*.md5", "*.sha1", "*.sha256", "*.sha512", "*.asc") |
| 44 | + rename { |
| 45 | + when (it) { |
| 46 | + // craft reads the POM from a fixed name |
| 47 | + "$artifactId-$version.pom" -> "pom-default.xml" |
| 48 | + // craft's android config maps <dir>-<version> to <dir>-release.aar |
| 49 | + "$artifactId-$version.aar" -> "$artifactId-release.aar" |
| 50 | + // kept for parity with the old layout |
| 51 | + "$artifactId-$version-kotlin-tooling-metadata.json" -> "kotlin-tooling-metadata.json" |
| 52 | + else -> it |
| 53 | + } |
| 54 | + } |
109 | 55 | } |
110 | 56 | } |
0 commit comments