Files
allwpilib/shared/examplecheck.gradle
sciencewhiz 21d921184a [examples] Add compilable code snippets (#7909)
This enables frc-docs to use RLIs for things that are currently in-line
code blocks, and ensures they compile, which is important with the 2027
breaking changes coming. They are kept separate from the examples to
ensure they don't polute the VSCode examples finder.

Adds the Encoder snippets used in the frc-docs Encoder article as the
first instance of this.
2025-04-22 15:26:26 -06:00

149 lines
4.9 KiB
Groovy

def fileCheck = { parsedJson, folder ->
def folderNames = parsedJson.collect { it.foldername }
def folders = []
folder.eachDir {
folders << it.name
}
def disjunct = (folders + folderNames) - folders.intersect(folderNames)
def missingFromFolders = folderNames.intersect(disjunct)
def missingFromJson = folders.intersect(disjunct)
if (!missingFromFolders.empty || !missingFromJson.empty) {
StringBuilder missingString = new StringBuilder();
missingString.append("Missing From Folders\n")
for (String symbol : missingFromFolders) {
missingString.append(symbol);
missingString.append('\n');
}
missingString.append("\nMissing from JSON\n")
for (String symbol : missingFromJson) {
missingString.append(symbol);
missingString.append('\n');
}
throw new GradleException("Found missing items\n" + missingString.toString());
}
}
task checkTemplates(type: Task) {
doLast {
def parsedJson = new groovy.json.JsonSlurper().parseText(templateFile.text)
fileCheck(parsedJson, templateDirectory)
parsedJson.each {
assert it.name != null
assert it.description != null
assert it.tags != null
assert it.foldername != null
assert it.gradlebase != null
assert it.commandversion != null
if (it.gradlebase == 'java') {
assert it.mainclass != null
}
}
}
}
def tagList = [
/* --- Categories --- */
// On-RIO image processing
"Vision",
// Command-based
"Command-based",
// Romi
"Romi",
// XRP
"XRP",
// Extremely simple programs showcasing a single hardware API
"Hardware",
// Full robot, with multiple mechanisms
"Complete Robot",
// A single mechanism in the Robot class
"Basic Robot",
/* --- Mechanisms --- */
"Intake", "Flywheel", "Elevator", "Arm", "Differential Drive", "Mecanum Drive",
"Swerve Drive",
/* --- Telemetry --- */
"SmartDashboard", "Shuffleboard", "Sendable", "DataLog",
/* --- Controls --- */
"Exponential Profile", "PID", "State-Space", "LTVUnicycleController", "Path Following",
"Trajectory", "SysId", "Simulation", "Trapezoid Profile", "Profiled PID", "Odometry",
"LQR", "Pose Estimator",
/* --- Hardware --- */
"Analog", "Ultrasonic", "Gyro", "Pneumatics", "I2C", "Duty Cycle", "PDP", "DMA", "Relay",
"AddressableLEDs", "HAL", "Encoder", "Smart Motor Controller", "Digital Input",
"Digital Output",
/* --- HID --- */
"XboxController", "PS4Controller", "PS5Controller", "Joystick",
/* --- Misc --- */
/* (try to keep this section minimal) */
"EventLoop", "AprilTags", "Mechanism2d", "Preferences",
]
task checkExamples(type: Task) {
doLast {
def parsedJson = new groovy.json.JsonSlurper().parseText(exampleFile.text)
fileCheck(parsedJson, exampleDirectory)
parsedJson.each {
assert it.name != null
assert it.description != null
assert it.tags != null
assert it.tags.findAll { !tagList.contains(it) }.empty
assert it.foldername != null
assert it.gradlebase != null
assert it.commandversion != null
if (it.gradlebase == 'java') {
assert it.mainclass != null
}
}
}
}
task checkSnippets(type: Task) {
doLast {
def parsedJson = new groovy.json.JsonSlurper().parseText(snippetsFile.text)
fileCheck(parsedJson, snippetsDirectory)
parsedJson.each {
assert it.name != null
assert it.description != null
assert it.tags != null
assert it.tags.findAll { !tagList.contains(it) }.empty
assert it.foldername != null
assert it.gradlebase != null
if (it.gradlebase == 'java') {
assert it.mainclass != null
}
}
}
}
task checkCommands(type: Task) {
doLast {
def parsedJson = new groovy.json.JsonSlurper().parseText(commandFile.text)
fileCheck(parsedJson, commandDirectory)
parsedJson.each {
assert it.name != null
assert it.description != null
assert it.tags != null
assert it.foldername != null
assert it.replacename != null
assert it.commandversion != null
if (project.isCppCommands) {
assert it.headers != null
assert !it.headers.isEmpty()
assert it.source != null
assert !it.source.isEmpty()
}
}
}
}
check.dependsOn checkTemplates
check.dependsOn checkExamples
check.dependsOn checkCommands
check.dependsOn checkSnippets