// This file relates to internal XMOS infrastructure and should be ignored by external users

@Library('xmos_jenkins_shared_library@v0.38.0') _

def archiveSandbox_withBin(String repoName) {
    sh "cp ${WORKSPACE}/${repoName}/build/manifest.txt ${WORKSPACE}"
    sh "rm -rf .get_tools .venv"
    sh "git -C ${repoName} clean -xdf -e **/bin/**/*.xe"
    zip zipFile: "${repoName}_sw.zip", archive: true, defaultExcludes: false
}

getApproval()
pipeline {
    agent {
        label 'documentation'
    }
    parameters {
        string(
            name: 'TOOLS_VERSION',
            defaultValue: '15.3.1',
            description: 'XTC tools version'
        )
        string(
            name: 'XMOSDOC_VERSION',
            defaultValue: 'v7.3.0',
            description: 'xmosdoc version'
        )
    }

    options {
        skipDefaultCheckout()
        timestamps()
        buildDiscarder(xmosDiscardBuildSettings(onlyArtifacts = false))
    }

    stages {
        stage('Checkout') {
            steps{

                println "Stage running on ${env.NODE_NAME}"

                script {
                    def (server, user, repo) = extractFromScmUrl()
                    env.REPO_NAME = repo
                }

                dir(REPO_NAME)
                {
                    checkoutScmShallow()
                    withTools(params.TOOLS_VERSION) {
                        sh "cmake -G 'Unix Makefiles' -B build -D DEPS_CLONE_SHALLOW=TRUE"
                    }
                    createVenv(reqFile: "requirements.txt")
                }
            }
        }

        stage('Code build') {
            steps{
                dir(REPO_NAME) {
                    withVenv {
                        xcoreBuild()
                    }
                }
            }
        }
        stage('Tests') {
            steps {
                dir(REPO_NAME) {
                    withVenv {
                        sh "python -m pip install pytest pytest-xdist"
                        dir("test") {
                          runPytest()
                        }
                    }
                }
            }
        }
        stage('Check autogen') {
            steps {
                dir(REPO_NAME) {
                    withVenv {
                        sh "python live_streaming_sound_card_pipeline.py"
                        sh "python tuning_gui.py --code-gen"
                        // Check that there are no changes to the code after running the autogen
                        sh "git add . && git diff --exit-code && git diff --cached --exit-code "
                    }
                }
            }
        }
        stage('Doc build') {
            steps {
                dir(REPO_NAME) {
                    buildDocs(xmosdocVenvPath:'.')
                }
            }
        }
        stage("Archive sandbox") {
            steps
            {
                archiveSandbox_withBin(REPO_NAME) // archive the sandbox with binary files
            }
        }

    } // stages
    post
    {
        cleanup
        {
             xcoreCleanSandbox()
        }
    }
} // pipeline
