pipeline {
    agent any

    options {
        buildDiscarder(
            logRotator(
                numToKeepStr: '5'
            )
        )
    }

    stages {
        stage("Build and Publish") {
            agent {
                label "flutter"
            }

            steps {
                script {
                    sh '''
                    #!/bin/bash

                    unset WORKSPACE
                    unset WORKSPACE_TMP
                    rm -rf doc || true

                    flutter pub get
                    flutter pub publish -f --skip-validation || true

                    dart doc
                    # Workaround for dart doc being broken at the moment
                    #dart pub global activate dartdoc
                    #dart pub global run dartdoc

                    
                    cd doc/api
                    tar -cvf ../../docs.tgz .
                    '''
                }
            }

            post {
                always {
                    archiveArtifacts artifacts: "docs.tgz"

                    cleanWs()
                }
            }
        }
    }
}