-
Couldn't load subscription status.
- Fork 660
Replace ArgumentParser with Spectre.Console.Cli package #4610
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
Draft
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
3c06f8e
Initial plan
Copilot db6b15b
Replace ArgumentParser with SpectreArgumentParser using Spectre.Conso...
Copilot 8863c9f
Complete SpectreArgumentParser implementation with full backward comp...
Copilot 242709c
Replace ArgumentParser delegation with full Spectre.Console.Cli imple...
Copilot 42b74b1
Enhance SpectreArgumentParser with comprehensive CLI argument support...
Copilot 0c05d92
Remove legacy forward slash syntax support from SpectreArgumentParser...
Copilot d04a748
Use -v for verbosity instead of show variable to follow standard CLI ...
Copilot 22e86b0
Update Spectre.Console.Cli to latest version and split classes into s...
Copilot 2661715
Fix build errors: remove trailing whitespace, suppress obsolete warni...
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
232 changes: 232 additions & 0 deletions
src/GitVersion.App/ArgumentInterceptor.cs
This file contains hidden or 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,232 @@ | ||
| using System.IO.Abstractions; | ||
| using GitVersion.Agents; | ||
| using GitVersion.Extensions; | ||
| using GitVersion.FileSystemGlobbing; | ||
| using GitVersion.Helpers; | ||
| using GitVersion.Logging; | ||
| using GitVersion.OutputVariables; | ||
|
Check warning on line 7 in src/GitVersion.App/ArgumentInterceptor.cs GitHub Actions / DotNet Format
Check warning on line 7 in src/GitVersion.App/ArgumentInterceptor.cs GitHub Actions / DotNet Format
|
||
| using Spectre.Console.Cli; | ||
|
|
||
| namespace GitVersion; | ||
|
|
||
| /// <summary> | ||
| /// Interceptor to capture parsed arguments | ||
| /// </summary> | ||
| internal class ArgumentInterceptor : ICommandInterceptor | ||
| { | ||
| private readonly ParseResultStorage storage; | ||
| private readonly IEnvironment environment; | ||
| private readonly IFileSystem fileSystem; | ||
| private readonly ICurrentBuildAgent buildAgent; | ||
| private readonly IConsole console; | ||
| private readonly IGlobbingResolver globbingResolver; | ||
|
|
||
| public ArgumentInterceptor(ParseResultStorage storage, IEnvironment environment, IFileSystem fileSystem, ICurrentBuildAgent buildAgent, IConsole console, IGlobbingResolver globbingResolver) | ||
| { | ||
| this.storage = storage; | ||
| this.environment = environment; | ||
| this.fileSystem = fileSystem; | ||
| this.buildAgent = buildAgent; | ||
| this.console = console; | ||
| this.globbingResolver = globbingResolver; | ||
| } | ||
|
|
||
| public void Intercept(CommandContext context, CommandSettings settings) | ||
| { | ||
| if (settings is GitVersionSettings gitVersionSettings) | ||
| { | ||
| var arguments = ConvertToArguments(gitVersionSettings); | ||
| AddAuthentication(arguments); | ||
| ValidateAndProcessArguments(arguments); | ||
| this.storage.SetResult(arguments); | ||
| } | ||
| } | ||
|
|
||
| private void AddAuthentication(Arguments arguments) | ||
| { | ||
| var username = this.environment.GetEnvironmentVariable("GITVERSION_REMOTE_USERNAME"); | ||
| if (!username.IsNullOrWhiteSpace()) | ||
| { | ||
| arguments.Authentication.Username = username; | ||
| } | ||
|
|
||
| var password = this.environment.GetEnvironmentVariable("GITVERSION_REMOTE_PASSWORD"); | ||
| if (!password.IsNullOrWhiteSpace()) | ||
| { | ||
| arguments.Authentication.Password = password; | ||
| } | ||
| } | ||
|
|
||
| private void ValidateAndProcessArguments(Arguments arguments) | ||
| { | ||
| // Apply default output if none specified | ||
| if (arguments.Output.Count == 0) | ||
| { | ||
| arguments.Output.Add(OutputType.Json); | ||
| } | ||
|
|
||
| // Set default output file if file output is specified | ||
| if (arguments.Output.Contains(OutputType.File) && arguments.OutputFile == null) | ||
| { | ||
| arguments.OutputFile = "GitVersion.json"; | ||
| } | ||
|
|
||
| // Apply build agent settings | ||
| arguments.NoFetch = arguments.NoFetch || this.buildAgent.PreventFetch(); | ||
|
|
||
| // Validate configuration file | ||
| ValidateConfigurationFile(arguments); | ||
|
|
||
| // Process assembly info files | ||
| if (!arguments.EnsureAssemblyInfo) | ||
| { | ||
| arguments.UpdateAssemblyInfoFileName = ResolveFiles(arguments.TargetPath ?? SysEnv.CurrentDirectory, arguments.UpdateAssemblyInfoFileName).ToHashSet(); | ||
| } | ||
| } | ||
|
|
||
| private void ValidateConfigurationFile(Arguments arguments) | ||
| { | ||
| if (arguments.ConfigurationFile.IsNullOrWhiteSpace()) return; | ||
|
|
||
| if (FileSystemHelper.Path.IsPathRooted(arguments.ConfigurationFile)) | ||
| { | ||
| if (!this.fileSystem.File.Exists(arguments.ConfigurationFile)) | ||
| throw new WarningException($"Could not find config file at '{arguments.ConfigurationFile}'"); | ||
| arguments.ConfigurationFile = FileSystemHelper.Path.GetFullPath(arguments.ConfigurationFile); | ||
| } | ||
| else | ||
| { | ||
| var configFilePath = FileSystemHelper.Path.GetFullPath(FileSystemHelper.Path.Combine(arguments.TargetPath, arguments.ConfigurationFile)); | ||
| if (!this.fileSystem.File.Exists(configFilePath)) | ||
| throw new WarningException($"Could not find config file at '{configFilePath}'"); | ||
| arguments.ConfigurationFile = configFilePath; | ||
| } | ||
| } | ||
|
|
||
| private IEnumerable<string> ResolveFiles(string workingDirectory, ISet<string>? assemblyInfoFiles) | ||
| { | ||
| if (assemblyInfoFiles == null || assemblyInfoFiles.Count == 0) | ||
| { | ||
| return []; | ||
| } | ||
|
|
||
| var stringList = new List<string>(); | ||
|
|
||
| foreach (var filePattern in assemblyInfoFiles) | ||
| { | ||
| if (FileSystemHelper.Path.IsPathRooted(filePattern)) | ||
| { | ||
| stringList.Add(filePattern); | ||
| } | ||
| else | ||
| { | ||
| var searchRoot = FileSystemHelper.Path.GetFullPath(workingDirectory); | ||
| var matchingFiles = this.globbingResolver.Resolve(searchRoot, filePattern); | ||
| stringList.AddRange(matchingFiles); | ||
| } | ||
| } | ||
|
|
||
| return stringList; | ||
| } | ||
|
|
||
| private static Arguments ConvertToArguments(GitVersionSettings settings) | ||
| { | ||
| var arguments = new Arguments(); | ||
|
|
||
| // Set target path - prioritize explicit targetpath option over positional argument | ||
| arguments.TargetPath = settings.TargetPathOption?.TrimEnd('/', '\\') | ||
| ?? settings.TargetPath?.TrimEnd('/', '\\') | ||
| ?? SysEnv.CurrentDirectory; | ||
|
|
||
| // Configuration options | ||
| arguments.ConfigurationFile = settings.ConfigurationFile; | ||
| arguments.ShowConfiguration = settings.ShowConfiguration; | ||
|
|
||
| // Handle override configuration | ||
| if (settings.OverrideConfiguration != null && settings.OverrideConfiguration.Any()) | ||
| { | ||
| var parser = new OverrideConfigurationOptionParser(); | ||
|
|
||
| foreach (var kvp in settings.OverrideConfiguration) | ||
| { | ||
| // Validate the key format - Spectre.Console.Cli should have already parsed key=value correctly | ||
| // but we still need to validate against supported properties | ||
| var keyValueOption = $"{kvp.Key}={kvp.Value}"; | ||
|
|
||
| var optionKey = kvp.Key.ToLowerInvariant(); | ||
| if (!OverrideConfigurationOptionParser.SupportedProperties.Contains(optionKey)) | ||
| { | ||
| throw new WarningException($"Could not parse --override-config option: {keyValueOption}. Unsupported 'key'."); | ||
| } | ||
|
|
||
| parser.SetValue(optionKey, kvp.Value); | ||
| } | ||
|
|
||
| arguments.OverrideConfiguration = parser.GetOverrideConfiguration(); | ||
| } | ||
| else | ||
| { | ||
| arguments.OverrideConfiguration = new Dictionary<object, object?>(); | ||
| } | ||
|
|
||
| // Output options | ||
| if (settings.Output != null && settings.Output.Any()) | ||
| { | ||
| foreach (var output in settings.Output) | ||
| { | ||
| if (Enum.TryParse<OutputType>(output, true, out var outputType)) | ||
| { | ||
| arguments.Output.Add(outputType); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| arguments.OutputFile = settings.OutputFile; | ||
| arguments.Format = settings.Format; | ||
| arguments.ShowVariable = settings.ShowVariable; | ||
|
|
||
| // Repository options | ||
| arguments.TargetUrl = settings.Url; | ||
| arguments.TargetBranch = settings.Branch; | ||
| arguments.CommitId = settings.Commit; | ||
| arguments.ClonePath = settings.DynamicRepoLocation; | ||
|
|
||
| // Authentication | ||
| if (!string.IsNullOrWhiteSpace(settings.Username)) | ||
| { | ||
| arguments.Authentication.Username = settings.Username; | ||
| } | ||
| if (!string.IsNullOrWhiteSpace(settings.Password)) | ||
| { | ||
| arguments.Authentication.Password = settings.Password; | ||
| } | ||
|
|
||
| // Behavioral flags | ||
| arguments.NoFetch = settings.NoFetch; | ||
| arguments.NoCache = settings.NoCache; | ||
| arguments.NoNormalize = settings.NoNormalize; | ||
| arguments.AllowShallow = settings.AllowShallow; | ||
| arguments.Diag = settings.Diag; | ||
|
|
||
| // Assembly info options | ||
| arguments.UpdateAssemblyInfo = settings.UpdateAssemblyInfo; | ||
| arguments.EnsureAssemblyInfo = settings.EnsureAssemblyInfo; | ||
| arguments.UpdateProjectFiles = settings.UpdateProjectFiles; | ||
| arguments.UpdateWixVersionFile = settings.UpdateWixVersionFile; | ||
|
|
||
| // Handle assembly info file names | ||
| if (settings.UpdateAssemblyInfoFileName != null && settings.UpdateAssemblyInfoFileName.Any()) | ||
| { | ||
| arguments.UpdateAssemblyInfoFileName = settings.UpdateAssemblyInfoFileName.ToHashSet(); | ||
| } | ||
|
|
||
| // Logging | ||
| arguments.LogFilePath = settings.LogFilePath; | ||
| if (Enum.TryParse<Verbosity>(settings.Verbosity, true, out var verbosity)) | ||
| { | ||
| arguments.Verbosity = verbosity; | ||
| } | ||
|
|
||
| return arguments; | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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
13 changes: 13 additions & 0 deletions
src/GitVersion.App/GitVersionCommand.cs
This file contains hidden or 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,13 @@ | ||
| using System.ComponentModel; | ||
| using Spectre.Console.Cli; | ||
|
|
||
| namespace GitVersion; | ||
|
|
||
| /// <summary> | ||
| /// Main GitVersion command with POSIX compliant options | ||
| /// </summary> | ||
| [Description("Generate version information based on Git repository")] | ||
| internal class GitVersionCommand : Command<GitVersionSettings> | ||
| { | ||
| public override int Execute(CommandContext context, GitVersionSettings settings) => 0; | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.