Browse Source

Merge pull request #12197 from ejohnson-dotnet/encoding-fix

Fixed project file encoding to retain existing encoding.
pull/12231/head
albert 5 years ago
committed by GitHub
parent
commit
f4d33dd994
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 39
      framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/SolutionFileModifier.cs
  2. 92
      framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/VoloNugetPackagesVersionUpdater.cs

39
framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/SolutionFileModifier.cs

@ -10,13 +10,30 @@ namespace Volo.Abp.Cli.ProjectModification;
public class SolutionFileModifier : ITransientDependency public class SolutionFileModifier : ITransientDependency
{ {
public static Encoding DefaultEncoding = Encoding.UTF8;
public async Task RemoveProjectFromSolutionFileAsync(string solutionFile, string projectName) public async Task RemoveProjectFromSolutionFileAsync(string solutionFile, string projectName)
{ {
var solutionFileContent = File.ReadAllText(solutionFile); using (var fileStream = File.Open(solutionFile, FileMode.Open, FileAccess.ReadWrite, FileShare.None))
solutionFileContent.NormalizeLineEndings(); {
var lines = solutionFileContent.Split(new[] { Environment.NewLine, "\n" }, StringSplitOptions.None); using (var sr = new StreamReader(fileStream, Encoding.Default, true))
File.WriteAllText(solutionFile, {
RemoveProject(lines.ToList(), projectName).JoinAsString(Environment.NewLine)); 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);
fileStream.Seek(0, SeekOrigin.Begin);
fileStream.SetLength(0);
using (var sw = new StreamWriter(fileStream, DefaultEncoding))
{
await sw.WriteAsync(updatedContent);
await sw.FlushAsync();
}
}
}
} }
public async Task AddModuleToSolutionFileAsync(ModuleWithMastersInfo module, string solutionFile) public async Task AddModuleToSolutionFileAsync(ModuleWithMastersInfo module, string solutionFile)
@ -33,8 +50,8 @@ public class SolutionFileModifier : ITransientDependency
{ {
var srcFolderId = await AddNewFolderAndGetIdOrGetExistingIdAsync(solutionFile, "src"); var srcFolderId = await AddNewFolderAndGetIdOrGetExistingIdAsync(solutionFile, "src");
var file = File.ReadAllText(solutionFile); var solutionFileContent = File.ReadAllText(solutionFile);
var lines = file.Split(Environment.NewLine).ToList(); var lines = solutionFileContent.Split(Environment.NewLine).ToList();
if (lines.Any(l => l.Contains($"\"{package.Name}\""))) if (lines.Any(l => l.Contains($"\"{package.Name}\"")))
{ {
@ -64,7 +81,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), DefaultEncoding);
} }
private List<string> RemoveProject(List<string> solutionFileLines, string projectName) private List<string> RemoveProject(List<string> solutionFileLines, string projectName)
@ -137,7 +154,7 @@ public class SolutionFileModifier : ITransientDependency
Path.Combine(Path.GetDirectoryName(solutionFile), "modules", module.Name, "test"), Path.Combine(Path.GetDirectoryName(solutionFile), "modules", module.Name, "test"),
"*.csproj", "*.csproj",
SearchOption.AllDirectories).ToList(); SearchOption.AllDirectories).ToList();
} }
foreach (var projectPath in projectsUnderModule) foreach (var projectPath in projectsUnderModule)
{ {
@ -174,7 +191,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 +236,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;
} }

92
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;
@ -17,6 +18,7 @@ public class VoloNugetPackagesVersionUpdater : ITransientDependency
private readonly NuGetService _nuGetService; private readonly NuGetService _nuGetService;
private readonly MyGetPackageListFinder _myGetPackageListFinder; private readonly MyGetPackageListFinder _myGetPackageListFinder;
public ILogger<VoloNugetPackagesVersionUpdater> Logger { get; set; } public ILogger<VoloNugetPackagesVersionUpdater> Logger { get; set; }
public static Encoding DefaultEncoding = Encoding.UTF8;
public VoloNugetPackagesVersionUpdater(NuGetService nuGetService, MyGetPackageListFinder myGetPackageListFinder) public VoloNugetPackagesVersionUpdater(NuGetService nuGetService, MyGetPackageListFinder myGetPackageListFinder)
{ {
@ -41,17 +43,30 @@ 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, {
includePreviews, using (var sr = new StreamReader(fs, Encoding.Default, true))
includeReleaseCandidates, {
switchToStable, var fileContent = await sr.ReadToEndAsync();
latestVersionFromNuget,
latestReleaseCandidateVersionFromNuget, var updatedContent = await UpdateVoloPackagesAsync(fileContent,
latestVersionFromMyGet, includePreviews,
version); includeReleaseCandidates,
switchToStable,
File.WriteAllText(filePath, updatedContent); latestVersionFromNuget,
latestReleaseCandidateVersionFromNuget,
latestVersionFromMyGet,
version);
fs.Seek(0, SeekOrigin.Begin);
fs.SetLength(0);
using (var sw = new StreamWriter(fs, DefaultEncoding))
{
await sw.WriteAsync(updatedContent);
await sw.FlushAsync();
}
}
}
} }
Task.WaitAll(projectPaths.Select(UpdateAsync).ToArray()); Task.WaitAll(projectPaths.Select(UpdateAsync).ToArray());
@ -70,27 +85,54 @@ 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))
{
await sw.WriteAsync(updatedContent);
await sw.FlushAsync();
}
}
}
} }
} }
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);
File.WriteAllText(projectPath, updatedContent); fs.Seek(0, SeekOrigin.Begin);
fs.SetLength(0);
using (var sw = new StreamWriter(fs, sr.CurrentEncoding))
{
await sw.WriteAsync(updatedContent);
await sw.FlushAsync();
}
}
}
} }
protected virtual async Task<bool> SpecifiedVersionExists(string version, string packageId) protected virtual async Task<bool> SpecifiedVersionExists(string version, string packageId)
@ -210,7 +252,7 @@ public class VoloNugetPackagesVersionUpdater : ITransientDependency
} }
catch (Exception ex) catch (Exception ex)
{ {
Logger.LogError("Cannot update Volo.* packages! An error occured while updating the package \"{0}\". Error: {1}", packageId, ex.Message); Logger.LogError("Cannot update Volo.* packages! An error occurred while updating the package \"{0}\". Error: {1}", packageId, ex.Message);
Logger.LogException(ex); Logger.LogException(ex);
} }

Loading…
Cancel
Save