Skip to content

Commit

Permalink
add paket folder to sln with dep and lock file
Browse files Browse the repository at this point in the history
  • Loading branch information
theimowski committed Sep 30, 2014
1 parent 0e0bd0c commit 818f791
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 17 deletions.
5 changes: 4 additions & 1 deletion src/Paket/NugetConvert.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
58 changes: 42 additions & 16 deletions src/Paket/SolutionFile.fs
Original file line number Diff line number Diff line change
@@ -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<string>) =
match slnContent |> Seq.tryFindIndex (fun line ->
line.StartsWith("""Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}")""") && line.Contains(".nuget")) with
type SolutionFile(fileName: string) =
[<Literal>]
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)
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

0 comments on commit 818f791

Please sign in to comment.