import dev.nokee.platform.jni.JniJarBinary import dev.nokee.platform.jni.JniLibrary import dev.nokee.platform.jni.JniLibraryExtension import dev.nokee.runtime.nativebase.TargetMachine import groovy.transform.CompileStatic import org.gradle.api.Plugin import org.gradle.api.Project import org.gradle.api.Transformer import org.gradle.api.file.CopySpec import org.gradle.api.provider.Provider import org.gradle.jvm.tasks.Jar @CompileStatic class UberJniJarPlugin implements Plugin { @Override void apply(Project project) { project.tasks.named('jar', Jar) { task -> configure(task) } } private static void configure(Jar task) { def project = task.getProject() def logger = task.getLogger() def library = project.extensions.getByType(JniLibraryExtension) library.binaries.withType(JniJarBinary).configureEach { if (it.jarTask.isPresent()) it.jarTask.get()?.enabled = false } logger.info("${project.name}: Merging binaries into the JVM Jar.") if (library.targetMachines.get().size() > 1) { for (TargetMachine targetMachine : library.targetMachines.get()) { Provider variant = library.variants .flatMap(targetMachineOf(targetMachine)) .map(onlyOne() as Transformer>) as Provider task.into(variant.map { it.resourcePath }) { CopySpec spec -> spec.from(variant.map { it.nativeRuntimeFiles }) } } } else { library.variants.configureEach { task.into(it.resourcePath) { CopySpec spec -> spec.from(it.nativeRuntimeFiles) } } } } // Filter variants that match the specified target machine. private static Transformer, JniLibrary> targetMachineOf(TargetMachine targetMachine) { return new Transformer, JniLibrary>() { @Override Iterable transform(JniLibrary variant) { if (variant.targetMachine == targetMachine) { return [variant] } return [] } } } // Ensure only a single variant is present in the collection and return the variant. private static Transformer> onlyOne() { return new Transformer>() { @Override JniLibrary transform(List variants) { assert variants.size() == 1 return variants.first() } } } }