<commit_before>param (
    [string]$antBuildFile,
    [string]$options,
    [string]$targets,
    [string]$publishJUnitResults,   
    [string]$testResultsFiles, 
    [string]$jdkVersion,
    [string]$jdkArchitecture,
	
    [string]$codeCoverageTool,
    [string]$classFilesDirectory,
    [string]$classFilter,
    [string]$srcDirectory
)

Write-Verbose 'Entering Ant.ps1' -Verbose
Write-Verbose "antBuildFile = $antBuildFile" -Verbose
Write-Verbose "options = $options" -Verbose
Write-Verbose "targets = $targets" -Verbose
Write-Verbose "publishJUnitResults = $publishJUnitResults" -Verbose
Write-Verbose "testResultsFiles = $testResultsFiles" -Verbose
Write-Verbose "jdkVersion = $jdkVersion" -Verbose
Write-Verbose "jdkArchitecture = $jdkArchitecture" -Verbose

$isCoverageEnabled = !$codeCoverageTool.equals("NoCoverage")
if($isCoverageEnabled)
{
    Write-Verbose "codeCoverageTool = $codeCoverageTool" -Verbose
    Write-Verbose "classFilesDirectory = $classFilesDirectory" -Verbose
    Write-Verbose "classFilter = $classFilter" -Verbose
    Write-Verbose "srcDirectory = $srcDirectory" -Verbose
}
	
#Verify Ant build file is specified
if(!$antBuildFile)
{
    throw "Ant build file is not specified"
}

# Import the Task.Common, Task.TestResults and Task.Internal dll that has all the cmdlets we need for Build
import-module "Microsoft.TeamFoundation.DistributedTask.Task.Internal"
import-module "Microsoft.TeamFoundation.DistributedTask.Task.Common"
import-module "Microsoft.TeamFoundation.DistributedTask.Task.TestResults"

if($jdkVersion -and $jdkVersion -ne "default")
{
    $jdkPath = Get-JavaDevelopmentKitPath -Version $jdkVersion -Arch $jdkArchitecture
    if (!$jdkPath) 
    {
        throw "Could not find JDK $jdkVersion $jdkArchitecture, please make sure the selected JDK is installed properly."
    }

    Write-Host "Setting JAVA_HOME to $jdkPath"
    $env:JAVA_HOME = $jdkPath
    Write-Verbose "JAVA_HOME set to $env:JAVA_HOME" -Verbose
}

$buildRootPath = Split-Path $antBuildFile -Parent
$reportDirectoryName = "CodeCoverage"
$reportDirectory = Join-Path $buildRootPath $reportDirectoryName

if(Test-Path $reportDirectory)
{
   # delete any previous code coverage data 
   rm -r $reportDirectory -force | Out-Null
}

$summaryFileName = "summary.xml"
$summaryFile = Join-Path $buildRootPath $reportDirectoryName 
$summaryFile = Join-Path $summaryFile $summaryFileName
# ensuring unique code coverage report task name by using guid
$CCReportTask = "CodeCoverage_" +[guid]::NewGuid()

if($isCoverageEnabled)
{
   # Enable code coverage in build file
   Enable-CodeCoverage -BuildTool 'Ant' -BuildFile $antBuildFile -CodeCoverageTool $codeCoverageTool -ClassFilter $classFilter -ClassFilesDirectory $classFilesDirectory -SourceDirectory $srcDirectory -SummaryFile $summaryFileName -ReportDirectory $reportDirectoryName -CCReportTask $CCReportTask -ErrorAction Stop
   Write-Verbose "code coverage is successfully enabled." -Verbose
}
else
{
    Write-Verbose "Option to enable code coverage was not selected and is being skipped." -Verbose
}	

Write-Verbose "Running Ant..." -Verbose
Invoke-Ant -AntBuildFile $antBuildFile -Options $options -Targets $targets

# Publish test results files
$publishJUnitResultsFromAntBuild = Convert-String $publishJUnitResults Boolean
if($publishJUnitResultsFromAntBuild)
{
    # check for JUnit test result files
    $matchingTestResultsFiles = Find-Files -SearchPattern $testResultsFiles
    if (!$matchingTestResultsFiles)
    {
        Write-Host "No JUnit test results files were found matching pattern '$testResultsFiles', so publishing JUnit test results is being skipped."
    }
    else
    {
        Write-Verbose "Calling Publish-TestResults" -Verbose
        Publish-TestResults -TestRunner "JUnit" -TestResultsFiles $matchingTestResultsFiles -Context $distributedTaskContext
    }    
}
else
{
    Write-Verbose "Option to publish JUnit Test results produced by Ant build was not selected and is being skipped." -Verbose
}

# check if code coverage has been enabled
if($isCoverageEnabled)
{
   # run report code coverage task which generates code coverage reports.
   Write-Verbose "Collecting code coverage reports" -Verbose
   try
   {
		Invoke-Ant -AntBuildFile $antBuildFile -Targets $CCReportTask 
   }
   catch
   {
		Write-Warning "Failed to collect code coverage. There might be no tests." -Verbose
   }
   
   if(Test-Path $summaryFile)
   {
		Write-Verbose "Calling Publish-CodeCoverage" -Verbose
		Publish-CodeCoverage -CodeCoverageTool $codeCoverageTool -SummaryFileLocation $summaryFile -ReportDirectory $reportDirectory -Context $distributedTaskContext    
   }
   else
   {
		Write-Warning "No code coverage found to publish." -Verbose
   }
}

Write-Verbose "Leaving script Ant.ps1" -Verbose<commit_msg>minor<commit_after>param (
    [string]$antBuildFile,
    [string]$options,
    [string]$targets,
    [string]$publishJUnitResults,   
    [string]$testResultsFiles, 
    [string]$jdkVersion,
    [string]$jdkArchitecture,
	
    [string]$codeCoverageTool,
    [string]$classFilesDirectory,
    [string]$classFilter,
    [string]$srcDirectory
)

Write-Verbose 'Entering Ant.ps1' -Verbose
Write-Verbose "antBuildFile = $antBuildFile" -Verbose
Write-Verbose "options = $options" -Verbose
Write-Verbose "targets = $targets" -Verbose
Write-Verbose "publishJUnitResults = $publishJUnitResults" -Verbose
Write-Verbose "testResultsFiles = $testResultsFiles" -Verbose
Write-Verbose "jdkVersion = $jdkVersion" -Verbose
Write-Verbose "jdkArchitecture = $jdkArchitecture" -Verbose

$isCoverageEnabled = !$codeCoverageTool.equals("None")
if($isCoverageEnabled)
{
    Write-Verbose "codeCoverageTool = $codeCoverageTool" -Verbose
    Write-Verbose "classFilesDirectory = $classFilesDirectory" -Verbose
    Write-Verbose "classFilter = $classFilter" -Verbose
    Write-Verbose "srcDirectory = $srcDirectory" -Verbose
}
	
#Verify Ant build file is specified
if(!$antBuildFile)
{
    throw "Ant build file is not specified"
}

# Import the Task.Common, Task.TestResults and Task.Internal dll that has all the cmdlets we need for Build
import-module "Microsoft.TeamFoundation.DistributedTask.Task.Internal"
import-module "Microsoft.TeamFoundation.DistributedTask.Task.Common"
import-module "Microsoft.TeamFoundation.DistributedTask.Task.TestResults"

if($jdkVersion -and $jdkVersion -ne "default")
{
    $jdkPath = Get-JavaDevelopmentKitPath -Version $jdkVersion -Arch $jdkArchitecture
    if (!$jdkPath) 
    {
        throw "Could not find JDK $jdkVersion $jdkArchitecture, please make sure the selected JDK is installed properly."
    }

    Write-Host "Setting JAVA_HOME to $jdkPath"
    $env:JAVA_HOME = $jdkPath
    Write-Verbose "JAVA_HOME set to $env:JAVA_HOME" -Verbose
}

$buildRootPath = Split-Path $antBuildFile -Parent
$reportDirectoryName = "CodeCoverage"
$reportDirectory = Join-Path $buildRootPath $reportDirectoryName

if(Test-Path $reportDirectory)
{
   # delete any previous code coverage data 
   rm -r $reportDirectory -force | Out-Null
}

$summaryFileName = "summary.xml"
$summaryFile = Join-Path $buildRootPath $reportDirectoryName 
$summaryFile = Join-Path $summaryFile $summaryFileName
# ensuring unique code coverage report task name by using guid
$CCReportTask = "CodeCoverage_" +[guid]::NewGuid()

if($isCoverageEnabled)
{
   # Enable code coverage in build file
   Enable-CodeCoverage -BuildTool 'Ant' -BuildFile $antBuildFile -CodeCoverageTool $codeCoverageTool -ClassFilter $classFilter -ClassFilesDirectory $classFilesDirectory -SourceDirectory $srcDirectory -SummaryFile $summaryFileName -ReportDirectory $reportDirectoryName -CCReportTask $CCReportTask -ErrorAction Stop
   Write-Verbose "code coverage is successfully enabled." -Verbose
}
else
{
    Write-Verbose "Option to enable code coverage was not selected and is being skipped." -Verbose
}	

Write-Verbose "Running Ant..." -Verbose
Invoke-Ant -AntBuildFile $antBuildFile -Options $options -Targets $targets

# Publish test results files
$publishJUnitResultsFromAntBuild = Convert-String $publishJUnitResults Boolean
if($publishJUnitResultsFromAntBuild)
{
    # check for JUnit test result files
    $matchingTestResultsFiles = Find-Files -SearchPattern $testResultsFiles
    if (!$matchingTestResultsFiles)
    {
        Write-Host "No JUnit test results files were found matching pattern '$testResultsFiles', so publishing JUnit test results is being skipped."
    }
    else
    {
        Write-Verbose "Calling Publish-TestResults" -Verbose
        Publish-TestResults -TestRunner "JUnit" -TestResultsFiles $matchingTestResultsFiles -Context $distributedTaskContext
    }    
}
else
{
    Write-Verbose "Option to publish JUnit Test results produced by Ant build was not selected and is being skipped." -Verbose
}

# check if code coverage has been enabled
if($isCoverageEnabled)
{
   # run report code coverage task which generates code coverage reports.
   Write-Verbose "Collecting code coverage reports" -Verbose
   try
   {
		Invoke-Ant -AntBuildFile $antBuildFile -Targets $CCReportTask 
   }
   catch
   {
		Write-Warning "Failed to collect code coverage. There might be no tests." -Verbose
   }
   
   if(Test-Path $summaryFile)
   {
		Write-Verbose "Calling Publish-CodeCoverage" -Verbose
		Publish-CodeCoverage -CodeCoverageTool $codeCoverageTool -SummaryFileLocation $summaryFile -ReportDirectory $reportDirectory -Context $distributedTaskContext    
   }
   else
   {
		Write-Warning "No code coverage found to publish." -Verbose
   }
}

Write-Verbose "Leaving script Ant.ps1" -Verbose
{'fraction_non_alphanumeric': 0.0895397489539749, 'fraction_numerical': 0.00041841004184100416, 'mean_word_length': 4.587960257159556, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4502103', 'n_tokens_mistral': 2969, 'n_tokens_neox': 2880, 'n_words': 783}