This repository has been archived by the owner on Dec 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from FRC3636/autlog
Auto-generate LoggableInputs and use Kotlin build script
- Loading branch information
Showing
46 changed files
with
646 additions
and
450 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
Empty file.
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,6 +1,6 @@ | ||
{ | ||
"enableCppIntellisense": false, | ||
"currentLanguage": "java", | ||
"projectYear": "2023", | ||
"projectYear": "2024", | ||
"teamNumber": 3636 | ||
} |
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,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2024 Team 8-Bit 9432 | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
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,21 @@ | ||
val kspVersion: String by project | ||
|
||
plugins { | ||
kotlin("jvm") | ||
} | ||
|
||
group = "org.team9432.lib" | ||
|
||
dependencies { | ||
implementation("com.squareup:kotlinpoet:1.14.2") | ||
implementation("com.squareup:kotlinpoet-ksp:1.14.2") | ||
implementation("com.google.devtools.ksp:symbol-processing-api:$kspVersion") | ||
|
||
implementation("org.littletonrobotics.akit.junction:junction-core:3.2.1") | ||
} | ||
|
||
repositories { | ||
maven { | ||
url = uri("https://frcmaven.wpi.edu/artifactory/release/") | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
annotation/src/main/kotlin/org/team9432/annotation/LogTableUtils.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,63 @@ | ||
package org.team9432.annotation | ||
|
||
import edu.wpi.first.units.Measure | ||
import edu.wpi.first.units.MutableMeasure | ||
import edu.wpi.first.units.Unit | ||
import edu.wpi.first.util.WPISerializable | ||
import edu.wpi.first.util.protobuf.Protobuf | ||
import edu.wpi.first.util.struct.Struct | ||
import edu.wpi.first.util.struct.StructSerializable | ||
import org.littletonrobotics.junction.LogTable | ||
import org.littletonrobotics.junction.LogTable.LogValue | ||
import us.hebi.quickbuf.ProtoMessage | ||
|
||
object LogTableUtils { | ||
// This is all just to get correct type inference working with kotlin | ||
fun <T: StructSerializable> LogTable.kGet(key: String, defaultValue: T) = this.getFromWPISerializable(key, defaultValue) | ||
private fun <T: WPISerializable> LogTable.getFromWPISerializable(key: String, defaultValue: T): T = this.get(key, defaultValue) | ||
|
||
fun LogTable.kPut(key: String, value: LogValue) = put(key, value) | ||
fun LogTable.kPut(key: String, value: ByteArray) = put(key, value) | ||
fun LogTable.kPut(key: String, value: Boolean) = put(key, value) | ||
fun LogTable.kPut(key: String, value: Int) = put(key, value) | ||
fun LogTable.kPut(key: String, value: Long) = put(key, value) | ||
fun LogTable.kPut(key: String, value: Float) = put(key, value) | ||
fun LogTable.kPut(key: String, value: Double) = put(key, value) | ||
fun LogTable.kPut(key: String, value: String) = put(key, value) | ||
fun <E: Enum<E>> LogTable.kPut(key: String, value: E) = put(key, value) | ||
fun <U: Unit<U>> LogTable.kPut(key: String, value: Measure<U>) = put(key, value) | ||
fun LogTable.kPut(key: String, value: BooleanArray) = put(key, value) | ||
fun LogTable.kPut(key: String, value: IntArray) = put(key, value) | ||
fun LogTable.kPut(key: String, value: LongArray) = put(key, value) | ||
fun LogTable.kPut(key: String, value: FloatArray) = put(key, value) | ||
fun LogTable.kPut(key: String, value: DoubleArray) = put(key, value) | ||
fun LogTable.kPut(key: String, value: Array<String>) = put(key, value) | ||
fun <T> LogTable.kPut(key: String, struct: Struct<T>, value: T) = put(key, struct, value) | ||
fun <T> LogTable.kPut(key: String, struct: Struct<T>, value: Array<T>) = put(key, struct, *value) | ||
fun <T, MessageType: ProtoMessage<*>> LogTable.kPut(key: String, proto: Protobuf<T, MessageType>, value: T) = put(key, proto, value) | ||
fun <T: WPISerializable> LogTable.kPut(key: String, value: T) = put(key, value) | ||
fun <T: StructSerializable> LogTable.kPut(key: String, value: Array<T>) = put(key, *value) | ||
|
||
fun LogTable.kGet(key: String): LogValue = get(key) | ||
fun LogTable.kGet(key: String, defaultValue: ByteArray): ByteArray = get(key, defaultValue) | ||
fun LogTable.kGet(key: String, defaultValue: Boolean) = get(key, defaultValue) | ||
fun LogTable.kGet(key: String, defaultValue: Int) = get(key, defaultValue) | ||
fun LogTable.kGet(key: String, defaultValue: Long) = get(key, defaultValue) | ||
fun LogTable.kGet(key: String, defaultValue: Float) = get(key, defaultValue) | ||
fun LogTable.kGet(key: String, defaultValue: Double) = get(key, defaultValue) | ||
fun LogTable.kGet(key: String, defaultValue: String): String = get(key, defaultValue) | ||
fun <E: Enum<E>> LogTable.kGet(key: String, defaultValue: E): E = get(key, defaultValue) | ||
fun <U: Unit<U>> LogTable.kGet(key: String, defaultValue: Measure<U>): Measure<U> = get(key, defaultValue) | ||
fun <U: Unit<U>> LogTable.kGet(key: String, defaultValue: MutableMeasure<U>): MutableMeasure<U> = get(key, defaultValue) | ||
fun LogTable.kGet(key: String, defaultValue: BooleanArray): BooleanArray = get(key, defaultValue) | ||
fun LogTable.kGet(key: String, defaultValue: IntArray): IntArray = get(key, defaultValue) | ||
fun LogTable.kGet(key: String, defaultValue: LongArray): LongArray = get(key, defaultValue) | ||
fun LogTable.kGet(key: String, defaultValue: FloatArray): FloatArray = get(key, defaultValue) | ||
fun LogTable.kGet(key: String, defaultValue: DoubleArray): DoubleArray = get(key, defaultValue) | ||
fun LogTable.kGet(key: String, defaultValue: Array<String>): Array<String> = get(key, defaultValue) | ||
fun <T> LogTable.kGet(key: String, struct: Struct<T>, defaultValue: T): T = get(key, struct, defaultValue) | ||
fun <T> LogTable.kGet(key: String, struct: Struct<T>, defaultValue: Array<T>): Array<T> = get(key, struct, *defaultValue) | ||
fun <T, MessageType: ProtoMessage<*>> LogTable.kGet(key: String, proto: Protobuf<T, MessageType>, defaultValue: T): T = get(key, proto, defaultValue) | ||
fun <T: WPISerializable> LogTable.kGet(key: String, defaultValue: T): T = get(key, defaultValue) | ||
fun <T: StructSerializable> LogTable.kGet(key: String, defaultValue: Array<T>): Array<T> = get(key, *defaultValue) | ||
} |
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,4 @@ | ||
package org.team9432.annotation | ||
|
||
@Target(AnnotationTarget.CLASS) | ||
annotation class Logged |
81 changes: 81 additions & 0 deletions
81
annotation/src/main/kotlin/org/team9432/annotation/LoggedProcessor.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,81 @@ | ||
package org.team9432.annotation | ||
|
||
import com.google.devtools.ksp.processing.* | ||
import com.google.devtools.ksp.symbol.KSAnnotated | ||
import com.google.devtools.ksp.symbol.KSClassDeclaration | ||
import com.google.devtools.ksp.symbol.Modifier | ||
import com.google.devtools.ksp.validate | ||
import com.squareup.kotlinpoet.FileSpec | ||
import com.squareup.kotlinpoet.FunSpec | ||
import com.squareup.kotlinpoet.KModifier | ||
import com.squareup.kotlinpoet.TypeSpec | ||
import com.squareup.kotlinpoet.ksp.toClassName | ||
import com.squareup.kotlinpoet.ksp.writeTo | ||
import org.littletonrobotics.junction.LogTable | ||
import org.littletonrobotics.junction.inputs.LoggableInputs | ||
|
||
class LoggedProcessor(private val codeGenerator: CodeGenerator): SymbolProcessor { | ||
private val logTableType = LogTable::class | ||
private val loggableInputsType = LoggableInputs::class | ||
|
||
override fun process(resolver: Resolver): List<KSAnnotated> { | ||
val annotatedClasses = resolver.getSymbolsWithAnnotation("org.team9432.annotation.Logged").filterIsInstance<KSClassDeclaration>() | ||
annotatedClasses.forEach { process(it) } | ||
return annotatedClasses.filterNot { it.validate() }.toList() | ||
} | ||
|
||
private fun process(classDeclaration: KSClassDeclaration) { | ||
if (!classDeclaration.modifiers.contains(Modifier.OPEN)) throw Exception("""[Logged] Please ensure the class you are annotating (${classDeclaration.simpleName.asString()}) has the open modifier!""") | ||
|
||
val packageName = classDeclaration.packageName.asString() | ||
val className = classDeclaration.simpleName.asString() | ||
|
||
val newClassName = "Logged${className}" | ||
|
||
val toLogBuilder = FunSpec.builder("toLog") | ||
.addModifiers(KModifier.OVERRIDE) | ||
.addParameter("table", logTableType) | ||
val fromLogBuilder = FunSpec.builder("fromLog") | ||
.addModifiers(KModifier.OVERRIDE) | ||
.addParameter("table", logTableType) | ||
|
||
classDeclaration.getAllProperties().forEach { property -> | ||
val simpleName = property.simpleName.asString() | ||
val logName = simpleName.substring(0, 1).uppercase() + simpleName.substring(1) | ||
|
||
if (!property.isMutable) throw Exception("""[Logged] Please ensure the class you are annotating (${classDeclaration.simpleName.asString()}.${simpleName}) has only mutable properties!""") | ||
|
||
toLogBuilder.addCode( | ||
""" |table.kPut("$logName", $simpleName) | ||
| | ||
""".trimMargin() | ||
) | ||
|
||
fromLogBuilder.addCode( | ||
""" |$simpleName = table.kGet("$logName", $simpleName) | ||
| | ||
""".trimMargin() | ||
) | ||
} | ||
|
||
val type = TypeSpec.classBuilder(newClassName) | ||
.addSuperinterface(loggableInputsType) | ||
.superclass(classDeclaration.toClassName()) | ||
.addFunction(toLogBuilder.build()) | ||
.addFunction(fromLogBuilder.build()) | ||
|
||
|
||
val file = FileSpec.builder(packageName, newClassName) | ||
file.addType(type.build()) | ||
file.indent(" ") | ||
file.addImport(LogTableUtils::class, "kGet", "kPut") | ||
file.addImport("com.frcteam3636.frc2024.utils.LogTableUtils", "kGet", "kPut") | ||
file.build().writeTo(codeGenerator, Dependencies(true, classDeclaration.containingFile!!)) | ||
} | ||
} | ||
|
||
class Provider: SymbolProcessorProvider { | ||
override fun create(environment: SymbolProcessorEnvironment) = LoggedProcessor( | ||
codeGenerator = environment.codeGenerator | ||
) | ||
} |
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,4 @@ | ||
## org/team9432/lib/annotation/ | ||
|
||
This package is for a custom annotation processor that replicates the behavior of AdvantageKit's AutoLog annotation, but | ||
in Kotlin. |
1 change: 1 addition & 0 deletions
1
...in/resources/META-INF/services/com.google.devtools.ksp.processing.SymbolProcessorProvider
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 @@ | ||
org.team9432.annotation.Provider |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.