<commit_before>﻿//   Copyright 2014-2017 <NAME>
//
//   Licensed 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.

module Commands.Workspace

open System.IO
open IoHelpers
open Env
open XmlHelpers
open System.Linq
open System.Xml.Linq
open Collections
open System
open Graph


let printPull ((repo, execResult) : (Repository * Exec.ExecResult)) =
    lock consoleLock (fun () -> IoHelpers.DisplayHighlight repo.Name
                                execResult |> Exec.PrintOutput)

let private checkoutRepo wsDir (checkoutInfo : CLI.Commands.CheckoutVersion) (repo : Repository) = async {
    return (repo, Tools.Vcs.Checkout wsDir repo checkoutInfo.Version) |> printPull
}




let Branch (branchInfo : CLI.Commands.BranchWorkspace) =
    match branchInfo.Branch with
    | Some x -> Configuration.SaveBranch x
    | None -> let name = Configuration.LoadBranch()
              printfn "%s" name

    Core.Indexation.ConsolidateAnthology()

let Create (createInfo : CLI.Commands.SetupWorkspace) =
    let wsDir = DirectoryInfo(createInfo.Path)
    wsDir.Create()
    if IsWorkspaceFolder wsDir then failwith "Workspace already exists"

    let currDir = Environment.CurrentDirectory
    try
        Environment.CurrentDirectory <- wsDir.FullName
        let graph = Graph.create createInfo.MasterRepository createInfo.MasterArtifacts createInfo.Type TestRunnerType.NUnit
        Tools.Vcs.Clone wsDir graph.MasterRepository true |> Exec.PrintOutput |> Exec.CheckResponseCode
        graph.Save()

        Tools.Vcs.Ignore wsDir graph.MasterRepository

        // setup additional files for views to work correctly
        let installDir = Env.GetFolder Env.Folder.Installation
        let confDir = Env.GetFolder Env.Folder.Config
        let publishSource = installDir |> GetFile Env.FULLBUILD_TARGETS
        let publishTarget = confDir |> GetFile Env.FULLBUILD_TARGETS
        publishSource.CopyTo(publishTarget.FullName) |> ignore

        Configuration.SaveBranch graph.MasterRepository.Branch
    finally
        Environment.CurrentDirectory <- currDir


let TagWorkspace (tagInfo : CLI.Commands.TagWorkspace) =
    let graph = Configuration.LoadAnthology() |> Graph.from

    // copy bin content
    let baselineRepository = Baselines.from graph
    let newBaseline = baselineRepository.CreateBaseline Baselines.BuildType.Draft tagInfo.BuildNumber
    newBaseline.Save()


let Checkout (checkoutInfo : CLI.Commands.CheckoutVersion) =
    // checkout repositories
    DisplayHighlight ".full-build"
    let graph = Configuration.LoadAnthology () |> Graph.from
    let wsDir = Env.GetFolder Env.Folder.Workspace
    let mainRepo = graph.MasterRepository
    Tools.Vcs.Checkout wsDir mainRepo checkoutInfo.Version |> Exec.CheckResponseCode

    // checkout each repository now
    let graph = Configuration.LoadAnthology () |> Graph.from
    let repos = graph.Repositories
    let maxThrottle = System.Environment.ProcessorCount*4
    let branchResults = repos |> Seq.filter (fun x -> x.IsCloned)
                              |> Seq.map (checkoutRepo wsDir checkoutInfo)
                              |> Threading.throttle maxThrottle |> Async.Parallel |> Async.RunSynchronously

    branchResults |> Exec.CheckMultipleResponseCode

    // update binaries with observable baseline
    Core.BuildArtifacts.PullReferenceBinaries graph.ArtifactsDir checkoutInfo.Version

    // consolidate anthology
    Core.Indexation.ConsolidateAnthology()

let Install () =
    Core.Package.RestorePackages ()
    Core.Conversion.GenerateProjectArtifacts()

let Init (initInfo : CLI.Commands.InitWorkspace) =
    let wsDir = DirectoryInfo(initInfo.Path)
    wsDir.Create()
    if IsWorkspaceFolder wsDir then
        printf "[WARNING] Workspace already exists - skipping"
    else
        let graph = Graph.init initInfo.MasterRepository initInfo.Type
        Tools.Vcs.Clone wsDir graph.MasterRepository true |> Exec.PrintOutput |> Exec.CheckResponseCode

        let currDir = Environment.CurrentDirectory
        try
            Environment.CurrentDirectory <- wsDir.FullName
            Configuration.SaveBranch graph.MasterRepository.Branch
        finally
            Environment.CurrentDirectory <- currDir



let consoleProgressBar max =
    MailboxProcessor.Start(fun inbox ->
        new String(' ', max) |> printf "[%s]\r["
        let rec loop n = async {
                let! msg = inbox.Receive()
                do printf "="
                if n = max then return printfn "]"
                return! n + 1 |> loop
            }
        loop 1)

let consoleLock = System.Object()

let private cloneRepo wsDir rebase (repo : Repository) = async {
    return (repo, Tools.Vcs.Pull wsDir repo rebase) |> printPull
}
<commit_msg>master repo is never shallow cloned<commit_after>﻿//   Copyright 2014-2017 <NAME>
//
//   Licensed 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.

module Commands.Workspace

open System.IO
open IoHelpers
open Env
open XmlHelpers
open System.Linq
open System.Xml.Linq
open Collections
open System
open Graph


let printPull ((repo, execResult) : (Repository * Exec.ExecResult)) =
    lock consoleLock (fun () -> IoHelpers.DisplayHighlight repo.Name
                                execResult |> Exec.PrintOutput)

let private checkoutRepo wsDir (checkoutInfo : CLI.Commands.CheckoutVersion) (repo : Repository) = async {
    return (repo, Tools.Vcs.Checkout wsDir repo checkoutInfo.Version) |> printPull
}




let Branch (branchInfo : CLI.Commands.BranchWorkspace) =
    match branchInfo.Branch with
    | Some x -> Configuration.SaveBranch x
    | None -> let name = Configuration.LoadBranch()
              printfn "%s" name

    Core.Indexation.ConsolidateAnthology()

let Create (createInfo : CLI.Commands.SetupWorkspace) =
    let wsDir = DirectoryInfo(createInfo.Path)
    wsDir.Create()
    if IsWorkspaceFolder wsDir then failwith "Workspace already exists"

    let currDir = Environment.CurrentDirectory
    try
        Environment.CurrentDirectory <- wsDir.FullName
        let graph = Graph.create createInfo.MasterRepository createInfo.MasterArtifacts createInfo.Type TestRunnerType.NUnit
        Tools.Vcs.Clone wsDir graph.MasterRepository true |> Exec.PrintOutput |> Exec.CheckResponseCode
        graph.Save()

        Tools.Vcs.Ignore wsDir graph.MasterRepository

        // setup additional files for views to work correctly
        let installDir = Env.GetFolder Env.Folder.Installation
        let confDir = Env.GetFolder Env.Folder.Config
        let publishSource = installDir |> GetFile Env.FULLBUILD_TARGETS
        let publishTarget = confDir |> GetFile Env.FULLBUILD_TARGETS
        publishSource.CopyTo(publishTarget.FullName) |> ignore

        Configuration.SaveBranch graph.MasterRepository.Branch
    finally
        Environment.CurrentDirectory <- currDir


let TagWorkspace (tagInfo : CLI.Commands.TagWorkspace) =
    let graph = Configuration.LoadAnthology() |> Graph.from

    // copy bin content
    let baselineRepository = Baselines.from graph
    let newBaseline = baselineRepository.CreateBaseline Baselines.BuildType.Draft tagInfo.BuildNumber
    newBaseline.Save()


let Checkout (checkoutInfo : CLI.Commands.CheckoutVersion) =
    // checkout repositories
    DisplayHighlight ".full-build"
    let graph = Configuration.LoadAnthology () |> Graph.from
    let wsDir = Env.GetFolder Env.Folder.Workspace
    let mainRepo = graph.MasterRepository
    Tools.Vcs.Checkout wsDir mainRepo checkoutInfo.Version |> Exec.CheckResponseCode

    // checkout each repository now
    let graph = Configuration.LoadAnthology () |> Graph.from
    let repos = graph.Repositories
    let maxThrottle = System.Environment.ProcessorCount*4
    let branchResults = repos |> Seq.filter (fun x -> x.IsCloned)
                              |> Seq.map (checkoutRepo wsDir checkoutInfo)
                              |> Threading.throttle maxThrottle |> Async.Parallel |> Async.RunSynchronously

    branchResults |> Exec.CheckMultipleResponseCode

    // update binaries with observable baseline
    Core.BuildArtifacts.PullReferenceBinaries graph.ArtifactsDir checkoutInfo.Version

    // consolidate anthology
    Core.Indexation.ConsolidateAnthology()

let Install () =
    Core.Package.RestorePackages ()
    Core.Conversion.GenerateProjectArtifacts()

let Init (initInfo : CLI.Commands.InitWorkspace) =
    let wsDir = DirectoryInfo(initInfo.Path)
    wsDir.Create()
    if IsWorkspaceFolder wsDir then
        printf "[WARNING] Workspace already exists - skipping"
    else
        let graph = Graph.init initInfo.MasterRepository initInfo.Type
        Tools.Vcs.Clone wsDir graph.MasterRepository false |> Exec.PrintOutput |> Exec.CheckResponseCode

        let currDir = Environment.CurrentDirectory
        try
            Environment.CurrentDirectory <- wsDir.FullName
            Configuration.SaveBranch graph.MasterRepository.Branch
        finally
            Environment.CurrentDirectory <- currDir



let consoleProgressBar max =
    MailboxProcessor.Start(fun inbox ->
        new String(' ', max) |> printf "[%s]\r["
        let rec loop n = async {
                let! msg = inbox.Receive()
                do printf "="
                if n = max then return printfn "]"
                return! n + 1 |> loop
            }
        loop 1)

let consoleLock = System.Object()

let private cloneRepo wsDir rebase (repo : Repository) = async {
    return (repo, Tools.Vcs.Pull wsDir repo rebase) |> printPull
}

{'fraction_non_alphanumeric': 0.0798323330475374, 'fraction_numerical': 0.002857959416976279, 'mean_word_length': 2.9735049205147615, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 59, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '2119678', 'n_tokens_mistral': 2903, 'n_tokens_neox': 2781, 'n_words': 813}