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 ee6319f
Show file tree
Hide file tree
Showing 11 changed files with 100 additions and 195 deletions.
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package io.github.zuccherosintattico.gradle

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

open class CheckNodeTask : DefaultTask() {
init {
group = "Node"
description = "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 ee6319f

Please sign in to comment.