-
Notifications
You must be signed in to change notification settings - Fork 295
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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. | ||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. | ||
|
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 -> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) } | ||
} |
There was a problem hiding this comment.
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.