|
| 1 | +// <copyright file="ProgramCommandHandler.cs" company="Rami Abughazaleh"> |
| 2 | +// Copyright (c) Rami Abughazaleh. All rights reserved. |
| 3 | +// </copyright> |
| 4 | + |
| 5 | +namespace PackageReferenceVersionToAttributeTool |
| 6 | +{ |
| 7 | + using System; |
| 8 | + using System.Collections.Generic; |
| 9 | + using System.Linq; |
| 10 | + using System.Reflection; |
| 11 | + using System.Threading.Tasks; |
| 12 | + using Microsoft.Extensions.DependencyInjection; |
| 13 | + using Microsoft.Extensions.Logging; |
| 14 | + using PackageReferenceVersionToAttribute; |
| 15 | + using SlnParser; |
| 16 | + using SlnParser.Contracts; |
| 17 | + |
| 18 | + /// <summary> |
| 19 | + /// Program command handler. |
| 20 | + /// </summary> |
| 21 | + internal class ProgramCommandHandler |
| 22 | + { |
| 23 | + /// <summary> |
| 24 | + /// Executes the command logic asynchronously based on the provided command line options. |
| 25 | + /// </summary> |
| 26 | + /// <param name="options">An instance of <see cref="ProgramCommandLineOptions"/> containing the parsed options from the command line.</param> |
| 27 | + /// <returns>A task representing the asynchronous operation.</returns> |
| 28 | + public static async Task HandleAsync(ProgramCommandLineOptions options) |
| 29 | + { |
| 30 | + if (options.Version) |
| 31 | + { |
| 32 | + var version = Assembly.GetExecutingAssembly().GetName().Version; |
| 33 | + Console.WriteLine($"Version: {version}"); |
| 34 | + return; |
| 35 | + } |
| 36 | + |
| 37 | + foreach (var input in options.Inputs) |
| 38 | + { |
| 39 | + await ConvertPackageReferencesAsync(input, options); |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + private static async Task ConvertPackageReferencesAsync( |
| 44 | + string input, ProjectConverterOptions options) |
| 45 | + { |
| 46 | + FilePatternMatcher filePatternMatcher = new(); |
| 47 | + |
| 48 | + // get matching csproj and sln files |
| 49 | + List<string> matchingFiles = filePatternMatcher.GetMatchingFiles(input) |
| 50 | + .Where(x => x.EndsWith(".csproj", StringComparison.OrdinalIgnoreCase) |
| 51 | + || x.EndsWith(".sln", StringComparison.OrdinalIgnoreCase)) |
| 52 | + .ToList(); |
| 53 | + |
| 54 | + // parse sln files to get only csproj files |
| 55 | + List<string> projectFiles = GetCsprojFiles(matchingFiles); |
| 56 | + if (projectFiles.Count == 0) |
| 57 | + { |
| 58 | + Console.WriteLine($"No matching project files found for pattern: {input}"); |
| 59 | + return; |
| 60 | + } |
| 61 | + |
| 62 | + if (options.Backup) |
| 63 | + { |
| 64 | + Console.WriteLine("Backup option is enabled."); |
| 65 | + } |
| 66 | + |
| 67 | + if (options.Force) |
| 68 | + { |
| 69 | + Console.WriteLine("Force option is enabled."); |
| 70 | + } |
| 71 | + |
| 72 | + if (options.DryRun) |
| 73 | + { |
| 74 | + Console.WriteLine("Dry run mode is enabled. No changes will be made."); |
| 75 | + } |
| 76 | + |
| 77 | + using var serviceProvider = new ServiceCollection() |
| 78 | + .AddSingleton(Microsoft.Extensions.Options.Options.Create(options)) |
| 79 | + .AddLogging(configure => configure.AddConsole()) |
| 80 | + .AddSingleton<ProjectConverter>() |
| 81 | + .AddSingleton<IFileService, FileService>() |
| 82 | + .AddSingleton<ISourceControlService, NullSourceControlService>() |
| 83 | + .BuildServiceProvider(); |
| 84 | + |
| 85 | + var projectConverter = serviceProvider.GetRequiredService<ProjectConverter>(); |
| 86 | + |
| 87 | + await projectConverter.ConvertAsync(projectFiles); |
| 88 | + } |
| 89 | + |
| 90 | + private static List<string> GetCsprojFiles(List<string> files) |
| 91 | + { |
| 92 | + var csprojFiles = new List<string>(); |
| 93 | + |
| 94 | + foreach (var file in files) |
| 95 | + { |
| 96 | + if (file.EndsWith(".csproj", StringComparison.OrdinalIgnoreCase)) |
| 97 | + { |
| 98 | + csprojFiles.Add(file); |
| 99 | + } |
| 100 | + else if (file.EndsWith(".sln", StringComparison.OrdinalIgnoreCase)) |
| 101 | + { |
| 102 | + csprojFiles.AddRange(GetCsprojFiles(file)); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + return csprojFiles; |
| 107 | + } |
| 108 | + |
| 109 | + private static IEnumerable<string> GetCsprojFiles(string solutionFilePath) |
| 110 | + { |
| 111 | + SolutionParser solutionParser = new SolutionParser(); |
| 112 | + ISolution solution = solutionParser.Parse(solutionFilePath); |
| 113 | + |
| 114 | + return solution.AllProjects |
| 115 | + .OfType<SolutionProject>() |
| 116 | + .Where(x => x.File.FullName.EndsWith(".csproj")) |
| 117 | + .Select(x => x.File.FullName); |
| 118 | + } |
| 119 | + } |
| 120 | +} |
0 commit comments