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
A new CodeGenerator.createFile method with more flexibility #1041
Merged
Merged
Changes from 4 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
eaed0b8
Support creating files with a more flexible API
jameskleeh 4cb564f
Add test
jameskleeh afd9699
Add space before arrow
jameskleeh 87f66b2
Remove accidental addition and add javadoc for FileType
jameskleeh 9a6bd2d
Use extension and prevent escaping the target base directory with ..
jameskleeh 4a2b1db
Merge branch 'main' of https://github.com/google/ksp into create-file…
jameskleeh 9cb52bd
Fix tests
jameskleeh 802e34c
Fix tests
jameskleeh d5ec223
Change associate api to use extension
jameskleeh 3652cb6
Remove FileType
jameskleeh ec7a486
Merge branch 'main' into create-file-flexibility
jameskleeh 8f38668
Fix test
jameskleeh dbefb20
Fix line length
jameskleeh c541217
Update api
jameskleeh File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
api/src/main/kotlin/com/google/devtools/ksp/processing/FileType.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.google.devtools.ksp.processing | ||
|
||
/** | ||
* Used to determine where files should be stored when being | ||
* created through the [CodeGenerator]. | ||
*/ | ||
enum class FileType { | ||
CLASS, | ||
JAVA_SOURCE, | ||
KOTLIN_SOURCE, | ||
RESOURCE | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
common-util/src/test/kotlin/com/google/devtools/ksp/processing/impl/CodeGeneratorImplTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package com.google.devtools.ksp.processing.impl | ||
|
||
import com.google.devtools.ksp.AnyChanges | ||
import com.google.devtools.ksp.processing.CodeGenerator | ||
import com.google.devtools.ksp.processing.Dependencies | ||
import com.google.devtools.ksp.processing.FileType | ||
import org.junit.Assert | ||
import org.junit.Before | ||
import org.junit.Test | ||
import java.io.File | ||
import java.nio.file.Files | ||
|
||
class CodeGeneratorImplTest { | ||
|
||
lateinit var codeGenerator: CodeGenerator | ||
lateinit var baseDir: File | ||
|
||
@Before | ||
fun setup() { | ||
baseDir = Files.createTempDirectory("project").toFile() | ||
val classesDir = File(baseDir, "classes") | ||
classesDir.mkdir() | ||
val javaDir = File(baseDir, "java") | ||
javaDir.mkdir() | ||
val kotlinDir = File(baseDir, "kotlin") | ||
kotlinDir.mkdir() | ||
val resourcesDir = File(baseDir, "resources") | ||
resourcesDir.mkdir() | ||
codeGenerator = CodeGeneratorImpl( | ||
classesDir, | ||
javaDir, | ||
kotlinDir, | ||
resourcesDir, | ||
baseDir, | ||
AnyChanges(baseDir), | ||
emptyList(), | ||
true | ||
) | ||
} | ||
|
||
@Test | ||
fun testCreatingAFile() { | ||
codeGenerator.createNewFile(Dependencies.ALL_FILES, "a.b.c", "Test", "java") | ||
codeGenerator.createNewFile(Dependencies.ALL_FILES, "a.b.c", "Test", "kt") | ||
codeGenerator.createNewFile(Dependencies.ALL_FILES, "a.b.c", "Test", "class") | ||
codeGenerator.createNewFile(Dependencies.ALL_FILES, "a.b.c", "Test", "") | ||
|
||
val files = codeGenerator.generatedFile.toList() | ||
Assert.assertEquals(File(baseDir, "java/a/b/c/Test.java"), files[0]) | ||
Assert.assertEquals(File(baseDir, "kotlin/a/b/c/Test.kt"), files[1]) | ||
Assert.assertEquals(File(baseDir, "classes/a/b/c/Test.class"), files[2]) | ||
Assert.assertEquals(File(baseDir, "resources/a/b/c/Test"), files[3]) | ||
} | ||
|
||
@Test | ||
fun testCreatingAFileWithSlash() { | ||
codeGenerator.createNewFile(Dependencies.ALL_FILES, "a/b/c", "Test", "java") | ||
codeGenerator.createNewFile(Dependencies.ALL_FILES, "a/b/c", "Test", "kt") | ||
codeGenerator.createNewFile(Dependencies.ALL_FILES, "a/b/c", "Test", "class") | ||
codeGenerator.createNewFile(Dependencies.ALL_FILES, "a/b/c", "Test", "") | ||
|
||
val files = codeGenerator.generatedFile.toList() | ||
Assert.assertEquals(File(baseDir, "java/a/b/c/Test.java"), files[0]) | ||
Assert.assertEquals(File(baseDir, "kotlin/a/b/c/Test.kt"), files[1]) | ||
Assert.assertEquals(File(baseDir, "classes/a/b/c/Test.class"), files[2]) | ||
Assert.assertEquals(File(baseDir, "resources/a/b/c/Test"), files[3]) | ||
} | ||
|
||
@Test | ||
fun testCreatingAFileWithPath() { | ||
codeGenerator.createNewFile(Dependencies.ALL_FILES, "a/b/c/Test.java", FileType.JAVA_SOURCE) | ||
codeGenerator.createNewFile(Dependencies.ALL_FILES, "a/b/c/Test.kt", FileType.KOTLIN_SOURCE) | ||
codeGenerator.createNewFile(Dependencies.ALL_FILES, "a/b/c/Test.class", FileType.CLASS) | ||
codeGenerator.createNewFile(Dependencies.ALL_FILES, "a/b/c/Test", FileType.RESOURCE) | ||
|
||
val files = codeGenerator.generatedFile.toList() | ||
Assert.assertEquals(File(baseDir, "java/a/b/c/Test.java"), files[0]) | ||
Assert.assertEquals(File(baseDir, "kotlin/a/b/c/Test.kt"), files[1]) | ||
Assert.assertEquals(File(baseDir, "classes/a/b/c/Test.class"), files[2]) | ||
Assert.assertEquals(File(baseDir, "resources/a/b/c/Test"), files[3]) | ||
} | ||
|
||
@Test | ||
fun testCreatingAFileWithPathAndDots() { | ||
codeGenerator.createNewFile(Dependencies.ALL_FILES, "a/b/c/dir.with.dot/Test.java", FileType.JAVA_SOURCE) | ||
codeGenerator.createNewFile(Dependencies.ALL_FILES, "a/b/c/dir.with.dot/Test.kt", FileType.KOTLIN_SOURCE) | ||
codeGenerator.createNewFile(Dependencies.ALL_FILES, "a/b/c/dir.with.dot/Test.class", FileType.CLASS) | ||
codeGenerator.createNewFile(Dependencies.ALL_FILES, "a/b/c/dir.with.dot/Test", FileType.RESOURCE) | ||
|
||
val files = codeGenerator.generatedFile.toList() | ||
Assert.assertEquals(File(baseDir, "java/a/b/c/dir.with.dot/Test.java"), files[0]) | ||
Assert.assertEquals(File(baseDir, "kotlin/a/b/c/dir.with.dot/Test.kt"), files[1]) | ||
Assert.assertEquals(File(baseDir, "classes/a/b/c/dir.with.dot/Test.class"), files[2]) | ||
Assert.assertEquals(File(baseDir, "resources/a/b/c/dir.with.dot/Test"), files[3]) | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This
FileType
should be unnecessary then? I see that you do have usages in your code relying on this enum, but since we do not have any APIs exposing this enum, maybe we can put it in our implementations, if this enum helps with your implementation.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.
Yeah, got rid of it