<commit_before># PSakefile script.
# Requires the module 'Narvalo.ProjectAutomation'.
#
# WARNING:
# - When enabling source analysis, you MUST also build the StyleCop project.

# We force the framework to be sure we use the v12.0 of the build tools.
# For instance, this is a requirement for the _MyGet-Publish target where
# the DeployOnBuild instruction is not understood by previous versions of MSBuild.
# TODO: Check if this is still necessary. We could also use Invoke-psake -framework '4.5.1x64' in
# make.ps1.
Framework '4.5.1x64'

# ------------------------------------------------------------------------------
# Properties
# ------------------------------------------------------------------------------

Properties {
    # Process mandatory parameters.
    Assert($Retail -ne $null) "`$Retail must not be null, e.g. run with -Parameters @{ 'retail' = `$true; }"

    # Process optional parameters.
    if ($Verbosity -eq $null) { $Verbosity = 'minimal' }
    if ($Developer -eq $null) { $Developer = $false }

    # Define the NuGet verbosity level.
    $NuGetVerbosity = ConvertTo-NuGetVerbosity $Verbosity

    # MSBuild options.
    $Opts = '/nologo', "/verbosity:$Verbosity", '/maxcpucount', '/nodeReuse:false'

    # Main MSBuild projects.
    $Everything  = Get-LocalPath 'tools\Make.proj'
    $Foundations = Get-LocalPath 'tools\Make.Foundations.proj'
}

FormatTaskName {
    param([Parameter(Mandatory = $true)] [string] $TaskName)

    Write-Host "Executing Task '$taskName'." -ForegroundColor DarkCyan
}

TaskTearDown {
    # Catch errors from both PowerShell and Win32 exe.
    if (!$?) {
        Exit-Gracefully -ExitCode 1 'Build failed.'
    }

    #if ($LastExitCode -ne 0) {
    #    Exit-Gracefully -ExitCode $LastExitCode 'Build failed.'
    #}
}

Task Default -Depends Build

# ------------------------------------------------------------------------------
# Continuous Integration and development tasks
# ------------------------------------------------------------------------------

# NB: No need to restore packages before building the projects $Everything
# or $Foundations; this will be done in MSBuild.

Task Build `
    -Description '[ALL+ ] Build.' `
    -Depends _CI-InitializeVariables `
{
    MSBuild $Everything $Opts $CI_Props '/t:Build'
}

Task FullClean `
    -Description '        Delete the entire build directory.' `
    -Alias Clean `
    -ContinueOnError `
{
    # Sometimes this task fails for some obscure reasons. Maybe the directory is locked?
    Remove-LocalItem 'work' -Recurse
}

Task Check `
    -Description '[SAFE ] Build, run StyleCop, FxCop and PEVerify (SLOW).' `
    -Depends _CI-InitializeVariables `
{
    # Perform the following operations:
    # - Build all projects
    # - Run Source Analysis
    # - Verify Portable Executable (PE) format
    MSBuild $Foundations $Opts $CI_Props `
        '/t:Build;PEVerify',
        '/p:Configuration=Debug',
        '/p:VisibleInternals=false',
        '/p:SkipCodeContractsReferenceAssembly=true',
        '/p:SkipDocumentation=true',
        '/p:RunCodeAnalysis=true',
        '/p:SourceAnalysisEnabled=true',
        '/p:Filter=_Safe_'
}
#Task SourceAnalysis `
#    -Description '[SAFE ] Run source analysis.' `
#    -Depends _CI-InitializeVariables `
#{
#    MSBuild $Foundations $Opts $CI_Props `
#        '/t:Build',
#        '/p:Configuration=Debug',
#        '/p:SkipCodeContractsReferenceAssembly=true',
#        '/p:SkipDocumentation=true',
#        '/p:SourceAnalysisEnabled=true',
#        '/p:Filter=_Safe_'
#}
#Task CodeAnalysis `
#    -Description '[SAFE ] Run Code Analysis (SLOW).' `
#    -Depends _CI-InitializeVariables `
#{
#    # For static analysis, we hide internals, otherwise we might not truly
#    # analyze the public API.
#    MSBuild $Foundations $Opts $CI_Props `
#        '/t:Build',
#        '/p:VisibleInternals=false',
#        '/p:RunCodeAnalysis=true',
#        '/p:SkipCodeContractsReferenceAssembly=true',
#        '/p:SkipDocumentation=true',
#        '/p:Filter=_Safe_'
#}

Task Test `
    -Description '[ALL  ] Build then run tests.' `
    -Depends _CI-InitializeVariables `
{
    MSBuild $Foundations $Opts $CI_Props `
        '/t:Xunit',
        '/p:Configuration=Debug',
        '/p:SkipCodeContractsReferenceAssembly=true',
        '/p:SkipDocumentation=true'
}

Task OpenCover `
    -Description '[CORE ] Run OpenCover (EXTREMELY SLOW).' `
    -Depends _CI-InitializeVariables `
    -Alias Cover `
{
    # Use debug build to also cover debug-only tests.
    # For static analysis, we hide internals, otherwise we might not truly
    # analyze the public API.
    MSBuild $Foundations $Opts $CI_Props `
        '/p:Configuration=Debug',
        '/t:Build',
        '/p:VisibleInternals=false',
        '/p:SkipCodeContractsReferenceAssembly=true',
        '/p:SkipDocumentation=true',
        '/p:Filter=_Core_'

    Invoke-OpenCover 'Debug+Closed'
    #Invoke-OpenCover 'Debug+Closed' -Summary
}

Task GendarmeAnalysis `
    -Description '[CORE ] Run Mono.Gendarme (SLOW).' `
    -Depends _CI-InitializeVariables `
    -Alias Keuf `
{
    # For static analysis, we hide internals, otherwise we might not truly
    # analyze the public API.
    MSBuild $Foundations $Opts $CI_Props `
        '/t:Build',
        '/p:EnableGendarme=true',
        '/p:VisibleInternals=false',
        '/p:SkipCodeContractsReferenceAssembly=true',
        '/p:SkipDocumentation=true',
        '/p:Filter=_Core_'

    Invoke-Gendarme 'Release+Closed'
}

Task CodeContractsAnalysis `
    -Description '[SAFE ] Run Code Contracts Analysis (EXTREMELY SLOW).' `
    -Depends _CI-InitializeVariables `
    -Alias CC `
{
    # For static analysis, we hide internals, otherwise we might not truly
    # analyze the public API.
    MSBuild $Foundations $Opts $CI_Props `
        '/t:Build',
        '/p:VisibleInternals=false',
        '/p:Configuration=CodeContracts',
        '/p:Filter=_Safe_'
}

Task SecurityAnalysis `
    -Description '[SAFE ] Run SecAnnotate (SLOW).' `
    -Depends _CI-InitializeVariables `
    -Alias SA `
{
    MSBuild $Foundations $Opts $CI_Props `
        '/t:Clean;SecAnnotate;PEVerify',
        # For static analysis, we hide internals, otherwise we might not truly
        # analyze the public API.
        '/p:VisibleInternals=false',
        '/p:SignAssembly=true',
        '/p:SkipCodeContractsReferenceAssembly=true',
        '/p:SkipDocumentation=true',
        '/p:Filter=_Safe_'
}

Task _CI-InitializeVariables `
    -Description 'Initialize variables only used by the CI tasks.' `
    -RequiredVariables Retail `
{
    # Default CI properties:
    # - Release configuration
    # - Do not generate assembly versions
    # - Do not sign assemblies
    # - Do not skip the generation of the Code Contracts reference assembly
    # - Leak internals to enable all white-box tests.
    # FIXME: CodeContracts disabled.
    $script:CI_Props = `
        '/p:Configuration=Release',
        '/p:BuildGeneratedVersion=false',
        "/p:Retail=$Retail",
        '/p:SignAssembly=false',
        '/p:SkipCodeContractsReferenceAssembly=true',
        '/p:VisibleInternals=true'

    # FIXME: Don't understand why doing what follows does not work.
    # Either MSBuild or PowerShell mixes up the MSBuild parameters.
    # The result is that Configuration property takes all following properties
    # as its value. For instance, Configuration is read as "Release /p:BuildGeneratedVersion=false...".
    # For static analysis, we hide internals, otherwise we might not truly
    # analyze the public API.
    #$script:CI_AnalysisProps = $CI_Props, '/p:VisibleInternals=false'
}

# ------------------------------------------------------------------------------
# Packaging tasks
# ------------------------------------------------------------------------------

Task Package-All `
    -Description '[ALL  ] Package.' `
    -Depends Package-Core, Package-Mvp, Package-Build `
    -Alias Pack

Task Package-Core `
    -Description '[CORE ] Package.' `
    -Depends _Package-InitializeVariables `
    -Alias PackCore `
{
    MSBuild $Foundations $Opts $Package_Targets $Package_Props `
        '/p:Filter=_Core_'
}

Task Package-Mvp `
    -Description '[MVP  ] Package.' `
    -Depends _Package-InitializeVariables `
    -Alias PackMvp `
{
    MSBuild $Foundations $Opts $Package_Targets $Package_Props `
        '/p:Filter=_Mvp_'
}

Task Package-Build `
    -Description '[BUILD] Package.' `
    -Depends _Package-InitializeVariables `
    -Alias PackBuild `
{
    MSBuild $Foundations $Opts $Package_Targets $Package_Props `
        '/p:Filter=_Build_'
}

Task _Package-InitializeVariables `
    -Description 'Initialize variables only used by the Package-* tasks.' `
    -Depends _Initialize-GitCommitHash, _Package-CheckVariablesForRetail `
    -RequiredVariables Retail `
{
    # Packaging properties:
    # - Release configuration
    # - Generate assembly versions (necessary for NuGet packaging)
    # - Sign assemblies
    # - Do not skip the generation of the Code Contracts reference assembly
    # - Unconditionally hide internals (implies no white-box testing)
    # FIXME: CodeContracts disabled.
    $script:Package_Props = `
        '/p:Configuration=Release',
        '/p:BuildGeneratedVersion=true',
        "/p:GitCommitHash=$GitCommitHash",
        "/p:Retail=$Retail",
        '/p:SignAssembly=true',
        '/p:SkipCodeContractsReferenceAssembly=true',
        '/p:VisibleInternals=false'

    # Packaging targets:
    # - Rebuild all
    # - Verify Portable Executable (PE) format
    # - Run Xunit tests
    # - Package
    $script:Package_Targets = '/t:Rebuild;PEVerify;Xunit;Package'
}

Task _Package-CheckVariablesForRetail `
    -Description 'Check conditions are met for creating retail packages.' `
    -Depends _Initialize-GitCommitHash `
    -PreCondition { $Retail } `<commit_msg>Reenable CC in command line builds.<commit_after># PSakefile script.
# Requires the module 'Narvalo.ProjectAutomation'.
#
# WARNING:
# - When enabling source analysis, you MUST also build the StyleCop project.

# We force the framework to be sure we use the v12.0 of the build tools.
# For instance, this is a requirement for the _MyGet-Publish target where
# the DeployOnBuild instruction is not understood by previous versions of MSBuild.
# TODO: Check if this is still necessary. We could also use Invoke-psake -framework '4.5.1x64' in
# make.ps1.
Framework '4.5.1x64'

# ------------------------------------------------------------------------------
# Properties
# ------------------------------------------------------------------------------

Properties {
    # Process mandatory parameters.
    Assert($Retail -ne $null) "`$Retail must not be null, e.g. run with -Parameters @{ 'retail' = `$true; }"

    # Process optional parameters.
    if ($Verbosity -eq $null) { $Verbosity = 'minimal' }
    if ($Developer -eq $null) { $Developer = $false }

    # Define the NuGet verbosity level.
    $NuGetVerbosity = ConvertTo-NuGetVerbosity $Verbosity

    # MSBuild options.
    $Opts = '/nologo', "/verbosity:$Verbosity", '/maxcpucount', '/nodeReuse:false'

    # Main MSBuild projects.
    $Everything  = Get-LocalPath 'tools\Make.proj'
    $Foundations = Get-LocalPath 'tools\Make.Foundations.proj'
}

FormatTaskName {
    param([Parameter(Mandatory = $true)] [string] $TaskName)

    Write-Host "Executing Task '$taskName'." -ForegroundColor DarkCyan
}

TaskTearDown {
    # Catch errors from both PowerShell and Win32 exe.
    if (!$?) {
        Exit-Gracefully -ExitCode 1 'Build failed.'
    }

    #if ($LastExitCode -ne 0) {
    #    Exit-Gracefully -ExitCode $LastExitCode 'Build failed.'
    #}
}

Task Default -Depends Build

# ------------------------------------------------------------------------------
# Continuous Integration and development tasks
# ------------------------------------------------------------------------------

# NB: No need to restore packages before building the projects $Everything
# or $Foundations; this will be done in MSBuild.

Task Build `
    -Description '[ALL+ ] Build.' `
    -Depends _CI-InitializeVariables `
{
    MSBuild $Everything $Opts $CI_Props '/t:Build'
}

Task FullClean `
    -Description '        Delete the entire build directory.' `
    -Alias Clean `
    -ContinueOnError `
{
    # Sometimes this task fails for some obscure reasons. Maybe the directory is locked?
    Remove-LocalItem 'work' -Recurse
}

Task Check `
    -Description '[SAFE ] Build, run StyleCop, FxCop and PEVerify (SLOW).' `
    -Depends _CI-InitializeVariables `
{
    # Perform the following operations:
    # - Build all projects
    # - Run Source Analysis
    # - Verify Portable Executable (PE) format
    MSBuild $Foundations $Opts $CI_Props `
        '/t:Build;PEVerify',
        '/p:Configuration=Debug',
        '/p:VisibleInternals=false',
        '/p:SkipCodeContractsReferenceAssembly=true',
        '/p:SkipDocumentation=true',
        '/p:RunCodeAnalysis=true',
        '/p:SourceAnalysisEnabled=true',
        '/p:Filter=_Safe_'
}
#Task SourceAnalysis `
#    -Description '[SAFE ] Run source analysis.' `
#    -Depends _CI-InitializeVariables `
#{
#    MSBuild $Foundations $Opts $CI_Props `
#        '/t:Build',
#        '/p:Configuration=Debug',
#        '/p:SkipCodeContractsReferenceAssembly=true',
#        '/p:SkipDocumentation=true',
#        '/p:SourceAnalysisEnabled=true',
#        '/p:Filter=_Safe_'
#}
#Task CodeAnalysis `
#    -Description '[SAFE ] Run Code Analysis (SLOW).' `
#    -Depends _CI-InitializeVariables `
#{
#    # For static analysis, we hide internals, otherwise we might not truly
#    # analyze the public API.
#    MSBuild $Foundations $Opts $CI_Props `
#        '/t:Build',
#        '/p:VisibleInternals=false',
#        '/p:RunCodeAnalysis=true',
#        '/p:SkipCodeContractsReferenceAssembly=true',
#        '/p:SkipDocumentation=true',
#        '/p:Filter=_Safe_'
#}

Task Test `
    -Description '[ALL  ] Build then run tests.' `
    -Depends _CI-InitializeVariables `
{
    MSBuild $Foundations $Opts $CI_Props `
        '/t:Xunit',
        '/p:Configuration=Debug',
        '/p:SkipCodeContractsReferenceAssembly=true',
        '/p:SkipDocumentation=true'
}

Task OpenCover `
    -Description '[CORE ] Run OpenCover (EXTREMELY SLOW).' `
    -Depends _CI-InitializeVariables `
    -Alias Cover `
{
    # Use debug build to also cover debug-only tests.
    # For static analysis, we hide internals, otherwise we might not truly
    # analyze the public API.
    MSBuild $Foundations $Opts $CI_Props `
        '/p:Configuration=Debug',
        '/t:Build',
        '/p:VisibleInternals=false',
        '/p:SkipCodeContractsReferenceAssembly=true',
        '/p:SkipDocumentation=true',
        '/p:Filter=_Core_'

    Invoke-OpenCover 'Debug+Closed'
    #Invoke-OpenCover 'Debug+Closed' -Summary
}

Task GendarmeAnalysis `
    -Description '[CORE ] Run Mono.Gendarme (SLOW).' `
    -Depends _CI-InitializeVariables `
    -Alias Keuf `
{
    # For static analysis, we hide internals, otherwise we might not truly
    # analyze the public API.
    MSBuild $Foundations $Opts $CI_Props `
        '/t:Build',
        '/p:EnableGendarme=true',
        '/p:VisibleInternals=false',
        '/p:SkipCodeContractsReferenceAssembly=true',
        '/p:SkipDocumentation=true',
        '/p:Filter=_Core_'

    Invoke-Gendarme 'Release+Closed'
}

Task CodeContractsAnalysis `
    -Description '[SAFE ] Run Code Contracts Analysis (EXTREMELY SLOW).' `
    -Depends _CI-InitializeVariables `
    -Alias CC `
{
    # For static analysis, we hide internals, otherwise we might not truly
    # analyze the public API.
    MSBuild $Foundations $Opts $CI_Props `
        '/t:Build',
        '/p:VisibleInternals=false',
        '/p:Configuration=CodeContracts',
        '/p:Filter=_Safe_'
}

Task SecurityAnalysis `
    -Description '[SAFE ] Run SecAnnotate (SLOW).' `
    -Depends _CI-InitializeVariables `
    -Alias SA `
{
    MSBuild $Foundations $Opts $CI_Props `
        '/t:Clean;SecAnnotate;PEVerify',
        # For static analysis, we hide internals, otherwise we might not truly
        # analyze the public API.
        '/p:VisibleInternals=false',
        '/p:SignAssembly=true',
        '/p:SkipCodeContractsReferenceAssembly=true',
        '/p:SkipDocumentation=true',
        '/p:Filter=_Safe_'
}

Task _CI-InitializeVariables `
    -Description 'Initialize variables only used by the CI tasks.' `
    -RequiredVariables Retail `
{
    # Default CI properties:
    # - Release configuration
    # - Do not generate assembly versions
    # - Do not sign assemblies
    # - Do not skip the generation of the Code Contracts reference assembly
    # - Leak internals to enable all white-box tests.
    # FIXME: CodeContracts disabled.
    $script:CI_Props = `
        '/p:Configuration=Release',
        '/p:BuildGeneratedVersion=false',
        "/p:Retail=$Retail",
        '/p:SignAssembly=false',
        '/p:SkipCodeContractsReferenceAssembly=false',
        '/p:VisibleInternals=true'

    # FIXME: Don't understand why doing what follows does not work.
    # Either MSBuild or PowerShell mixes up the MSBuild parameters.
    # The result is that Configuration property takes all following properties
    # as its value. For instance, Configuration is read as "Release /p:BuildGeneratedVersion=false...".
    # For static analysis, we hide internals, otherwise we might not truly
    # analyze the public API.
    #$script:CI_AnalysisProps = $CI_Props, '/p:VisibleInternals=false'
}

# ------------------------------------------------------------------------------
# Packaging tasks
# ------------------------------------------------------------------------------

Task Package-All `
    -Description '[ALL  ] Package.' `
    -Depends Package-Core, Package-Mvp, Package-Build `
    -Alias Pack

Task Package-Core `
    -Description '[CORE ] Package.' `
    -Depends _Package-InitializeVariables `
    -Alias PackCore `
{
    MSBuild $Foundations $Opts $Package_Targets $Package_Props `
        '/p:Filter=_Core_'
}

Task Package-Mvp `
    -Description '[MVP  ] Package.' `
    -Depends _Package-InitializeVariables `
    -Alias PackMvp `
{
    MSBuild $Foundations $Opts $Package_Targets $Package_Props `
        '/p:Filter=_Mvp_'
}

Task Package-Build `
    -Description '[BUILD] Package.' `
    -Depends _Package-InitializeVariables `
    -Alias PackBuild `
{
    MSBuild $Foundations $Opts $Package_Targets $Package_Props `
        '/p:Filter=_Build_'
}

Task _Package-InitializeVariables `
    -Description 'Initialize variables only used by the Package-* tasks.' `
    -Depends _Initialize-GitCommitHash, _Package-CheckVariablesForRetail `
    -RequiredVariables Retail `
{
    # Packaging properties:
    # - Release configuration
    # - Generate assembly versions (necessary for NuGet packaging)
    # - Sign assemblies
    # - Do not skip the generation of the Code Contracts reference assembly
    # - Unconditionally hide internals (implies no white-box testing)
    # FIXME: CodeContracts disabled.
    $script:Package_Props = `
        '/p:Configuration=Release',
        '/p:BuildGeneratedVersion=true',
        "/p:GitCommitHash=$GitCommitHash",
        "/p:Retail=$Retail",
        '/p:SignAssembly=true',
        '/p:SkipCodeContractsReferenceAssembly=false',
        '/p:VisibleInternals=false'

    # Packaging targets:
    # - Rebuild all
    # - Verify Portable Executable (PE) format
    # - Run Xunit tests
    # - Package
    $script:Package_Targets = '/t:Rebuild;PEVerify;Xunit;Package'
}

Task _Package-CheckVariablesForRetail `
    -Description 'Check conditions are met for creating retail packages.' `
    -Depends _Initialize-GitCommitHash `
    -PreCondition { $Retail } `
{'fraction_non_alphanumeric': 0.15515844892173736, 'fraction_numerical': 0.0018224157132732612, 'mean_word_length': 2.535886880257741, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1262743', 'n_tokens_mistral': 5689, 'n_tokens_neox': 5672, 'n_words': 1537}