Skip to content
This repository has been archived by the owner on Dec 19, 2024. It is now read-only.

Commit

Permalink
refactor: use kotlin build script
Browse files Browse the repository at this point in the history
  • Loading branch information
doinkythederp committed Nov 25, 2024
1 parent 1863f69 commit 1227696
Show file tree
Hide file tree
Showing 43 changed files with 672 additions and 430 deletions.
2 changes: 1 addition & 1 deletion .wpilib/wpilib_preferences.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"enableCppIntellisense": false,
"currentLanguage": "java",
"projectYear": "2023",
"projectYear": "2024",
"teamNumber": 3636
}
21 changes: 21 additions & 0 deletions annotation/LICENSE
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.
32 changes: 32 additions & 0 deletions annotation/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
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 {
mavenCentral()
maven { setUrl("https://frcmaven.wpi.edu/artifactory/release/") }
maven {
url = uri("https://maven.pkg.github.com/Mechanical-Advantage/AdvantageKit")
credentials {
username = "Mechanical-Advantage-Bot"
password = "\u0067\u0068\u0070\u005f\u006e\u0056\u0051\u006a\u0055\u004f\u004c\u0061\u0079\u0066\u006e\u0078\u006e\u0037\u0051\u0049\u0054\u0042\u0032\u004c\u004a\u006d\u0055\u0070\u0073\u0031\u006d\u0037\u004c\u005a\u0030\u0076\u0062\u0070\u0063\u0051"
}
}
}

sourceSets.main {
java.srcDirs("src/main/kotlin")
}

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)
}
4 changes: 4 additions & 0 deletions annotation/src/main/kotlin/org/team9432/annotation/Logged.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package org.team9432.annotation

@Target(AnnotationTarget.CLASS)
annotation class Logged
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
)
}
4 changes: 4 additions & 0 deletions annotation/src/main/kotlin/org/team9432/annotation/README.md
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.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
org.team9432.annotation.Provider
162 changes: 0 additions & 162 deletions build.gradle

This file was deleted.

Loading

0 comments on commit 1227696

Please sign in to comment.