import com.squareup.javapoet.ClassName import com.squareup.javapoet.TypeSpec import io.gitlab.jfronny.scripts.* import java.nio.file.Files import java.util.* import javax.lang.model.element.Modifier.* plugins { id("jfmod") version "1.3-SNAPSHOT" id("jf.codegen") version "1.3-SNAPSHOT" } if (flavour == "") flavour = "modrinth" dependencies { modImplementation("io.gitlab.jfronny.libjf:libjf-config-core-v1:${prop("libjf_version")}") // Dev env modLocalRuntime("io.gitlab.jfronny.libjf:libjf-devutil:${prop("libjf_version")}") modLocalRuntime("io.gitlab.jfronny.libjf:libjf-config-ui-tiny-v1:${prop("libjf_version")}") modLocalRuntime("net.fabricmc.fabric-api:fabric-api:${prop("fabric_version")}") modLocalRuntime("com.terraformersmc:modmenu:6.1.0-rc.1") } loom { runs { this.named("client").get().vmArg("-Xmx2G") } } fun list(`package`: String) = Files.list(projectDir.resolve("src/main/java").resolve(`package`.replace('.', '/')).toPath()).use { stream -> stream .map { it.fileName.toString() } .map { it.substring(0, it.lastIndexOf('.')) } .filter { it.endsWith("Provider") } .map { ClassName.get(`package`, it) } .toList() } val classes = LinkedList(list("io.gitlab.jfronny.breakme.crash.safe")) if (flavour == "curseforge") { sourceSets.main.get().java.filter.exclude("**/unsafe/*") sourceSets.main.get().resources.exclude("**/native/*") } else { classes.addAll(list("io.gitlab.jfronny.breakme.crash.unsafe")) } sourceSets { main { generate(project) { val providerType = ClassName.get("io.gitlab.jfronny.breakme.crash", "CrashProvider") `enum`("io.gitlab.jfronny.breakme.crash", "Method") { modifiers(PUBLIC) superInterface(providerType) for (klazz in classes) { val name = klazz.simpleName() enumConstant(name.substring(0, name.length - "Provider".length), TypeSpec.anonymousClassBuilder("new \$T()", klazz).build()) } field(providerType, "provider", PRIVATE, FINAL) constructor { parameter(providerType, "provider") code { addStatement("this.provider = provider") } } method("crash") { modifiers(PUBLIC) exception(Exception::class.java) annotation(Override::class.java) code { addStatement("provider.crash()") } } } } } }