Skip to content

Commit

Permalink
preserve encoding upon saving solution - references fsprojects#934
Browse files Browse the repository at this point in the history
  • Loading branch information
theimowski committed Jul 24, 2015
1 parent 18360db commit 02525a8
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/Paket.Core/SolutionFile.fs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ open System
type SolutionFile(fileName: string) =

let originalContent = File.ReadAllLines fileName |> Array.toList
let originalEncoding = getFileEncoding fileName
let content = ResizeArray( originalContent )

let removeNugetSlnFolderIfEmpty() =
Expand Down Expand Up @@ -80,5 +81,5 @@ type SolutionFile(fileName: string) =
member __.Save() =
if content |> Seq.toList <> originalContent
then
File.WriteAllLines(fileName, content)
File.WriteAllLines(fileName, content, originalEncoding)
tracefn "Solution %s changed" fileName
13 changes: 13 additions & 0 deletions src/Paket.Core/Utils.fs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,19 @@ let CleanDir path =
// set writeable
File.SetAttributes(path, FileAttributes.Normal)

// http://stackoverflow.com/a/19283954/1397724
let getFileEncoding path =
let bom = Array.zeroCreate 4
use fs = new FileStream(path, FileMode.Open, FileAccess.Read)
fs.Read(bom, 0, 4) |> ignore
match bom with
| [| 0x2buy ; 0x2fuy ; 0x76uy ; _ |] -> Encoding.UTF7
| [| 0xefuy ; 0xbbuy ; 0xbfuy ; _ |] -> Encoding.UTF8
| [| 0xffuy ; 0xfeuy ; _ ; _ |] -> Encoding.Unicode //UTF-16LE
| [| 0xfeuy ; 0xffuy ; _ ; _ |] -> Encoding.BigEndianUnicode //UTF-16BE
| [| 0uy ; 0uy ; 0xfeuy ; 0xffuy |] -> Encoding.UTF32
| _ -> Encoding.ASCII

/// [omit]
let inline createRelativePath root path =
let basePath =
Expand Down

0 comments on commit 02525a8

Please sign in to comment.