pipeline 
{   
    agent 
	{
        node 
		{
            label 'Jenkins_Homer'
        }                
	}            
    stages 
	{        
        stage('Build') 
		{
            environment
            {
                dotnet ="C:\\Program Files\\dotnet\\"    
                scannerHome = "C:\\home\\jenkins\\tools\\hudson.plugins.sonar.MsBuildSQRunnerInstallation\\C_sonar-runner"                
                token = 'sqa_71202f188aacbe40492ba767cf3e841179d3f41a'
                project = 'hochbaustatistik'
            }	
            steps 
			{     
                  cleanWs()
                  checkout scm
                  bat "dotnet restore Hochbaustatistik\\Hochbaustatistik.sln"                
                  bat "${scannerHome}\\SonarScanner.MSBuild.exe begin /k:${project} /d:sonar.login=${token} /d:sonar.verbose=true /d:sonar.cs.opencover.reportsPaths=${WORKSPACE}\\coverage.xml"
                  bat "\"${tool 'MSBuild17'}\" ${WORKSPACE}\\Hochbaustatistik\\Hochbaustatistik.sln /t:Restore /t:Rebuild /p:Configuration=Debug"                       
             }
        }     
        stage('Test')
        {
            environment
            {
                dotnet ="C:\\Program Files\\dotnet\\"
            }
            steps
             {
                 catchError(buildResult: 'SUCCESS', stageResult: 'FAILURE') 
        	     {                    
				       bat "dotnet test ${WORKSPACE}\\Hochbaustatistik\\Hochbaustatistik.Testing\\Hochbaustatistik.Testing.csproj /p:CollectCoverage=true /p:CoverletOutputFormat=opencover /p:CoverletOutput=..\\..\\coverage.xml /p:Exclude=[Scan2Compress.GUI]*%2c[Scan2Compress.Properties]* --no-build"                   
                 }                
             }               
        }
        stage('SonarQube')
        {     
            environment
            {               
                scannerHome = "C:\\home\\jenkins\\tools\\hudson.plugins.sonar.MsBuildSQRunnerInstallation\\C_sonar-runner"                
                token = 'sqa_71202f188aacbe40492ba767cf3e841179d3f41a'
                project = 'hochbaustatistik'
            }	
            steps
            {
                bat "${scannerHome}\\SonarScanner.MSBuild.exe end /d:sonar.login=${token}"
            }            
        }
        stage('Coverage')
        {
            environment
            {
                 reportGen = "C:\\Nuget-Packages\\reportgenerator\\5.1.6\\tools\\net6.0\\ReportGenerator.exe"
            }
            steps
            {           
                 bat "${reportGen} -verbosity:Info -reports:${WORKSPACE}\\coverage.xml -targetdir:${WORKSPACE} -reporttypes:SonarQube;Cobertura;Html"
            }
        }        
    }
     post {
        always {
               
            step([$class: 'CoberturaPublisher', autoUpdateHealth: false, autoUpdateStability: false, coberturaReportFile: '**/Cobertura.xml', failUnhealthy: false, failUnstable: false, maxNumberOfBuilds: 0, onlyStable: false, sourceEncoding: 'ASCII', zoomCoverageChart: false])           
        }
    }
}