Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement support for reading resource files in a symbol processor #2068

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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>
Comment on lines +41 to +42
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Obviously these would have to be documented before merging.

I also don't know how they would behave on non JVM targets which would have to be tested.

Comment on lines +41 to +42
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TODO: We'd need a way to associate an output file with a specific resource.


/**
* 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 ->
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TODO: Add a test that verifies that you cannot access things in parent directories.

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
Loading