From 1b87d7e7e210f70b00f01c5f5b14457f8a519503 Mon Sep 17 00:00:00 2001 From: Eric Johnson Date: Wed, 6 Apr 2022 15:05:36 -0500 Subject: [PATCH] Fixed project file encoding to retain existing encoding. --- .../SolutionFileModifier.cs | 29 +++++-- .../VoloNugetPackagesVersionUpdater.cs | 83 +++++++++++++------ 2 files changed, 80 insertions(+), 32 deletions(-) diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/SolutionFileModifier.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/SolutionFileModifier.cs index 08d8fa182d..b78ec017c2 100644 --- a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/SolutionFileModifier.cs +++ b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/SolutionFileModifier.cs @@ -12,11 +12,24 @@ public class SolutionFileModifier : ITransientDependency { public async Task RemoveProjectFromSolutionFileAsync(string solutionFile, string projectName) { - var solutionFileContent = File.ReadAllText(solutionFile); - solutionFileContent.NormalizeLineEndings(); - var lines = solutionFileContent.Split(new[] { Environment.NewLine, "\n" }, StringSplitOptions.None); - File.WriteAllText(solutionFile, - RemoveProject(lines.ToList(), projectName).JoinAsString(Environment.NewLine)); + using (var fs = File.Open(solutionFile, FileMode.Open, FileAccess.ReadWrite, FileShare.None)) + using (var sr = new StreamReader(fs, Encoding.Default, true)) + { + var solutionFileContent = await sr.ReadToEndAsync(); + solutionFileContent.NormalizeLineEndings(); + + var lines = solutionFileContent.Split(new[] { Environment.NewLine, "\n" }, StringSplitOptions.None); + var updatedContent = RemoveProject(lines.ToList(), projectName).JoinAsString(Environment.NewLine); + + fs.Seek(0, SeekOrigin.Begin); + fs.SetLength(0); + + using (var sw = new StreamWriter(fs, sr.CurrentEncoding)) + { + sw.Write(updatedContent); + sw.Flush(); + } + } } public async Task AddModuleToSolutionFileAsync(ModuleWithMastersInfo module, string solutionFile) @@ -64,7 +77,7 @@ public class SolutionFileModifier : ITransientDependency lines.InsertAfter(l => l.Contains("GlobalSection") && l.Contains("NestedProjects"), newPreSolutionLine); - File.WriteAllText(solutionFile, string.Join(Environment.NewLine, lines)); + File.WriteAllText(solutionFile, string.Join(Environment.NewLine, lines), Encoding.UTF8); } private List RemoveProject(List solutionFileLines, string projectName) @@ -174,7 +187,7 @@ public class SolutionFileModifier : ITransientDependency lines.InsertAfter(l => l.Contains("GlobalSection") && l.Contains("NestedProjects"), newPreSolutionLine); } - File.WriteAllText(solutionFile, string.Join(Environment.NewLine, lines)); + File.WriteAllText(solutionFile, string.Join(Environment.NewLine, lines), Encoding.UTF8); if (module.MasterModuleInfos != null) { @@ -219,7 +232,7 @@ public class SolutionFileModifier : ITransientDependency .Split(" ").Last(); } - File.WriteAllText(solutionFile, string.Join(Environment.NewLine, lines)); + File.WriteAllText(solutionFile, string.Join(Environment.NewLine, lines), Encoding.UTF8); return folderId; } diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/VoloNugetPackagesVersionUpdater.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/VoloNugetPackagesVersionUpdater.cs index 900930d472..4e94c983a9 100644 --- a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/VoloNugetPackagesVersionUpdater.cs +++ b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/VoloNugetPackagesVersionUpdater.cs @@ -9,6 +9,7 @@ using Volo.Abp.Cli.NuGet; using Volo.Abp.DependencyInjection; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging.Abstractions; +using System.Text; namespace Volo.Abp.Cli.ProjectModification; @@ -41,17 +42,28 @@ public class VoloNugetPackagesVersionUpdater : ITransientDependency async Task UpdateAsync(string filePath) { - var fileContent = File.ReadAllText(filePath); - var updatedContent = await UpdateVoloPackagesAsync(fileContent, - includePreviews, - includeReleaseCandidates, - switchToStable, - latestVersionFromNuget, - latestReleaseCandidateVersionFromNuget, - latestVersionFromMyGet, - version); - - File.WriteAllText(filePath, updatedContent); + using (var fs = File.Open(filePath, FileMode.Open, FileAccess.ReadWrite, FileShare.None)) + using (var sr = new StreamReader(fs, Encoding.Default, true)) + { + var fileContent = await sr.ReadToEndAsync(); + + var updatedContent = await UpdateVoloPackagesAsync(fileContent, + includePreviews, + includeReleaseCandidates, + switchToStable, + latestVersionFromNuget, + latestReleaseCandidateVersionFromNuget, + latestVersionFromMyGet, + version); + + fs.Seek(0, SeekOrigin.Begin); + fs.SetLength(0); + using (var sw = new StreamWriter(fs, sr.CurrentEncoding)) + { + sw.Write(updatedContent); + sw.Flush(); + } + } } Task.WaitAll(projectPaths.Select(UpdateAsync).ToArray()); @@ -70,27 +82,50 @@ public class VoloNugetPackagesVersionUpdater : ITransientDependency var latestReleaseCandidateVersionFromNuget = await _nuGetService.GetLatestVersionOrNullAsync("Volo.Abp.Core", includeReleaseCandidates: true); var latestVersionFromMyGet = await GetLatestVersionFromMyGet("Volo.Abp.Core"); - var fileContent = File.ReadAllText(projectPath); + using (var fs = File.Open(projectPath, FileMode.Open, FileAccess.ReadWrite, FileShare.None)) + using (var sr = new StreamReader(fs, Encoding.Default, true)) + { + var fileContent = await sr.ReadToEndAsync(); + + var updatedContent = await UpdateVoloPackagesAsync(fileContent, + includeNightlyPreviews, + includeReleaseCandidates, + switchToStable, + latestVersionFromNuget, + latestReleaseCandidateVersionFromNuget, + latestVersionFromMyGet, + version); - var updatedContent = await UpdateVoloPackagesAsync(fileContent, - includeNightlyPreviews, - includeReleaseCandidates, - switchToStable, - latestVersionFromNuget, - latestReleaseCandidateVersionFromNuget, - latestVersionFromMyGet, - version); + fs.Seek(0, SeekOrigin.Begin); + fs.SetLength(0); - File.WriteAllText(projectPath, updatedContent); + using (var sw = new StreamWriter(fs, sr.CurrentEncoding)) + { + sw.Write(updatedContent); + sw.Flush(); + } + } } } protected virtual async Task UpdateInternalAsync(string projectPath, bool includeNightlyPreviews = false, bool includeReleaseCandidates = false, bool switchToStable = false) { - var fileContent = File.ReadAllText(projectPath); - var updatedContent = await UpdateVoloPackagesAsync(fileContent, includeNightlyPreviews, includeReleaseCandidates, switchToStable); + using (var fs = File.Open(projectPath, FileMode.Open, FileAccess.ReadWrite, FileShare.None)) + using (var sr = new StreamReader(fs, Encoding.Default, true)) + { + var fileContent = await sr.ReadToEndAsync(); + + var updatedContent = await UpdateVoloPackagesAsync(fileContent, includeNightlyPreviews, includeReleaseCandidates, switchToStable); + + fs.Seek(0, SeekOrigin.Begin); + fs.SetLength(0); - File.WriteAllText(projectPath, updatedContent); + using (var sw = new StreamWriter(fs, sr.CurrentEncoding)) + { + sw.Write(updatedContent); + sw.Flush(); + } + } } protected virtual async Task SpecifiedVersionExists(string version, string packageId)