Skip to content

Commit

Permalink
refactor: Upgrade CSharpier.MSBuild
Browse files Browse the repository at this point in the history
  • Loading branch information
Oskar Klintrot committed Nov 1, 2024
1 parent 0a2f7a2 commit 8e61863
Show file tree
Hide file tree
Showing 14 changed files with 173 additions and 211 deletions.
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="CSharpier.MSBuild" Version="0.16.0" PrivateAssets="all" />
<PackageReference Include="CSharpier.MSBuild" Version="0.29.2" PrivateAssets="all" />
</ItemGroup>

</Project>
16 changes: 7 additions & 9 deletions src/UpdatR/Domain/Csproj.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public static Csproj Create(string path)
);
}

var doc = new XmlDocument { PreserveWhitespace = true, };
var doc = new XmlDocument { PreserveWhitespace = true };

doc.Load(file.FullName);

Expand Down Expand Up @@ -203,8 +203,8 @@ private NuGetFramework GetTargetFramework()
?? GetTargetFrameworkFromDirectoryBuildProps(new(Parent));

return targetFramework is null
? NuGetFramework.AnyFramework
: NuGetFramework.Parse(targetFramework);
? NuGetFramework.AnyFramework
: NuGetFramework.Parse(targetFramework);
}

private void UpdateEntityFrameworkVersion()
Expand Down Expand Up @@ -248,13 +248,11 @@ private void UpdateEntityFrameworkVersion()
private Dictionary<string, NuGetVersion> GetPackages() =>
_doc.SelectNodes("/Project/ItemGroup/PackageReference")!
.OfType<XmlElement>()
.Select(
x => (PackageId: x!.GetAttribute("Include"), Version: x!.GetAttribute("Version"))
.Select(x =>
(PackageId: x!.GetAttribute("Include"), Version: x!.GetAttribute("Version"))
)
.Where(
x =>
!string.IsNullOrWhiteSpace(x.PackageId)
&& NuGetVersion.TryParse(x.Version, out _)
.Where(x =>
!string.IsNullOrWhiteSpace(x.PackageId) && NuGetVersion.TryParse(x.Version, out _)
)
.DistinctBy(x => x.PackageId)
.ToDictionary(
Expand Down
5 changes: 3 additions & 2 deletions src/UpdatR/Domain/DotnetTools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ internal sealed partial class DotnetTools
private readonly FileInfo _path;
private readonly IEnumerable<Csproj> _affectedCsprojs;
private static readonly JsonSerializerOptions s_jsonSerializerOptions =
new(JsonSerializerDefaults.Web) { WriteIndented = true, };
new(JsonSerializerDefaults.Web) { WriteIndented = true };

private DotnetTools(FileInfo path, IEnumerable<Csproj> affectedCsprojs)
{
Expand Down Expand Up @@ -195,7 +195,8 @@ private List<string> GetPackageIds()
var json = File.ReadAllText(Path);
var foo = JsonSerializer.Deserialize<JsonObject>(json);

var packageIds = foo?["tools"]?.AsObject()
var packageIds = foo
?["tools"]?.AsObject()
.Select(x => (PackageId: x.Key, Version: x.Value?["version"]?.GetValue<string>()))
.Where(x => NuGetVersion.TryParse(x.Version, out _))
.Select(x => x.PackageId);
Expand Down
30 changes: 14 additions & 16 deletions src/UpdatR/Domain/NuGetPackage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,28 @@ internal record NuGetPackage(string PackageId, IEnumerable<PackageMetadata> Pack

private PackageMetadata? LatestStable(NuGetFramework targetFramework) =>
_latestStable ??= PackageMetadatas
.Where(
x =>
!x.Version.IsPrerelease
&& (
!x.TargetFrameworks.Any()
|| x.TargetFrameworks.Any(
y => CompatibilityProvider.IsCompatible(targetFramework, y)
)
.Where(x =>
!x.Version.IsPrerelease
&& (
!x.TargetFrameworks.Any()
|| x.TargetFrameworks.Any(y =>
CompatibilityProvider.IsCompatible(targetFramework, y)
)
)
) // Todo: Bodge for tools
.OrderByDescending(x => x.Version)
.FirstOrDefault();

private PackageMetadata? LatestPrerelease(NuGetFramework targetFramework) =>
_latestPrerelease ??= PackageMetadatas
.Where(
x =>
x.Version.IsPrerelease
&& (
!x.TargetFrameworks.Any()
|| x.TargetFrameworks.Any(
y => CompatibilityProvider.IsCompatible(targetFramework, y)
)
.Where(x =>
x.Version.IsPrerelease
&& (
!x.TargetFrameworks.Any()
|| x.TargetFrameworks.Any(y =>
CompatibilityProvider.IsCompatible(targetFramework, y)
)
)
) // Todo: Bodge for tools
.OrderByDescending(x => x.Version)
.FirstOrDefault();
Expand Down
45 changes: 20 additions & 25 deletions src/UpdatR/Domain/RootDir.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ var projectFile in Directory.EnumerateFiles(
new EnumerationOptions
{
MatchCasing = MatchCasing.CaseInsensitive,
RecurseSubdirectories = true
RecurseSubdirectories = true,
}
)
)
Expand All @@ -81,7 +81,7 @@ var configFile in Directory.EnumerateFiles(
{
MatchCasing = MatchCasing.CaseInsensitive,
RecurseSubdirectories = true,
AttributesToSkip = FileAttributes.System
AttributesToSkip = FileAttributes.System,
}
)
)
Expand Down Expand Up @@ -143,12 +143,7 @@ private static RootDir CreateFromFile(FileInfo path)

var dir = new RootDir(path.Directory!);

dir.AddDotnetTools(
Domain.DotnetTools.Create(
path.FullName,
projects
)
);
dir.AddDotnetTools(Domain.DotnetTools.Create(path.FullName, projects));

return dir;
}
Expand All @@ -170,12 +165,7 @@ static void AddDotnetToolsFromCsproj(RootDir dir)
continue;
}

dir.AddDotnetTools(
Domain.DotnetTools.Create(
configPath,
dir.Csprojs ?? []
)
);
dir.AddDotnetTools(Domain.DotnetTools.Create(configPath, dir.Csprojs ?? []));
}
}
}
Expand All @@ -189,16 +179,18 @@ private static HashSet<Csproj> GetProjectsRecursiveFromParent(DirectoryInfo path
if (isInConfigFolder)
{
projects.AddRange(
Directory.EnumerateFiles(
path.Parent!.FullName,
"*.csproj",
new EnumerationOptions
{
MatchCasing = MatchCasing.CaseInsensitive,
RecurseSubdirectories = true
}
)
.Select(Csproj.Create));
Directory
.EnumerateFiles(
path.Parent!.FullName,
"*.csproj",
new EnumerationOptions
{
MatchCasing = MatchCasing.CaseInsensitive,
RecurseSubdirectories = true,
}
)
.Select(Csproj.Create)
);
}

return projects;
Expand All @@ -217,7 +209,10 @@ private static IEnumerable<Csproj> GetProjectsFromSolution(FileInfo solution) =>
.Where(x => x.Exists)
.Select(x => Csproj.Create(x.FullName));

private static IEnumerable<DotnetTools> GetDotnetToolsConfigFromSolution(FileInfo solution, IEnumerable<Csproj> csprojs) =>
private static IEnumerable<DotnetTools> GetDotnetToolsConfigFromSolution(
FileInfo solution,
IEnumerable<Csproj> csprojs
) =>
Regex
.Matches(
File.ReadAllText(solution.FullName),
Expand Down
12 changes: 6 additions & 6 deletions src/UpdatR/Formatters/MarkdownFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ public static string GenerateDescription(Summary summary)
sb.Append(summary.UpdatedPackagesCount)
.Append(" package(s) were updated in ")
.Append(
summary.UpdatedPackages
.SelectMany(x => x.Updates.Select(y => y.Project))
summary
.UpdatedPackages.SelectMany(x => x.Updates.Select(y => y.Project))
.Distinct()
.Count()
)
Expand Down Expand Up @@ -263,13 +263,13 @@ IEnumerable<UpdatedPackage> updatedPackages
continue;
}

var padRightProject = packages.Updates
.Select(x => x.Project.Length)
var padRightProject = packages
.Updates.Select(x => x.Project.Length)
.OrderByDescending(x => x)
.First();

var padRightFrom = packages.Updates
.Select(x => x.From.ToString().Length)
var padRightFrom = packages
.Updates.Select(x => x.From.ToString().Length)
.OrderByDescending(x => x)
.First();

Expand Down
8 changes: 4 additions & 4 deletions src/UpdatR/Formatters/TextFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -219,13 +219,13 @@ IEnumerable<UpdatedPackage> updatedPackages
continue;
}

var padRightProject = packages.Updates
.Select(x => x.Project.Length)
var padRightProject = packages
.Updates.Select(x => x.Project.Length)
.OrderByDescending(x => x)
.First();

var padRightFrom = packages.Updates
.Select(x => x.From.ToString().Length)
var padRightFrom = packages
.Updates.Select(x => x.From.ToString().Length)
.OrderByDescending(x => x)
.First();

Expand Down
6 changes: 3 additions & 3 deletions src/UpdatR/Internals/Result.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ internal bool TryAddProject(ProjectWithPackages project)
project = project with
{
Path = Path.GetRelativePath(_rootPath, project.Path)
.Replace(Path.DirectorySeparatorChar, '\\')
.Replace(Path.DirectorySeparatorChar, '\\'),
};

foreach (var unknown in project.UnknownPackages)
Expand Down Expand Up @@ -143,7 +143,7 @@ internal sealed class DeprecatedPackage(
string packageId,
NuGetVersion version,
PackageDeprecationMetadata deprecationMetadata
)
)
{
public string PackageId { get; } = packageId;
public NuGetVersion Version { get; } = version;
Expand All @@ -154,7 +154,7 @@ internal sealed class VulnerablePackage(
string packageId,
NuGetVersion version,
IEnumerable<PackageVulnerabilityMetadata> vulnerabilities
)
)
{
public string PackageId { get; } = packageId;
public NuGetVersion Version { get; } = version;
Expand Down
Loading

0 comments on commit 8e61863

Please sign in to comment.