From 02525a8ba26e0539bf7c555a388fb8b850be8af9 Mon Sep 17 00:00:00 2001 From: Tomasz Heimowski Date: Fri, 24 Jul 2015 16:10:54 +0200 Subject: [PATCH] preserve encoding upon saving solution - references #934 --- src/Paket.Core/SolutionFile.fs | 3 ++- src/Paket.Core/Utils.fs | 13 +++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/Paket.Core/SolutionFile.fs b/src/Paket.Core/SolutionFile.fs index 2108e1f343..74df05f000 100644 --- a/src/Paket.Core/SolutionFile.fs +++ b/src/Paket.Core/SolutionFile.fs @@ -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() = @@ -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 \ No newline at end of file diff --git a/src/Paket.Core/Utils.fs b/src/Paket.Core/Utils.fs index c08930f4c2..2395086c8c 100644 --- a/src/Paket.Core/Utils.fs +++ b/src/Paket.Core/Utils.fs @@ -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 =