<commit_before>﻿/// Contains methods to remove of installed packages
module Paket.RemoveProcess

open Paket
open System.IO
open Paket.Domain

let Remove(dependenciesFileName, package:PackageName, force, hard, interactive, installAfter) =
    let (PackageName name) = package
    let root = Path.GetDirectoryName dependenciesFileName
    let allProjects = ProjectFile.FindAllProjects root
    for project in allProjects do
        let proj = FileInfo(project.FileName)
        match ProjectFile.FindReferencesFile proj with
        | None -> ()
        | Some fileName -> 
            let lines = File.ReadAllLines(fileName)
            let installed = lines |> Seq.exists (fun l -> l.ToLower() = name.ToLower())
            if installed then
                if (not interactive) || Utils.askYesNo(sprintf "  Remove from %s?" project.Name) then
                    let newLines = lines |> Seq.filter (fun l -> l.ToLower() <> name.ToLower())
                    File.WriteAllLines(fileName,newLines)

    // check we have it removed from paket.references files
    for project in allProjects do
        let proj = FileInfo(project.FileName)
        match ProjectFile.FindReferencesFile proj with
        | None -> ()
        | Some fileName -> 
            let lines = File.ReadAllLines(fileName)
            let installed = lines |> Seq.exists (fun l -> l.ToLower() = name.ToLower())
            if installed then
                failwithf "%s is still installed in %s" name project.Name

    let exisitingDependenciesFile = DependenciesFile.ReadFromFile dependenciesFileName
    let dependenciesFile =
        exisitingDependenciesFile
          .Remove(package)

    let changed = exisitingDependenciesFile <> dependenciesFile
    let lockFile = 
        if changed then
            UpdateProcess.updateWithModifiedDependenciesFile(true,dependenciesFile,package,force)
        else
            let lockFileName = DependenciesFile.FindLockfile dependenciesFileName
            LockFile.LoadFrom(lockFileName.FullName)
    
    if installAfter then
        let sources = DependenciesFile.ReadFromFile(dependenciesFileName).GetAllPackageSources()
        InstallProcess.Install(sources, force, hard, lockFile)

    if changed then
        dependenciesFile.Save()<commit_msg>Allow to remove nugets interactive - references #431<commit_after>﻿/// Contains methods to remove of installed packages
module Paket.RemoveProcess

open Paket
open System.IO
open Paket.Domain

let Remove(dependenciesFileName, package:PackageName, force, hard, interactive, installAfter) =
    let (PackageName name) = package
    let root = Path.GetDirectoryName dependenciesFileName
    let allProjects = ProjectFile.FindAllProjects root
    for project in allProjects do
        let proj = FileInfo(project.FileName)
        match ProjectFile.FindReferencesFile proj with
        | None -> ()
        | Some fileName -> 
            let lines = File.ReadAllLines(fileName)
            let installed = lines |> Seq.exists (fun l -> l.ToLower() = name.ToLower())
            if installed then
                if (not interactive) || Utils.askYesNo(sprintf "  Remove from %s?" project.Name) then
                    let newLines = lines |> Seq.filter (fun l -> l.ToLower() <> name.ToLower())
                    File.WriteAllLines(fileName,newLines)

    // check we have it removed from all paket.references files
    let stillInstalled =
        allProjects
        |> Seq.exists (fun project -> 
            let proj = FileInfo(project.FileName)
            match ProjectFile.FindReferencesFile proj with
            | None -> false 
            | Some fileName -> 
                let lines = File.ReadAllLines(fileName)
                lines |> Seq.exists (fun l -> l.ToLower() = name.ToLower()))

    let oldLockFile =    
        let lockFileName = DependenciesFile.FindLockfile dependenciesFileName
        LockFile.LoadFrom(lockFileName.FullName)

    let lockFile =
        if stillInstalled then oldLockFile else
        let exisitingDependenciesFile = DependenciesFile.ReadFromFile dependenciesFileName
        let dependenciesFile =
            exisitingDependenciesFile
                .Remove(package)

        let changed = exisitingDependenciesFile <> dependenciesFile

        if changed then
            dependenciesFile.Save()
        
        if changed then
            let lockFile = UpdateProcess.updateWithModifiedDependenciesFile(true,dependenciesFile,package,force)
            lockFile.Save()
            lockFile
        else
            oldLockFile
    
    if installAfter then
        let sources = DependenciesFile.ReadFromFile(dependenciesFileName).GetAllPackageSources()
        InstallProcess.Install(sources, force, hard, lockFile)
{'fraction_non_alphanumeric': 0.0723781388478582, 'fraction_numerical': 0.0006330449461911796, 'mean_word_length': 2.5215453194650816, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 29, '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': '3494663', 'n_tokens_mistral': 1211, 'n_tokens_neox': 1165, 'n_words': 338}