Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Keep original line ending in merge postactions #312

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

using System;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace Microsoft.Templates.Core.Helpers
{
Expand All @@ -25,5 +28,45 @@ public static string GetFileContent(string filePath)
return string.Empty;
}
}

public static Encoding GetEncoding(string originalFilePath)
{
// Will read the file, and look at the BOM to check the encoding.
using (var reader = new StreamReader(File.OpenRead(originalFilePath), true))
{
var bytes = File.ReadAllBytes(originalFilePath);
var encoding = reader.CurrentEncoding;

// The preamble is the first couple of bytes that may be appended to define an encoding.
var preamble = encoding.GetPreamble();

// We preserve the read encoding unless there is no BOM, if it is UTF-8 we return the non BOM encoding.
if (preamble == null || preamble.Length == 0 || preamble.Where((p, i) => p != bytes[i]).Any())
{
if (encoding.EncodingName == Encoding.UTF8.EncodingName)
{
return new UTF8Encoding(false);
}
}

return encoding;
}
}

public const string LineEndingWindows = "\r\n";
public const string LineEndingUnix = "\n";

public static string GetLineEnding(string originalFilePath)
{
// Will read the file, and check last file ending.
using (var reader = new StreamReader(File.OpenRead(originalFilePath), true))
{
var fileContent = File.ReadAllText(originalFilePath);
string pattern = @"(\n)|(\r\n)";
var lineEnding = Regex.Match(fileContent, pattern, RegexOptions.RightToLeft);

return lineEnding.Value;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using Microsoft.TemplateEngine.Abstractions;
using Microsoft.Templates.Core.Helpers;
using Microsoft.Templates.Core.Templates;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
Expand Down Expand Up @@ -48,33 +50,13 @@ internal override void ExecuteInternal()

json[keyToDict] = JObject.FromObject(newContent);

var originalEncoding = GetEncoding(jsonPath);
string lineEndingPattern = @"(\n)|(\r\n)";
var originalEncoding = FileHelper.GetEncoding(jsonPath);
var originalLineEnding = FileHelper.GetLineEnding(jsonPath);

var jsonString = Regex.Replace(json.ToString(Formatting.Indented), lineEndingPattern, originalLineEnding);

File.WriteAllText(jsonPath, json.ToString(Formatting.Indented), originalEncoding);
}

private Encoding GetEncoding(string originalFilePath)
{
// Will read the file, and look at the BOM to check the encoding.
using (var reader = new StreamReader(File.OpenRead(originalFilePath), true))
{
var bytes = File.ReadAllBytes(originalFilePath);
var encoding = reader.CurrentEncoding;

// The preamble is the first couple of bytes that may be appended to define an encoding.
var preamble = encoding.GetPreamble();

// We preserve the read encoding unless there is no BOM, if it is UTF-8 we return the non BOM encoding.
if (preamble == null || preamble.Length == 0 || preamble.Where((p, i) => p != bytes[i]).Any())
{
if (encoding.EncodingName == Encoding.UTF8.EncodingName)
{
return new UTF8Encoding(false);
}
}

return encoding;
}
File.WriteAllText(jsonPath, jsonString, originalEncoding);
}
}
}
Loading