Skip to content

Commit

Permalink
Extract Updater login into separate class.
Browse files Browse the repository at this point in the history
Move various enums and interfaces to organized subdirectories under Models and Interfaces namespaces. Extract the update logic from the Program class into a new, dedicated Updater class to improve code modularity and maintainability. Also, update affected import statements accordingly.
  • Loading branch information
torbacz committed Sep 6, 2024
1 parent 2720011 commit 724c322
Show file tree
Hide file tree
Showing 17 changed files with 109 additions and 82 deletions.
1 change: 1 addition & 0 deletions src/DependencyUpdated.Core/Config/Project.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using DependencyUpdated.Core.Models.Enums;
using System.ComponentModel.DataAnnotations;

namespace DependencyUpdated.Core.Config;
Expand Down
1 change: 1 addition & 0 deletions src/DependencyUpdated.Core/Config/UpdaterConfig.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using DependencyUpdated.Core.Models.Enums;
using System.ComponentModel.DataAnnotations;

namespace DependencyUpdated.Core.Config;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using DependencyUpdated.Core.Config;
using DependencyUpdated.Core.Models;

namespace DependencyUpdated.Core;
namespace DependencyUpdated.Core.Interfaces;

public interface IProjectUpdater
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace DependencyUpdated.Core;
using DependencyUpdated.Core.Models;

namespace DependencyUpdated.Core.Interfaces;

public interface IRepositoryProvider
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
namespace DependencyUpdated.Core;
namespace DependencyUpdated.Core.Models;

public record DependencyDetails(string Name, Version Version);
6 changes: 6 additions & 0 deletions src/DependencyUpdated.Core/Models/Enums/ProjectType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace DependencyUpdated.Core.Models.Enums;

public enum ProjectType
{
DotNet = 1,
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace DependencyUpdated.Core;
namespace DependencyUpdated.Core.Models.Enums;

public enum RepositoryType
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace DependencyUpdated.Core;
namespace DependencyUpdated.Core.Models.Enums;

public enum VersionUpdateType
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
namespace DependencyUpdated.Core;
namespace DependencyUpdated.Core.Models;

public record UpdateResult(string PackageName, string OldVersion, string NewVersion);
6 changes: 0 additions & 6 deletions src/DependencyUpdated.Core/ProjectType.cs

This file was deleted.

76 changes: 76 additions & 0 deletions src/DependencyUpdated.Core/Updater.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
using DependencyUpdated.Core.Config;
using DependencyUpdated.Core.Interfaces;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using System.IO.Enumeration;

namespace DependencyUpdated.Core;

public sealed class Updater(IServiceProvider serviceProvider, IOptions<UpdaterConfig> config)
{
public async Task DoUpdate()
{
var repositoryProvider =
serviceProvider.GetRequiredKeyedService<IRepositoryProvider>(config.Value.RepositoryType);
var repositoryPath = Environment.CurrentDirectory;
repositoryProvider.SwitchToDefaultBranch(repositoryPath);

foreach (var configEntry in config.Value.Projects)
{
var updater = serviceProvider.GetRequiredKeyedService<IProjectUpdater>(configEntry.Type);

foreach (var directory in configEntry.Directories)
{
if (!Path.Exists(directory))
{
throw new FileNotFoundException("Search path not found", directory);
}

var projectFiles = updater.GetAllProjectFiles(directory);
var allDependenciesToUpdate =
await updater.ExtractAllPackagesThatNeedToBeUpdated(projectFiles, configEntry);

if (allDependenciesToUpdate.Count == 0)
{
continue;
}

var uniqueListOfDependencies = allDependenciesToUpdate.DistinctBy(x => x.Name).ToList();
var projectName = ResolveProjectName(configEntry, directory);
foreach (var group in configEntry.Groups)
{
var matchesForGroup = uniqueListOfDependencies
.Where(x => FileSystemName.MatchesSimpleExpression(group, x.Name)).ToArray();
if (matchesForGroup.Length == 0)
{
continue;
}

uniqueListOfDependencies.RemoveAll(x => FileSystemName.MatchesSimpleExpression(group, x.Name));
repositoryProvider.SwitchToUpdateBranch(repositoryPath, projectName, group);

var allUpdates = updater.HandleProjectUpdate(projectFiles, matchesForGroup);
if (allUpdates.Count == 0)
{
continue;
}

repositoryProvider.CommitChanges(repositoryPath, projectName, group);
await repositoryProvider.SubmitPullRequest(allUpdates.DistinctBy(x => x.PackageName).ToArray(),
projectName, group);
repositoryProvider.SwitchToDefaultBranch(repositoryPath);
}
}
}
}

private static string ResolveProjectName(Project project, string directory)
{
if (!project.EachDirectoryAsSeparate)
{
return project.Name;
}

return Path.GetFileName(directory);
}
}
3 changes: 2 additions & 1 deletion src/DependencyUpdated.Projects.DotNet/ConfigureServices.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using DependencyUpdated.Core;
using DependencyUpdated.Core.Interfaces;
using DependencyUpdated.Core.Models.Enums;
using Microsoft.Extensions.DependencyInjection;

namespace DependencyUpdated.Projects.DotNet;
Expand Down
6 changes: 4 additions & 2 deletions src/DependencyUpdated.Projects.DotNet/DotNetUpdater.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using DependencyUpdated.Core;
using DependencyUpdated.Core.Config;
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;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using DependencyUpdated.Core;
using DependencyUpdated.Core.Config;
using DependencyUpdated.Core.Interfaces;
using DependencyUpdated.Core.Models;
using DependencyUpdated.Repositories.AzureDevOps.Dto;
using LibGit2Sharp;
using LibGit2Sharp.Handlers;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
using DependencyUpdated.Core;
using DependencyUpdated.Core.Interfaces;
using DependencyUpdated.Core.Models.Enums;
using Microsoft.Extensions.DependencyInjection;

namespace DependencyUpdated.Repositories.AzureDevOps;

public static class ConfigureServices
{
public static IServiceCollection RegisterGithub(this IServiceCollection serviceCollection)
public static IServiceCollection RegisterAzureDevOps(this IServiceCollection serviceCollection)
{
serviceCollection.AddKeyedSingleton<IRepositoryProvider, AzureDevOps>(RepositoryType.AzureDevOps);

Expand Down
66 changes: 3 additions & 63 deletions src/DependencyUpdated/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
using Microsoft.Extensions.Options;
using Serilog;
using System.ComponentModel.DataAnnotations;
using System.IO.Enumeration;
using ILogger = Serilog.ILogger;

namespace DependencyUpdated;
Expand Down Expand Up @@ -37,68 +36,9 @@ private static async Task RunApplication(Options options)
throw new OptionsValidationException(nameof(UpdaterConfig), typeof(UpdaterConfig),
validationResult.Where(x => !string.IsNullOrEmpty(x.ErrorMessage)).Select(x => x.ErrorMessage!));
}

var repositoryProvider =
_serviceProvider.GetRequiredKeyedService<IRepositoryProvider>(config.Value.RepositoryType);
var repositoryPath = Environment.CurrentDirectory;
repositoryProvider.SwitchToDefaultBranch(repositoryPath);

foreach (var configEntry in config.Value.Projects)
{
var updater = _serviceProvider.GetRequiredKeyedService<IProjectUpdater>(configEntry.Type);

foreach (var directory in configEntry.Directories)
{
if (!Path.Exists(directory))
{
throw new FileNotFoundException("Search path not found", directory);
}

var projectFiles = updater.GetAllProjectFiles(directory);
var allDependenciesToUpdate =
await updater.ExtractAllPackagesThatNeedToBeUpdated(projectFiles, configEntry);

if (allDependenciesToUpdate.Count == 0)
{
continue;
}

var uniqueListOfDependencies = allDependenciesToUpdate.DistinctBy(x => x.Name).ToList();
var projectName = ResolveProjectName(configEntry, directory);
foreach (var group in configEntry.Groups)
{
var matchesForGroup = uniqueListOfDependencies
.Where(x => FileSystemName.MatchesSimpleExpression(group, x.Name)).ToArray();
if (matchesForGroup.Length == 0)
{
continue;
}

uniqueListOfDependencies.RemoveAll(x => FileSystemName.MatchesSimpleExpression(group, x.Name));
repositoryProvider.SwitchToUpdateBranch(repositoryPath, projectName, group);

var allUpdates = updater.HandleProjectUpdate(projectFiles, matchesForGroup);
if (allUpdates.Count == 0)
{
continue;
}

repositoryProvider.CommitChanges(repositoryPath, projectName, group);
await repositoryProvider.SubmitPullRequest(allUpdates.DistinctBy(x => x.PackageName).ToArray(), projectName, group);
repositoryProvider.SwitchToDefaultBranch(repositoryPath);
}
}
}
}

private static string ResolveProjectName(Project project, string directory)
{
if (!project.EachDirectoryAsSeparate)
{
return project.Name;
}

return Path.GetFileName(directory);
var updater = ActivatorUtilities.CreateInstance<Updater>(_serviceProvider);
await updater.DoUpdate();
}

private static void Configure(Options appOptions)
Expand All @@ -122,7 +62,7 @@ private static void ConfigureServices()
.AddSingleton(TimeProvider.System)
.AddMemoryCache()
.RegisterDotNetServices()
.RegisterGithub()
.RegisterAzureDevOps()
.AddOptions<UpdaterConfig>().Bind(_configuration.GetSection("UpdaterConfig"));

_serviceProvider = services.BuildServiceProvider();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using DependencyUpdated.Core;
using DependencyUpdated.Core.Config;
using DependencyUpdated.Core.Models;
using DependencyUpdated.Core.Models.Enums;
using FluentAssertions;
using FluentAssertions.Execution;
using Microsoft.Extensions.Caching.Memory;
Expand Down

0 comments on commit 724c322

Please sign in to comment.