def includeWithApi(String moduleName) {
    //先正常加载这个模块
    include(moduleName)
    //找到这个模块的路径
    String originDir = project(moduleName).projectDir
    //这个是新的路径
    String targetDir = "${originDir}-api"
    //原模块的名字
    String originName=project(moduleName).name;
    //新模块的名字
    def sdkName = "${originName}-api"

    //这个是公共模块的位置，预先放了一个 新建的api.gradle 文件进去
    String apiGradle = project(":ydl-platform").projectDir

    // 每次编译删除之前的文件
    deleteDir(targetDir)

    //复制.api文件到新的路径
    copy() {
        from originDir
        into targetDir
        exclude '**/build/'
        exclude '**/res/'
        include '**/*.api'
    }


    //直接复制公共模块的AndroidManifest文件到新的路径，作为该模块的文件
    copy() {
        from "${apiGradle}/template/AndroidManifest.xml"
        into "${targetDir}/src/main/"
    }

    //复制 gradle文件到新的路径，作为该模块的gradle
    copy() {
        from "${apiGradle}/template/template.gradle"
        into "${targetDir}/"
    }

    //删除空文件夹
    deleteEmptyDir(new File(targetDir))

    //为AndroidManifest新建路径，路径就是在原来的包下面新建一个api包，作为AndroidManifest里面的包名
    String packagePath = "${targetDir}/src/main/java/com/ydl/${originName.replaceAll("m-","")}/api";


    //修改AndroidManifest文件包路径
    fileReader("${targetDir}/src/main/AndroidManifest.xml", "template","${originName.replaceAll("m-","")}.api");

    new File(packagePath).mkdirs()

    //重命名一下gradle
    def build = new File(targetDir + "/template.gradle")
    if (build.exists()) {
        build.renameTo(new File(targetDir + "/build.gradle"))
    }

    // 重命名.api文件，生成正常的.java文件
    renameApiFiles(targetDir, '.api', '.java')

    //正常加载新的模块
    include ":$sdkName"
}

private void deleteEmptyDir(File dir) {
    if (dir.isDirectory()) {
        File[] fs = dir.listFiles();
        if (fs != null && fs.length > 0) {
            for (int i = 0; i < fs.length; i++) {
                File tmpFile = fs[i];
                if (tmpFile.isDirectory()) {
                    deleteEmptyDir(tmpFile);
                }
                if (tmpFile.isDirectory() && tmpFile.listFiles().length <= 0) {
                    tmpFile.delete();
                }
            }
        }
        if (dir.isDirectory() && dir.listFiles().length == 0) {
            dir.delete();
        }
    }
}

private void deleteDir(String targetDir) {
    FileTree targetFiles = fileTree(targetDir)
    targetFiles.exclude "*.iml"
    targetFiles.each { File file ->
        file.delete()
    }
}

/**
 * rename api files(java, kotlin...)
 */
private def renameApiFiles(root_dir, String suffix, String replace) {
    FileTree files = fileTree(root_dir).include("**/*$suffix")
    files.each {
        File file ->
            file.renameTo(new File(file.absolutePath.replace(suffix, replace)))
    }
}

//替换AndroidManifest里面的字段
def fileReader(path, name,sdkName) {
    def readerString = "";
    def hasReplace = false

    file(path).withReader('UTF-8') { reader ->
        reader.eachLine {
            if (it.find(name)) {
                it = it.replace(name, sdkName)
                hasReplace = true
            }
            readerString <<= it
            readerString << '\n'
        }

        if (hasReplace) {
            file(path).withWriter('UTF-8') {
                within ->
                    within.append(readerString)
            }
        }
        return readerString
    }
}

include ':app', ':ydl-net', ':ydl-utils', ':ydl-platform'


includeWithApi ":m-user"
includeWithApi ":m-other"
