-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Don't add
.lightsaber
files inside the classpath (#292)
* Improve code * Pass output path as argument
- Loading branch information
1 parent
ec92f58
commit 5eedaa5
Showing
10 changed files
with
160 additions
and
81 deletions.
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
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
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
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
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
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
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
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
45 changes: 9 additions & 36 deletions
45
lightsaber/src/main/java/schwarz/it/lightsaber/utils/FileGenerator.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 |
---|---|---|
@@ -1,48 +1,21 @@ | ||
package schwarz.it.lightsaber.utils | ||
|
||
import com.google.devtools.ksp.processing.CodeGenerator | ||
import com.google.devtools.ksp.processing.Dependencies | ||
import dagger.spi.model.DaggerProcessingEnv | ||
import schwarz.it.lightsaber.Issue | ||
import java.io.OutputStream | ||
import java.io.PrintWriter | ||
import javax.annotation.processing.Filer | ||
import javax.tools.StandardLocation | ||
|
||
internal interface FileGenerator { | ||
fun createFile(packageName: String, fileName: String, extension: String): OutputStream | ||
|
||
companion object { | ||
operator fun invoke(processingEnv: DaggerProcessingEnv): FileGenerator { | ||
return processingEnv.fold({ FileGenerator(it.filer) }, { FileGenerator(it.codeGenerator) }) | ||
} | ||
|
||
operator fun invoke(codeGenerator: CodeGenerator): FileGenerator { | ||
return FileGeneratorKsp(codeGenerator) | ||
} | ||
|
||
operator fun invoke(filer: Filer): FileGenerator { | ||
return FileGeneratorJavac(filer) | ||
} | ||
} | ||
} | ||
|
||
private class FileGeneratorJavac(private val filer: Filer) : FileGenerator { | ||
override fun createFile(packageName: String, fileName: String, extension: String): OutputStream { | ||
return filer | ||
.createResource(StandardLocation.CLASS_OUTPUT, packageName, "$fileName.$extension") | ||
.openOutputStream() | ||
} | ||
} | ||
|
||
private class FileGeneratorKsp(private val codeGenerator: CodeGenerator) : FileGenerator { | ||
override fun createFile(packageName: String, fileName: String, extension: String): OutputStream { | ||
return codeGenerator.createNewFile(Dependencies.ALL_FILES, packageName, fileName, extension) | ||
import java.nio.file.Path | ||
import kotlin.io.path.createDirectories | ||
import kotlin.io.path.outputStream | ||
|
||
internal class FileGenerator(private val path: Path) { | ||
fun createFile(fileName: String): OutputStream { | ||
path.createDirectories() | ||
return path.resolve(fileName).outputStream() | ||
} | ||
} | ||
|
||
internal fun FileGenerator.writeFile(fileName: String, issues: List<Issue>) { | ||
this.createFile("schwarz.it.lightsaber", fileName, "lightsaber") | ||
this.createFile("$fileName.lightsaber") | ||
.let(::PrintWriter) | ||
.use { writer -> issues.forEach { writer.println("${it.codePosition}: ${it.message} [${it.rule}]") } } | ||
} |
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