Skip to content

Commit

Permalink
Add task dependencies and unpacking functionality in KlangPlugin
Browse files Browse the repository at this point in the history
Extended the KlangPlugin with task dependencies for "generateBinding" and introduced a new task "unpackCHeader". Functionality to unzip files from the classpath was also added. Optimized the existing operations by adding task conditions to avoid redundant executions. Updated the parsing mechanism for improved handling of Function type string conversions.
  • Loading branch information
Alexandre Mommers committed Jan 29, 2024
1 parent bb49d14 commit c34a1b6
Showing 1 changed file with 40 additions and 2 deletions.
42 changes: 40 additions & 2 deletions klang/gradle-plugin/src/main/kotlin/io/ygdrasil/KlangPlugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import java.io.File
import java.io.FileInputStream
import java.net.URL
import java.nio.file.Files
import java.nio.file.Paths
import java.nio.file.StandardCopyOption
import java.security.MessageDigest
import java.util.zip.ZipInputStream
Expand All @@ -36,10 +37,14 @@ enum class ParsingMethod {
internal sealed class KlangPluginTask {
// TODO use a value object instead of a string
class DownloadFile(val sourceUrl: URL, val targetFile: String) : KlangPluginTask()

// TODO use a value object instead of a string
class Unpack(val sourceFile: String, val targetPath: String) : KlangPluginTask()

// TODO use a value object instead of a string
class Parse(val sourceFile: String, val sourcePath: String, val onSuccess: DeclarationRepository.() -> Unit) : KlangPluginTask()
class Parse(val sourceFile: String, val sourcePath: String, val onSuccess: DeclarationRepository.() -> Unit) :
KlangPluginTask()

// TODO use a value object instead of a string
class GenerateBinding(val basePackage: String, val libraryName: String) : KlangPluginTask()
}
Expand Down Expand Up @@ -80,11 +85,13 @@ class KlangPlugin : Plugin<Project> {
override fun apply(project: Project) {
val extension = project.extensions.create("klang", KlangPluginExtension::class.java)

val unpackCHeader = project.unpackCHeaderTask()
val downloadFile = project.downloadTask(extension)
val unpackFile = project.unpackTask(downloadFile, extension)
val generateAst = project.generateAstTask(unpackFile, extension)
val generateBinding = project.task("generateBinding") { task ->
task.dependsOn(generateAst)
task.dependsOn(unpackCHeader)
task.doFirst {
extension.tasks
.asSequence()
Expand Down Expand Up @@ -124,7 +131,7 @@ class KlangPlugin : Plugin<Project> {
assert(localFileToParse.canRead()) { "${localFileToParse.absolutePath} is not readable" }
assert(localFileToParse.length() > 0) { "${localFileToParse.absolutePath} is empty" }

extension.declarations = when(extension.parsingMethod) {
extension.declarations = when (extension.parsingMethod) {
ParsingMethod.Docker -> {
val jsonFile = workingDirectory.resolve("${fileToParse.hash}.json")
generateAstFromDocker(
Expand All @@ -134,6 +141,7 @@ class KlangPlugin : Plugin<Project> {
)
parseAstJson(jsonFile.absolutePath)
}

ParsingMethod.Libclang -> {
parseFile(
fileToParse,
Expand Down Expand Up @@ -197,6 +205,36 @@ class KlangPlugin : Plugin<Project> {
}
}
}

private fun Project.unpackCHeaderTask(): Task = task("unpackCHeader") { task ->
val targetPath = workingDirectory.resolve("c-headers")
task.onlyIf { !targetPath.exists() }
task.doFirst {
unzipFromClasspath("c-header.zip", targetPath.absolutePath)
}
}


fun unzipFromClasspath(sourceFile: String, targetPath: String) {
File(targetPath).mkdirs()
// Load the zip file from the classpath
val inputStream = KlangPlugin::class.java.getResourceAsStream(sourceFile)

ZipInputStream(inputStream).use { zipInputStream ->
generateSequence { zipInputStream.nextEntry }
.forEach { entry ->
// Be sure to adjust the file path as needed
if (!entry.isDirectory) {
Files.copy(
zipInputStream,
Paths.get(targetPath, entry.name)
)
}
}

}

}
}

private fun DeclarationRepository.generateKotlinFiles(outputDirectory: File, basePackage: String, libraryName: String) {
Expand Down

0 comments on commit c34a1b6

Please sign in to comment.