Skip to content

Commit

Permalink
feat(plugin): add first try of task
Browse files Browse the repository at this point in the history
  • Loading branch information
manuandru committed Mar 12, 2024
1 parent 78b9fcb commit 44a92ca
Show file tree
Hide file tree
Showing 13 changed files with 109 additions and 197 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/build-and-deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ jobs:
&& !github.event.repository.fork
&& github.event_name != 'pull_request'
}}
maven-central-username: ${{ secrets.MAVEN_CENTRAL_USERNAME }}
maven-central-password: ${{ secrets.MAVEN_CENTRAL_PASSWORD }}
signing-key: ${{ secrets.SIGNING_KEY }}
signing-password: ${{ secrets.SIGNING_PASSWORD }}
Expand Down Expand Up @@ -75,7 +76,7 @@ jobs:
github-token: ${{ github.token }}
gradle-publish-secret: ${{ secrets.GRADLE_PUBLISH_SECRET }}
gradle-publish-key: ${{ secrets.GRADLE_PUBLISH_KEY }}
maven-central-username: 'zucchero-sintattico'
maven-central-username: ${{ secrets.MAVEN_CENTRAL_USERNAME }}
maven-central-password: ${{ secrets.MAVEN_CENTRAL_PASSWORD }}
signing-key: ${{ secrets.SIGNING_KEY }}
signing-password: ${{ secrets.SIGNING_PASSWORD }}
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@
build/
.idea/
node_modules/

.DS_Store
2 changes: 1 addition & 1 deletion settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ gradleEnterprise {

gitHooks {
preCommit {
tasks("ktlintCheck")
tasks("ktlintCheck", "detekt", "--parallel")
}
commitMsg { conventionalCommits() }
createHooks(true)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package io.github.zuccherosintattico.gradle

import org.gradle.api.DefaultTask
import org.gradle.api.tasks.TaskAction

/**
* A task to check if Node is installed.
*/
open class CheckNodeTask : DefaultTask() {
init {
group = "Node"
description = "Check if Node is installed"
}

/**
* The task action to check if Node is installed.
*/
@TaskAction
fun checkNode() {
val res = Runtime.getRuntime().runCatching { exec("node --version").waitFor() }
if (res.isSuccess) {
logger.quiet("Node is installed")
} else {
logger.error("Node is not installed")
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,12 @@ import java.io.Serializable
open class Typescript : Plugin<Project> {
override fun apply(project: Project) {
val extension = project.extensions.create<TypescriptExtension>("typescript")
project.tasks.register<TypescriptTask>("hello") {
author.set(extension.author)

val checkNodeTask = project.tasks.register<CheckNodeTask>("checkNode")

project.tasks.register<TypescriptTask>("compileTypescript") {
dependsOn(checkNodeTask)
sourceSet.set(extension.sourceSet)
}
}
}
Expand All @@ -35,13 +39,13 @@ open class TypescriptTask : DefaultTask() {
* Just a template.
*/
@Input
val author: Property<String> = project.objects.property()
val sourceSet: Property<String> = project.objects.property()

/**
* Read-only property calculated from the greeting.
*/
@Internal
val message: Provider<String> = author.map { "Hello from $it" }
val message: Provider<String> = sourceSet.map { "Hello from $it" }

/**
* Just a template.
Expand All @@ -60,7 +64,7 @@ open class TypescriptExtension(objects: ObjectFactory) : Serializable {
/**
* Just a template.
*/
val author: Property<String> = objects.property()
val sourceSet: Property<String> = objects.property<String>().convention("src/main/typescript")

companion object {
private const val serialVersionUID = 1L
Expand Down
52 changes: 52 additions & 0 deletions src/test/kotlin/io/github/zuccherosintattico/gradle/MyTests.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package io.github.zuccherosintattico.gradle

import io.kotest.core.spec.style.AnnotationSpec
import io.kotest.matchers.paths.shouldExist
import org.gradle.testkit.runner.GradleRunner
import java.io.File
import java.nio.file.Files
import kotlin.io.path.createTempDirectory

class MyTests : AnnotationSpec() {

@BeforeAll
fun setup() {
val testkitProperties = javaClass.classLoader.getResource("testkit-gradle.properties")?.readText()
checkNotNull(testkitProperties) {
"No file testkit-gradle.properties was generated"
}
}

@Test
fun `test the plugin`() {
val folder = createTempDirectory("test")
folder.shouldExist()

val testResources = File("src/test/resources/plugin-base-env").toPath()
Files.walk(testResources)
.filter { it != testResources }
.map { testResources.relativize(it) }
.map { testResources.resolve(it) to folder.resolve(it) }
.forEach { (source, destination) ->
Files.createDirectories(destination.parent)
Files.copy(source, destination)
}

println(folder.toFile())

val result = with(GradleRunner.create()) {
withProjectDir(folder.toFile())
withArguments("compileTypescript", "--stacktrace")
withPluginClasspath()
build()
}

result.output.lines().forEach { println(it) }

// ProcessBuilder("npx", "tsc", "--outDir", "./build/bin", "src/main/typescript/*.ts")
// .directory(folder.toFile())
// .inheritIO()
// .start()
// .waitFor()
}
}
75 changes: 0 additions & 75 deletions src/test/kotlin/org/danilopianini/gradle/test/TestingDSL.kt

This file was deleted.

89 changes: 0 additions & 89 deletions src/test/kotlin/org/danilopianini/gradle/test/Tests.kt

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

6 changes: 6 additions & 0 deletions src/test/resources/plugin-base-env/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
plugins {
id("io.github.zuccherosintattico.typescript-gradle-plugin")
}
typescript {
sourceSet = "src/main/typescript"
}
10 changes: 10 additions & 0 deletions src/test/resources/plugin-base-env/src/main/typescript/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class Person {
constructor(public name: string, public age: number) {
}
toString() {
return `${this.name} is ${this.age} years old`;
}
}

let p = new Person("John", 42);
console.log(p.toString());

0 comments on commit 44a92ca

Please sign in to comment.