Skip to content

Commit

Permalink
stdlib: generate stdlib.manifest
Browse files Browse the repository at this point in the history
  • Loading branch information
azenla committed Sep 11, 2023
1 parent e8c984f commit 033da5b
Show file tree
Hide file tree
Showing 9 changed files with 87 additions and 14 deletions.
4 changes: 4 additions & 0 deletions ast/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,7 @@ plugins {
id("gay.pizza.pork.module")
id("gay.pizza.pork.ast")
}

tasks.compileKotlin {
dependsOn(tasks.generateAstCode)
}
8 changes: 8 additions & 0 deletions buildext/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,13 @@ gradlePlugin {
displayName = "Pork Module"
description = "Module convention for pork"
}

create("pork_stdlib") {
id = "gay.pizza.pork.stdlib"
implementationClass = "gay.pizza.pork.buildext.PorkStdlibPlugin"

displayName = "Pork Stdlib"
description = "Stdlib generation code for pork"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@ import kotlin.io.path.deleteIfExists
import kotlin.io.path.writeText

open class GenerateAstCode : DefaultTask() {
init {
outputs.upToDateWhen { false }
}

@get:InputFile
var astDescriptionFile: File = project.file("src/main/ast/pork.yml")

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package gay.pizza.pork.buildext

import org.gradle.api.DefaultTask
import org.gradle.api.file.RegularFile
import org.gradle.api.provider.Provider
import org.gradle.api.tasks.InputDirectory
import org.gradle.api.tasks.OutputFile
import org.gradle.api.tasks.TaskAction
import java.io.File
import kotlin.io.path.deleteIfExists
import kotlin.io.path.writeText

open class GenerateStdlibManifest : DefaultTask() {
@get:InputDirectory
val porkStdlibCode: File = project.file("src/main/pork")

@get:OutputFile
val stdlibManifestFile: Provider<RegularFile> =
project.layout.buildDirectory.file("generated/pork/stdlib.manifest")

@TaskAction
fun generate() {
val files = porkStdlibCode.walkTopDown().mapNotNull { file ->
if (!file.isFile || !file.name.endsWith(".pork")) {
null
} else {
file.relativeTo(porkStdlibCode).path
}
}
val manifestFilePath = stdlibManifestFile.get().asFile.toPath()
manifestFilePath.deleteIfExists()
manifestFilePath.writeText(files.joinToString("\n") + "\n")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,15 @@ package gay.pizza.pork.buildext

import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.kotlin.dsl.create

class PorkAstPlugin : Plugin<Project> {
override fun apply(target: Project) {
target.tasks.create("generateAstCode", GenerateAstCode::class.java)
val generateAstCode = createGenerateAstCode(target)
val processResources = target.tasks.getByName("processResources")
processResources.dependsOn(generateAstCode)
}

private fun createGenerateAstCode(project: Project): GenerateAstCode =
project.tasks.create("generateAstCode", GenerateAstCode::class)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package gay.pizza.pork.buildext

import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.kotlin.dsl.create

class PorkStdlibPlugin : Plugin<Project> {
override fun apply(target: Project) {
val generateStdlibManifest = createGenerateStdlibManifest(target)
val processResources = target.tasks.getByName("processResources")
processResources.dependsOn(generateStdlibManifest)
}

private fun createGenerateStdlibManifest(project: Project): GenerateStdlibManifest =
project.tasks.create("generateStdlibManifest", GenerateStdlibManifest::class)
}
8 changes: 8 additions & 0 deletions stdlib/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
plugins {
id("gay.pizza.pork.module")
id("gay.pizza.pork.stdlib")
}

tasks.processResources {
dependsOn(tasks.generateStdlibManifest)
inputs.file(tasks.generateStdlibManifest.get().stdlibManifestFile)

from("src/main/pork") {
into("pork/stdlib")
}

from(tasks.generateStdlibManifest.get().outputs) {
into("pork")
}
}

dependencies {
Expand Down
16 changes: 10 additions & 6 deletions stdlib/src/main/kotlin/gay/pizza/pork/stdlib/PorkStdlib.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ object PorkStdlib : ContentSource {
private val stdlibClass = PorkStdlib::class.java

private fun readManifestFiles(): List<String> {
val manifestContent = read("stdlib.manifest", check = false)
val manifestContent = read("stdlib.manifest")
return manifestContent.split("\n").filter { line ->
val trimmed = line.trim()
trimmed.isNotEmpty() && !trimmed.startsWith("#")
Expand All @@ -17,17 +17,21 @@ object PorkStdlib : ContentSource {

val files: List<String> = readManifestFiles()

private fun read(path: String, check: Boolean = true): String {
if (check && !files.contains(path)) {
private fun readPorkFile(path: String): String {
if (!files.contains(path)) {
throw RuntimeException("Stdlib does not contain file '${path}'")
}
val stream = stdlibClass.getResourceAsStream("/pork/stdlib/${path}")
?: throw RuntimeException("Stdlib does not contain file '${path}'")
return read("stdlib/${path}")
}

private fun read(path: String): String {
val stream = stdlibClass.getResourceAsStream("/pork/${path}")
?: throw RuntimeException("Unable to find file '${path}'")
return String(stream.readAllBytes())
}

override fun loadAsCharSource(path: String): CharSource {
return StringCharSource(read(path))
return StringCharSource(readPorkFile(path))
}

override fun stableContentIdentity(path: String): String {
Expand Down
3 changes: 0 additions & 3 deletions stdlib/src/main/pork/stdlib.manifest

This file was deleted.

0 comments on commit 033da5b

Please sign in to comment.