|
| 1 | +import io.papermc.paperweight.util.Git |
| 2 | +import io.papermc.paperweight.util.createParentDirectories |
| 3 | +import io.papermc.paperweight.util.path |
| 4 | +import kotlinx.serialization.json.Json |
| 5 | +import kotlinx.serialization.json.buildJsonObject |
| 6 | +import kotlinx.serialization.json.put |
| 7 | +import org.gradle.api.DefaultTask |
| 8 | +import org.gradle.api.Project |
| 9 | +import org.gradle.api.file.RegularFileProperty |
| 10 | +import org.gradle.api.tasks.Input |
| 11 | +import org.gradle.api.tasks.OutputFile |
| 12 | +import org.gradle.api.tasks.TaskAction |
| 13 | +import org.gradle.kotlin.dsl.property |
| 14 | +import kotlin.io.path.writeText |
| 15 | + |
| 16 | +abstract class IncludeVersionInfoTask : DefaultTask() { |
| 17 | + @get:Input |
| 18 | + val implementationName = project.objects.property<String>().convention(project.name) |
| 19 | + |
| 20 | + @get:Input |
| 21 | + val implementationVersion = project.objects.property<String>().convention(project.version.toString()) |
| 22 | + |
| 23 | + @get:Input |
| 24 | + val implementationVendor = project.objects.property<String>().convention("ElectroPlay Development Team") |
| 25 | + |
| 26 | + @get:Input |
| 27 | + val specificationName = project.objects.property<String>().convention("proxycheck.io") |
| 28 | + |
| 29 | + @get:Input |
| 30 | + val specificationVersion = project.objects.property<String>() |
| 31 | + |
| 32 | + @get:Input |
| 33 | + val specificationVendor = project.objects.property<String>().convention("https://proxycheck.io/api/") |
| 34 | + |
| 35 | + @get:Input |
| 36 | + val gitBranch = project.objects |
| 37 | + .property<String>() |
| 38 | + .convention(project.git().exec(project.providers, "rev-parse", "--abbrev-ref", "HEAD").map { it.trim() }) |
| 39 | + |
| 40 | + @get:Input |
| 41 | + val gitCommit = project.objects |
| 42 | + .property<String>() |
| 43 | + .convention(project.git().exec(project.providers, "rev-parse", "--short=7", "HEAD").map { it.trim() }) |
| 44 | + |
| 45 | + @get:Input |
| 46 | + val gitTimestamp = project.objects |
| 47 | + .property<String>() |
| 48 | + .convention(gitCommit.flatMap { gitCommit -> project.git().exec(project.providers, "show", "-s", "--format=%cI", gitCommit) }.map { it.trim() }) |
| 49 | + |
| 50 | + @get:Input |
| 51 | + val contactWebsite = project.objects.property<String>().convention("https://epserv.ru/") |
| 52 | + |
| 53 | + @get:Input |
| 54 | + val contactEmail = project.objects.property<String>().convention("admin@epserv.ru") |
| 55 | + |
| 56 | + @get:OutputFile |
| 57 | + abstract val outputFile: RegularFileProperty |
| 58 | + |
| 59 | + @TaskAction |
| 60 | + fun includeVersionInfo() { |
| 61 | + val outputPath = outputFile.get().path |
| 62 | + outputPath.createParentDirectories() |
| 63 | + |
| 64 | + val versionInfo = buildJsonObject { |
| 65 | + put("implementation_name", implementationName.get()) |
| 66 | + put("implementation_version", implementationVersion.get()) |
| 67 | + put("implementation_vendor", implementationVendor.get()) |
| 68 | + put("specification_name", specificationName.get()) |
| 69 | + put("specification_version", specificationVersion.get()) |
| 70 | + put("specification_vendor", specificationVendor.get()) |
| 71 | + put("git_branch", gitBranch.get()) |
| 72 | + put("git_commit", gitCommit.get()) |
| 73 | + put("git_timestamp", gitTimestamp.get()) |
| 74 | + put("contact_website", contactWebsite.get()) |
| 75 | + put("contact_email", contactEmail.get()) |
| 76 | + } |
| 77 | + |
| 78 | + outputPath.writeText(Json.encodeToString(versionInfo)) |
| 79 | + } |
| 80 | + |
| 81 | + companion object { |
| 82 | + fun Project.git(): Git = Git(rootProject.layout.projectDirectory.path) |
| 83 | + } |
| 84 | +} |
0 commit comments