mirror of
https://github.com/GayPizzaSpecifications/pork.git
synced 2025-08-03 05:10:55 +00:00
stdlib: generate stdlib.manifest
This commit is contained in:
parent
e8c984f2dc
commit
033da5b2ea
@ -2,3 +2,7 @@ plugins {
|
|||||||
id("gay.pizza.pork.module")
|
id("gay.pizza.pork.module")
|
||||||
id("gay.pizza.pork.ast")
|
id("gay.pizza.pork.ast")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
tasks.compileKotlin {
|
||||||
|
dependsOn(tasks.generateAstCode)
|
||||||
|
}
|
||||||
|
@ -33,5 +33,13 @@ gradlePlugin {
|
|||||||
displayName = "Pork Module"
|
displayName = "Pork Module"
|
||||||
description = "Module convention for pork"
|
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"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -15,10 +15,6 @@ import kotlin.io.path.deleteIfExists
|
|||||||
import kotlin.io.path.writeText
|
import kotlin.io.path.writeText
|
||||||
|
|
||||||
open class GenerateAstCode : DefaultTask() {
|
open class GenerateAstCode : DefaultTask() {
|
||||||
init {
|
|
||||||
outputs.upToDateWhen { false }
|
|
||||||
}
|
|
||||||
|
|
||||||
@get:InputFile
|
@get:InputFile
|
||||||
var astDescriptionFile: File = project.file("src/main/ast/pork.yml")
|
var astDescriptionFile: File = project.file("src/main/ast/pork.yml")
|
||||||
|
|
||||||
|
@ -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")
|
||||||
|
}
|
||||||
|
}
|
@ -2,9 +2,15 @@ package gay.pizza.pork.buildext
|
|||||||
|
|
||||||
import org.gradle.api.Plugin
|
import org.gradle.api.Plugin
|
||||||
import org.gradle.api.Project
|
import org.gradle.api.Project
|
||||||
|
import org.gradle.kotlin.dsl.create
|
||||||
|
|
||||||
class PorkAstPlugin : Plugin<Project> {
|
class PorkAstPlugin : Plugin<Project> {
|
||||||
override fun apply(target: 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)
|
||||||
}
|
}
|
||||||
|
@ -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)
|
||||||
|
}
|
@ -1,11 +1,19 @@
|
|||||||
plugins {
|
plugins {
|
||||||
id("gay.pizza.pork.module")
|
id("gay.pizza.pork.module")
|
||||||
|
id("gay.pizza.pork.stdlib")
|
||||||
}
|
}
|
||||||
|
|
||||||
tasks.processResources {
|
tasks.processResources {
|
||||||
|
dependsOn(tasks.generateStdlibManifest)
|
||||||
|
inputs.file(tasks.generateStdlibManifest.get().stdlibManifestFile)
|
||||||
|
|
||||||
from("src/main/pork") {
|
from("src/main/pork") {
|
||||||
into("pork/stdlib")
|
into("pork/stdlib")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
from(tasks.generateStdlibManifest.get().outputs) {
|
||||||
|
into("pork")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
|
@ -8,7 +8,7 @@ object PorkStdlib : ContentSource {
|
|||||||
private val stdlibClass = PorkStdlib::class.java
|
private val stdlibClass = PorkStdlib::class.java
|
||||||
|
|
||||||
private fun readManifestFiles(): List<String> {
|
private fun readManifestFiles(): List<String> {
|
||||||
val manifestContent = read("stdlib.manifest", check = false)
|
val manifestContent = read("stdlib.manifest")
|
||||||
return manifestContent.split("\n").filter { line ->
|
return manifestContent.split("\n").filter { line ->
|
||||||
val trimmed = line.trim()
|
val trimmed = line.trim()
|
||||||
trimmed.isNotEmpty() && !trimmed.startsWith("#")
|
trimmed.isNotEmpty() && !trimmed.startsWith("#")
|
||||||
@ -17,17 +17,21 @@ object PorkStdlib : ContentSource {
|
|||||||
|
|
||||||
val files: List<String> = readManifestFiles()
|
val files: List<String> = readManifestFiles()
|
||||||
|
|
||||||
private fun read(path: String, check: Boolean = true): String {
|
private fun readPorkFile(path: String): String {
|
||||||
if (check && !files.contains(path)) {
|
if (!files.contains(path)) {
|
||||||
throw RuntimeException("Stdlib does not contain file '${path}'")
|
throw RuntimeException("Stdlib does not contain file '${path}'")
|
||||||
}
|
}
|
||||||
val stream = stdlibClass.getResourceAsStream("/pork/stdlib/${path}")
|
return read("stdlib/${path}")
|
||||||
?: throw RuntimeException("Stdlib does not contain file '${path}'")
|
}
|
||||||
|
|
||||||
|
private fun read(path: String): String {
|
||||||
|
val stream = stdlibClass.getResourceAsStream("/pork/${path}")
|
||||||
|
?: throw RuntimeException("Unable to find file '${path}'")
|
||||||
return String(stream.readAllBytes())
|
return String(stream.readAllBytes())
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun loadAsCharSource(path: String): CharSource {
|
override fun loadAsCharSource(path: String): CharSource {
|
||||||
return StringCharSource(read(path))
|
return StringCharSource(readPorkFile(path))
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun stableContentIdentity(path: String): String {
|
override fun stableContentIdentity(path: String): String {
|
||||||
|
@ -1,3 +0,0 @@
|
|||||||
lang/prelude.pork
|
|
||||||
ffi/malloc.pork
|
|
||||||
numerics/operator.pork
|
|
Loading…
Reference in New Issue
Block a user