Skip to content
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

Moving abstract classes to a core package so that commands that want … #24

Merged
merged 1 commit into from
Jun 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Xcaciv.Command.sln
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "zTestCommandPackage", "src\
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Xcaciv.Command.Tests", "src\Xcaciv.Command.Tests\Xcaciv.Command.Tests.csproj", "{1AA7D848-7F82-4190-8CB8-FDE32B74B0E7}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Xcaciv.Command.Core", "src\Xcaciv.Command.Core\Xcaciv.Command.Core.csproj", "{7BE3D0B8-651A-4C44-A46F-04345DFDE3EC}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -53,6 +55,10 @@ Global
{1AA7D848-7F82-4190-8CB8-FDE32B74B0E7}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1AA7D848-7F82-4190-8CB8-FDE32B74B0E7}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1AA7D848-7F82-4190-8CB8-FDE32B74B0E7}.Release|Any CPU.Build.0 = Release|Any CPU
{7BE3D0B8-651A-4C44-A46F-04345DFDE3EC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{7BE3D0B8-651A-4C44-A46F-04345DFDE3EC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7BE3D0B8-651A-4C44-A46F-04345DFDE3EC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7BE3D0B8-651A-4C44-A46F-04345DFDE3EC}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
using Xcaciv.Command.Interface;
using Xcaciv.Command.Interface.Attributes;

namespace Xcaciv.Command.Commands
namespace Xcaciv.Command.Core
{
public abstract class AbstractCommand : Xcaciv.Command.Interface.ICommandDelegate
{
public abstract class AbstractCommand : ICommandDelegate
{

/// <summary>
/// this should be overwritten to dispose of any unmanaged items
Expand All @@ -27,25 +27,25 @@ public virtual ValueTask DisposeAsync()
/// <param name="outputContext"></param>
public virtual void Help(IIoContext outputContext)
{
outputContext.OutputChunk(this.BuildHelpString());
outputContext.OutputChunk(BuildHelpString());
}
/// <summary>
/// single line help command description, used for listing all commands
/// </summary>
/// <param name="outputContext"></param>
public virtual void OneLineHelp(IIoContext outputContext)
{
var baseCommand = Attribute.GetCustomAttribute(this.GetType(), typeof(CommandRegisterAttribute)) as CommandRegisterAttribute;
var baseCommand = Attribute.GetCustomAttribute(GetType(), typeof(CommandRegisterAttribute)) as CommandRegisterAttribute;
if (baseCommand != null)
outputContext.OutputChunk($"{baseCommand.Command,-12} {baseCommand.Description}");
outputContext.OutputChunk($"{baseCommand.Command,-12} {baseCommand.Description}");
}
/// <summary>
/// create a nicely formated
/// </summary>
/// <returns></returns>
protected virtual string BuildHelpString()
{
var thisType = this.GetType();
var thisType = GetType();
var baseCommand = Attribute.GetCustomAttribute(thisType, typeof(CommandRegisterAttribute)) as CommandRegisterAttribute;
var commandParametersOrdered = GetOrderedParameters(false);
var commandParametersFlag = GetFlagParameters();
Expand Down Expand Up @@ -110,7 +110,7 @@ public async IAsyncEnumerable<string> Main(IIoContext io, IEnvironmentContext en
await foreach (var p in io.ReadInputPipeChunks())
{
if (string.IsNullOrEmpty(p)) continue;
yield return this.HandlePipedChunk(p, io.Parameters, environment);
yield return HandlePipedChunk(p, io.Parameters, environment);
}
}
else
Expand All @@ -131,7 +131,7 @@ protected Dictionary<string, string> ProcessParameters(string[] parameters, bool
var parameterList = parameters.ToList();

var parameterLookup = new Dictionary<string, string>();
Type thisType = this.GetType();
Type thisType = GetType();
CommandParameters.
ProcessOrderedParameters(parameterList, parameterLookup, GetOrderedParameters(hasPipedInput));
CommandParameters.ProcessFlags(parameterList, parameterLookup, GetFlagParameters());
Expand All @@ -147,13 +147,13 @@ protected Dictionary<string, string> ProcessParameters(string[] parameters, bool
/// <returns></returns>
protected CommandParameterOrderedAttribute[] GetOrderedParameters(bool hasPipedInput)
{
var thisType = this.GetType();
var thisType = GetType();
var ordered = Attribute.GetCustomAttributes(thisType, typeof(CommandParameterOrderedAttribute)) as CommandParameterOrderedAttribute[] ?? ([]);
if (hasPipedInput)
{
ordered = ordered.Where(x => !x.UsePipe).ToArray();
}

return ordered;
}
/// <summary>
Expand All @@ -162,7 +162,7 @@ protected CommandParameterOrderedAttribute[] GetOrderedParameters(bool hasPipedI
/// <returns></returns>
protected CommandParameterNamedAttribute[] GetNamedParameters(bool hasPipedInput)
{
var thisType = this.GetType();
var thisType = GetType();
var named = Attribute.GetCustomAttributes(thisType, typeof(CommandParameterNamedAttribute)) as CommandParameterNamedAttribute[] ?? ([]);
if (hasPipedInput)
{
Expand All @@ -176,7 +176,7 @@ protected CommandParameterNamedAttribute[] GetNamedParameters(bool hasPipedInput
/// <returns></returns>
protected CommandFlagAttribute[] GetFlagParameters()
{
var thisType = this.GetType();
var thisType = GetType();
var flags = Attribute.GetCustomAttributes(thisType, typeof(CommandFlagAttribute)) as CommandFlagAttribute[] ?? ([]);
return flags;
}
Expand All @@ -186,7 +186,7 @@ protected CommandFlagAttribute[] GetFlagParameters()
/// <returns></returns>
protected CommandParameterSuffixAttribute[] GetSuffixParameters(bool hasPipedInput)
{
var thisType = this.GetType();
var thisType = GetType();
var flags = Attribute.GetCustomAttributes(thisType, typeof(CommandParameterSuffixAttribute)) as CommandParameterSuffixAttribute[] ?? ([]);
if (hasPipedInput)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
using System.Threading.Tasks;
using Xcaciv.Command.Interface;

namespace Xcaciv.Command
namespace Xcaciv.Command.Core
{
/// <summary>
/// Implements the more generic parts of the ITextIoContext
Expand All @@ -36,7 +36,7 @@ public abstract class AbstractTextIo(string name, string[] parameters, Guid? par

public Task SetParameters(string[] parameters)
{
this.Parameters = parameters;
Parameters = parameters;
return Task.CompletedTask;
}

Expand All @@ -56,9 +56,9 @@ public Task SetParameters(string[] parameters)
/// <returns></returns>
public virtual Task OutputChunk(string message)
{
if (this.outputPipe == null)
if (outputPipe == null)
{
return this.HandleOutputChunk(message);
return HandleOutputChunk(message);
}
return outputPipe.WriteAsync(message).AsTask();
}
Expand Down Expand Up @@ -93,16 +93,16 @@ public async IAsyncEnumerable<string> ReadInputPipeChunks()
/// <param name="reader"></param>
public void SetInputPipe(ChannelReader<string> reader)
{
this.HasPipedInput = true;
this.inputPipe = reader;
HasPipedInput = true;
inputPipe = reader;
}
/// <summary>
/// set channel writer for pipeline
/// </summary>
/// <param name="writer"></param>
public void SetOutputPipe(ChannelWriter<string> writer)
{
this.outputPipe = writer;
outputPipe = writer;
}
/// <summary>
/// display progress
Expand All @@ -123,15 +123,15 @@ public void SetOutputPipe(ChannelWriter<string> writer)
/// <returns></returns>
public ValueTask DisposeAsync()
{
this.Complete().Wait();
Complete().Wait();
return ValueTask.CompletedTask;
}

public Task Complete(string? message = null)
{
if (!String.IsNullOrEmpty(message)) this.SetStatusMessage(message).Wait();
if (!string.IsNullOrEmpty(message)) SetStatusMessage(message).Wait();

this.outputPipe?.TryComplete();
outputPipe?.TryComplete();
return Task.CompletedTask;
}

Expand All @@ -147,12 +147,12 @@ public void SetTraceLog(string logName)

public virtual Task AddTraceMessage(string message)
{
if (this.Verbose)
if (Verbose)
{
return this.OutputChunk("\tTRACE: " + message);
return OutputChunk("\tTRACE: " + message);
}
// if we are not verbose, send the output to DEBUG
System.Diagnostics.Trace.WriteLine(message);
Trace.WriteLine(message);
return Task.CompletedTask;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System.Text.RegularExpressions;
using Xcaciv.Command.Interface.Attributes;

namespace Xcaciv.Command.Commands;
namespace Xcaciv.Command.Core;

public static class CommandParameters
{
Expand All @@ -12,7 +12,7 @@ public static void ProcessFlags(List<string> parameterList, Dictionary<string, s
{
var index = 0;
Regex fullName = new Regex("-{1,2}" + parameter.Name);
Regex abbrName = (String.IsNullOrEmpty(parameter.ShortAlias)) ? new Regex("^$") :
Regex abbrName = string.IsNullOrEmpty(parameter.ShortAlias) ? new Regex("^$") :
new Regex("-{1,2}" + parameter.ShortAlias);

var found = false;
Expand All @@ -38,7 +38,7 @@ public static void ProcessNamedParameters(List<string> parameterList, Dictionary
{
var index = 0;
Regex fullName = new Regex("-{1,2}" + parameter.Name);
Regex abbrName = (String.IsNullOrEmpty(parameter.ShortAlias)) ? new Regex("^$") :
Regex abbrName = string.IsNullOrEmpty(parameter.ShortAlias) ? new Regex("^$") :
new Regex("-{1,2}" + parameter.ShortAlias);

foreach (var value in parameterList)
Expand Down
34 changes: 34 additions & 0 deletions src/Xcaciv.Command.Core/Xcaciv.Command.Core.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<Version>1.0.1</Version>
<AssemblyName>Xcaciv.Command.Core</AssemblyName>
<RootNamespace>Xcaciv.Command.Core</RootNamespace>
<IsPublishable>True</IsPublishable>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<TargetFramework>net8.0</TargetFramework>
<PackageLicenseExpression>BSD-3-Clause</PackageLicenseExpression>
<Copyright>Alton Crossley</Copyright>
<PackageProjectUrl>https://github.com/Xcaciv/Xcaciv.Command</PackageProjectUrl>
<PackageReadmeFile>readme.md</PackageReadmeFile>
<RepositoryUrl>[email protected]:Xcaciv/Xcaciv.Command.git</RepositoryUrl>
<IncludeSymbols>True</IncludeSymbols>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
<EnforceCodeStyleInBuild>True</EnforceCodeStyleInBuild>
<Title>Xcaciv Command Core</Title>
</PropertyGroup>

<ItemGroup>
<ProjectReference Packge="True" Include="..\Xcaciv.Command.Interface\Xcaciv.Command.Interface.csproj" />
</ItemGroup>

<ItemGroup>
<None Update="readme.md">
<PackagePath>\</PackagePath>
<Pack>True</Pack>
</None>
</ItemGroup>

</Project>
4 changes: 4 additions & 0 deletions src/Xcaciv.Command.Core/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Xcaciv.Command.Core

Core abstractions for Xcaciv Command.

2 changes: 1 addition & 1 deletion src/Xcaciv.Command.Tests/Commands/ParameterTestCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using Xcaciv.Command.Commands;
using Xcaciv.Command.Core;
using Xcaciv.Command.Interface;
using Xcaciv.Command.Interface.Attributes;

Expand Down
2 changes: 1 addition & 1 deletion src/Xcaciv.Command.Tests/TestImpementations/TestTextIo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
using System.Text;
using System.Threading.Channels;
using System.Threading.Tasks;
using Xcaciv.Command;
using Xcaciv.Command.Core;
using Xcaciv.Command.Interface;

namespace Xcaciv.Command.Tests.TestImpementations
Expand Down
1 change: 1 addition & 0 deletions src/Xcaciv.Command/Commands/EnvCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xcaciv.Command.Core;
using Xcaciv.Command.Interface;
using Xcaciv.Command.Interface.Attributes;

Expand Down
1 change: 1 addition & 0 deletions src/Xcaciv.Command/Commands/RegifCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Xcaciv.Command.Core;
using Xcaciv.Command.Interface;
using Xcaciv.Command.Interface.Attributes;

Expand Down
1 change: 1 addition & 0 deletions src/Xcaciv.Command/Commands/SayCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Xcaciv.Command.Core;
using Xcaciv.Command.Interface;
using Xcaciv.Command.Interface.Attributes;

Expand Down
1 change: 1 addition & 0 deletions src/Xcaciv.Command/Commands/SetCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xcaciv.Command.Core;
using Xcaciv.Command.Interface;
using Xcaciv.Command.Interface.Attributes;

Expand Down
15 changes: 0 additions & 15 deletions src/Xcaciv.Command/ICommandController.cs

This file was deleted.

1 change: 1 addition & 0 deletions src/Xcaciv.Command/MemoryIoContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xcaciv.Command.Core;
using Xcaciv.Command.Interface;

namespace Xcaciv.Command
Expand Down
5 changes: 3 additions & 2 deletions src/Xcaciv.Command/Xcaciv.Command.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<PropertyGroup>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<Version>1.4.14</Version>
<Version>1.4.16</Version>
<AssemblyName>Xcaciv.Command</AssemblyName>
<RootNamespace>Xcaciv.Command</RootNamespace>
<IsPublishable>True</IsPublishable>
Expand All @@ -28,7 +28,8 @@
<ProjectReference Include="..\Xcaciv.Command.FileLoader\Xcaciv.Command.FileLoader.csproj">
<IncludeAssets>Xcaciv.Command.FileLoader.dll</IncludeAssets>
</ProjectReference>
<ProjectReference Packge="True" Include="..\Xcaciv.Command.Interface\Xcaciv.Command.Interface.csproj" />
<ProjectReference Packge="True" Include="..\Xcaciv.Command.Interface\Xcaciv.Command.Interface.csproj" />
<ProjectReference Packge="True" Include="..\Xcaciv.Command.Core\Xcaciv.Command.Core.csproj" />
</ItemGroup>

<ItemGroup>
Expand Down