<commit_before>﻿namespace Paket

open System.IO

open Paket.Rop
open Paket.Domain

type PaketEnv = {
    RootDirectory : DirectoryInfo
    DependenciesFile : DependenciesFile
    LockFile : option<LockFile>
    Projects : list<ProjectFile * ReferencesFile>
}

[<CompilationRepresentation(CompilationRepresentationFlags.ModuleSuffix)>]
module PaketEnv = 
    
    let create root dependenciesFile lockFile projects = 
        { RootDirectory = root
          DependenciesFile = dependenciesFile
          LockFile = lockFile
          Projects = projects }       

    let fromRootDirectory (directory : DirectoryInfo) = 
        if not directory.Exists then 
            fail (DirectoryDoesntExist directory)
        else
            let dependenciesFile = 
                let fi = FileInfo(Path.Combine(directory.FullName, Constants.DependenciesFileName))
                if not fi.Exists then
                    fail (DependenciesFileNotFoundInDir directory)
                else
                    try
                        succeed (DependenciesFile.ReadFromFile(fi.FullName))
                    with _ ->
                        fail (DependenciesFileParseError fi)

            let lockFile =
                let fi = FileInfo(Path.Combine(directory.FullName, Constants.LockFileName))
                if not fi.Exists then
                    None |> succeed
                else
                    try
                        LockFile.LoadFrom(fi.FullName) |> Some |> succeed
                    with _ ->
                        fail (LockFileParseError fi)

            let projects = InstallProcess.findAllReferencesFiles(directory.FullName)

            create directory
            <!> dependenciesFile
            <*> lockFile
            <*> projects

    let locatePaketRootDirectory (directory : DirectoryInfo) = 
        if not directory.Exists then 
            None
        else
            directory
            |> Seq.unfold (function
                | null -> None
                | dir -> Some(Path.Combine(dir.FullName, Constants.DependenciesFileName), dir.Parent))
            |> Seq.tryFind File.Exists
            |> Option.map (fun f -> DirectoryInfo(Path.GetDirectoryName(f)))

    let ensureNotExists (directory : DirectoryInfo) =
        match fromRootDirectory directory with
        | Success(_) -> fail (PaketEnvAlreadyExistsInDirectory directory)
        | Failure(msgs) -> 
            let filtered = 
                msgs
                |> List.filter (function<commit_msg>replace custom operators with builders in PaketEnv module<commit_after>﻿namespace Paket

open System.IO

open Paket.Rop
open Paket.Domain

type PaketEnv = {
    RootDirectory : DirectoryInfo
    DependenciesFile : DependenciesFile
    LockFile : option<LockFile>
    Projects : list<ProjectFile * ReferencesFile>
}

[<CompilationRepresentation(CompilationRepresentationFlags.ModuleSuffix)>]
module PaketEnv = 

    let create root dependenciesFile lockFile projects = 
        { RootDirectory = root
          DependenciesFile = dependenciesFile
          LockFile = lockFile
          Projects = projects }

    let fromRootDirectory (directory : DirectoryInfo) = rop {
        if not directory.Exists then 
            return! fail (DirectoryDoesntExist directory)
        else
            let! dependenciesFile = 
                let fi = FileInfo(Path.Combine(directory.FullName, Constants.DependenciesFileName))
                if not fi.Exists then
                    fail (DependenciesFileNotFoundInDir directory)
                else
                    try
                        succeed (DependenciesFile.ReadFromFile(fi.FullName))
                    with _ ->
                        fail (DependenciesFileParseError fi)

            let! lockFile =
                let fi = FileInfo(Path.Combine(directory.FullName, Constants.LockFileName))
                if not fi.Exists then
                    None |> succeed
                else
                    try
                        LockFile.LoadFrom(fi.FullName) |> Some |> succeed
                    with _ ->
                        fail (LockFileParseError fi)

            let! projects = InstallProcess.findAllReferencesFiles(directory.FullName)

            return create directory dependenciesFile lockFile projects
    }

    let locatePaketRootDirectory (directory : DirectoryInfo) = 
        if not directory.Exists then 
            None
        else
            directory
            |> Seq.unfold (function
                | null -> None
                | dir -> Some(Path.Combine(dir.FullName, Constants.DependenciesFileName), dir.Parent))
            |> Seq.tryFind File.Exists
            |> Option.map (fun f -> DirectoryInfo(Path.GetDirectoryName(f)))

    let ensureNotExists (directory : DirectoryInfo) =
        match fromRootDirectory directory with
        | Success(_) -> fail (PaketEnvAlreadyExistsInDirectory directory)
        | Failure(msgs) -> 
            let filtered = 
                msgs
                |> List.filter (function
{'fraction_non_alphanumeric': 0.0651743264659271, 'fraction_numerical': 0.0, 'mean_word_length': 1.768092105263158, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 40, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '2736287', 'n_tokens_mistral': 1255, 'n_tokens_neox': 1136, 'n_words': 291}