Skip to content

Commit

Permalink
Fix GitHub output and environment variables in the printSemver task
Browse files Browse the repository at this point in the history
  • Loading branch information
JavierSegoviaCordoba committed Dec 14, 2023
1 parent 972df0b commit f700012
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 54 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

### Fixed

- GitHub output and environment variables in the `printSemver` task

### Updated

- `com.javiersc.hubdle:com.javiersc.hubdle.gradle.plugin -> 0.5.0-beta.15`
Expand Down
18 changes: 4 additions & 14 deletions semver-project-gradle-plugin/api/semver-project-gradle-plugin.api
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ public abstract class com/javiersc/semver/project/gradle/plugin/services/GitBuil

public abstract class com/javiersc/semver/project/gradle/plugin/tasks/CreateSemverTagTask : org/gradle/api/DefaultTask {
public static final field Companion Lcom/javiersc/semver/project/gradle/plugin/tasks/CreateSemverTagTask$Companion;
public static final field TaskName Ljava/lang/String;
public static final field NAME Ljava/lang/String;
public fun <init> ()V
public final fun run ()V
}
Expand All @@ -165,7 +165,7 @@ public final class com/javiersc/semver/project/gradle/plugin/tasks/CreateSemverT

public abstract class com/javiersc/semver/project/gradle/plugin/tasks/PrintSemverTask : org/gradle/api/DefaultTask {
public static final field Companion Lcom/javiersc/semver/project/gradle/plugin/tasks/PrintSemverTask$Companion;
public static final field TaskName Ljava/lang/String;
public static final field NAME Ljava/lang/String;
public fun <init> (ZLjava/lang/String;Lorg/gradle/api/model/ObjectFactory;)V
public final fun getGithubEnv ()Lorg/gradle/api/provider/Property;
public final fun getGithubEnvTag ()Lorg/gradle/api/provider/Property;
Expand All @@ -182,14 +182,9 @@ public abstract class com/javiersc/semver/project/gradle/plugin/tasks/PrintSemve
public final class com/javiersc/semver/project/gradle/plugin/tasks/PrintSemverTask$Companion {
}

public final class com/javiersc/semver/project/gradle/plugin/tasks/PrintSemverTask$Companion$inlined$sam$i$org_gradle_api_specs_Spec$0 : org/gradle/api/specs/Spec {
public fun <init> (Lkotlin/jvm/functions/Function1;)V
public final synthetic fun isSatisfiedBy (Ljava/lang/Object;)Z
}

public abstract class com/javiersc/semver/project/gradle/plugin/tasks/PushSemverTagTask : org/gradle/api/DefaultTask {
public static final field Companion Lcom/javiersc/semver/project/gradle/plugin/tasks/PushSemverTagTask$Companion;
public static final field TaskName Ljava/lang/String;
public static final field NAME Ljava/lang/String;
public fun <init> ()V
public final fun run ()V
}
Expand All @@ -199,7 +194,7 @@ public final class com/javiersc/semver/project/gradle/plugin/tasks/PushSemverTag

public abstract class com/javiersc/semver/project/gradle/plugin/tasks/WriteSemverTask : org/gradle/api/DefaultTask {
public static final field Companion Lcom/javiersc/semver/project/gradle/plugin/tasks/WriteSemverTask$Companion;
public static final field TaskName Ljava/lang/String;
public static final field NAME Ljava/lang/String;
public fun <init> (Lorg/gradle/api/model/ObjectFactory;Lorg/gradle/api/file/ProjectLayout;)V
public final fun getSemverFile ()Lorg/gradle/api/file/RegularFileProperty;
public abstract fun getTagPrefix ()Lorg/gradle/api/provider/Property;
Expand All @@ -210,8 +205,3 @@ public abstract class com/javiersc/semver/project/gradle/plugin/tasks/WriteSemve
public final class com/javiersc/semver/project/gradle/plugin/tasks/WriteSemverTask$Companion {
}

public final class com/javiersc/semver/project/gradle/plugin/tasks/WriteSemverTask$Companion$inlined$sam$i$org_gradle_api_specs_Spec$0 : org/gradle/api/specs/Spec {
public fun <init> (Lkotlin/jvm/functions/Function1;)V
public final synthetic fun isSatisfiedBy (Ljava/lang/Object;)Z
}

Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ public class SemverProjectPlugin : Plugin<Project> {
private fun Project.configureBuildServicesAndTasks(
gitTagBuildService: Provider<GitBuildService>
) {
CreateSemverTagTask.register(this, gitTagBuildService)
PushSemverTagTask.register(this, gitTagBuildService)
PrintSemverTask.register(this)
WriteSemverTask.register(this)
CreateSemverTagTask.register(this, gitTagBuildService)
PushSemverTagTask.register(this, gitTagBuildService)
}

private fun Project.configureVersion() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ import com.javiersc.semver.project.gradle.plugin.internal.tagPrefixProperty
import com.javiersc.semver.project.gradle.plugin.services.GitBuildService
import org.gradle.api.DefaultTask
import org.gradle.api.Project
import org.gradle.api.Task
import org.gradle.api.provider.Property
import org.gradle.api.provider.Provider
import org.gradle.api.tasks.Internal
import org.gradle.api.tasks.TaskAction
import org.gradle.api.tasks.TaskProvider
import org.gradle.kotlin.dsl.register

public abstract class CreateSemverTagTask : DefaultTask() {
Expand All @@ -33,15 +35,20 @@ public abstract class CreateSemverTagTask : DefaultTask() {
}

public companion object {
public const val TaskName: String = "createSemverTag"

public const val NAME: String = "createSemverTag"

internal fun register(project: Project, gitTagBuildService: Provider<GitBuildService>) {
project.tasks.register<CreateSemverTagTask>(TaskName).configure { task ->
val printSemverTask: TaskProvider<Task> = project.tasks.named(PrintSemverTask.NAME)
val writeSemverTask: TaskProvider<Task> = project.tasks.named(WriteSemverTask.NAME)
project.tasks.register<CreateSemverTagTask>(NAME).configure { task ->
task.tagPrefixProperty.set(project.tagPrefixProperty)
task.projectTagPrefix.set(project.projectTagPrefix)
task.version.set(project.version.toString())
task.gitTagBuildService.set(gitTagBuildService)
task.usesService(gitTagBuildService)
task.dependsOn(printSemverTask)
task.dependsOn(writeSemverTask)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package com.javiersc.semver.project.gradle.plugin.tasks

import com.javiersc.gradle.project.extensions.isRootProject
import com.javiersc.gradle.tasks.extensions.maybeRegisterLazily
import com.javiersc.gradle.tasks.extensions.namedLazily
import com.javiersc.semver.project.gradle.plugin.internal.projectTagPrefix
import com.javiersc.semver.project.gradle.plugin.internal.semverMessage
import java.io.File
Expand Down Expand Up @@ -133,21 +131,14 @@ constructor(

private fun executeGitHubOutput(key: String, value: String) {
val snakeCaseKey = key.toSnakeCase()
semverMessage("\nSetting $value with the `$snakeCaseKey` as step output...")
File(System.getenv("GITHUB_OUTPUT")).putEnvironmentVariable(snakeCaseKey, value)
semverMessage("\nSetting `$snakeCaseKey` to `$value` as step output...")
File(System.getenv("GITHUB_OUTPUT")).appendText("\n$snakeCaseKey=$value")
}

private fun executeGitHubEnvironmentVariable(key: String, value: String) {
val snakeCaseKey = key.toSnakeCase()
semverMessage("\nSetting $value with the key `$snakeCaseKey` as environment variable...")
File(System.getenv("GITHUB_ENV")).putEnvironmentVariable(snakeCaseKey, value)
}

private fun File.putEnvironmentVariable(key: String, value: String) {
val lines = readLines().toMutableList()
val keyIndex = lines.indexOfFirst { line -> line.substringBefore('=') == key }
if (keyIndex != -1) lines[keyIndex] = value else lines.add("$key=$value")
writeText(lines.joinToString("\n"))
semverMessage("\nSetting `$snakeCaseKey` to `$value` as environment variable...")
File(System.getenv("GITHUB_ENV")).appendText("\n$snakeCaseKey=$value")
}

private fun String.toSnakeCase(): String =
Expand All @@ -157,33 +148,31 @@ constructor(
.replace("-", "_")

public companion object {
public const val TaskName: String = "printSemver"

public const val NAME: String = "printSemver"

internal fun register(project: Project): TaskProvider<PrintSemverTask> {
val prepareKotlinIdeaImportTask: Task =
project.tasks.maybeCreate("prepareKotlinIdeaImport")

val printSemverTask: TaskProvider<PrintSemverTask> =
project.tasks.register(TaskName, project.isRootProject, project.name)
project.tasks.register(NAME, project.isRootProject, project.name)

prepareKotlinIdeaImportTask.dependsOn(printSemverTask)

printSemverTask.configure { task ->
task.dependsOn(WriteSemverTask.TaskName)
task.dependsOn(WriteSemverTask.NAME)
task.tagPrefix.set(project.projectTagPrefix)
task.version.set(project.version.toString())
}

project.tasks.namedLazily<CreateSemverTagTask>(CreateSemverTagTask.TaskName) { task ->
task.finalizedBy(printSemverTask)
}
project.tasks.maybeRegisterLazily<Task>(ASSEMBLE_TASK_NAME) { task ->
project.tasks.named(ASSEMBLE_TASK_NAME).configure { task ->
task.dependsOn(printSemverTask)
}
project.tasks.maybeRegisterLazily<Task>(BUILD_TASK_NAME) { task ->
project.tasks.named(BUILD_TASK_NAME).configure { task ->
task.dependsOn(printSemverTask)
}
project.tasks.maybeRegisterLazily<Task>(CHECK_TASK_NAME) { task ->
project.tasks.named(CHECK_TASK_NAME).configure { task ->
task.dependsOn(printSemverTask)
}
project.tasks.withType<Jar>().configureEach { task -> task.dependsOn(printSemverTask) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,11 @@ public abstract class PushSemverTagTask : DefaultTask() {
}

public companion object {
public const val TaskName: String = "pushSemverTag"

public const val NAME: String = "pushSemverTag"

internal fun register(project: Project, gitTagBuildService: Provider<GitBuildService>) {
project.tasks.register<PushSemverTagTask>(TaskName).configure { task ->
project.tasks.register<PushSemverTagTask>(NAME).configure { task ->
task.tagPrefixProperty.set(project.tagPrefixProperty)
task.projectTagPrefix.set(project.projectTagPrefix)
task.version.set(project.version.toString())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
package com.javiersc.semver.project.gradle.plugin.tasks

import com.javiersc.gradle.tasks.extensions.maybeRegisterLazily
import com.javiersc.gradle.tasks.extensions.namedLazily
import com.javiersc.semver.project.gradle.plugin.internal.projectTagPrefix
import javax.inject.Inject
import org.gradle.api.DefaultTask
import org.gradle.api.Project
import org.gradle.api.Task
import org.gradle.api.file.ProjectLayout
import org.gradle.api.file.RegularFileProperty
import org.gradle.api.model.ObjectFactory
Expand Down Expand Up @@ -65,27 +62,25 @@ constructor(
}

public companion object {
public const val TaskName: String = "writeSemver"

public const val NAME: String = "writeSemver"

internal fun register(project: Project): TaskProvider<WriteSemverTask> {
val writeSemverTask: TaskProvider<WriteSemverTask> =
project.tasks.register<WriteSemverTask>(TaskName)
project.tasks.register<WriteSemverTask>(NAME)

writeSemverTask.configure { task ->
task.tagPrefix.set(project.projectTagPrefix)
task.version.set(project.version.toString())
}

project.tasks.namedLazily<CreateSemverTagTask>(CreateSemverTagTask.TaskName) { task ->
task.dependsOn(writeSemverTask)
}
project.tasks.maybeRegisterLazily<Task>(ASSEMBLE_TASK_NAME) { task ->
project.tasks.named(ASSEMBLE_TASK_NAME).configure { task ->
task.dependsOn(writeSemverTask)
}
project.tasks.maybeRegisterLazily<Task>(BUILD_TASK_NAME) { task ->
project.tasks.named(BUILD_TASK_NAME).configure { task ->
task.dependsOn(writeSemverTask)
}
project.tasks.maybeRegisterLazily<Task>(CHECK_TASK_NAME) { task ->
project.tasks.named(CHECK_TASK_NAME).configure { task ->
task.dependsOn(writeSemverTask)
}
project.tasks.withType<Jar>().configureEach { task -> task.dependsOn(writeSemverTask) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,5 +100,5 @@ internal abstract class VersionValueSource : ValueSource<String, VersionValueSou
private val Project.isCreatingSemverTag: Boolean
get() =
gradle.startParameter.taskNames.any { taskName: String ->
taskName == CreateSemverTagTask.TaskName || taskName == PushSemverTagTask.TaskName
taskName == CreateSemverTagTask.NAME || taskName == PushSemverTagTask.NAME
}

0 comments on commit f700012

Please sign in to comment.