2021-01-19 13:48:09 -05: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 {
2023-05-30 08:07:11 -04:00
if ( it . name = = "deprecated" ) {
return
}
2022-05-01 10:44:16 -04:00
2021-01-19 13:48:09 -05:00
// Create the task
task validateModules ( type: ValidateModuleTask )
}
/ * *
* 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 >
* /
class ValidateModuleTask extends DefaultTask {
ValidateModuleTask ( ) {
group = "verification"
// Hook up validation to check task
project . tasks . check . dependsOn ( this )
}
@TaskAction
void validate ( ) {
2022-05-21 11:26:46 -04:00
def clientOnlyMod = false
def file = ( project . file ( "src/main/resources/fabric.mod.json" ) as File )
if ( ! file . exists ( ) ) {
file = ( project . file ( "src/client/resources/fabric.mod.json" ) as File )
clientOnlyMod = true
}
2023-05-30 08:07:11 -04:00
def json = new JsonSlurper ( ) . parse ( file )
2021-01-19 13:48:09 -05:00
if ( json . custom = = null ) {
throw new GradleException ( "Module ${project} does not have a custom value containing module lifecycle!" )
}
def moduleLifecycle = json . custom . get ( "fabric-api:module-lifecycle" )
if ( moduleLifecycle = = null ) {
throw new GradleException ( "Module ${project} does not have module lifecycle in custom values!" )
}
if ( ! moduleLifecycle instanceof String ) {
throw new GradleException ( "Module ${project} has an invalid module lifecycle value. The value must be a string but read a ${moduleLifecycle.class}" )
}
// Validate the lifecycle value
switch ( moduleLifecycle ) {
case "stable" :
case "experimental" :
2022-05-01 10:44:16 -04:00
break
2021-01-19 13:48:09 -05:00
case "deprecated" :
2022-05-01 10:44:16 -04:00
if ( ! project . path . startsWith ( ":deprecated" ) ) {
throw new GradleException ( "Deprecated module ${project} must be in the deprecated sub directory." )
}
break
2021-01-19 13:48:09 -05:00
default :
2022-05-01 10:44:16 -04:00
throw new GradleException ( "Module ${project} has an invalid module lifecycle ${json.custom.get('fabric-api:module-lifecycle')}" )
2021-01-19 13:48:09 -05:00
}
2023-12-08 10:19:17 -05:00
if ( json . depends = = null ) {
throw new GradleException ( "Module ${project} does not have a depends value!" )
}
if ( json . depends . fabricloader ! = ">=${project.loader_version}" ) {
throw new GradleException ( "Module ${project} does not have a valid fabricloader value! Got \"${json.depends.fabricloader}\" but expected \">=${project.loader_version}\"" )
}
2021-01-19 13:48:09 -05:00
}
}