
task clean(type: Delete) {
    group = 'build'
    delete("$project.rootDir/pipe-cli/dist")
}

task build(type: Exec, dependsOn: ':pipe-cli:test') {
    group = 'build'
    doFirst {
        setCLIVersion()
    }
    commandLine 'python', "$project.rootDir/pipe-cli/setup.py", 'sdist'
    doLast {
        cleanCLIVersion()
        delete("$project.rootDir/api/src/main/resources/static/PipelineCLI.tar.gz")
        copy {
            from("$project.rootDir/pipe-cli/dist/PipelineCLI-${version}.tar.gz")
            into("$project.rootDir/api/src/main/resources/static/")
            rename { String fileName ->
                fileName.replace("PipelineCLI-${version}", "PipelineCLI")
            }
        }
    }
}

task buildLinux(type: Exec, dependsOn: ':pipe-cli:test') {
    group = 'build'
    doFirst {
        setCLIVersion()
    }
    environment "PIPE_CLI_SOURCES_DIR": "$project.rootDir/pipe-cli"
    environment "PIPE_CLI_DIST_DIR": "$project.rootDir/pipe-cli"
    environment "PIPE_CLI_LINUX_DIST_DIR": "$project.rootDir/pipe-cli"
    environment "PIPE_OMICS_SOURCES_DIR": "$project.rootDir/pipe-cli/pipe-omics"
    environment "PIPE_MOUNT_SOURCES_DIR": "$project.rootDir/pipe-cli/mount"
    environment "PIPE_CLI_RUNTIME_TMP_DIR": "\\~/.pipe/tmp"
    environment "PYINSTALLER_PATH": "/pyinstaller"

    commandLine "bash", "$project.rootDir/pipe-cli/build_linux.sh"
    doLast {
        cleanCLIVersion()
        delete("$project.rootDir/api/src/main/resources/static/pipe")
        copy {
            from("$project.rootDir/pipe-cli/dist/dist-file/pipe")
            into("$project.rootDir/api/src/main/resources/static/")
        }
        copy {
            from("$project.rootDir/pipe-cli/dist/dist-folder/pipe.tar.gz")
            into("$project.rootDir/api/src/main/resources/static/")
        }
    }
}

task buildWin(type: Exec, dependsOn: ':pipe-cli:test') {
    group = 'build'
    doFirst {
        setCLIVersion()
    }
    environment "PIPE_CLI_SOURCES_DIR": "$project.rootDir/pipe-cli"
    environment "PIPE_CLI_WIN_DIST_DIR": "$project.rootDir/pipe-cli/dist/win"
    environment "PIPE_CLI_MAJOR_VERSION": "$version.major"
    environment "PIPE_CLI_MINOR_VERSION": "$version.minor"
    environment "PIPE_CLI_PATCH_VERSION": "$version.patch"
    environment "PIPE_CLI_BUILD_VERSION": "$version.build"

    commandLine "bash", "$project.rootDir/pipe-cli/build_windows.sh"
    doLast {
        cleanCLIVersion()
        delete("$project.rootDir/api/src/main/resources/static/pipe.zip")
        copy {
            from("$project.rootDir/pipe-cli/dist/win/pipe.zip")
            into("$project.rootDir/api/src/main/resources/static/")
        }
    }
}

// This task is not included into any global "Build All" wrapper
// It is intended to be run separately within the OSX environment
task buildMac(type: Exec, dependsOn: ':pipe-cli:test') {
    group = 'build'
    doFirst {
        setCLIVersion()
    }
    environment "PIPE_CLI_SOURCES_DIR": "$project.rootDir/pipe-cli"
    environment "PIPE_CLI_LINUX_DIST_DIR": "$project.rootDir/pipe-cli"
    environment "PIPE_MOUNT_SOURCES_DIR": "$project.rootDir/pipe-cli/mount"
    environment "PIPE_OMICS_SOURCES_DIR": "$project.rootDir/pipe-cli/pipe-omics"
    environment "PYINSTALLER_PATH": "/tmp/pyinstaller"

    commandLine "bash", "$project.rootDir/pipe-cli/build_mac.sh"
    doLast {
        cleanCLIVersion()
        delete("$project.rootDir/api/src/main/resources/static/pipe_osx")
        copy {
            from("$project.rootDir/pipe-cli/dist/dist-file/pipe_osx")
            into("$project.rootDir/api/src/main/resources/static/")
        }
        copy {
            from("$project.rootDir/pipe-cli/dist/dist-folder/pipe_osx.tar.gz")
            into("$project.rootDir/api/src/main/resources/static/")
        }
    }
}

task buildMacPy39(type: Exec) {
    group = 'build'
    doFirst {
        setCLIVersion()
    }
    environment "PIPE_CLI_SOURCES_DIR": "$project.rootDir/pipe-cli"
    environment "PIPE_CLI_LINUX_DIST_DIR": "$project.rootDir/pipe-cli"
    environment "PIPE_MOUNT_SOURCES_DIR": "$project.rootDir/pipe-cli/mount"
    environment "PYINSTALLER_PATH": "/tmp/pyinstaller"

    commandLine "bash", "$project.rootDir/pipe-cli/build_mac_py39.sh"
    doLast {
        cleanCLIVersion()
        delete("$project.rootDir/api/src/main/resources/static/pipe_osx")
        copy {
            from("$project.rootDir/pipe-cli/dist/dist-file/pipe_osx")
            into("$project.rootDir/api/src/main/resources/static/")
        }
        copy {
            from("$project.rootDir/pipe-cli/dist/dist-folder/pipe_osx.tar.gz")
            into("$project.rootDir/api/src/main/resources/static/")
        }
    }
}

task buildMacArm(type: Exec) {
    group = 'build'
    doFirst {
        setCLIVersion()
    }
    environment "PIPE_CLI_SOURCES_DIR": "$project.rootDir/pipe-cli"
    environment "PIPE_CLI_LINUX_DIST_DIR": "$project.rootDir/pipe-cli"
    environment "PIPE_MOUNT_SOURCES_DIR": "$project.rootDir/pipe-cli/mount"
    environment "PYINSTALLER_PATH": "/tmp/pyinstaller"

    commandLine "bash", "$project.rootDir/pipe-cli/build_mac_arm.sh"
    doLast {
        cleanCLIVersion()
        delete("$project.rootDir/api/src/main/resources/static/pipe_osx_arm")
        copy {
            from("$project.rootDir/pipe-cli/dist/dist-file/pipe_osx_arm")
            into("$project.rootDir/api/src/main/resources/static/")
        }
        copy {
            from("$project.rootDir/pipe-cli/dist/dist-folder/pipe_osx_arm.tar.gz")
            into("$project.rootDir/api/src/main/resources/static/")
        }
    }
}

task test(type: Exec, dependsOn: ':pipe-cli:installPackages') {
    group = 'test'
    commandLine "pytest", "-s", "-vvv", "--cov=$project.rootDir/pipe-cli", "--cov-config=$project.rootDir/pipe-cli/tests/cov_config"
}

task installPackages(type: Exec) {
    group = 'other'
    commandLine "pip", "install", "--user", "-r", "$project.rootDir/pipe-cli/requirements.txt"
}

def setCLIVersion() {
    copy {
        from('src/version.py')
        into('temp')
    }
    def proj = file('src/version.py')
    def text = proj.getText("UTF-8")
    text = text.replaceAll(/(__version__='.+')/, "__version__=\'$version\'")
    proj.write(text, "UTF-8")
}

def cleanCLIVersion() {
    copy {
        from('temp/version.py')
        into('src')
    }
    delete("$project.rootDir/pipe-cli/temp")
}
