<commit_before>#I @"tools/FAKE/tools"
#r "FakeLib.dll"

open System
open System.IO
open System.Text

open Fake
open Fake.DotNetCli
open Fake.FileUtils
open Fake.TaskRunnerHelper

// Information about the project for Nuget and Assembly info files
let product = "NBench"
let authors = [ "<NAME>" ]
let copyright = "Copyright  2015-2016"
let company = "Petabridge"
let description = "X-Platform .NET Performance Testing and Measuring Framework"
let tags = ["performance";"benchmarking";"benchmark";"perf testing";"NBench";]
let configuration = "Release"

// Read release notes and version
let parsedRelease =
    File.ReadLines "RELEASE_NOTES.md"
    |> ReleaseNotesHelper.parseReleaseNotes
let envBuildNumber = System.Environment.GetEnvironmentVariable("BUILD_NUMBER") //populated by TeamCity build agent
let buildNumber = if String.IsNullOrWhiteSpace(envBuildNumber) then "0" else envBuildNumber
let version = parsedRelease.AssemblyVersion + "." + buildNumber
let preReleaseVersion = version + "-beta" //suffixes the assembly for pre-releases
let isUnstableDocs = hasBuildParam "unstable"
let isPreRelease = hasBuildParam "nugetprerelease"
let release = if isPreRelease then ReleaseNotesHelper.ReleaseNotes.New(version, version + "-beta", parsedRelease.Notes) else parsedRelease
let isMono = Type.GetType("Mono.Runtime") <> null;

// Directories
let output = __SOURCE_DIRECTORY__  @@ "bin"
let outputTests = __SOURCE_DIRECTORY__ @@ "TestResults"
let outputPerfTests = __SOURCE_DIRECTORY__ @@ "PerfResults"
let outputNuGet = output @@ "nuget"

// Copied from original NugetCreate target
let nugetDir = output @@ "nuget"
let workingDir = output @@ "build"
let nugetExe = FullName @"./tools/nuget.exe"

open AssemblyInfoFile
Target "AssemblyInfo" (fun _ ->
    let version = release.AssemblyVersion

    CreateCSharpAssemblyInfoWithConfig "src/SharedAssemblyInfo.cs" [
        Attribute.Company company
        Attribute.Copyright copyright
        Attribute.Version version
        Attribute.FileVersion version ] <| AssemblyInfoFileConfig(false)
)

Target "Clean" (fun _ ->
    CleanDir output
    CleanDir outputTests
    CleanDir outputPerfTests
    CleanDir outputNuGet
    CleanDirs !! "./**/bin"
    CleanDirs !! "./**/obj"
)

Target "RestorePackages" (fun _ ->
    DotNetCli.Restore
        (fun p -> 
            { p with
                Project = "./NBench.sln"
                NoCache = false })
)

Target "Build" (fun _ ->          
    let runSingleProject project =
        DotNetCli.Build
            (fun p -> 
                { p with
                    Project = project
                    Configuration = configuration })   

    let projects = !! "./src/**/*.csproj" ++ "./tests/**/*.csproj"
     
    projects |> Seq.iter (runSingleProject)
)

Target "RunTests" (fun _ ->
    let runSingleProject project =
        DotNetCli.Test
            (fun p -> 
                { p with
                    Project = project
                    Configuration = configuration})      

    let projects = (!! "./tests/**/*NBench.Tests*.csproj" 
                    -- "./tests/**/*NBench.PerformanceCounters.Tests.*.csproj"
                    -- "./tests/**/*NBench.Tests.Performance.csproj"
                    -- "./tests/**/*NBench.Tests.Performance.WithDependencies.csproj"
                    -- "./tests/**/*NBench.Tests.Assembly.csproj")

    projects |> Seq.iter (log)
    projects |> Seq.iter (runSingleProject)
)

Target "NBench" <| fun _ ->
    if (isWindows) then
        // .NET 4.5.2
        let nbenchRunner = findToolInSubPath "NBench.Runner.exe" "src/NBench.Runner/bin/Release/net452/win7-x64"
        let assembly = __SOURCE_DIRECTORY__ @@ "/tests/NBench.Tests.Performance.WithDependencies/bin/Release/net452/NBench.Tests.Performance.WithDependencies.dll"
        
        let spec = getBuildParam "spec"

        let args = new StringBuilder()
                    |> append assembly
                    |> append (sprintf "output-directory=\"%s\"" outputPerfTests)
                    |> append (sprintf "concurrent=\"%b\"" true)
                    |> append (sprintf "trace=\"%b\"" true)
                    |> toText

        let result = ExecProcess(fun info -> 
            info.FileName <- nbenchRunner
            info.WorkingDirectory <- (Path.GetDirectoryName (FullName nbenchRunner))
            info.Arguments <- args) (System.TimeSpan.FromMinutes 15.0) (* Reasonably long-running task. *)
        if result <> 0 then failwithf "NBench.Runner failed. %s %s" nbenchRunner args
    
        // .NET Core
        let netCoreNbenchRunnerProject = "./src/NBench.Runner.DotNetCli/NBench.Runner.DotNetCli.csproj"
        DotNetCli.Restore
            (fun p ->
                { p with
                    Project = netCoreNbenchRunnerProject
                    AdditionalArgs = ["-r win7-x64"] })
        // build a win7-x64 version of dotnet-nbench.dll so we know we're testing the same architecture
        DotNetCli.Build
            (fun p -> 
                { p with
                    Project = netCoreNbenchRunnerProject
                    Configuration = configuration 
                    Runtime = "win7-x64"
                    Framework = "netcoreapp1.1"})   

        let netCoreNbenchRunner = findToolInSubPath "dotnet-nbench.exe" "/src/NBench.Runner.DotNetCli/bin/Release/netcoreapp1.1/win7-x64/"
        let netCoreAssembly = __SOURCE_DIRECTORY__ @@ "/tests/NBench.Tests.Performance.WithDependencies/bin/Release/netcoreapp1.1/NBench.Tests.Performance.WithDependencies.dll"
        
        let netCoreNbenchRunnerArgs = new StringBuilder()
                                        |> append netCoreAssembly
                                        |> append (sprintf "output-directory=\"%s\"" outputPerfTests)
                                        |> append (sprintf "concurrent=\"%b\"" true)
                                        |> append (sprintf "trace=\"%b\"" true)
                                        |> toText

        let result = ExecProcess(fun info -> 
            info.FileName <- netCoreNbenchRunner
            info.WorkingDirectory <- (Path.GetDirectoryName (FullName netCoreNbenchRunner))
            info.Arguments <- netCoreNbenchRunnerArgs) (System.TimeSpan.FromMinutes 15.0) (* Reasonably long-running task. *)
        if result <> 0 then failwithf "NBench.Runner failed. %s %s" netCoreNbenchRunner netCoreNbenchRunnerArgs
    else
        // .NET Core
        let netCoreNbenchRunnerProject = "./src/NBench.Runner.DotNetCli/NBench.Runner.DotNetCli.csproj"
        DotNetCli.Restore
            (fun p ->
                { p with
                    Project = netCoreNbenchRunnerProject
                    AdditionalArgs = ["-r debian.8-x64"] })
        // build a win7-x64 version of dotnet-nbench.dll so we know we're testing the same architecture
        DotNetCli.Build
            (fun p -> 
                { p with
                    Project = netCoreNbenchRunnerProject
                    Configuration = configuration 
                    Runtime = "debian.8-x64"
                    Framework = "netcoreapp1.1"})   
        
        let linuxNbenchRunner =  __SOURCE_DIRECTORY__ @@ "/src/NBench.Runner.DotNetCli/bin/Release/netcoreapp1.1/debian.8-x64/dotnet-nbench"
        let linuxPerfAssembly = __SOURCE_DIRECTORY__ @@ "/tests/NBench.Tests.Performance.WithDependencies/bin/Release/netcoreapp1.1/NBench.Tests.Performance.WithDependencies.dll"
        
        let linuxNbenchRunnerArgs = new StringBuilder()
                                        |> append linuxPerfAssembly
                                        |> append (sprintf "output-directory=\"%s\"" outputPerfTests)
                                        |> append (sprintf "concurrent=\"%b\"" true)
                                        |> append (sprintf "trace=\"%b\"" true)
                                        |> toText

        let result = ExecProcess(fun info -> 
            info.FileName <- linuxNbenchRunner
            info.WorkingDirectory <- __SOURCE_DIRECTORY__ @@ "/src/NBench.Runner.DotNetCli/bin/Release/netcoreapp1.1/debian.8-x64/"
            info.Arguments <- linuxNbenchRunnerArgs) (System.TimeSpan.FromMinutes 15.0) (* Reasonably long-running task. *)
        if result <> 0 then failwithf "NBench.Runner failed. %s %s" linuxNbenchRunner linuxNbenchRunnerArgs

Target "CopyOutput" (fun _ ->    
    // .NET 4.5
    if (isWindows) then 
        let projects = [ ("NBench", "./src/NBench/NBench.csproj", "net452");
                         ("NBench.PerformanceCounters", "./src/NBench.PerformanceCounters/NBench.PerformanceCounters.csproj", "net452"); 
                         ("NBench.Runner", "./src/NBench.Runner/NBench.Runner.csproj", "net452") ]

        let publishSingleProjectNet45 project =
            let projectName, projectPath, projectFramework = project
            DotNetCli.Publish
                (fun p -> 
                    { p with<commit_msg>fix perf<commit_after>#I @"tools/FAKE/tools"
#r "FakeLib.dll"

open System
open System.IO
open System.Text

open Fake
open Fake.DotNetCli
open Fake.FileUtils
open Fake.TaskRunnerHelper

// Information about the project for Nuget and Assembly info files
let product = "NBench"
let authors = [ "<NAME>" ]
let copyright = "Copyright  2015-2016"
let company = "Petabridge"
let description = "X-Platform .NET Performance Testing and Measuring Framework"
let tags = ["performance";"benchmarking";"benchmark";"perf testing";"NBench";]
let configuration = "Release"

// Read release notes and version
let parsedRelease =
    File.ReadLines "RELEASE_NOTES.md"
    |> ReleaseNotesHelper.parseReleaseNotes
let envBuildNumber = System.Environment.GetEnvironmentVariable("BUILD_NUMBER") //populated by TeamCity build agent
let buildNumber = if String.IsNullOrWhiteSpace(envBuildNumber) then "0" else envBuildNumber
let version = parsedRelease.AssemblyVersion + "." + buildNumber
let preReleaseVersion = version + "-beta" //suffixes the assembly for pre-releases
let isUnstableDocs = hasBuildParam "unstable"
let isPreRelease = hasBuildParam "nugetprerelease"
let release = if isPreRelease then ReleaseNotesHelper.ReleaseNotes.New(version, version + "-beta", parsedRelease.Notes) else parsedRelease
let isMono = Type.GetType("Mono.Runtime") <> null;

// Directories
let output = __SOURCE_DIRECTORY__  @@ "bin"
let outputTests = __SOURCE_DIRECTORY__ @@ "TestResults"
let outputPerfTests = __SOURCE_DIRECTORY__ @@ "PerfResults"
let outputNuGet = output @@ "nuget"

// Copied from original NugetCreate target
let nugetDir = output @@ "nuget"
let workingDir = output @@ "build"
let nugetExe = FullName @"./tools/nuget.exe"

open AssemblyInfoFile
Target "AssemblyInfo" (fun _ ->
    let version = release.AssemblyVersion

    CreateCSharpAssemblyInfoWithConfig "src/SharedAssemblyInfo.cs" [
        Attribute.Company company
        Attribute.Copyright copyright
        Attribute.Version version
        Attribute.FileVersion version ] <| AssemblyInfoFileConfig(false)
)

Target "Clean" (fun _ ->
    CleanDir output
    CleanDir outputTests
    CleanDir outputPerfTests
    CleanDir outputNuGet
    CleanDirs !! "./**/bin"
    CleanDirs !! "./**/obj"
)

Target "RestorePackages" (fun _ ->
    DotNetCli.Restore
        (fun p -> 
            { p with
                Project = "./NBench.sln"
                NoCache = false })
)

Target "Build" (fun _ ->          
    let runSingleProject project =
        DotNetCli.Build
            (fun p -> 
                { p with
                    Project = project
                    Configuration = configuration })   

    let projects = !! "./src/**/*.csproj" ++ "./tests/**/*.csproj"
     
    projects |> Seq.iter (runSingleProject)
)

Target "RunTests" (fun _ ->
    let runSingleProject project =
        DotNetCli.Test
            (fun p -> 
                { p with
                    Project = project
                    Configuration = configuration})      

    let projects = (!! "./tests/**/*NBench.Tests*.csproj" 
                    -- "./tests/**/*NBench.PerformanceCounters.Tests.*.csproj"
                    -- "./tests/**/*NBench.Tests.Performance.csproj"
                    -- "./tests/**/*NBench.Tests.Performance.WithDependencies.csproj"
                    -- "./tests/**/*NBench.Tests.Assembly.csproj")

    projects |> Seq.iter (log)
    projects |> Seq.iter (runSingleProject)
)

Target "NBench" <| fun _ ->
    if (isWindows) then
        // .NET 4.5.2
        let nbenchRunner = findToolInSubPath "NBench.Runner.exe" "src/NBench.Runner/bin/Release/net452/win7-x64"
        let assembly = __SOURCE_DIRECTORY__ @@ "/tests/NBench.Tests.Performance.WithDependencies/bin/Release/net452/NBench.Tests.Performance.WithDependencies.dll"
        
        let spec = getBuildParam "spec"

        let args = new StringBuilder()
                    |> append assembly
                    |> append (sprintf "output-directory=\"%s\"" outputPerfTests)
                    |> append (sprintf "concurrent=\"%b\"" true)
                    |> append (sprintf "trace=\"%b\"" true)
                    |> toText

        let result = ExecProcess(fun info -> 
            info.FileName <- nbenchRunner
            info.WorkingDirectory <- (Path.GetDirectoryName (FullName nbenchRunner))
            info.Arguments <- args) (System.TimeSpan.FromMinutes 15.0) (* Reasonably long-running task. *)
        if result <> 0 then failwithf "NBench.Runner failed. %s %s" nbenchRunner args
    
        // .NET Core
        let netCoreNbenchRunnerProject = "./src/NBench.Runner.DotNetCli/NBench.Runner.DotNetCli.csproj"
        DotNetCli.Restore
            (fun p ->
                { p with
                    Project = netCoreNbenchRunnerProject
                    AdditionalArgs = ["-r win7-x64"] })
        // build a win7-x64 version of dotnet-nbench.dll so we know we're testing the same architecture
        DotNetCli.Build
            (fun p -> 
                { p with
                    Project = netCoreNbenchRunnerProject
                    Configuration = configuration 
                    Runtime = "win7-x64"
                    Framework = "netcoreapp1.1"})   

        let netCoreNbenchRunner = findToolInSubPath "dotnet-nbench.exe" "/src/NBench.Runner.DotNetCli/bin/Release/netcoreapp1.1/win7-x64/"
        let netCoreAssembly = __SOURCE_DIRECTORY__ @@ "/tests/NBench.Tests.Performance.WithDependencies/bin/Release/netstandard1.6/NBench.Tests.Performance.WithDependencies.dll"
        
        let netCoreNbenchRunnerArgs = new StringBuilder()
                                        |> append netCoreAssembly
                                        |> append (sprintf "output-directory=\"%s\"" outputPerfTests)
                                        |> append (sprintf "concurrent=\"%b\"" true)
                                        |> append (sprintf "trace=\"%b\"" true)
                                        |> toText

        let result = ExecProcess(fun info -> 
            info.FileName <- netCoreNbenchRunner
            info.WorkingDirectory <- (Path.GetDirectoryName (FullName netCoreNbenchRunner))
            info.Arguments <- netCoreNbenchRunnerArgs) (System.TimeSpan.FromMinutes 15.0) (* Reasonably long-running task. *)
        if result <> 0 then failwithf "NBench.Runner failed. %s %s" netCoreNbenchRunner netCoreNbenchRunnerArgs
    else
        // .NET Core
        let netCoreNbenchRunnerProject = "./src/NBench.Runner.DotNetCli/NBench.Runner.DotNetCli.csproj"
        DotNetCli.Restore
            (fun p ->
                { p with
                    Project = netCoreNbenchRunnerProject
                    AdditionalArgs = ["-r debian.8-x64"] })
        // build a win7-x64 version of dotnet-nbench.dll so we know we're testing the same architecture
        DotNetCli.Build
            (fun p -> 
                { p with
                    Project = netCoreNbenchRunnerProject
                    Configuration = configuration 
                    Runtime = "debian.8-x64"
                    Framework = "netcoreapp1.1"})   
        
        let linuxNbenchRunner =  __SOURCE_DIRECTORY__ @@ "/src/NBench.Runner.DotNetCli/bin/Release/netcoreapp1.1/debian.8-x64/dotnet-nbench"
        let linuxPerfAssembly = __SOURCE_DIRECTORY__ @@ "/tests/NBench.Tests.Performance.WithDependencies/bin/Release/netstandard1.6/NBench.Tests.Performance.WithDependencies.dll"
        
        let linuxNbenchRunnerArgs = new StringBuilder()
                                        |> append linuxPerfAssembly
                                        |> append (sprintf "output-directory=\"%s\"" outputPerfTests)
                                        |> append (sprintf "concurrent=\"%b\"" true)
                                        |> append (sprintf "trace=\"%b\"" true)
                                        |> toText

        let result = ExecProcess(fun info -> 
            info.FileName <- linuxNbenchRunner
            info.WorkingDirectory <- __SOURCE_DIRECTORY__ @@ "/src/NBench.Runner.DotNetCli/bin/Release/netcoreapp1.1/debian.8-x64/"
            info.Arguments <- linuxNbenchRunnerArgs) (System.TimeSpan.FromMinutes 15.0) (* Reasonably long-running task. *)
        if result <> 0 then failwithf "NBench.Runner failed. %s %s" linuxNbenchRunner linuxNbenchRunnerArgs

Target "CopyOutput" (fun _ ->    
    // .NET 4.5
    if (isWindows) then 
        let projects = [ ("NBench", "./src/NBench/NBench.csproj", "net452");
                         ("NBench.PerformanceCounters", "./src/NBench.PerformanceCounters/NBench.PerformanceCounters.csproj", "net452"); 
                         ("NBench.Runner", "./src/NBench.Runner/NBench.Runner.csproj", "net452") ]

        let publishSingleProjectNet45 project =
            let projectName, projectPath, projectFramework = project
            DotNetCli.Publish
                (fun p -> 
                    { p with
{'fraction_non_alphanumeric': 0.10616304408560094, 'fraction_numerical': 0.009722299826786612, 'mean_word_length': 2.065775950668037, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 87, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 40, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '37874', 'n_tokens_mistral': 5238, 'n_tokens_neox': 4705, 'n_words': 1088}