<commit_before>Param(
    [Parameter(Mandatory=$false)]
    [string]$GitURL="https://github.com/dcos/dcos-net",
    [Parameter(Mandatory=$false)]
    [string]$Branch="master",
    [Parameter(Mandatory=$false)]
    [string]$CommitID,
    [Parameter(Mandatory=$false)]
    [string]$ParametersFile="${env:WORKSPACE}\build-parameters.json"
)

$ErrorActionPreference = "Stop"

$globalVariables = (Resolve-Path "$PSScriptRoot\..\global-variables.ps1").Path
$ciUtils = (Resolve-Path "$PSScriptRoot\..\Modules\CIUtils").Path

Import-Module $ciUtils
. $globalVariables

$global:PARAMETERS = @{
    "BUILD_STATUS" = $null
    "LOGS_URLS" = @()
    "FAILED_COMMAND" = $null
}


function Start-DCOSNetCIProcess {
    Param(
        [Parameter(Mandatory=$true)]
        [string]$ProcessPath,
        [Parameter(Mandatory=$false)]
        [string[]]$ArgumentList,
        [Parameter(Mandatory=$true)]
        [string]$StdoutFileName,
        [Parameter(Mandatory=$true)]
        [string]$StderrFileName,
        [Parameter(Mandatory=$true)]
        [string]$BuildErrorMessage
    )
    $stdoutFile = Join-Path $DCOS_NET_BUILD_LOGS_DIR $StdoutFileName
    $stderrFile = Join-Path $DCOS_NET_BUILD_LOGS_DIR $StderrFileName
    New-Item -ItemType File -Path $stdoutFile
    New-Item -ItemType File -Path $stderrFile
    $logsUrl = Get-BuildLogsUrl
    $stdoutUrl = "${logsUrl}/${StdoutFileName}"
    $stderrUrl = "${logsUrl}/${StderrFileName}"
    $command = $ProcessPath -replace '\\', '\\'
    if($ArgumentList.Count) {
        $ArgumentList | Foreach-Object { $command += " $($_ -replace '\\', '\\')" }
    }
    try {
        Wait-ProcessToFinish -ProcessPath $ProcessPath -ArgumentList $ArgumentList `
                             -StandardOutput $stdoutFile -StandardError $stderrFile
        $msg = "Successfully executed: $command"
    } catch {
        $msg = "Failed command: $command"
        $global:PARAMETERS["BUILD_STATUS"] = 'FAIL'
        $global:PARAMETERS["LOGS_URLS"] += $($stdoutUrl, $stderrUrl)
        $global:PARAMETERS["FAILED_COMMAND"] = $command

        Write-Output "Exception: $($_.ToString())"
        Throw $BuildErrorMessage
    } finally {
        Write-Output $msg
        Write-Output "Stdout log available at: $stdoutUrl"
        Write-Output "Stderr log available at: $stderrUrl"
    }
}

function Get-LatestCommitID {
    if(!$global:LATEST_COMMIT_ID) {
        Throw "Failed to get the latest dcos-net commit ID. Perhaps it was not yet saved."
    }
    return $global:LATEST_COMMIT_ID
}

function Get-DCOSNetBuildRelativePath {
    $repositoryOwner = $GitURL.Split("/")[-2]
    $dcosNetCommitID = Get-LatestCommitID
    return "$repositoryOwner/$Branch/$dcosNetCommitID"
}

function Get-BuildOutputsUrl {
    $relativePath = Get-DCOSNetBuildRelativePath
    return "$DCOS_NET_BUILD_BASE_URL/$relativePath"
}

function Get-BuildLogsUrl {
    $buildOutUrl = Get-BuildOutputsUrl
    return "$buildOutUrl/logs"
}

function Get-RemoteBuildDirectoryPath {
    $relativePath = Get-DCOSNetBuildRelativePath
    return "$REMOTE_DCOS_NET_BUILD_DIR/$relativePath"
}

function Get-RemoteLatestSymlinkPath {
    return "$REMOTE_DCOS_NET_BUILD_DIR/$Branch/latest"
}

function Set-LatestDCOSNetCommit {
    Push-Location $DCOS_NET_GIT_REPO_DIR
    try {
        if($CommitID) {
            Start-ExternalCommand { git.exe reset --hard $CommitID } -ErrorMessage "Failed to set the dcos-net git repository last commit to: $CommitID."
        }
        Start-ExternalCommand { git.exe log -n 1 } -ErrorMessage "Failed to get the latest commit message for the dcos-net git repository" | Out-File "$DCOS_NET_BUILD_LOGS_DIR\latest-commit.log"
        $dcosNetCommitId = Start-ExternalCommand { git.exe log --format="%H" -n 1 } -ErrorMessage "Failed to get the latest commit id for the dcos-net git repository"
        Set-Variable -Name "LATEST_COMMIT_ID" -Value $dcosNetCommitId -Scope Global -Option ReadOnly
    } finally {
        Pop-Location
    }
}

function New-Environment {
    Start-EnvironmentCleanup # Do an environment cleanup to make sure everything is fresh
    New-Directory $DCOS_NET_DIR
    New-Directory $DCOS_NET_BUILD_OUT_DIR
    New-Directory $DCOS_NET_BUILD_LOGS_DIR
    New-Directory $DCOS_NET_BUILD_RELEASE_DIR
    Start-GitClone -URL $GitURL -Branch $Branch -Path $DCOS_NET_GIT_REPO_DIR
    Start-GitClone -URL $LIBSODIUM_GIT_URL -Path $DCOS_NET_LIBSODIUM_GIT_DIR
    $global:PARAMETERS["BRANCH"] = $Branch
    Set-LatestDCOSNetCommit
}

function Set-WindowsSDK {
    Param(
        [Parameter(Mandatory=$true)]
        [string]$VCXProjFile,
        [Parameter(Mandatory=$true)]
        [string]$Version
    )

    [xml]$settings = Get-Content $VCXProjFile
    $target = $settings.Project.PropertyGroup | ? { $_.Label -eq "Globals" }
    if($target.WindowsTargetPlatformVersion) {
        $target.WindowsTargetPlatformVersion = $Version
    } else {
        $element = $settings.CreateElement('WindowsTargetPlatformVersion', $settings.DocumentElement.NamespaceURI)
        $element.InnerText = $Version
        $target.AppendChild($element) | Out-Null
    }
    $settings.Save($VCXProjFile)
}

function Start-LibsodiumBuild {
    Set-VCVariables "15.0"
    Push-Location $DCOS_NET_LIBSODIUM_GIT_DIR
    Set-WindowsSDK -VCXProjFile "$DCOS_NET_LIBSODIUM_GIT_DIR\builds\msvc\vs2017\libsodium\libsodium.vcxproj" -Version "10.0.16299.0"
    Write-Output "Starting the libsodium build"
    try {
        Start-DCOSNetCIProcess -ProcessPath "MSBuild.exe" `
                               -ArgumentList @('builds\msvc\vs2017\libsodium.sln', '/nologo', '/target:Build', '/p:Platform=x64', '/p:Configuration="DynRelease"') `
                               -BuildErrorMessage "dcos-net common tests run was not successful" `
                               -StdoutFileName "libsodium-build-stdout.log" -StderrFileName "libsodium-build-stderr.log"
    } finally {
        Pop-Location
    }
    $dynamicDir = Join-Path $DCOS_NET_LIBSODIUM_GIT_DIR "bin\x64\Release\v141\dynamic"
    $env:LDFLAGS=" /LIBPATH:$($dynamicDir -replace '\\', '/') libsodium.lib "
    $includeDir = Join-Path $DCOS_NET_LIBSODIUM_GIT_DIR "src/libsodium/include"
    $env:CFLAGS=" -I$($includeDir -replace '\\', '/') "
    $env:PATH = "$DCOS_NET_LIBSODIUM_GIT_DIR\bin\x64\Release\v141\dynamic;" + $env:PATH
    Write-Output "Successfully built libsodium"
}

function Start-DCOSNetBuild {
    Push-Location $DCOS_NET_GIT_REPO_DIR
    Write-Output "Starting the dcos-net build"
    try {
        Start-DCOSNetCIProcess -ProcessPath "escript.exe" -ArgumentList @(".\rebar3", "as", "windows", "release") `
                               -StdoutFileName "dcos-net-make-stdout.log" -StderrFileName "dcos-net-make-stderr.log" `
                               -BuildErrorMessage "dcos-net failed to build."
    } finally {
        Pop-Location
    }<commit_msg>Fix dcos-net libsodium dependency build<commit_after>Param(
    [Parameter(Mandatory=$false)]
    [string]$GitURL="https://github.com/dcos/dcos-net",
    [Parameter(Mandatory=$false)]
    [string]$Branch="master",
    [Parameter(Mandatory=$false)]
    [string]$CommitID,
    [Parameter(Mandatory=$false)]
    [string]$ParametersFile="${env:WORKSPACE}\build-parameters.json"
)

$ErrorActionPreference = "Stop"

$globalVariables = (Resolve-Path "$PSScriptRoot\..\global-variables.ps1").Path
$ciUtils = (Resolve-Path "$PSScriptRoot\..\Modules\CIUtils").Path

Import-Module $ciUtils
. $globalVariables

$global:PARAMETERS = @{
    "BUILD_STATUS" = $null
    "LOGS_URLS" = @()
    "FAILED_COMMAND" = $null
}


function Start-DCOSNetCIProcess {
    Param(
        [Parameter(Mandatory=$true)]
        [string]$ProcessPath,
        [Parameter(Mandatory=$false)]
        [string[]]$ArgumentList,
        [Parameter(Mandatory=$true)]
        [string]$StdoutFileName,
        [Parameter(Mandatory=$true)]
        [string]$StderrFileName,
        [Parameter(Mandatory=$true)]
        [string]$BuildErrorMessage
    )
    $stdoutFile = Join-Path $DCOS_NET_BUILD_LOGS_DIR $StdoutFileName
    $stderrFile = Join-Path $DCOS_NET_BUILD_LOGS_DIR $StderrFileName
    New-Item -ItemType File -Path $stdoutFile
    New-Item -ItemType File -Path $stderrFile
    $logsUrl = Get-BuildLogsUrl
    $stdoutUrl = "${logsUrl}/${StdoutFileName}"
    $stderrUrl = "${logsUrl}/${StderrFileName}"
    $command = $ProcessPath -replace '\\', '\\'
    if($ArgumentList.Count) {
        $ArgumentList | Foreach-Object { $command += " $($_ -replace '\\', '\\')" }
    }
    try {
        Wait-ProcessToFinish -ProcessPath $ProcessPath -ArgumentList $ArgumentList `
                             -StandardOutput $stdoutFile -StandardError $stderrFile
        $msg = "Successfully executed: $command"
    } catch {
        $msg = "Failed command: $command"
        $global:PARAMETERS["BUILD_STATUS"] = 'FAIL'
        $global:PARAMETERS["LOGS_URLS"] += $($stdoutUrl, $stderrUrl)
        $global:PARAMETERS["FAILED_COMMAND"] = $command

        Write-Output "Exception: $($_.ToString())"
        Throw $BuildErrorMessage
    } finally {
        Write-Output $msg
        Write-Output "Stdout log available at: $stdoutUrl"
        Write-Output "Stderr log available at: $stderrUrl"
    }
}

function Get-LatestCommitID {
    if(!$global:LATEST_COMMIT_ID) {
        Throw "Failed to get the latest dcos-net commit ID. Perhaps it was not yet saved."
    }
    return $global:LATEST_COMMIT_ID
}

function Get-DCOSNetBuildRelativePath {
    $repositoryOwner = $GitURL.Split("/")[-2]
    $dcosNetCommitID = Get-LatestCommitID
    return "$repositoryOwner/$Branch/$dcosNetCommitID"
}

function Get-BuildOutputsUrl {
    $relativePath = Get-DCOSNetBuildRelativePath
    return "$DCOS_NET_BUILD_BASE_URL/$relativePath"
}

function Get-BuildLogsUrl {
    $buildOutUrl = Get-BuildOutputsUrl
    return "$buildOutUrl/logs"
}

function Get-RemoteBuildDirectoryPath {
    $relativePath = Get-DCOSNetBuildRelativePath
    return "$REMOTE_DCOS_NET_BUILD_DIR/$relativePath"
}

function Get-RemoteLatestSymlinkPath {
    return "$REMOTE_DCOS_NET_BUILD_DIR/$Branch/latest"
}

function Set-LatestDCOSNetCommit {
    Push-Location $DCOS_NET_GIT_REPO_DIR
    try {
        if($CommitID) {
            Start-ExternalCommand { git.exe reset --hard $CommitID } -ErrorMessage "Failed to set the dcos-net git repository last commit to: $CommitID."
        }
        Start-ExternalCommand { git.exe log -n 1 } -ErrorMessage "Failed to get the latest commit message for the dcos-net git repository" | Out-File "$DCOS_NET_BUILD_LOGS_DIR\latest-commit.log"
        $dcosNetCommitId = Start-ExternalCommand { git.exe log --format="%H" -n 1 } -ErrorMessage "Failed to get the latest commit id for the dcos-net git repository"
        Set-Variable -Name "LATEST_COMMIT_ID" -Value $dcosNetCommitId -Scope Global -Option ReadOnly
    } finally {
        Pop-Location
    }
}

function New-Environment {
    Start-EnvironmentCleanup # Do an environment cleanup to make sure everything is fresh
    New-Directory $DCOS_NET_DIR
    New-Directory $DCOS_NET_BUILD_OUT_DIR
    New-Directory $DCOS_NET_BUILD_LOGS_DIR
    New-Directory $DCOS_NET_BUILD_RELEASE_DIR
    Start-GitClone -URL $GitURL -Branch $Branch -Path $DCOS_NET_GIT_REPO_DIR
    Start-GitClone -URL $LIBSODIUM_GIT_URL -Path $DCOS_NET_LIBSODIUM_GIT_DIR
    $global:PARAMETERS["BRANCH"] = $Branch
    Set-LatestDCOSNetCommit
}

function Set-WindowsSDK {
    Param(
        [Parameter(Mandatory=$true)]
        [string]$VCXProjFile,
        [Parameter(Mandatory=$true)]
        [string]$Version
    )

    [xml]$settings = Get-Content $VCXProjFile
    $target = $settings.Project.PropertyGroup | ? { $_.Label -eq "Globals" }
    if($target.WindowsTargetPlatformVersion) {
        $target.WindowsTargetPlatformVersion = $Version
    } else {
        $element = $settings.CreateElement('WindowsTargetPlatformVersion', $settings.DocumentElement.NamespaceURI)
        $element.InnerText = $Version
        $target.AppendChild($element) | Out-Null
    }
    $settings.Save($VCXProjFile)
}

function Start-LibsodiumBuild {
    Set-VCVariables "15.0"
    Push-Location $DCOS_NET_LIBSODIUM_GIT_DIR
    Set-WindowsSDK -VCXProjFile "$DCOS_NET_LIBSODIUM_GIT_DIR\builds\msvc\vs2017\libsodium\libsodium.vcxproj" -Version "10.0.17134.0"
    Write-Output "Starting the libsodium build"
    try {
        Start-DCOSNetCIProcess -ProcessPath "MSBuild.exe" `
                               -ArgumentList @('builds\msvc\vs2017\libsodium.sln', '/nologo', '/target:Build', '/p:Platform=x64', '/p:Configuration="DynRelease"') `
                               -BuildErrorMessage "dcos-net common tests run was not successful" `
                               -StdoutFileName "libsodium-build-stdout.log" -StderrFileName "libsodium-build-stderr.log"
    } finally {
        Pop-Location
    }
    $dynamicDir = Join-Path $DCOS_NET_LIBSODIUM_GIT_DIR "bin\x64\Release\v141\dynamic"
    $env:LDFLAGS=" /LIBPATH:$($dynamicDir -replace '\\', '/') libsodium.lib "
    $includeDir = Join-Path $DCOS_NET_LIBSODIUM_GIT_DIR "src/libsodium/include"
    $env:CFLAGS=" -I$($includeDir -replace '\\', '/') "
    $env:PATH = "$DCOS_NET_LIBSODIUM_GIT_DIR\bin\x64\Release\v141\dynamic;" + $env:PATH
    Write-Output "Successfully built libsodium"
}

function Start-DCOSNetBuild {
    Push-Location $DCOS_NET_GIT_REPO_DIR
    Write-Output "Starting the dcos-net build"
    try {
        Start-DCOSNetCIProcess -ProcessPath "escript.exe" -ArgumentList @(".\rebar3", "as", "windows", "release") `
                               -StdoutFileName "dcos-net-make-stdout.log" -StderrFileName "dcos-net-make-stderr.log" `
                               -BuildErrorMessage "dcos-net failed to build."
    } finally {
        Pop-Location
    }
{'fraction_non_alphanumeric': 0.12319474835886214, 'fraction_numerical': 0.005397520058351568, 'mean_word_length': 3.244891640866873, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, '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': '1331527', 'n_tokens_mistral': 4583, 'n_tokens_neox': 4454, 'n_words': 794}