2021-01-19 12:48:09 -06:00
import groovy.json.JsonSlurper
/ *
* This buildscript contains tasks related to the validation of each module in fabric api .
*
* Right now this task verifies each Fabric API module has a module lifecycle specified .
* More functionality will probably be added in the future .
* /
subprojects {
2024-01-11 08:45:18 -07:00
if ( it . name = = "deprecated" | | it . name = = "fabric-api-bom" | | it . name = = "fabric-api-catalog" ) {
2023-05-30 13:07:11 +01:00
return
}
2022-05-01 15:44:16 +01:00
2021-01-19 12:48:09 -06:00
// Create the task
2025-03-28 18:45:46 +00:00
def validateModules = tasks . register ( "validateModules" , ValidateModuleTask )
tasks . check . dependsOn ( validateModules )
2021-01-19 12:48:09 -06:00
}
/ * *
* Verifies that each module has the required custom values for module lifecycle in it ' s FMJ .
*
* < p > Example:
* < pre > { @code
* "custom" : {
* "fabric-api:module-lifecycle" : "stable"
* }
* } < / pre >
* /
2024-12-18 18:22:30 +00:00
abstract class ValidateModuleTask extends DefaultTask {
@InputFile
abstract RegularFileProperty getFmj ( )
@Input
abstract Property < String > getProjectName ( )
@Input
abstract Property < String > getProjectPath ( )
@Input
abstract Property < String > getLoaderVersion ( )
2021-01-19 12:48:09 -06:00
ValidateModuleTask ( ) {
group = "verification"
2025-03-28 18:45:46 +00:00
// No outputs
outputs . upToDateWhen { true }
def file = project . file ( "src/main/resources/fabric.mod.json" )
if ( ! file . exists ( ) ) {
file = project . file ( "src/client/resources/fabric.mod.json" )
}
2024-12-18 18:22:30 +00:00
2025-03-28 18:45:46 +00:00
fmj . set ( file )
2024-12-18 18:22:30 +00:00
projectName . set ( project . name )
projectPath . set ( project . path )
loaderVersion . set ( project . loader_version )
2021-01-19 12:48:09 -06:00
}
@TaskAction
void validate ( ) {
2024-12-18 18:22:30 +00:00
def file = fmj . get ( ) . asFile
2022-05-21 16:26:46 +01:00
2023-05-30 13:07:11 +01:00
def json = new JsonSlurper ( ) . parse ( file )
2021-01-19 12:48:09 -06:00
if ( json . custom = = null ) {
2024-12-18 18:22:30 +00:00
throw new GradleException ( "Module ${projectName.get()} does not have a custom value containing module lifecycle!" )
2021-01-19 12:48:09 -06:00
}
def moduleLifecycle = json . custom . get ( "fabric-api:module-lifecycle" )
if ( moduleLifecycle = = null ) {
2024-12-18 18:22:30 +00:00
throw new GradleException ( "Module ${projectName.get()} does not have module lifecycle in custom values!" )
2021-01-19 12:48:09 -06:00
}
if ( ! moduleLifecycle instanceof String ) {
2024-12-18 18:22:30 +00:00
throw new GradleException ( "Module ${projectName.get()} has an invalid module lifecycle value. The value must be a string but read a ${moduleLifecycle.class}" )
2021-01-19 12:48:09 -06:00
}
// Validate the lifecycle value
switch ( moduleLifecycle ) {
case "stable" :
case "experimental" :
2022-05-01 15:44:16 +01:00
break
2021-01-19 12:48:09 -06:00
case "deprecated" :
2024-12-18 18:22:30 +00:00
if ( ! projectPath . get ( ) . startsWith ( ":deprecated" ) ) {
throw new GradleException ( "Deprecated module ${projectName.get()} must be in the deprecated sub directory." )
2022-05-01 15:44:16 +01:00
}
break
2021-01-19 12:48:09 -06:00
default :
2024-12-18 18:22:30 +00:00
throw new GradleException ( "Module ${projectName.get()} has an invalid module lifecycle ${json.custom.get('fabric-api:module-lifecycle')}" )
2021-01-19 12:48:09 -06:00
}
2023-12-08 15:19:17 +00:00
if ( json . depends = = null ) {
2024-12-18 18:22:30 +00:00
throw new GradleException ( "Module ${projectName.get()} does not have a depends value!" )
2023-12-08 15:19:17 +00:00
}
2024-12-18 18:22:30 +00:00
if ( json . depends . fabricloader ! = ">=${loaderVersion.get()}" ) {
throw new GradleException ( "Module ${projectName.get()} does not have a valid fabricloader value! Got \"${json.depends.fabricloader}\" but expected \">=${project.loader_version}\"" )
2023-12-08 15:19:17 +00:00
}
2021-01-19 12:48:09 -06:00
}
}