Skip to content

Commit

Permalink
chore(abg): Unify content of ActionCoords name
Browse files Browse the repository at this point in the history
  • Loading branch information
Vampire committed Sep 10, 2024
1 parent 71fcf1a commit 37375d9
Show file tree
Hide file tree
Showing 10 changed files with 89 additions and 111 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,26 @@ public data class ActionCoords(
* A top-level action is an action with its `action.y(a)ml` file in the repository root, as opposed to actions stored
* in subdirectories.
*/
public val ActionCoords.isTopLevel: Boolean get() = "/" !in name
public val ActionCoords.isTopLevel: Boolean get() = "__" !in name

public val ActionCoords.prettyPrint: String get() = "$owner/$name@$version"
public val ActionCoords.prettyPrint: String get() = "$owner/${
name.replace("__", "/")
}@$version"

/**
* For most actions, it's the same as [ActionCoords.name].
* For actions that aren't executed from the root of the repo, it returns the repo name.
*/
public val ActionCoords.repoName: String get() =
name.substringBefore("/")
name.substringBefore("__")

/**
* For most actions, it's empty.
* For actions that aren't executed from the root of the repo, it returns the path relative to the repo root where the
* action lives.
*/
public val ActionCoords.subName: String get() =
if (isTopLevel) "" else "/${name.substringAfter("/")}"
if (isTopLevel) "" else "/${name.substringAfter("__").replace("__", "/")}"

internal fun String.toActionCoords(): ActionCoords {
val (ownerAndName, version) = this.split('@')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ import com.squareup.kotlinpoet.buildCodeBlock
import io.github.typesafegithub.workflows.actionbindinggenerator.domain.ActionCoords
import io.github.typesafegithub.workflows.actionbindinggenerator.domain.MetadataRevision
import io.github.typesafegithub.workflows.actionbindinggenerator.domain.TypingActualSource
import io.github.typesafegithub.workflows.actionbindinggenerator.domain.isTopLevel
import io.github.typesafegithub.workflows.actionbindinggenerator.domain.repoName
import io.github.typesafegithub.workflows.actionbindinggenerator.domain.subName
import io.github.typesafegithub.workflows.actionbindinggenerator.generation.Properties.CUSTOM_INPUTS
import io.github.typesafegithub.workflows.actionbindinggenerator.generation.Properties.CUSTOM_VERSION
import io.github.typesafegithub.workflows.actionbindinggenerator.metadata.Input
Expand Down Expand Up @@ -614,9 +617,7 @@ private fun actionKdoc(
|
|${metadata.description.escapedForComments.removeTrailingWhitespacesForEachLine()}
|
|[Action on GitHub](https://github.com/${coords.owner}/${coords.name.substringBefore(
'/',
)}${if ("/" in coords.name) "/tree/${coords.version}/${coords.name.substringAfter('/')}" else ""})
|[Action on GitHub](https://github.com/${coords.owner}/${coords.repoName}${if (!coords.isTopLevel) "/tree/${coords.version}${coords.subName}" else ""})
""".trimMargin()

private fun Map<String, Typing>?.getInputTyping(key: String) = this?.get(key) ?: StringTyping
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import io.github.typesafegithub.workflows.actionbindinggenerator.domain.ActionCo
import io.github.typesafegithub.workflows.actionbindinggenerator.domain.CommitHash
import io.github.typesafegithub.workflows.actionbindinggenerator.domain.MetadataRevision
import io.github.typesafegithub.workflows.actionbindinggenerator.domain.NewestForVersion
import io.github.typesafegithub.workflows.actionbindinggenerator.domain.repoName
import io.github.typesafegithub.workflows.actionbindinggenerator.domain.subName
import kotlinx.serialization.Serializable
import kotlinx.serialization.decodeFromString
import java.io.IOException
Expand Down Expand Up @@ -35,15 +37,9 @@ public data class Output(
val description: String = "",
)

private fun ActionCoords.actionYmlUrl(gitRef: String) =
"https://raw.githubusercontent.com/$owner/${name.substringBefore(
'/',
)}/$gitRef/${if ("/" in name) "${name.substringAfter('/')}/" else ""}action.yml"
private fun ActionCoords.actionYmlUrl(gitRef: String) = "https://raw.githubusercontent.com/$owner/$repoName/$gitRef$subName/action.yml"

private fun ActionCoords.actionYamlUrl(gitRef: String) =
"https://raw.githubusercontent.com/$owner/${name.substringBefore(
'/',
)}/$gitRef/${if ("/" in name) "${name.substringAfter('/')}/" else ""}action.yaml"
private fun ActionCoords.actionYamlUrl(gitRef: String) = "https://raw.githubusercontent.com/$owner/$repoName/$gitRef$subName/action.yaml"

internal val ActionCoords.gitHubUrl: String get() = "https://github.com/$owner/$name"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ internal fun String.toPascalCase(): String {
val normalizedString = if (hasOnlyUppercases) lowercase() else this
return normalizedString
.replace("+", "-plus-")
.split("-", "_", " ", ".", "/")
.split("-", "_", " ", ".", "/", "__")
.joinToString("") {
it.replaceFirstChar { if (it.isLowerCase()) it.titlecase(Locale.getDefault()) else it.toString() }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ class ClassNamingTest :
context("buildActionClassName") {
listOf(
ActionCoords("irrelevant", "some-action-name", "v2") to "SomeActionName",
ActionCoords("irrelevant", "some-action-name/subaction", "v2") to "SomeActionNameSubaction",
ActionCoords("irrelevant", "some-action-name/foo/bar/baz", "v2") to "SomeActionNameFooBarBaz",
ActionCoords("irrelevant", "some-action-name__subaction", "v2") to "SomeActionNameSubaction",
ActionCoords("irrelevant", "some-action-name__foo__bar__baz", "v2") to "SomeActionNameFooBarBaz",
).forEach { (input, output) ->
test("should get '$input' and produce '$output'") {
input.buildActionClassName() shouldBe output
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ class TypesProvidingTest :
else -> throw IOException()
}
}
val actionCoord = ActionCoords("some-owner", "some-name/some-sub", "v3")
val actionCoord = ActionCoords("some-owner", "some-name__some-sub", "v3")

// When
val types = actionCoord.provideTypes(metadataRevision = CommitHash("some-hash"), fetchUri = fetchUri)
Expand Down Expand Up @@ -164,7 +164,7 @@ class TypesProvidingTest :
else -> throw IOException()
}
}
val actionCoord = ActionCoords("some-owner", "some-name/some-sub", "v3")
val actionCoord = ActionCoords("some-owner", "some-name__some-sub", "v3")

// When
val types = actionCoord.provideTypes(metadataRevision = CommitHash("some-hash"), fetchUri = fetchUri)
Expand Down Expand Up @@ -206,7 +206,7 @@ class TypesProvidingTest :
else -> throw IOException()
}
}
val actionCoord = ActionCoords("some-owner", "some-name/some-sub", "v3")
val actionCoord = ActionCoords("some-owner", "some-name__some-sub", "v3")

// When
val types = actionCoord.provideTypes(metadataRevision = CommitHash("some-hash"), fetchUri = fetchUri)
Expand Down Expand Up @@ -248,7 +248,7 @@ class TypesProvidingTest :
else -> throw IOException()
}
}
val actionCoord = ActionCoords("some-owner", "some-name/some-sub", "v3")
val actionCoord = ActionCoords("some-owner", "some-name__some-sub", "v3")

// When
val types = actionCoord.provideTypes(metadataRevision = CommitHash("some-hash"), fetchUri = fetchUri)
Expand Down Expand Up @@ -300,7 +300,7 @@ class TypesProvidingTest :
else -> throw IOException()
}
}
val actionCoord = ActionCoords("some-owner", "some-name/some-sub", "v3")
val actionCoord = ActionCoords("some-owner", "some-name__some-sub", "v3")

// When
val types = actionCoord.provideTypes(metadataRevision = CommitHash("some-hash"), fetchUri = fetchUri)
Expand Down Expand Up @@ -352,7 +352,7 @@ class TypesProvidingTest :
else -> throw IOException()
}
}
val actionCoord = ActionCoords("some-owner", "some-name/some-sub", "v6")
val actionCoord = ActionCoords("some-owner", "some-name__some-sub", "v6")

// When
val types = actionCoord.provideTypes(metadataRevision = CommitHash("some-hash"), fetchUri = fetchUri)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,9 @@ internal data class Jars(
val sourcesJar: ByteArray,
)

internal fun buildJars(
owner: String,
name: String,
version: String,
): Jars? {
internal fun ActionCoords.buildJars(): Jars? {
val binding =
generateBinding(owner = owner, name = name, version = version).also {
generateBinding(metadataRevision = NewestForVersion).also {
if (it.isEmpty()) return null
}
val (sourceFilePaths, compilationInputDir) = binding.prepareDirectoryWithSources()
Expand All @@ -52,22 +48,6 @@ internal fun buildJars(
)
}

private fun generateBinding(
owner: String,
name: String,
version: String,
): List<ActionBinding> {
val actionCoords =
ActionCoords(
owner = owner,
name = name,
version = version,
)
return actionCoords.generateBinding(
metadataRevision = NewestForVersion,
)
}

private fun compileBinding(sourceFilePaths: List<Path>): Path {
val compilationOutput = createTempDirectory(prefix = "gwkt-classes_")

Expand Down
Original file line number Diff line number Diff line change
@@ -1,67 +1,68 @@
package io.github.typesafegithub.workflows.mavenbinding

internal fun buildModuleFile(
owner: String,
name: String,
version: String,
): String =
"""
{
"formatVersion": "1.1",
"component": {
"group": "$owner",
"module": "$name",
"version": "$version",
"attributes": {
"org.gradle.status": "release"
}
},
"createdBy": {
"gradle": {
"version": "8.7"
}
},
"variants": [
import io.github.typesafegithub.workflows.actionbindinggenerator.domain.ActionCoords

internal fun ActionCoords.buildModuleFile() =
run {
val name = name.replace("__", "/")
"""
{
"name": "apiElements",
"attributes": {
"org.gradle.category": "library",
"org.gradle.dependency.bundling": "external",
"org.gradle.jvm.environment": "standard-jvm",
"org.gradle.jvm.version": 11,
"org.gradle.libraryelements": "jar",
"org.gradle.usage": "java-api",
"org.jetbrains.kotlin.platform.type": "jvm"
"formatVersion": "1.1",
"component": {
"group": "$owner",
"module": "$name",
"version": "$version",
"attributes": {
"org.gradle.status": "release"
}
},
"dependencies": [],
"files": [
{
"name": "$name-$version.jar",
"url": "$name-$version.jar",
"size": 1
"createdBy": {
"gradle": {
"version": "8.7"
}
]
},
{
"name": "runtimeElements",
"attributes": {
"org.gradle.category": "library",
"org.gradle.dependency.bundling": "external",
"org.gradle.jvm.environment": "standard-jvm",
"org.gradle.jvm.version": 11,
"org.gradle.libraryelements": "jar",
"org.gradle.usage": "java-runtime",
"org.jetbrains.kotlin.platform.type": "jvm"
},
"dependencies": [],
"files": [
"variants": [
{
"name": "apiElements",
"attributes": {
"org.gradle.category": "library",
"org.gradle.dependency.bundling": "external",
"org.gradle.jvm.environment": "standard-jvm",
"org.gradle.jvm.version": 11,
"org.gradle.libraryelements": "jar",
"org.gradle.usage": "java-api",
"org.jetbrains.kotlin.platform.type": "jvm"
},
"dependencies": [],
"files": [
{
"name": "$name-$version.jar",
"url": "$name-$version.jar",
"size": 1
}
]
},
{
"name": "$name-$version.jar",
"url": "$name-$version.jar",
"size": 1
"name": "runtimeElements",
"attributes": {
"org.gradle.category": "library",
"org.gradle.dependency.bundling": "external",
"org.gradle.jvm.environment": "standard-jvm",
"org.gradle.jvm.version": 11,
"org.gradle.libraryelements": "jar",
"org.gradle.usage": "java-runtime",
"org.jetbrains.kotlin.platform.type": "jvm"
},
"dependencies": [],
"files": [
{
"name": "$name-$version.jar",
"url": "$name-$version.jar",
"size": 1
}
]
}
]
}
]
""".trimIndent()
}
""".trimIndent()
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package io.github.typesafegithub.workflows.mavenbinding

import io.github.typesafegithub.workflows.actionbindinggenerator.domain.ActionCoords

internal const val LATEST_RELASED_LIBRARY_VERSION = "3.0.0"

internal fun buildPomFile(
owner: String,
name: String,
version: String,
): String {
val nameForRepo = name.substringBefore("/")
internal fun ActionCoords.buildPomFile(): String {
val nameForRepo = name.substringBefore("__")
return """
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ data class JarArtifact(
) : Artifact

fun ActionCoords.buildVersionArtifacts(): Map<String, Artifact>? {
val jars = buildJars(owner = owner, name = name.replace("__", "/"), version = version) ?: return null
val pom = buildPomFile(owner = owner, name = name.replace("__", "/"), version = version)
val module = buildModuleFile(owner = owner, name = name.replace("__", "/"), version = version)
val jars = buildJars() ?: return null
val pom = buildPomFile()
val module = buildModuleFile()
return mapOf(
"$name-$version.jar" to JarArtifact(jars.mainJar),
"$name-$version.jar.md5" to TextArtifact(jars.mainJar.md5Checksum()),
Expand Down

0 comments on commit 37375d9

Please sign in to comment.