Skip to content
This repository has been archived by the owner on Sep 12, 2024. It is now read-only.

Commit

Permalink
Merge pull request #30 from devklick/feature/#29-GetCurrentGDManVersion
Browse files Browse the repository at this point in the history
Feature #29  - Get current gdman version
  • Loading branch information
devklick authored Aug 10, 2024
2 parents 815ed47 + e12eae6 commit 9016ea9
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 22 deletions.
7 changes: 3 additions & 4 deletions src/GDMan.Cli/Help/AppHelpInfo.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System.Data;

using GDMan.Cli.Version;

namespace GDMan.Cli.Help;

/// <summary>
Expand All @@ -10,13 +12,10 @@ namespace GDMan.Cli.Help;
public class AppHelpInfo : CliHelpInfo
{
public static string AppName => "GDMan";
public static string AppVersion =>
typeof(CliHelpInfo).Assembly.GetName().Version?.ToString()
?? throw new VersionNotFoundException("Unable to determine the application version");
public static string AppDescription => "Command line application for managing versions of Godot";
public List<(string FullName, string ShortName, string Description)> KnownCommands { get; set; } = [];
public override string ToString()
=> $"{AppName} (v{AppVersion})"
=> $"{AppName} {CliVersionInfo.ToString()}"
+ Environment.NewLine
+ AppDescription
+ Environment.NewLine + Environment.NewLine
Expand Down
1 change: 1 addition & 0 deletions src/GDMan.Cli/Help/CliHelpInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ public class CliHelpInfo
{
public static readonly string FullName = "--help";
public static readonly string ShortName = "-h";
public static readonly string[] Names = [FullName, ShortName];
}
1 change: 1 addition & 0 deletions src/GDMan.Cli/Parsing/ParseResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ public class ParseResult
{
public BaseOptions? Options { get; set; }
public bool RequiresHelp { get; set; }
public bool RequiresVersion { get; set; }
public CliHelpInfo? HelpInfo { get; set; }
public string HelpInfoString => HelpInfo?.ToString() ?? throw new NullReferenceException("Expected HelpInfo to have a value but found null");
public List<string> Errors { get; set; } = [];
Expand Down
52 changes: 34 additions & 18 deletions src/GDMan.Cli/Parsing/Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using GDMan.Cli.Attributes;
using GDMan.Cli.Help;
using GDMan.Cli.Options;
using GDMan.Cli.Version;
using GDMan.Core.Attributes;
using GDMan.Core.Extensions;
using GDMan.Core.Infrastructure;
Expand All @@ -14,6 +15,7 @@ namespace GDMan.Cli.Parsing;
public class Parser(ConsoleLogger logger)
{
private readonly ConsoleLogger _logger = logger;
private readonly string[] _globalOptions = [.. CliHelpInfo.Names, .. CliVersionInfo.Names];

public ParseResult Parse(params string[] args)
{
Expand All @@ -24,18 +26,26 @@ public ParseResult Parse(params string[] args)
var arg1 = args.FirstOrDefault();
if (!TryInitCommandOptions(arg1, out var options, out var helpInfo))
{
// TODO: Apply a proper fix for this
if (!string.IsNullOrEmpty(arg1) && !new List<string>([CliHelpInfo.FullName, CliHelpInfo.ShortName]).Contains(arg1))
if (!string.IsNullOrEmpty(arg1) && !_globalOptions.Contains(arg1))
{
result.Errors.Add($"{arg1} is not a known command");
}
}

result.HelpInfo = helpInfo;
result.RequiresHelp = true;
result.HelpInfo = helpInfo;
result.Options = options;

if (CliVersionInfo.Names.Contains(arg1))
{
result.RequiresVersion = true;
return result;
}

result.Options = options;
if (result.Options == null || CliHelpInfo.Names.Contains(arg1))
{
result.RequiresHelp = true;
return result;
}

var props = GetOptionProps(result.Options.GetType());

Expand All @@ -47,8 +57,23 @@ public ParseResult Parse(params string[] args)
return result;
}

// skip first arg, we've already handled it
// by determining the command to run
ProcessArgs(args, props, result);

if (result.Errors.Count != 0) return result;

var objectValidation = result.Options.Validate();

if (!objectValidation.Valid)
{
result.Errors.AddRange(objectValidation.Messages);
}

return result;
}

private void ProcessArgs(string[] args, IEnumerable<(PropertyInfo argProp, OptionAttribute attr)> props, ParseResult result)
{
// skip first arg, we've already handled it by determining the command to run
var i = 1;
while (i < args.Length)
{
Expand All @@ -60,7 +85,7 @@ public ParseResult Parse(params string[] args)
if (attr == null)
{
result.Errors.Add($"Unknown argument: {name}");
return result;
return;
}

string? value = null;
Expand All @@ -81,22 +106,13 @@ public ParseResult Parse(params string[] args)
if (!argValidation.Valid)
{
result.Errors.Add($"Invalid value for argument {name}: {value}");
return result;
return;
}

argProp.SetValue(result.Options, argValidation.Value);

i += attr.IsFlag ? 1 : 2;
}

var objectValidation = result.Options.Validate();

if (!objectValidation.Valid)
{
result.Errors.AddRange(objectValidation.Messages);
}

return result;
}

private static bool TryInitCommandOptions(string? arg1, [NotNullWhen(true)] out BaseOptions? options, out AppHelpInfo helpInfo)
Expand Down
13 changes: 13 additions & 0 deletions src/GDMan.Cli/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using GDMan.Core.Services.FileSystem;
using GDMan.Core.Infrastructure;
using GDMan.Core.Models;
using GDMan.Cli.Version;

namespace GDMan.Cli;

Expand Down Expand Up @@ -59,6 +60,11 @@ private static async Task HandleParseResult(ParseResult result)
HandleHelp(result);
}

if (result.RequiresVersion)
{
HandleVersion(result);
}

if (result.Options == null)
{
throw new NullReferenceException("Expected Options to have a value but found null");
Expand Down Expand Up @@ -137,6 +143,13 @@ private static void HandleHelp(ParseResult cliArgs)
Environment.Exit(0);
}

[DoesNotReturn]
private static void HandleVersion(ParseResult parseResult)
{
_logger.LogInformation(CliVersionInfo.ToString());
Environment.Exit(0);
}

[DoesNotReturn]
private static void HandleError(ParseResult result)
{
Expand Down
21 changes: 21 additions & 0 deletions src/GDMan.Cli/Version/CliVersionInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
namespace GDMan.Cli.Version;

public class CliVersionInfo
{
public static readonly string FullName = "--version";
public static readonly string ShortName = "-v";
public static readonly string[] Names = [FullName, ShortName];

public static SemanticVersioning.Version? AppVersion => GetVersion();
private static SemanticVersioning.Version? GetVersion()
{
var version = typeof(CliVersionInfo).Assembly.GetName().Version;

return version == null
? null
: SemanticVersioning.Version.Parse($"{version.Major}.{version.Minor}.{version.Build}");
}

public new static string ToString()
=> AppVersion?.ToString() ?? "unknown version";
}

0 comments on commit 9016ea9

Please sign in to comment.