Fixed project file encoding to retain existing encoding.

pull/12197/head
Eric Johnson 4 years ago
parent 8b2d88c1a5
commit 1b87d7e7e2

@ -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<string> RemoveProject(List<string> 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;
}

@ -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<bool> SpecifiedVersionExists(string version, string packageId)

Loading…
Cancel
Save