Skip to content

Commit

Permalink
Implement support for reading resource files in a symbol processor
Browse files Browse the repository at this point in the history
  • Loading branch information
ansman committed Aug 29, 2024
1 parent 1f7b4dc commit abd7527
Show file tree
Hide file tree
Showing 22 changed files with 368 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package com.google.devtools.ksp.processing

import com.google.devtools.ksp.KspExperimental
import com.google.devtools.ksp.symbol.*
import java.io.InputStream

/**
* [Resolver] provides [SymbolProcessor] with access to compiler details such as Symbols.
Expand All @@ -37,6 +38,9 @@ interface Resolver {
*/
fun getAllFiles(): Sequence<KSFile>

fun getResource(path: String): InputStream?
fun getAllResources(): Sequence<String>

/**
* Get all symbols with specified annotation.
* Note that in multiple round processing, only symbols from deferred symbols of last round and symbols from newly generated files will be returned in this function.
Expand Down
Empty file added api/src/main/resources/foo.txt
Empty file.
14 changes: 14 additions & 0 deletions common-deps/src/main/kotlin/com/google/devtools/ksp/KSPConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ abstract class KSPConfig(
val moduleName: String,
val sourceRoots: List<File>,
val commonSourceRoots: List<File>,
val resourceRoots: List<File>,
val libraries: List<File>,

val processorOptions: Map<String, String>,
Expand Down Expand Up @@ -36,6 +37,7 @@ abstract class KSPConfig(
abstract class Builder {
lateinit var moduleName: String
lateinit var sourceRoots: List<File>
lateinit var resourceRoots: List<File>
var commonSourceRoots: List<File> = emptyList()
var libraries: List<File> = emptyList()

Expand Down Expand Up @@ -72,6 +74,7 @@ class KSPJvmConfig(
moduleName: String,
sourceRoots: List<File>,
commonSourceRoots: List<File>,
resourceRoots: List<File>,
libraries: List<File>,

processorOptions: Map<String, String>,
Expand Down Expand Up @@ -99,6 +102,7 @@ class KSPJvmConfig(
moduleName,
sourceRoots,
commonSourceRoots,
resourceRoots,
libraries,

processorOptions,
Expand Down Expand Up @@ -142,6 +146,7 @@ class KSPJvmConfig(
moduleName,
sourceRoots,
commonSourceRoots,
resourceRoots,
libraries,

processorOptions,
Expand Down Expand Up @@ -174,6 +179,7 @@ class KSPNativeConfig(
moduleName: String,
sourceRoots: List<File>,
commonSourceRoots: List<File>,
resourceRoots: List<File>,
libraries: List<File>,

processorOptions: Map<String, String>,
Expand Down Expand Up @@ -201,6 +207,7 @@ class KSPNativeConfig(
moduleName,
sourceRoots,
commonSourceRoots,
resourceRoots,
libraries,

processorOptions,
Expand Down Expand Up @@ -235,6 +242,7 @@ class KSPNativeConfig(
moduleName,
sourceRoots,
commonSourceRoots,
resourceRoots,
libraries,

processorOptions,
Expand Down Expand Up @@ -267,6 +275,7 @@ class KSPJsConfig(
moduleName: String,
sourceRoots: List<File>,
commonSourceRoots: List<File>,
resourceRoots: List<File>,
libraries: List<File>,

processorOptions: Map<String, String>,
Expand Down Expand Up @@ -294,6 +303,7 @@ class KSPJsConfig(
moduleName,
sourceRoots,
commonSourceRoots,
resourceRoots,
libraries,

processorOptions,
Expand Down Expand Up @@ -328,6 +338,7 @@ class KSPJsConfig(
moduleName,
sourceRoots,
commonSourceRoots,
resourceRoots,
libraries,

processorOptions,
Expand Down Expand Up @@ -365,6 +376,7 @@ class KSPCommonConfig(
moduleName: String,
sourceRoots: List<File>,
commonSourceRoots: List<File>,
resourceRoots: List<File>,
libraries: List<File>,

processorOptions: Map<String, String>,
Expand Down Expand Up @@ -392,6 +404,7 @@ class KSPCommonConfig(
moduleName,
sourceRoots,
commonSourceRoots,
resourceRoots,
libraries,

processorOptions,
Expand Down Expand Up @@ -426,6 +439,7 @@ class KSPCommonConfig(
moduleName,
sourceRoots,
commonSourceRoots,
resourceRoots,
libraries,

processorOptions,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.google.devtools.ksp.common

import java.io.File
import java.io.FileInputStream
import java.io.FileNotFoundException
import java.io.InputStream

fun Iterable<File>.getResource(path: String): InputStream? = firstNotNullOfOrNull { root ->
val file = root.resolve(path)
try {
if (file.toPath().normalize().startsWith(root.toPath().normalize())) {
FileInputStream(file)
} else {
null
}
} catch (e: FileNotFoundException) {
null
}
}

fun Iterable<File>.getAllResources(): Sequence<String> =
asSequence().flatMap { root ->
println(root)
root
.walkTopDown()
.filter { it.isFile }
.map { it.toRelativeString(root) }
}
Original file line number Diff line number Diff line change
Expand Up @@ -219,11 +219,18 @@ abstract class AbstractKotlinSymbolProcessingExtension(

// dirtyFiles cannot be reused because they are created in the old container.
val resolver = ResolverImpl(
module,
ksFiles.filterNot {
module = module,
allKSFiles = ksFiles.filterNot {
it.filePath in cleanFilenames
},
newFiles, deferredSymbols, bindingTrace, project, componentProvider, incrementalContext, options
newKSFiles = newFiles,
resourceRoots = options.resourceRoots,
deferredSymbols = deferredSymbols,
bindingTrace = bindingTrace,
project = project,
componentProvider = componentProvider,
incrementalContext = incrementalContext,
options = options
)

if (!initialized) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class KspOptions(
val projectBaseDir: File,
val compileClasspath: List<File>,
val javaSourceRoots: List<File>,
val resourceRoots: List<File>,

val classOutputDir: File,
val javaOutputDir: File,
Expand Down Expand Up @@ -66,6 +67,7 @@ class KspOptions(
var projectBaseDir: File? = null
val compileClasspath: MutableList<File> = mutableListOf()
val javaSourceRoots: MutableList<File> = mutableListOf()
val resourceRoots: MutableList<File> = mutableListOf()

var classOutputDir: File? = null
var javaOutputDir: File? = null
Expand Down Expand Up @@ -103,6 +105,7 @@ class KspOptions(
requireNotNull(projectBaseDir) { "A non-null projectBaseDir must be provided" },
compileClasspath,
javaSourceRoots,
resourceRoots,
requireNotNull(classOutputDir) { "A non-null classOutputDir must be provided" },
requireNotNull(javaOutputDir) { "A non-null javaOutputDir must be provided" },
requireNotNull(kotlinOutputDir) { "A non-null kotlinOutputDir must be provided" },
Expand Down Expand Up @@ -174,6 +177,14 @@ enum class KspCliOption(
false
),

RESOURCE_ROOT_OPTION(
"resourceRoot",
"<resourceRoot>",
"a root directory for resources",
false,
allowMultipleOccurrences = true
),

RESOURCE_OUTPUT_DIR_OPTION(
"resourceOutputDir",
"<resourceOutputDir>",
Expand Down Expand Up @@ -322,6 +333,7 @@ fun KspOptions.Builder.processOption(option: KspCliOption, value: String) = when
KspCliOption.CLASS_OUTPUT_DIR_OPTION -> classOutputDir = File(value)
KspCliOption.JAVA_OUTPUT_DIR_OPTION -> javaOutputDir = File(value)
KspCliOption.KOTLIN_OUTPUT_DIR_OPTION -> kotlinOutputDir = File(value)
KspCliOption.RESOURCE_ROOT_OPTION -> resourceRoots.add(File(value))
KspCliOption.RESOURCE_OUTPUT_DIR_OPTION -> resourceOutputDir = File(value)
KspCliOption.CACHES_DIR_OPTION -> cachesDir = File(value)
KspCliOption.KSP_OUTPUT_DIR_OPTION -> kspOutputDir = File(value)
Expand Down
Loading

0 comments on commit abd7527

Please sign in to comment.