Skip to content

Commit 357f8fe

Browse files
Fix update functionality
1 parent 4ece6dc commit 357f8fe

File tree

12 files changed

+94
-33
lines changed

12 files changed

+94
-33
lines changed

src/LibYear.Core/FileTypes/XmlProjectFile.cs

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,18 @@ namespace LibYear.Core.FileTypes;
44

55
public abstract class XmlProjectFile : IProjectFile
66
{
7+
private enum Whitespace
8+
{
9+
Tabs,
10+
FourSpaces,
11+
TwoSpaces
12+
}
13+
714
public string FileName { get; }
815
public IDictionary<string, PackageVersion?> Packages { get; }
916

1017
private readonly XDocument _xmlContents;
18+
private readonly Whitespace _whitespace;
1119
private readonly string _elementName;
1220
private readonly string[] _packageAttributeNames;
1321
private readonly string _versionAttributeName;
@@ -21,13 +29,20 @@ protected XmlProjectFile(string filename, string contents, string elementName, s
2129
_versionAttributeName = versionAttributeName;
2230

2331
_xmlContents = XDocument.Parse(contents);
32+
_whitespace = DetermineWhitespace(contents);
33+
2434
Packages = _xmlContents.Descendants(elementName)
2535
.ToDictionary(
2636
d => packageAttributeNames.Select(p => d.Attribute(p)?.Value ?? d.Element(p)?.Value).FirstOrDefault(v => v != null)!,
2737
d => ParseCurrentVersion(d, versionAttributeName)
2838
);
2939
}
3040

41+
private static Whitespace DetermineWhitespace(string contents)
42+
=> contents.Contains("\n\t") ? Whitespace.Tabs
43+
: contents.Contains("\n <") ? Whitespace.TwoSpaces
44+
: Whitespace.FourSpaces;
45+
3146
public string Update(IReadOnlyCollection<Result> results)
3247
{
3348
foreach (var result in results.Where(r => r.Latest != null))
@@ -36,7 +51,13 @@ public string Update(IReadOnlyCollection<Result> results)
3651
UpdateElement(element, result.Latest!.Version.ToString());
3752
}
3853

39-
return _xmlContents.ToString();
54+
var xml = _xmlContents.ToString();
55+
return _whitespace switch
56+
{
57+
Whitespace.Tabs => xml.Replace(" ", "\t"),
58+
Whitespace.FourSpaces => xml.Replace(" ", " "),
59+
_ => xml
60+
};
4061
}
4162

4263
private PackageVersion? ParseCurrentVersion(XElement element, string versionAttributeName)
@@ -71,7 +92,7 @@ private void UpdateElement(XElement element, string latestVersion)
7192
private XElement[] GetMatchingElements(Result result)
7293
=> _xmlContents.Descendants(_elementName)
7394
.Where(d => _packageAttributeNames.Any(attributeName => (d.Attribute(attributeName)?.Value ?? d.Element(attributeName)?.Value) == result.Name
74-
&& (d.Attribute(_versionAttributeName)?.Value ?? d.Element(_versionAttributeName)?.Value) == result.Installed?.Version.ToString()
95+
&& (d.Attribute(_versionAttributeName)?.Value ?? d.Element(_versionAttributeName)?.Value) == result.Installed?.Version.ToString()
7596
)
7697
)
7798
.ToArray();

src/LibYear.Core/LibYear.Core.csproj

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
3-
<Version>9.0.1</Version>
3+
<Version>9.0.2</Version>
44
<TargetFramework>net9.0</TargetFramework>
55
<Product>libyear</Product>
66
<PackageId>LibYear.Core</PackageId>
@@ -16,11 +16,10 @@
1616
<ImplicitUsings>enable</ImplicitUsings>
1717
<Nullable>enable</Nullable>
1818
</PropertyGroup>
19-
2019
<ItemGroup>
2120
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="8.0.0" PrivateAssets="all" />
22-
<PackageReference Include="NuGet.Protocol" Version="6.12.1" />
23-
<PackageReference Include="System.IO.Abstractions" Version="21.2.1" />
21+
<PackageReference Include="NuGet.Protocol" Version="6.14.0" />
22+
<PackageReference Include="System.IO.Abstractions" Version="22.0.16" />
2423
<None Include="../../README.md" CopyToOutputDirectory="PreserveNewest" Pack="true" PackagePath="." />
2524
</ItemGroup>
2625
</Project>

src/LibYear.Core/ProjectFileManager.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,7 @@ public async Task<IReadOnlyCollection<string>> Update(SolutionResult result)
8181
{
8282
var update = project.ProjectFile.Update(project.Details);
8383

84-
var stream = _fileSystem.FileStream.New(project.ProjectFile.FileName, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None);
85-
await new StreamWriter(stream).WriteAsync(update);
86-
stream.Close();
84+
await _fileSystem.File.WriteAllTextAsync(project.ProjectFile.FileName, update);
8785
updated.Add(project.ProjectFile.FileName);
8886
}
8987

src/LibYear/LibYear.csproj

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
3-
<Version>9.0.1</Version>
3+
<Version>9.0.2</Version>
44
<OutputType>Exe</OutputType>
55
<PackAsTool>true</PackAsTool>
66
<Product>libyear</Product>
@@ -19,10 +19,9 @@
1919
<ImplicitUsings>enable</ImplicitUsings>
2020
<Nullable>enable</Nullable>
2121
</PropertyGroup>
22-
2322
<ItemGroup>
2423
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="8.0.0" PrivateAssets="all" />
25-
<PackageReference Include="Spectre.Console.Cli" Version="0.49.1" />
24+
<PackageReference Include="Spectre.Console.Cli" Version="0.51.1" />
2625
<ProjectReference Include="../LibYear.Core/LibYear.Core.csproj" />
2726
<None Include="../../README.md" CopyToOutputDirectory="PreserveNewest" Pack="true" PackagePath="." />
2827
</ItemGroup>

test/LibYear.Core.Tests/FileTypes/CsProjFileTests.cs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,56 @@ public async Task CanUpdateCsProjFile()
5656
Assert.Equal(WildcardType.Major, newFile.Packages["test7"]!.WildcardType);
5757
}
5858

59+
[Fact]
60+
public async Task UpdateMaintainsIndent()
61+
{
62+
//arrange
63+
var filename = Path.Combine("FileTypes", "project.csproj");
64+
var original = await File.ReadAllTextAsync(filename);
65+
var file = new CsProjFile(filename, original);
66+
var results = Array.Empty<Result>();
67+
68+
//act
69+
var updated = file.Update(results);
70+
71+
//assert
72+
Assert.Equal(updated, original);
73+
}
74+
75+
[Fact]
76+
public async Task UpdateMaintainsFourSpaces()
77+
{
78+
//arrange
79+
var filename = Path.Combine("FileTypes", "project.csproj");
80+
var original = await File.ReadAllTextAsync(filename);
81+
original = original.Replace(" ", " ");
82+
var file = new CsProjFile(filename, original);
83+
var results = Array.Empty<Result>();
84+
85+
//act
86+
var updated = file.Update(results);
87+
88+
//assert
89+
Assert.Equal(updated, original);
90+
}
91+
92+
[Fact]
93+
public async Task UpdateMaintainsTabs()
94+
{
95+
//arrange
96+
var filename = Path.Combine("FileTypes", "project.csproj");
97+
var original = await File.ReadAllTextAsync(filename);
98+
original = original.Replace(" ", "\t");
99+
var file = new CsProjFile(filename, original);
100+
var results = Array.Empty<Result>();
101+
102+
//act
103+
var updated = file.Update(results);
104+
105+
//assert
106+
Assert.Equal(updated, original);
107+
}
108+
59109
[Fact]
60110
public async Task InvalidVersionShowsExceptionDetails()
61111
{

test/LibYear.Core.Tests/FileTypes/Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@
77
<Version>0.3.0</Version>
88
</PackageReference>
99
</ItemGroup>
10-
</Project>
10+
</Project>

test/LibYear.Core.Tests/FileTypes/Directory.Build.targets

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@
77
<Version>0.3.0</Version>
88
</PackageReference>
99
</ItemGroup>
10-
</Project>
10+
</Project>

test/LibYear.Core.Tests/FileTypes/RecursiveFolderTest/sub-project.csproj

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
2-
1+
<Project Sdk="Microsoft.NET.Sdk">
32
<PropertyGroup>
43
<OutputType>Exe</OutputType>
54
<TargetFramework>netcoreapp1.1</TargetFramework>
65
</PropertyGroup>
7-
86
<ItemGroup>
97
<PackageReference Include="test1" Version="0.1.0.1" />
108
<PackageReference Include="test2" Version="0.2.0" />

test/LibYear.Core.Tests/FileTypes/packages.config

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
<?xml version="1.0" encoding="utf-8"?>
21
<packages>
3-
<package id="test1" version="0.1.0"/>
4-
<package id="test2" version="0.2.0"/>
2+
<package id="test1" version="0.1.0" />
3+
<package id="test2" version="0.2.0" />
54
<package>
65
<id>test3</id>
76
<version>0.3.0</version>

test/LibYear.Core.Tests/FileTypes/project.csproj

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
2-
1+
<Project Sdk="Microsoft.NET.Sdk">
32
<PropertyGroup>
43
<OutputType>Exe</OutputType>
54
<TargetFramework>netcoreapp1.1</TargetFramework>
65
</PropertyGroup>
7-
86
<ItemGroup>
97
<PackageReference Include="test1" Version="0.1.0.1" />
108
<PackageReference Include="test2" Version="0.2.0" />

0 commit comments

Comments
 (0)