|
| 1 | +// <copyright file="ProgramCommand.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.CommandLine; |
| 10 | + using System.CommandLine.NamingConventionBinder; |
| 11 | + using System.Reflection; |
| 12 | + using System.Threading.Tasks; |
| 13 | + using Microsoft.Extensions.DependencyInjection; |
| 14 | + using Microsoft.Extensions.Logging; |
| 15 | + using PackageReferenceVersionToAttribute; |
| 16 | + |
| 17 | + /// <summary> |
| 18 | + /// Program command. |
| 19 | + /// </summary> |
| 20 | + internal class ProgramCommand : RootCommand |
| 21 | + { |
| 22 | + /// <summary> |
| 23 | + /// Initializes a new instance of the <see cref="ProgramCommand"/> class. |
| 24 | + /// </summary> |
| 25 | + public ProgramCommand() |
| 26 | + : base("Converts PackageReference Version child elements to attributes in C# projects.") |
| 27 | + { |
| 28 | + var inputsArgument = new Argument<string[]>( |
| 29 | + name: "inputs", |
| 30 | + description: "The project files or wildcard patterns to convert.") |
| 31 | + { |
| 32 | + Arity = ArgumentArity.OneOrMore, |
| 33 | + }; |
| 34 | + |
| 35 | + var backupOption = new Option<bool>( |
| 36 | + aliases: ["--backup", "-b"], |
| 37 | + description: "Create a backup of the project files."); |
| 38 | + |
| 39 | + var forceOption = new Option<bool>( |
| 40 | + aliases: ["--force", "-f"], |
| 41 | + description: "Force conversion even if the project files are read-only."); |
| 42 | + |
| 43 | + var dryRunOption = new Option<bool>( |
| 44 | + aliases: ["--dry-run", "-d"], |
| 45 | + description: "Preview changes without making any modifications."); |
| 46 | + |
| 47 | + var versionOption = new Option<bool>("--version", "Display the version information."); |
| 48 | + |
| 49 | + this.Add(inputsArgument); |
| 50 | + this.Add(backupOption); |
| 51 | + this.Add(forceOption); |
| 52 | + this.Add(dryRunOption); |
| 53 | + |
| 54 | + // Add validation |
| 55 | + this.AddValidator(result => |
| 56 | + { |
| 57 | + bool backup = result.GetValueForOption(backupOption); |
| 58 | + bool force = result.GetValueForOption(forceOption); |
| 59 | + bool dryRun = result.GetValueForOption(dryRunOption); |
| 60 | + |
| 61 | + var options = new ProjectConverterOptions |
| 62 | + { |
| 63 | + Backup = backup, |
| 64 | + Force = force, |
| 65 | + DryRun = dryRun, |
| 66 | + }; |
| 67 | + |
| 68 | + var validator = new ProjectConverterOptionsValidator(); |
| 69 | + var validationResult = validator.Validate(nameof(ProjectConverterOptions), options); |
| 70 | + if (validationResult.Failed) |
| 71 | + { |
| 72 | + result.ErrorMessage = validationResult.FailureMessage; |
| 73 | + } |
| 74 | + }); |
| 75 | + |
| 76 | + // Set the handler for the command |
| 77 | + this.Handler = CommandHandler.Create<ProgramCommandLineOptions>(async (options) => |
| 78 | + { |
| 79 | + if (options.Version) |
| 80 | + { |
| 81 | + var version = Assembly.GetExecutingAssembly().GetName().Version; |
| 82 | + Console.WriteLine($"Version: {version}"); |
| 83 | + return; |
| 84 | + } |
| 85 | + |
| 86 | + foreach (var input in options.Inputs) |
| 87 | + { |
| 88 | + await ConvertPackageReferencesAsync(input, options); |
| 89 | + } |
| 90 | + }); |
| 91 | + } |
| 92 | + |
| 93 | + private static async Task ConvertPackageReferencesAsync( |
| 94 | + string input, ProjectConverterOptions options) |
| 95 | + { |
| 96 | + FilePatternMatcher filePatternMatcher = new(); |
| 97 | + List<string> matchingFiles = filePatternMatcher.GetMatchingFiles(input); |
| 98 | + if (matchingFiles.Count == 0) |
| 99 | + { |
| 100 | + Console.WriteLine($"No matching files found for pattern: {input}"); |
| 101 | + return; |
| 102 | + } |
| 103 | + |
| 104 | + if (options.Backup) |
| 105 | + { |
| 106 | + Console.WriteLine("Backup option is enabled."); |
| 107 | + } |
| 108 | + |
| 109 | + if (options.Force) |
| 110 | + { |
| 111 | + Console.WriteLine("Force option is enabled."); |
| 112 | + } |
| 113 | + |
| 114 | + if (options.DryRun) |
| 115 | + { |
| 116 | + Console.WriteLine("Dry run mode is enabled. No changes will be made."); |
| 117 | + } |
| 118 | + |
| 119 | + using var serviceProvider = new ServiceCollection() |
| 120 | + .AddSingleton(Microsoft.Extensions.Options.Options.Create(options)) |
| 121 | + .AddLogging(configure => configure.AddConsole()) |
| 122 | + .AddSingleton<ProjectConverter>() |
| 123 | + .AddSingleton<IFileService, FileService>() |
| 124 | + .AddSingleton<ISourceControlService, NullSourceControlService>() |
| 125 | + .BuildServiceProvider(); |
| 126 | + |
| 127 | + var projectConverter = serviceProvider.GetRequiredService<ProjectConverter>(); |
| 128 | + |
| 129 | + await projectConverter.ConvertAsync(matchingFiles); |
| 130 | + } |
| 131 | + } |
| 132 | +} |
0 commit comments