From 818f79103043f8d2adefed93ec708da17ca6acb4 Mon Sep 17 00:00:00 2001 From: Tomasz Heimowski Date: Tue, 30 Sep 2014 19:01:33 +0200 Subject: [PATCH] add paket folder to sln with dep and lock file --- src/Paket/NugetConvert.fs | 5 +++- src/Paket/SolutionFile.fs | 58 ++++++++++++++++++++++++++++----------- 2 files changed, 46 insertions(+), 17 deletions(-) diff --git a/src/Paket/NugetConvert.fs b/src/Paket/NugetConvert.fs index 3e42684faf..e3b2c5141a 100644 --- a/src/Paket/NugetConvert.fs +++ b/src/Paket/NugetConvert.fs @@ -119,7 +119,10 @@ let ConvertFromNuget(force, installAfter, initAutoRestore, dependenciesFileName) | SolutionLevel -> () for slnFile in FindAllFiles(".", "*.sln") do - SolutionFile.RemoveNugetEntries(slnFile.FullName) + let solution = SolutionFile(slnFile.FullName) + solution.RemoveNugetEntries() + solution.AddPaketFolder(dependenciesFileName, if installAfter then Some("paket.lock") else None) + solution.Save() for projFile in ProjectFile.FindAllProjects(".") do let project = ProjectFile.Load(projFile.FullName) diff --git a/src/Paket/SolutionFile.fs b/src/Paket/SolutionFile.fs index 843ead2b11..ce0387c871 100644 --- a/src/Paket/SolutionFile.fs +++ b/src/Paket/SolutionFile.fs @@ -1,28 +1,54 @@ namespace Paket open System.IO +open Logging +open System /// Contains methods to read and manipulate solution files. -module SolutionFile = - let private removeNugetSlnFolderIfEmpty(slnContent: ResizeArray) = - match slnContent |> Seq.tryFindIndex (fun line -> - line.StartsWith("""Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}")""") && line.Contains(".nuget")) with +type SolutionFile(fileName: string) = + [] + let slnFolderProjectGuid = "2150E333-8FDC-42A3-9474-1A3956D46DE8" + let originalContent = File.ReadAllLines fileName |> Array.toList + let content = ResizeArray( originalContent ) + + let removeNugetSlnFolderIfEmpty() = + match content |> Seq.tryFindIndex (fun line -> + line.StartsWith(sprintf """Project({%s})""" slnFolderProjectGuid) && line.Contains(".nuget")) with | Some(index) -> - if slnContent.[index+1].Contains("ProjectSection(SolutionItems)") && - slnContent.[index+2].Contains("EndProjectSection") && - slnContent.[index+3].Contains("EndProject") + if content.[index+1].Contains("ProjectSection(SolutionItems)") && + content.[index+2].Contains("EndProjectSection") && + content.[index+3].Contains("EndProject") then - slnContent.RemoveRange(index, 4) + content.RemoveRange(index, 4) | None -> () - let RemoveNugetEntries(solutionName: string) = - let slnContent = ResizeArray( File.ReadAllLines solutionName ) - let mutable modified = false - + member __.RemoveNugetEntries() = for file in ["nuget.targets";"packages.config";"nuget.exe"] do - match slnContent |> Seq.tryFindIndex (fun line -> line.ToLower().Contains(sprintf ".nuget\\%s" file)) with - | Some(index) -> slnContent.RemoveAt(index); modified <- true + match content |> Seq.tryFindIndex (fun line -> line.ToLower().Contains(sprintf ".nuget\\%s" file)) with + | Some(index) -> content.RemoveAt(index) | None -> () - removeNugetSlnFolderIfEmpty(slnContent) - if modified then File.WriteAllLines(solutionName, slnContent) \ No newline at end of file + removeNugetSlnFolderIfEmpty() + + member __.AddPaketFolder(dependenciesFile, lockFile) = + match content |> Seq.tryFindIndex (fun line -> line.StartsWith("MinimumVisualStudioVersion")) with + | Some index -> + let depFile = createRelativePath dependenciesFile fileName + let lockFile = lockFile |> Option.map (fun l -> createRelativePath l fileName) + let lines = ResizeArray<_>() + + lines.Add(sprintf "Project(\"{%s}\") = \".paket\", \".paket\", \"{%s}\"" slnFolderProjectGuid <| Guid.NewGuid().ToString("D")) + lines.Add " ProjectSection(SolutionItems) = preProject" + lines.Add(sprintf " %s = %s" depFile depFile) + if lockFile |> Option.isSome then + lines.Add(sprintf" %s = %s" lockFile.Value lockFile.Value) + lines.Add " EndProjectSection" + lines.Add "EndProject" + content.InsertRange(index + 1, lines) + | None -> failwithf "Unable to add paket folder to solution %s" fileName + + member __.Save() = + if content |> Seq.toList <> originalContent + then + File.WriteAllLines(fileName, content) + tracefn "Solution %s changed" fileName \ No newline at end of file