<commit_before>﻿# -----------------------------------------------------------------------------------
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the ""License""); you may not use this file except in compliance with
# the License.  You may obtain a copy of the License at
# 
# http://www.apache.org/licenses/LICENSE-2.0
# 
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an ""AS IS"" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# -----------------------------------------------------------------------------------

properties {
    [string]$baseDirectory   = Resolve-Path "../."
    [string]$artifactsDirectory  = "$baseDirectory/_artifacts"
    [string]$sourceDirectory = "$baseDirectory/src"
    [string]$testDirectory = "$baseDirectory/src"
    [string]$toolsDirectory  = "$baseDirectory/lib"
    [string]$nugetPackageDirectory = "$artifactsDirectory/NuGetPackages"
    [string]$testResultsDirectory = "$artifactsDirectory/TestResults"
    [string]$publishDirectory = "$artifactsDirectory/Publish"
    [string]$solutionFile = "$baseDirectory/Lucene.Net.sln"
    [string]$minimumSdkVersion = "7.0.100"
    [string]$globalJsonFile = "$baseDirectory/global.json"
    [string]$versionPropsFile = "$baseDirectory/version.props"
    [string]$luceneReadmeFile = "$baseDirectory/src/Lucene.Net/readme-nuget.md"
    [string]$luceneCLIReadmeFile = "$baseDirectory/src/dotnet/tools/lucene-cli/docs/index.md"
    [string]$rootWebsiteUrl = "https://lucenenet.apache.org"
    [string]$rootDocsWebsiteUrl = "$rootWebsiteUrl/docs"

    [string]$buildCounter     = $(if ($buildCounter) { $buildCounter } else { $env:BuildCounter }) #NOTE: Pass in as a parameter (not a property) or environment variable to override
    [string]$preReleaseCounterPattern = $(if ($preReleaseCounterPattern) { $preReleaseCounterPattern } else { if ($env:PreReleaseCounterPattern) { $env:PreReleaseCounterPattern } else { "0000000000" } })  #NOTE: Pass in as a parameter (not a property) or environment variable to override
    [string]$versionSuffix    = $(if ($versionSuffix -ne $null) { $versionSuffix } else { if ($env:VersionSuffix -ne $null) { $env:VersionSuffix } else { 'ci' }}) #NOTE: Pass in as a parameter (not a property) or environment variable to override
    [string]$packageVersion   = Get-Package-Version #NOTE: Pass in as a parameter (not a property) or environment variable to override
    [string]$version          = Get-Version
    [string]$configuration    = $(if ($configuration) { $configuration } else { if ($env:BuildConfiguration) { $env:BuildConfiguration } else { "Release" } })  #NOTE: Pass in as a parameter (not a property) or environment variable to override
    [string]$platform   = $(if ($platform) { $platform } else { if ($env:BuildPlatform) { $env:BuildPlatform } else { "Any CPU" } })  #NOTE: Pass in as a parameter (not a property) or environment variable to override
    [bool]$backupFiles       = $true
    [bool]$prepareForBuild    = $true
    [bool]$zipPublishedArtifacts = $false
    [string]$publishedArtifactZipFileName = "artifact.zip"

    [int]$maximumParallelJobs = 8
    
    #test parameters
    #The build uses Lucene.Net.Tests.Analysis.Common to determine all of the targets for the solution:
    [string]$projectWithAllTestFrameworks = "$baseDirectory/src/Lucene.Net.Tests.Analysis.Common/Lucene.Net.Tests.Analysis.Common.csproj"
    [string]$where = ""
}

$backedUpFiles = New-Object System.Collections.ArrayList
$addedFiles = New-Object System.Collections.ArrayList

task default -depends Pack

task Clean -description "This task cleans up the build directory" {
    Write-Host "##teamcity[progressMessage 'Cleaning']"
    Write-Host "##vso[task.setprogress]'Cleaning'"
    Remove-Item $artifactsDirectory -Force -Recurse -ErrorAction SilentlyContinue
    Get-ChildItem $baseDirectory -Include *.bak -Recurse | foreach ($_) {Remove-Item $_.FullName}
}

task UpdateLocalSDKVersion -description "Backs up the project.json file and pins the version to $minimumSdkVersion" {
    Backup-File $globalJsonFile
    Generate-Global-Json `
        -sdkVersion $minimumSdkVersion `
        -file $globalJsonFile
}

task CheckSDK -description "This task makes sure the correct SDK version is installed" {
    # Check prerequisites
    $sdkVersion = ((& dotnet --version) | Out-String).Trim()
    if ($LASTEXITCODE -ne 0) {
        throw "dotnet command was not found. Please install .NET $minimumSdkVersion or higher SDK and make sure it is in your PATH."
    }
    $releaseVersion = if ($sdkVersion.Contains('-')) { "$sdkVersion".Substring(0, "$sdkVersion".IndexOf('-')) } else { $sdkVersion }
    if ([version]$releaseVersion -lt ([version]$minimumSdkVersion)) {
        throw "Minimum .NET SDK $minimumSdkVersion required. Current SDK version is $releaseVersion. Please install the required SDK before running the command."
    }
}

task Init -depends CheckSDK, UpdateLocalSDKVersion -description "This task makes sure the build environment is correctly setup" {
    #Update TeamCity, MyGet, or Azure Pipelines with packageVersion
    Write-Output "##teamcity[buildNumber '$packageVersion']"
    Write-Output "##myget[buildNumber '$packageVersion']"
    Write-Host "##vso[build.updatebuildnumber]$packageVersion"

    & dotnet --version
    & dotnet --info
    Write-Host "Base Directory: $(Normalize-FileSystemSlashes "$baseDirectory")"
    Write-Host "Release Directory: $(Normalize-FileSystemSlashes "$artifactsDirectory")"
    Write-Host "Source Directory: $(Normalize-FileSystemSlashes "$sourceDirectory")"
    Write-Host "Test Directory: $(Normalize-FileSystemSlashes "$testDirectory")"
    Write-Host "Tools Directory: $(Normalize-FileSystemSlashes "$toolsDirectory")"
    Write-Host "NuGet Package Directory: $(Normalize-FileSystemSlashes "$nugetPackageDirectory")"
    Write-Host "BuildCounter: $buildCounter"
    Write-Host "PreReleaseCounterPattern: $preReleaseCounterPattern"
    Write-Host "VersionSuffix: $versionSuffix"
    Write-Host "Package Version: $packageVersion"
    Write-Host "File Version: $version"
    Write-Host "Configuration: $configuration"
    Write-Host "Platform: $platform"
    Write-Host "MaximumParallelJobs: $($maximumParallelJobs.ToString())"
    Write-Host "Powershell Version: $($PSVersionTable.PSVersion)"

    Ensure-Directory-Exists "$artifactsDirectory"
}

task Restore -description "This task restores the dependencies" {
    Write-Host "##teamcity[progressMessage 'Restoring']"
    Write-Host "##vso[task.setprogress]'Restoring'"
    Exec { 
        & dotnet restore $solutionFile --no-dependencies /p:TestFrameworks=true
    }
}

task Compile -depends Clean, Init, Restore -description "This task compiles the solution" {
    Write-Host "##teamcity[progressMessage 'Compiling']"
    Write-Host "##vso[task.setprogress]'Compiling'"
    try {
        if ($prepareForBuild -eq $true) {
            Prepare-For-Build
        }

        Exec {
            # NOTE: Version information is not passed in at the command line,
            # instead it is output to the version.props file. This file is then
            # used during a release to "freeze" the build at a specific version
            # so it is always a constant in release distributions.
            & dotnet build "$solutionFile" `
                --configuration "$configuration" `
                --no-restore `
                -p:Platform=$platform `
                -p:PortableDebugTypeOnly=true `
                -p:TestFrameworks=true # workaround for parsing issue: https://github.com/Microsoft/msbuild/issues/471#issuecomment-181963350
        }

        $success = $true
    } finally {
        if ($success -ne $true) {
            Delete-Added-Files $addedFiles
            Restore-Files $backedUpFiles
        }
    }
}

task Pack -depends Compile -description "This task creates the NuGet packages" {
    Write-Host "##teamcity[progressMessage 'Packing']"
    Write-Host "##vso[task.setprogress]'Packing'"
    #create the nuget package output directory
    Ensure-Directory-Exists "$nugetPackageDirectory"
    Update-Lucene-Readme-For-Pack $packageVersion
    Update-LuceneCLI-Readme-For-Pack $packageVersion

    try {
        Exec {
            # NOTE: Package version information is not passed in at the command line,
            # instead it is output to the version.props file. This file is then
            # used during a release to "freeze" the build at a specific version
            # so it is always a constant in release distributions.
            & dotnet pack $solutionFile --configuration $configuration --output $nugetPackageDirectory --no-build
        }

        # Set the SYSTEM_DEFAULTWORKINGDIRECTORY if we are not on Azure DevOps. This will optimize the lucene-cli
        # installation test so it doesn't rebuild the NuGet package during a local build.
        if (-Not (Test-Path 'env:SYSTEM_DEFAULTWORKINGDIRECTORY')) {
            $env:SYSTEM_DEFAULTWORKINGDIRECTORY = "$baseDirectory"
        }

        $success = $true
    } finally {
        #if ($success -ne $true) {
            Delete-Added-Files $addedFiles
            Restore-Files $backedUpFiles
        #}
    }
}

# Loops through each framework in the TestTargetFrameworks variable and
# publishes the solution in the artifact staging directory with the framework
# as part of the folder structure.
task Publish -depends Compile -description "This task uses dotnet publish to package the binaries with all of their dependencies so they can be run xplat" {
    Write-Host "##teamcity[progressMessage 'Publishing']"
    Write-Host "##vso[task.setprogress]'Publishing'"

    try {
        $frameworksToTest = Get-FrameworksToTest
        
        if ($zipPublishedArtifacts) {
            $outDirectory = New-TemporaryDirectory
        } else {
            $outDirectory = $publishDirectory
        }
        
        foreach ($framework in $frameworksToTest) {

            # Pause if we have queued too many parallel jobs<commit_msg>.build/runbuild.ps1: Print the SYSTEM_DEFAULTWORKINGDIRECTORY environment variable for debugging purposes<commit_after>﻿# -----------------------------------------------------------------------------------
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the ""License""); you may not use this file except in compliance with
# the License.  You may obtain a copy of the License at
# 
# http://www.apache.org/licenses/LICENSE-2.0
# 
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an ""AS IS"" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# -----------------------------------------------------------------------------------

properties {
    [string]$baseDirectory   = Resolve-Path "../."
    [string]$artifactsDirectory  = "$baseDirectory/_artifacts"
    [string]$sourceDirectory = "$baseDirectory/src"
    [string]$testDirectory = "$baseDirectory/src"
    [string]$toolsDirectory  = "$baseDirectory/lib"
    [string]$nugetPackageDirectory = "$artifactsDirectory/NuGetPackages"
    [string]$testResultsDirectory = "$artifactsDirectory/TestResults"
    [string]$publishDirectory = "$artifactsDirectory/Publish"
    [string]$solutionFile = "$baseDirectory/Lucene.Net.sln"
    [string]$minimumSdkVersion = "7.0.100"
    [string]$globalJsonFile = "$baseDirectory/global.json"
    [string]$versionPropsFile = "$baseDirectory/version.props"
    [string]$luceneReadmeFile = "$baseDirectory/src/Lucene.Net/readme-nuget.md"
    [string]$luceneCLIReadmeFile = "$baseDirectory/src/dotnet/tools/lucene-cli/docs/index.md"
    [string]$rootWebsiteUrl = "https://lucenenet.apache.org"
    [string]$rootDocsWebsiteUrl = "$rootWebsiteUrl/docs"

    [string]$buildCounter     = $(if ($buildCounter) { $buildCounter } else { $env:BuildCounter }) #NOTE: Pass in as a parameter (not a property) or environment variable to override
    [string]$preReleaseCounterPattern = $(if ($preReleaseCounterPattern) { $preReleaseCounterPattern } else { if ($env:PreReleaseCounterPattern) { $env:PreReleaseCounterPattern } else { "0000000000" } })  #NOTE: Pass in as a parameter (not a property) or environment variable to override
    [string]$versionSuffix    = $(if ($versionSuffix -ne $null) { $versionSuffix } else { if ($env:VersionSuffix -ne $null) { $env:VersionSuffix } else { 'ci' }}) #NOTE: Pass in as a parameter (not a property) or environment variable to override
    [string]$packageVersion   = Get-Package-Version #NOTE: Pass in as a parameter (not a property) or environment variable to override
    [string]$version          = Get-Version
    [string]$configuration    = $(if ($configuration) { $configuration } else { if ($env:BuildConfiguration) { $env:BuildConfiguration } else { "Release" } })  #NOTE: Pass in as a parameter (not a property) or environment variable to override
    [string]$platform   = $(if ($platform) { $platform } else { if ($env:BuildPlatform) { $env:BuildPlatform } else { "Any CPU" } })  #NOTE: Pass in as a parameter (not a property) or environment variable to override
    [bool]$backupFiles       = $true
    [bool]$prepareForBuild    = $true
    [bool]$zipPublishedArtifacts = $false
    [string]$publishedArtifactZipFileName = "artifact.zip"

    [int]$maximumParallelJobs = 8
    
    #test parameters
    #The build uses Lucene.Net.Tests.Analysis.Common to determine all of the targets for the solution:
    [string]$projectWithAllTestFrameworks = "$baseDirectory/src/Lucene.Net.Tests.Analysis.Common/Lucene.Net.Tests.Analysis.Common.csproj"
    [string]$where = ""
}

$backedUpFiles = New-Object System.Collections.ArrayList
$addedFiles = New-Object System.Collections.ArrayList

task default -depends Pack

task Clean -description "This task cleans up the build directory" {
    Write-Host "##teamcity[progressMessage 'Cleaning']"
    Write-Host "##vso[task.setprogress]'Cleaning'"
    Remove-Item $artifactsDirectory -Force -Recurse -ErrorAction SilentlyContinue
    Get-ChildItem $baseDirectory -Include *.bak -Recurse | foreach ($_) {Remove-Item $_.FullName}
}

task UpdateLocalSDKVersion -description "Backs up the project.json file and pins the version to $minimumSdkVersion" {
    Backup-File $globalJsonFile
    Generate-Global-Json `
        -sdkVersion $minimumSdkVersion `
        -file $globalJsonFile
}

task CheckSDK -description "This task makes sure the correct SDK version is installed" {
    # Check prerequisites
    $sdkVersion = ((& dotnet --version) | Out-String).Trim()
    if ($LASTEXITCODE -ne 0) {
        throw "dotnet command was not found. Please install .NET $minimumSdkVersion or higher SDK and make sure it is in your PATH."
    }
    $releaseVersion = if ($sdkVersion.Contains('-')) { "$sdkVersion".Substring(0, "$sdkVersion".IndexOf('-')) } else { $sdkVersion }
    if ([version]$releaseVersion -lt ([version]$minimumSdkVersion)) {
        throw "Minimum .NET SDK $minimumSdkVersion required. Current SDK version is $releaseVersion. Please install the required SDK before running the command."
    }
}

task Init -depends CheckSDK, UpdateLocalSDKVersion -description "This task makes sure the build environment is correctly setup" {
    #Update TeamCity, MyGet, or Azure Pipelines with packageVersion
    Write-Output "##teamcity[buildNumber '$packageVersion']"
    Write-Output "##myget[buildNumber '$packageVersion']"
    Write-Host "##vso[build.updatebuildnumber]$packageVersion"

    & dotnet --version
    & dotnet --info
    Write-Host "Base Directory: $(Normalize-FileSystemSlashes "$baseDirectory")"
    Write-Host "Release Directory: $(Normalize-FileSystemSlashes "$artifactsDirectory")"
    Write-Host "Source Directory: $(Normalize-FileSystemSlashes "$sourceDirectory")"
    Write-Host "Test Directory: $(Normalize-FileSystemSlashes "$testDirectory")"
    Write-Host "Tools Directory: $(Normalize-FileSystemSlashes "$toolsDirectory")"
    Write-Host "NuGet Package Directory: $(Normalize-FileSystemSlashes "$nugetPackageDirectory")"
    Write-Host "BuildCounter: $buildCounter"
    Write-Host "PreReleaseCounterPattern: $preReleaseCounterPattern"
    Write-Host "VersionSuffix: $versionSuffix"
    Write-Host "Package Version: $packageVersion"
    Write-Host "File Version: $version"
    Write-Host "Configuration: $configuration"
    Write-Host "Platform: $platform"
    Write-Host "MaximumParallelJobs: $($maximumParallelJobs.ToString())"
    Write-Host "Powershell Version: $($PSVersionTable.PSVersion)"

    Ensure-Directory-Exists "$artifactsDirectory"
}

task Restore -description "This task restores the dependencies" {
    Write-Host "##teamcity[progressMessage 'Restoring']"
    Write-Host "##vso[task.setprogress]'Restoring'"
    Exec { 
        & dotnet restore $solutionFile --no-dependencies /p:TestFrameworks=true
    }
}

task Compile -depends Clean, Init, Restore -description "This task compiles the solution" {
    Write-Host "##teamcity[progressMessage 'Compiling']"
    Write-Host "##vso[task.setprogress]'Compiling'"
    try {
        if ($prepareForBuild -eq $true) {
            Prepare-For-Build
        }

        Exec {
            # NOTE: Version information is not passed in at the command line,
            # instead it is output to the version.props file. This file is then
            # used during a release to "freeze" the build at a specific version
            # so it is always a constant in release distributions.
            & dotnet build "$solutionFile" `
                --configuration "$configuration" `
                --no-restore `
                -p:Platform=$platform `
                -p:PortableDebugTypeOnly=true `
                -p:TestFrameworks=true # workaround for parsing issue: https://github.com/Microsoft/msbuild/issues/471#issuecomment-181963350
        }

        $success = $true
    } finally {
        if ($success -ne $true) {
            Delete-Added-Files $addedFiles
            Restore-Files $backedUpFiles
        }
    }
}

task Pack -depends Compile -description "This task creates the NuGet packages" {
    Write-Host "##teamcity[progressMessage 'Packing']"
    Write-Host "##vso[task.setprogress]'Packing'"
    #create the nuget package output directory
    Ensure-Directory-Exists "$nugetPackageDirectory"
    Update-Lucene-Readme-For-Pack $packageVersion
    Update-LuceneCLI-Readme-For-Pack $packageVersion

    try {
        Exec {
            # NOTE: Package version information is not passed in at the command line,
            # instead it is output to the version.props file. This file is then
            # used during a release to "freeze" the build at a specific version
            # so it is always a constant in release distributions.
            & dotnet pack $solutionFile --configuration $configuration --output $nugetPackageDirectory --no-build
        }

        # Set the SYSTEM_DEFAULTWORKINGDIRECTORY if we are not on Azure DevOps. This will optimize the lucene-cli
        # installation test so it doesn't rebuild the NuGet package during a local build.
        if (-Not (Test-Path 'env:SYSTEM_DEFAULTWORKINGDIRECTORY')) {
            $env:SYSTEM_DEFAULTWORKINGDIRECTORY = "$baseDirectory"
        }

        Write-Host ""
        Write-Host "\nSYSTEM_DEFAULTWORKINGDIRECTORY is $env:SYSTEM_DEFAULTWORKINGDIRECTORY (for lucene-cli installation tests)" -ForegroundColor Yellow

        $success = $true
    } finally {
        #if ($success -ne $true) {
            Delete-Added-Files $addedFiles
            Restore-Files $backedUpFiles
        #}
    }
}

# Loops through each framework in the TestTargetFrameworks variable and
# publishes the solution in the artifact staging directory with the framework
# as part of the folder structure.
task Publish -depends Compile -description "This task uses dotnet publish to package the binaries with all of their dependencies so they can be run xplat" {
    Write-Host "##teamcity[progressMessage 'Publishing']"
    Write-Host "##vso[task.setprogress]'Publishing'"

    try {
        $frameworksToTest = Get-FrameworksToTest
        
        if ($zipPublishedArtifacts) {
            $outDirectory = New-TemporaryDirectory
        } else {
            $outDirectory = $publishDirectory
        }
        
        foreach ($framework in $frameworksToTest) {

            # Pause if we have queued too many parallel jobs
{'fraction_non_alphanumeric': 0.11672985781990522, 'fraction_numerical': 0.003270142180094787, 'mean_word_length': 3.339983545865899, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 4, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6230439', 'n_tokens_mistral': 5782, 'n_tokens_neox': 5562, 'n_words': 1750}