Skip to content

Commit

Permalink
chore: Add automatic search to VSS files
Browse files Browse the repository at this point in the history
  • Loading branch information
Chrylo committed Feb 2, 2024
1 parent 09411de commit daec923
Show file tree
Hide file tree
Showing 10 changed files with 156 additions and 58 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/deploy-snapshot.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ concurrency: snapshot
on:
push:
branches:
- main
- feature-45-work

jobs:
deployment:
Expand Down
13 changes: 3 additions & 10 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@

@file:Suppress("UnstableApiUsage")

import com.google.devtools.ksp.gradle.KspTask
import org.eclipse.kuksa.property.PropertiesLoader
import org.eclipse.kuksa.vssprocessor.plugin.ProvideVssDefinitionTask

plugins {
id("com.android.application")
Expand Down Expand Up @@ -126,14 +124,9 @@ android {
}
}

tasks.register<ProvideVssDefinitionTask>("ProvideVssDefinition") {
val vssDefinitionFilePath = "$projectDir/src/main/assets/vss_rel_4.0.yaml"
val regularFile = RegularFile { File(vssDefinitionFilePath) }
vssDefinitionFile.add(regularFile)
}

tasks.withType<KspTask> {
dependsOn(tasks.withType<ProvideVssDefinitionTask>())
vssProcessor {
searchPath = "$projectDir/src/main/assets"
fileName = "vss_rel_4.0.yaml"
}

tasks.withType<Test>().configureEach {
Expand Down
30 changes: 30 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,33 @@ tasks.register("mergeDashFiles") {
}
}
}

// Tasks for included composite builds need to be called separately. For convenience sake we depend on the most used
// tasks. Every task execution of the main project will then be forwarded to the included build project.
//
// We have to manually define the task names because the task() method of the included build throws an error for any
// unknown task.
val dependentCompositeTasks = setOf(
"setSnapshotVersion",
"setReleaseVersion",
"publishToMavenLocal",
"publishAllPublicationsToOSSRHSnapshotRepository",
"publishAllPublicationsToOSSRHReleaseRepository",
)

gradle.projectsEvaluated {
val subProjectTasks = tasks + subprojects.flatMap { it.tasks }

subProjectTasks
.filter { dependentCompositeTasks.contains(it.name) }
.forEach { task ->
val compositeTask = gradle.includedBuilds.map { compositeBuild ->
val compositeTaskPath = task.path.substringAfterLast(":")
println("Linking composite task - ${compositeBuild.name} <-> ${task.project}:${task.name}")

compositeBuild.task(":$compositeTaskPath")
}

task.dependsOn(compositeTask)
}
}
23 changes: 1 addition & 22 deletions kuksa-sdk/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ dependencies {
testImplementation(libs.mockk)
}

configure<Publish_gradle.PublishPluginExtension> {
publish {
mavenPublicationName = "release"
componentName = "release"
description = "Android Connectivity Library for the KUKSA Databroker"
Expand Down Expand Up @@ -123,25 +123,4 @@ protobuf {
}
}

// Tasks for included composite builds need to be called separately. For convenience sake we depend on the most used
// tasks. Every task execution of the main project will then be forwarded to the included build project.
//
// We have to manually define the task names because the task() method of the included build throws an error for any
// unknown task.
val dependentCompositeTasks = setOf(
"setSnapshotVersion",
"setReleaseVersion",
"publishToMavenLocal",
"publishAllPublicationsToOSSRHSnapshotRepository",
"publishAllPublicationsToOSSRHReleaseRepository",
)
tasks
.filter { dependentCompositeTasks.contains(it.name) }
.forEach { task ->
val compositeTask = gradle.includedBuilds.map { compositeBuild ->
val compositeTaskPath = task.path.substringAfter(":kuksa-sdk")
compositeBuild.task(compositeTaskPath)
}

task.dependsOn(compositeTask)
}
14 changes: 3 additions & 11 deletions samples/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
import com.google.devtools.ksp.gradle.KspTask
import org.eclipse.kuksa.vssprocessor.plugin.ProvideVssDefinitionTask

/*
* Copyright (c) 2023 Contributors to the Eclipse Foundation
*
Expand Down Expand Up @@ -58,14 +55,9 @@ android {
}
}

tasks.register<ProvideVssDefinitionTask>("ProvideVssDefinition") {
val vssDefinitionFilePath = "$projectDir/src/main/assets/vss_rel_4.0.yaml"
val regularFile = RegularFile { File(vssDefinitionFilePath) }
vssDefinitionFile.add(regularFile)
}

tasks.withType<KspTask> {
dependsOn(tasks.withType<ProvideVssDefinitionTask>())
vssProcessor {
searchPath = "$projectDir/src/main/assets"
fileName = "vss_rel_4.0.yaml"
}

dependencies {
Expand Down
2 changes: 1 addition & 1 deletion vss-core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ tasks.withType<Test>().configureEach {
}
}

configure<Publish_gradle.PublishPluginExtension> {
publish {
mavenPublicationName = "release"
componentName = "java"
description = "Vehicle Signal Specification (VSS) Core Module of the KUKSA SDK"
Expand Down
2 changes: 1 addition & 1 deletion vss-processor-plugin/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,6 @@ dependencies {
implementation(kotlin("stdlib"))
}

configure<Publish_gradle.PublishPluginExtension> {
publish {
description = "Vehicle Signal Specification (VSS) Plugin of the KUKSA SDK"
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,40 +22,144 @@ import org.gradle.api.DefaultTask
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.file.RegularFile
import org.gradle.api.model.ObjectFactory
import org.gradle.api.provider.ListProperty
import org.gradle.api.provider.Property
import org.gradle.api.tasks.InputFiles
import org.gradle.api.tasks.TaskAction
import org.gradle.kotlin.dsl.create
import org.gradle.kotlin.dsl.register
import java.io.File
import javax.inject.Inject

open class VssProcessorPluginExtension
@Inject
internal constructor(objectFactory: ObjectFactory) {
val searchPath: Property<String> = objectFactory.property(String::class.java).convention("")
val fileName: Property<String> = objectFactory.property(String::class.java).convention("")
}

/**
* This Plugin searches for compatible specification files and copies them into an input folder for the
* KSP VSS Processor. This is necessary because the Symbol Processor does not have access to the android assets folder.
* The default search path is the main assets folder.
*
*/
class VssProcessorPlugin : Plugin<Project> {
override fun apply(project: Project) {
// Configuration done by the custom task
val extension = project.extensions.create<VssProcessorPluginExtension>(EXTENSION_NAME)

val buildDirPath = project.buildDir.absolutePath
val mainAssetsDirectory = "${project.projectDir}${fileSeparator}$mainAssetsDirectory"
val searchDirectory = extension.searchPath.get().ifEmpty { mainAssetsDirectory }

val compatibleFiles = findFiles(
directory = searchDirectory,
fileName = extension.fileName.get(),
validExtensions = compatibleExtensions
)

// compatibleFiles.forEach { vssDefinitionInputFile ->
// val vssDefinitionBuildFile = File(
// "$buildDirPath$fileSeparator" +
// "${KSP_INPUT_BUILD_DIRECTORY}$fileSeparator" +
// vssDefinitionInputFile.name,
// )
//
//// println(
//// "Found VSS definition input file: ${vssDefinitionInputFile.name}, " +
//// "copying to: $vssDefinitionBuildFile",
//// )
//
// vssDefinitionInputFile.copyTo(vssDefinitionBuildFile, true)
// }

val provideVssDefinitionTask = project.tasks.register<ProvideVssDefinitionTask>("ProvideVssDefinition") {
compatibleFiles.forEach { definitionFile ->
val regularFile = RegularFile { definitionFile }
vssDefinitionFiles.add(regularFile)
}
}

project.tasks.getByName("preBuild").finalizedBy(
provideVssDefinitionTask.get()
)

// project.afterEvaluate {
project.tasks.forEach {
// println("HIHI $it")
// }
}

// provideVssDefinitionTask.get().
}

private fun findFiles(
directory: String,
fileName: String = "",
validExtensions: Collection<String>,
): Sequence<File> {
val mainAssetsFolder = File(directory)

return mainAssetsFolder
.walk()
.filter { validExtensions.contains(it.extension) }
.filter { file ->
if (fileName.isEmpty()) return@filter true

file.name == fileName
}
}

companion object {
private const val EXTENSION_NAME = "vssProcessor"

private val compatibleExtensions = setOf("yaml")
private val fileSeparator = File.separator
private val mainAssetsDirectory = "src$fileSeparator" + "main$fileSeparator" + "assets"
}
}

/**
* This task takes a list of files and copies them into an input folder for the KSP VSS Processor. This is necessary
* because the Symbol Processor does not have access to the android assets folder.
*/
abstract class ProvideVssDefinitionTask : DefaultTask() {

@get:InputFiles
abstract val vssDefinitionFile: ListProperty<RegularFile>
abstract val vssDefinitionFiles: ListProperty<RegularFile>

@TaskAction
fun provideFile() {
// val buildDirPath = project.buildDir.absolutePath
// val mainAssets = "${project.projectDir}$fileSeparator$mainAssetsDirectory"
// val mainAssetsFolder = File(mainAssets)

// mainAssetsFolder
// .walk()
// .filter { compatibleExtensions.contains(it.extension) }
// .forEach { vssDefinitionInputFile ->
// val vssDefinitionBuildFile = File(
// "$buildDirPath$fileSeparator" +
// "$KSP_INPUT_BUILD_DIRECTORY$fileSeparator" +
// vssDefinitionInputFile.name,
// )
//
// println(
// "Found VSS definition input file: ${vssDefinitionInputFile.name}, " +
// "copying to: $vssDefinitionBuildFile",
// )
//
// vssDefinitionInputFile.copyTo(vssDefinitionBuildFile, true)
// }

val buildDirPath = project.buildDir.absolutePath
vssDefinitionFile.get().forEach { file ->
vssDefinitionFiles.get().forEach { file ->
val vssDefinitionInputFile = file.asFile
println("Searching for VSS file: ${vssDefinitionInputFile.absolutePath}")
val vssDefinitionBuildFile = File(
"$buildDirPath$fileSeparator" +
"$KSP_INPUT_BUILD_DIRECTORY$fileSeparator" +
vssDefinitionInputFile.name,
)

println(
"Found VSS definition input file: ${vssDefinitionInputFile.name}, copying to: $vssDefinitionBuildFile",
)
println("Found VSS input file: ${vssDefinitionInputFile.name}, copying to: $vssDefinitionBuildFile")

vssDefinitionInputFile.copyTo(vssDefinitionBuildFile, true)
}
Expand Down
2 changes: 1 addition & 1 deletion vss-processor/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ tasks.withType<Test>().configureEach {
}
}

configure<Publish_gradle.PublishPluginExtension> {
publish {
mavenPublicationName = "release"
componentName = "java"
description = "Vehicle Signal Specification (VSS) Code Generator for the KUKSA SDK"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ class VssDefinitionProcessor(
private fun loadAssetFile(fileName: String): File? {
val generatedFile = codeGenerator.generatedFile.firstOrNull() ?: return null
val generationPath = generatedFile.absolutePath
val buildPath = generationPath.replaceAfter("$BUILD_FOLDER_NAME$fileSeparator", "")
val buildPath = generationPath.replaceAfterLast("$BUILD_FOLDER_NAME$fileSeparator", "")
val kspInputFilePath = "$buildPath$fileSeparator$KSP_INPUT_BUILD_DIRECTORY"
val kspInputFolder = File(kspInputFilePath)

Expand Down

0 comments on commit daec923

Please sign in to comment.