Browse Source

Fixed project file encoding to retain existing encoding.

pull/12197/head
Eric Johnson 5 years ago
parent
commit
1b87d7e7e2
  1. 29
      framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/SolutionFileModifier.cs
  2. 83
      framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/VoloNugetPackagesVersionUpdater.cs

29
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) public async Task RemoveProjectFromSolutionFileAsync(string solutionFile, string projectName)
{ {
var solutionFileContent = File.ReadAllText(solutionFile); using (var fs = File.Open(solutionFile, FileMode.Open, FileAccess.ReadWrite, FileShare.None))
solutionFileContent.NormalizeLineEndings(); using (var sr = new StreamReader(fs, Encoding.Default, true))
var lines = solutionFileContent.Split(new[] { Environment.NewLine, "\n" }, StringSplitOptions.None); {
File.WriteAllText(solutionFile, var solutionFileContent = await sr.ReadToEndAsync();
RemoveProject(lines.ToList(), projectName).JoinAsString(Environment.NewLine)); 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) 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); 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) 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); 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) if (module.MasterModuleInfos != null)
{ {
@ -219,7 +232,7 @@ public class SolutionFileModifier : ITransientDependency
.Split(" ").Last(); .Split(" ").Last();
} }
File.WriteAllText(solutionFile, string.Join(Environment.NewLine, lines)); File.WriteAllText(solutionFile, string.Join(Environment.NewLine, lines), Encoding.UTF8);
return folderId; return folderId;
} }

83
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 Volo.Abp.DependencyInjection;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions; using Microsoft.Extensions.Logging.Abstractions;
using System.Text;
namespace Volo.Abp.Cli.ProjectModification; namespace Volo.Abp.Cli.ProjectModification;
@ -41,17 +42,28 @@ public class VoloNugetPackagesVersionUpdater : ITransientDependency
async Task UpdateAsync(string filePath) async Task UpdateAsync(string filePath)
{ {
var fileContent = File.ReadAllText(filePath); using (var fs = File.Open(filePath, FileMode.Open, FileAccess.ReadWrite, FileShare.None))
var updatedContent = await UpdateVoloPackagesAsync(fileContent, using (var sr = new StreamReader(fs, Encoding.Default, true))
includePreviews, {
includeReleaseCandidates, var fileContent = await sr.ReadToEndAsync();
switchToStable,
latestVersionFromNuget, var updatedContent = await UpdateVoloPackagesAsync(fileContent,
latestReleaseCandidateVersionFromNuget, includePreviews,
latestVersionFromMyGet, includeReleaseCandidates,
version); switchToStable,
latestVersionFromNuget,
File.WriteAllText(filePath, updatedContent); 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()); 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 latestReleaseCandidateVersionFromNuget = await _nuGetService.GetLatestVersionOrNullAsync("Volo.Abp.Core", includeReleaseCandidates: true);
var latestVersionFromMyGet = await GetLatestVersionFromMyGet("Volo.Abp.Core"); 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, fs.Seek(0, SeekOrigin.Begin);
includeNightlyPreviews, fs.SetLength(0);
includeReleaseCandidates,
switchToStable,
latestVersionFromNuget,
latestReleaseCandidateVersionFromNuget,
latestVersionFromMyGet,
version);
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) protected virtual async Task UpdateInternalAsync(string projectPath, bool includeNightlyPreviews = false, bool includeReleaseCandidates = false, bool switchToStable = false)
{ {
var fileContent = File.ReadAllText(projectPath); using (var fs = File.Open(projectPath, FileMode.Open, FileAccess.ReadWrite, FileShare.None))
var updatedContent = await UpdateVoloPackagesAsync(fileContent, includeNightlyPreviews, includeReleaseCandidates, switchToStable); 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) protected virtual async Task<bool> SpecifiedVersionExists(string version, string packageId)

Loading…
Cancel
Save