class UnimodulesPlugin implements Plugin<Project> {
    void apply(Project project) {
        // Exclude '*/flutter/*' files if not compiling for Flutter
        if (!(new File(project.rootProject.projectDir.parentFile, 'pubspec.yaml').exists())) {
            project.android.sourceSets {
                main {
                    java {
                        srcDir 'src'
                        exclude '**/flutter/**'
                    }
                }
            }
        }
        
        project.ext.unimodule = {
            String dep, Closure closure = null ->
                Object dependency = null;

                if (new File(project.rootProject.projectDir.parentFile, 'package.json').exists()) {
                    // Parent directory of the android project has package.json -- probably React Native
                    dependency = project.project(":$dep")
                } else {
                    // There's no package.json and no pubspec.yaml
                    throw new GradleException(
                        "'unimodules-core.gradle' used in a project that seems to be neither a Flutter nor a React Native project."
                    )
                }

                String configurationName = project.configurations.findByName("implementation") ? "implementation" : "compile"

                project.dependencies.add(configurationName, dependency, closure)
        }
    }
}

apply plugin: UnimodulesPlugin