Skip to content

Commit

Permalink
Modify runner project upgrades and bugfixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Noggog committed Nov 26, 2024
1 parent b57154a commit 6efaa4c
Show file tree
Hide file tree
Showing 10 changed files with 163 additions and 38 deletions.
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
using System.Xml.Linq;
using Mutagen.Bethesda;
using Noggog;
using NuGet.Versioning;

namespace Synthesis.Bethesda.Execution.Patchers.Git.Services.ModifyProject;

public class AddAllReleasesToOldVersions
{
public static readonly Version IntroducingSynthesisVersion = new Version(0, 29);
public static readonly SemanticVersion IntroducingSynthesisVersion = new(0, 29, 0);

public void Add(
XElement proj,
Version? curSynthVersion,
Version targetMutagenVersion,
Version? targetSynthVersion)
SemanticVersion? curSynthVersion,
SemanticVersion targetMutagenVersion,
SemanticVersion? targetSynthVersion)
{
if (targetSynthVersion != null
&& curSynthVersion != null
Expand All @@ -25,6 +27,10 @@ public void Add(
if (!elem.Name.LocalName.Equals("PackageReference")) continue;
if (!elem.TryGetAttribute("Include", out var include)) continue;
if (include.Value.Equals("Mutagen.Bethesda")) return;
foreach (var cat in Enums<GameCategory>.Values)
{
if (include.Value.Equals($"Mutagen.Bethesda.{cat}")) return;
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
using System.Xml.Linq;
using Mutagen.Bethesda.Synthesis.Versioning;
using Noggog;
using NuGet.Versioning;

namespace Synthesis.Bethesda.Execution.Patchers.Git.Services.ModifyProject;

public interface IAddNewtonsoftToOldSetups
{
void Add(
XElement proj,
Version? mutaVersion,
Version? synthVersion);
SemanticVersion? mutaVersion,
SemanticVersion? synthVersion);
}

public class AddNewtonsoftToOldSetups : IAddNewtonsoftToOldSetups
{
private readonly IProvideCurrentVersions _provideCurrentVersions;
public static readonly System.Version NewtonSoftAddMutaVersion = new(0, 26);
public static readonly System.Version NewtonSoftAddSynthVersion = new(0, 14, 1);
public static readonly SemanticVersion NewtonSoftAddMutaVersion = new(0, 26, 0);
public static readonly SemanticVersion NewtonSoftAddSynthVersion = new(0, 14, 1);

public AddNewtonsoftToOldSetups(IProvideCurrentVersions provideCurrentVersions)
{
Expand All @@ -25,8 +26,8 @@ public AddNewtonsoftToOldSetups(IProvideCurrentVersions provideCurrentVersions)

public void Add(
XElement proj,
Version? mutaVersion,
Version? synthVersion)
SemanticVersion? mutaVersion,
SemanticVersion? synthVersion)
{
if ((mutaVersion != null
&& mutaVersion <= NewtonSoftAddMutaVersion)
Expand All @@ -39,7 +40,7 @@ public void Add(
{
if (!elem.Name.LocalName.Equals("PackageReference")) continue;
if (!elem.TryGetAttribute("Include", out var include)) continue;
if (include.Equals("Newtonsoft.Json")) return;
if (include.Value.Equals("Newtonsoft.Json")) return;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ void Modify(

public class ModifyRunnerProjects : IModifyRunnerProjects
{
public static readonly System.Version NewtonSoftRemoveMutaVersion = new(0, 28);
public static readonly System.Version NewtonSoftRemoveSynthVersion = new(0, 17, 5);
public static readonly System.Version NamespaceMutaVersion = new(0, 30, 0);
public static readonly SemanticVersion NewtonSoftRemoveMutaVersion = new(0, 28, 0);
public static readonly SemanticVersion NewtonSoftRemoveSynthVersion = new(0, 17, 5);
public static readonly SemanticVersion NamespaceMutaVersion = new(0, 30, 0);
private readonly ILogger _logger;
private readonly IFileSystem _fileSystem;
private readonly IAvailableProjectsRetriever _availableProjectsRetriever;
Expand Down Expand Up @@ -65,6 +65,46 @@ public ModifyRunnerProjects(
_removeProject = removeProject;
_addAllReleasesToOldVersions = addAllReleasesToOldVersions;
}

string? TrimVersion(string? version, out string? prereleaseLabel)
{
if (version == null)
{
prereleaseLabel = null;
return null;
}
var index = version.IndexOf('-');
if (index == -1)
{
prereleaseLabel = null;
return version;
}

prereleaseLabel = version.Substring(index + 1);
return version.Substring(0, index);
}

private SemanticVersion? SemanticVersionParse(string? str)
{
if (str == null) return null;
if (SemanticVersion.TryParse(str, out var semVer))
{
return semVer;
}

var trimmed = TrimVersion(str, out var prereleaseLabel);

if (Version.TryParse(trimmed, out var vers))
{
if (prereleaseLabel == null)
{
return new SemanticVersion(vers.Major, vers.Minor, vers.Build == -1 ? 0 : vers.Build);
}
return new SemanticVersion(vers.Major, vers.Minor, vers.Build == -1 ? 0 : vers.Build, prereleaseLabel);
}

return null;
}

public void Modify(
FilePath solutionPath,
Expand All @@ -74,16 +114,6 @@ public void Modify(
{
listedVersions = new NugetVersionPair(null, null);

string? TrimVersion(string? version)
{
if (version == null) return null;
var index = version.IndexOf('-');
if (index == -1) return version;
return version.Substring(0, index);
}

var trimmedMutagenVersion = TrimVersion(versions.Mutagen);
var trimmedSynthesisVersion = TrimVersion(versions.Synthesis);
foreach (var subProj in _availableProjectsRetriever.Get(solutionPath))
{
var proj = Path.Combine(Path.GetDirectoryName(solutionPath)!, subProj);
Expand All @@ -97,11 +127,11 @@ public void Modify(
_turnOffNullability.TurnOff(projXml);
_removeGitInfo.Remove(projXml);
_turnOffWindowsSpec.TurnOff(projXml);
System.Version.TryParse(TrimVersion(curListedVersions.Mutagen), out var mutaVersion);
System.Version.TryParse(TrimVersion(curListedVersions.Synthesis), out var synthVersion);
var mutaVersion = SemanticVersionParse(curListedVersions.Mutagen);
var synthVersion = SemanticVersionParse(curListedVersions.Synthesis);
_addNewtonsoftToOldSetups.Add(projXml, mutaVersion, synthVersion);
System.Version.TryParse(trimmedMutagenVersion, out var targetMutaVersion);
System.Version.TryParse(trimmedSynthesisVersion, out var targetSynthesisVersion);
var targetMutaVersion = SemanticVersionParse(versions.Mutagen);
var targetSynthesisVersion = SemanticVersionParse(versions.Synthesis);
if ((targetMutaVersion != null
&& targetMutaVersion >= NewtonSoftRemoveMutaVersion)
|| (targetSynthesisVersion != null
Expand All @@ -120,7 +150,9 @@ public void Modify(
_addAllReleasesToOldVersions.Add(projXml, synthVersion, targetMutaVersion, targetSynthesisVersion);
}

if (targetMutaVersion >= NamespaceMutaVersion
if (targetMutaVersion != null
&& mutaVersion != null
&& targetMutaVersion >= NamespaceMutaVersion
&& mutaVersion < NamespaceMutaVersion)
{
_processProjUsings.Process(proj);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,35 @@
using System.Xml.Linq;
using Serilog;
using Noggog;
using NuGet.Versioning;

namespace Synthesis.Bethesda.Execution.Patchers.Git.Services.ModifyProject;

public interface ISwapToProperNetVersion
{
void Swap(XElement proj, Version targetMutagenVersion);
void Swap(XElement proj, SemanticVersion targetMutagenVersion);
}

public class SwapToProperNetVersion : ISwapToProperNetVersion
{
private readonly ILogger _logger;
private readonly Version Net8Version = new Version(0, 45);
private readonly SemanticVersion Net8Version = new(0, 45, 0);
// private readonly Version Net9Version = new Version(0, 49);
private readonly CultureInfo Culture = new CultureInfo("en");
private readonly CultureInfo Culture = new("en");

private readonly SortedList<Version, byte> _netMapping;
private readonly SortedList<SemanticVersion, byte> _netMapping;

public SwapToProperNetVersion(ILogger logger)
{
_logger = logger;
_netMapping = new SortedList<Version, byte>()
_netMapping = new SortedList<SemanticVersion, byte>()
{
{ Net8Version, 8 },
// { Net9Version, 9 }
};
}

private void ProcessTargetFrameworkNode(XElement elem, Version targetMutagenVersion)
private void ProcessTargetFrameworkNode(XElement elem, SemanticVersion targetMutagenVersion)
{
if (!elem.Name.LocalName.Equals("TargetFramework")) return;
_logger.Information("Target mutagen version: {Target}", targetMutagenVersion);
Expand Down Expand Up @@ -75,7 +76,7 @@ private bool NeedsUpgrade(string elem, byte target)
return true;
}

private void ProcessNormal(XElement elem, Version targetMutagenVersion)
private void ProcessNormal(XElement elem, SemanticVersion targetMutagenVersion)
{
if (!_netMapping.TryGetInDirection(targetMutagenVersion, higher: false, out var targetNetVersion))
{
Expand All @@ -88,7 +89,7 @@ private void ProcessNormal(XElement elem, Version targetMutagenVersion)
_logger.Information("Swapping to {Target}", elem.Value);
}

public void Swap(XElement proj, Version targetMutagenVersion)
public void Swap(XElement proj, SemanticVersion targetMutagenVersion)
{
foreach (var group in proj.Elements("PropertyGroup"))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@
</ItemGroup>
<ItemGroup />
<ItemGroup>
<PackageReference Include="Mutagen.Bethesda" Version="0.45" />
<PackageReference Include="Mutagen.Bethesda" Version="0.45.0" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Mutagen.Bethesda.Skyrim" Version="0.49.0-alpha.7" />
<PackageReference Include="Mutagen.Bethesda.Synthesis" Version="0.29" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Mutagen.Bethesda.Skyrim" Version="0.49.0-alpha.7" />
<PackageReference Include="Mutagen.Bethesda.Synthesis" Version="0.29" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,44 @@ public async Task Legacy(
out var pair);
await Verify(fileSystem.File.ReadAllText(projPath));
}

[Theory, DefaultAutoData]
public async Task LegacySpecificMutagenCategoryLib(
IFileSystem fileSystem,
FilePath existingSlnPath)
{
var subPath = Path.Combine("SomeProj", "SomeProj.csproj");
fileSystem.File.WriteAllText(existingSlnPath, Sln);
var projPath = Path.Combine(Path.GetDirectoryName(existingSlnPath)!, subPath);
fileSystem.Directory.CreateDirectory(Path.GetDirectoryName(projPath)!);

string proj =
"""
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Mutagen.Bethesda.Skyrim" Version="0.39.5" />
<PackageReference Include="Mutagen.Bethesda.Synthesis" Version="0.24.1" />
</ItemGroup>
</Project>
""";
fileSystem.File.WriteAllText(projPath, proj);
var sut = new ModifyRunnerProjectsContainer(fileSystem);
sut.Resolve().Value.Modify(
existingSlnPath,
subPath,
new NugetVersionPair(
"0.49.0-alpha.7",
"0.29"),
out var pair);
await Verify(fileSystem.File.ReadAllText(projPath));
}

[Theory, DefaultAutoData]
public async Task NetCoreApp(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,4 +156,30 @@ public void NoMutagen(SwapInDesiredVersionsForProjectString sut)
.Should()
.BeEquivalentTo(expectedString);
}

[Theory, SynthAutoData]
public void AlphaVersion(SwapInDesiredVersionsForProjectString sut)
{
var projStr = CreateProj(
("Mutagen.Bethesda.Synthesis", "0.3.0"),
("Mutagen.Bethesda", "0.1.0"),
("Mutagen.Bethesda.Skyrim", "0.1.0"),
("Mutagen.Bethesda.Core", "0.1.0"),
("Mutagen.Bethesda.Kernel", "0.1.0"));
var projXml = XElement.Parse(projStr);
sut.Swap(
projXml,
new NugetVersionPair(Mutagen: "0.49.0-alpha.7", Synthesis: null),
out var _);
var swapString = projXml.ToString();
var expectedString = CreateProj(
("Mutagen.Bethesda.Synthesis", "0.3.0"),
("Mutagen.Bethesda", "0.49.0-alpha.7"),
("Mutagen.Bethesda.Skyrim", "0.49.0-alpha.7"),
("Mutagen.Bethesda.Core", "0.49.0-alpha.7"),
("Mutagen.Bethesda.Kernel", "0.49.0-alpha.7"));
swapString
.Should()
.BeEquivalentTo(expectedString);
}
}

0 comments on commit 6efaa4c

Please sign in to comment.