-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract version logic to main program + add tests.
- Loading branch information
Showing
9 changed files
with
373 additions
and
121 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,6 @@ | ||
using DependencyUpdated.Core.Config; | ||
using DependencyUpdated.Core.Interfaces; | ||
using DependencyUpdated.Core.Models; | ||
using DependencyUpdated.Core.Models.Enums; | ||
using Microsoft.Extensions.Caching.Memory; | ||
using NuGet.Common; | ||
using NuGet.Configuration; | ||
|
@@ -34,51 +33,9 @@ public IReadOnlyCollection<UpdateResult> HandleProjectUpdate(IReadOnlyCollection | |
return UpdateCsProj(fullPath, dependenciesToUpdate); | ||
} | ||
|
||
public async Task<ICollection<DependencyDetails>> ExtractAllPackagesThatNeedToBeUpdated(IReadOnlyCollection<string> fullPath, Project projectConfiguration) | ||
public async Task<ICollection<DependencyDetails>> ExtractAllPackages(IReadOnlyCollection<string> fullPath) | ||
{ | ||
var nugets = ParseCsproj(fullPath); | ||
|
||
var returnList = new List<DependencyDetails>(); | ||
foreach (var nuget in nugets) | ||
{ | ||
logger.Verbose("Processing {PackageName}:{PackageVersion}", nuget.Name, nuget.Version); | ||
var latestVersion = await GetLatestVersion(nuget, projectConfiguration); | ||
if (latestVersion is null) | ||
{ | ||
logger.Warning("{PacakgeName} unable to find in sources", nuget.Name); | ||
continue; | ||
} | ||
|
||
logger.Information("{PacakgeName} new version {Version} available", nuget.Name, latestVersion); | ||
returnList.Add(nuget with { Version = latestVersion.Version }); | ||
} | ||
|
||
return returnList; | ||
} | ||
|
||
private static NuGetVersion? GetMaxVersion(IEnumerable<NuGetVersion> versions, Version currentVersion, | ||
Project projectConfiguration) | ||
{ | ||
var baseQuery = versions.Where(x => !x.IsPrerelease); | ||
if (projectConfiguration.Version == VersionUpdateType.Major) | ||
{ | ||
return baseQuery.Max(); | ||
} | ||
|
||
if (projectConfiguration.Version == VersionUpdateType.Minor) | ||
{ | ||
return baseQuery.Where(x => | ||
x.Version.Major == currentVersion.Major && x.Version.Minor > currentVersion.Minor).Max(); | ||
} | ||
|
||
if (projectConfiguration.Version == VersionUpdateType.Patch) | ||
{ | ||
return baseQuery.Where(x => | ||
x.Version.Major == currentVersion.Major && x.Version.Minor == currentVersion.Minor && | ||
x.Version.Build > currentVersion.Build).Max(); | ||
} | ||
|
||
throw new NotSupportedException($"Version configuration {projectConfiguration.Version} is not supported"); | ||
return await Task.FromResult(ParseCsproj(fullPath)); | ||
} | ||
|
||
private static HashSet<DependencyDetails> ParseCsproj(IReadOnlyCollection<string> paths) | ||
|
@@ -116,10 +73,12 @@ private static HashSet<DependencyDetails> ParseCsproj(string path) | |
return nugets; | ||
} | ||
|
||
private async Task<NuGetVersion?> GetLatestVersion(DependencyDetails package, Project projectConfiguration) | ||
public async Task<IReadOnlyCollection<DependencyDetails>> GetVersions(DependencyDetails package, | ||
Check failure on line 76 in src/DependencyUpdated.Projects.DotNet/DotNetUpdater.cs GitHub Actions / build
Check failure on line 76 in src/DependencyUpdated.Projects.DotNet/DotNetUpdater.cs GitHub Actions / build
|
||
Project projectConfiguration) | ||
{ | ||
var existsInCache = memoryCache.TryGetValue<NuGetVersion?>(package.Name, out var cachedVersion); | ||
if (existsInCache) | ||
var existsInCache = | ||
memoryCache.TryGetValue<IReadOnlyCollection<DependencyDetails>>(package.Name, out var cachedVersion); | ||
if (existsInCache && cachedVersion is not null) | ||
{ | ||
return cachedVersion; | ||
} | ||
|
@@ -138,7 +97,7 @@ private static HashSet<DependencyDetails> ParseCsproj(string path) | |
packageSources.Add(new PackageSource(projectConfigurationPath)); | ||
continue; | ||
} | ||
|
||
var setting = Settings.LoadSpecificSettings(Path.GetDirectoryName(projectConfigurationPath)!, | ||
Path.GetFileName(projectConfigurationPath)); | ||
var packageSourceProvider = new PackageSourceProvider(setting); | ||
|
@@ -150,7 +109,7 @@ private static HashSet<DependencyDetails> ParseCsproj(string path) | |
var sourceRepositoryProvider = | ||
new SourceRepositoryProvider(new PackageSourceProvider(NullSettings.Instance, packageSources), providers); | ||
var repositories = sourceRepositoryProvider.GetRepositories(); | ||
var version = default(NuGetVersion?); | ||
var allVersions = new List<NuGetVersion>(); | ||
foreach (var repository in repositories) | ||
{ | ||
var findPackageByIdResource = await repository.GetResourceAsync<FindPackageByIdResource>(); | ||
|
@@ -159,15 +118,15 @@ private static HashSet<DependencyDetails> ParseCsproj(string path) | |
new SourceCacheContext(), | ||
NullLogger.Instance, | ||
CancellationToken.None); | ||
var maxVersion = GetMaxVersion(versions, package.Version, projectConfiguration); | ||
if (version is null || (maxVersion is not null && maxVersion >= version)) | ||
{ | ||
version = maxVersion; | ||
} | ||
allVersions.AddRange(versions.Where(x => !x.IsPrerelease)); | ||
} | ||
|
||
memoryCache.Set(package.Name, version); | ||
return version; | ||
var result = allVersions | ||
.DistinctBy(x => x.Version) | ||
.Select(x => package with { Version = x.Version }) | ||
.ToHashSet(); | ||
memoryCache.Set(package.Name, result); | ||
return result; | ||
} | ||
|
||
private IReadOnlyCollection<UpdateResult> UpdateCsProj(IReadOnlyCollection<string> fullPaths, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
tests/DependencyUpdated.Core.UnitTests/DependencyUpdated.Core.UnitTests.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
|
||
<IsPackable>false</IsPackable> | ||
<IsTestProject>true</IsTestProject> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\src\DependencyUpdated.Core\DependencyUpdated.Core.csproj" /> | ||
</ItemGroup> | ||
</Project> |
Oops, something went wrong.