Skip to content

Commit

Permalink
Merge pull request #34 from domingoladron/feature/27-add-cli-to-deplo…
Browse files Browse the repository at this point in the history
…yment

27 Add CLI to deployment
  • Loading branch information
domingoladron authored Jul 28, 2023
2 parents 1a98b62 + a5083a6 commit 6a7f87e
Show file tree
Hide file tree
Showing 12 changed files with 118 additions and 113 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/build-and-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ jobs:
- uses: actions/checkout@v2
- name: Setup dotnet
id: setup_dotnet
uses: actions/setup-dotnet@v1
uses: actions/setup-dotnet@v3
with:
dotnet-version: |
6.0.x
7.0.x
- name: Install dependencies
run: dotnet restore
- name: Build
Expand Down
3 changes: 2 additions & 1 deletion .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ jobs:
- name: Checkout
uses: actions/checkout@v2
- name: Setup .NET SDK
uses: actions/setup-dotnet@v1
uses: actions/setup-dotnet@v3
with:
dotnet-version: |
6.0.x
7.0.x
- name: Build
run: dotnet build -c Release
- name: Test EpubCore
Expand Down
2 changes: 1 addition & 1 deletion EpubCore.Cli.Tests/EpubCore.Cli.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

Expand Down
92 changes: 92 additions & 0 deletions EpubCore.Cli/ActionHandlers/GenerateDocumentationActionHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
using System.IO.Abstractions;
using System.Reflection;
using System.Text;
using CommandLine;
using DotMarkdown;
using EpubCore.Cli.Models;

namespace EpubCore.Cli.ActionHandlers;

public class GenerateDocumentationActionHandler : EpubActionHandlerBase, ICliActionHandler
{
private readonly IFileSystem _fileSystem;
private readonly IConsoleWriter _consoleWriter;
public GenerateDocumentationActionHandler(
IFileSystem fileSystem,
IConsoleWriter consoleWriter) : base(fileSystem, consoleWriter)
{
_fileSystem = fileSystem;
_consoleWriter = consoleWriter;
}

public void HandleCliAction(object options)
{
if (options is not GenerateDocumentationOptions genDocOptions) return;

var helpDocEntries = new List<HelpDocEntry>();

var types = CommandHandler.LoadVerbs();

foreach (var curType in types)
{
var verbAttribute = curType.GetCustomAttribute<VerbAttribute>();
if (verbAttribute == null) continue;

var helpDocEntry = new HelpDocEntry
{
VerbName = verbAttribute.Name,
HelpText = verbAttribute.HelpText
};

var properties = curType.GetProperties();
foreach (var curProperty in properties)
{
var optionVal = curProperty.GetCustomAttribute<OptionAttribute>();

if (optionVal == null) continue;
var helpParam = new HelpParamEntry
{
HelpText = optionVal.HelpText,
ShortName = optionVal.ShortName,
LongName = optionVal.LongName,
Required = optionVal.Required
};

helpDocEntry.Parameters.Add(helpParam);
}

helpDocEntries.Add(helpDocEntry);
}

WriteMarkdownDocumentation(genDocOptions.DocumentationOutputFilePath, helpDocEntries);
}

private void WriteMarkdownDocumentation(string docFileName, List<HelpDocEntry> helpDocEntries)
{
var sb = new StringBuilder();

using var writer = MarkdownWriter.Create(sb);
writer.WriteHeading1("EpubCore.Cli Verbs");
foreach (var helpDocEntry in helpDocEntries.OrderBy(g => g.VerbName))
{
writer.WriteHeading2(helpDocEntry.VerbName);
writer.WriteLinkOrText(helpDocEntry.HelpText);
if (helpDocEntry.Parameters.Any())
{
writer.WriteLinkOrText("");
writer.WriteFencedCodeBlock($"epub {helpDocEntry.VerbName} <parameters>");
writer.WriteHeading3("Parameters");
}
foreach (var curParameter in helpDocEntry.Parameters.OrderByDescending(g => g.Required).ThenBy(g => g.LongName))
{
var requiredString = curParameter.Required ? "[required]" : "";
writer.WriteHeading4($" --{curParameter.LongName} ( -{curParameter.ShortName} ) {requiredString}");

writer.WriteLinkOrText($"{curParameter.HelpText}");
}
}

_fileSystem.File.WriteAllText(docFileName, sb.ToString());
_consoleWriter.WriteSuccess($"epub cli documentation written to {docFileName}");
}
}
2 changes: 2 additions & 0 deletions EpubCore.Cli/CliActionHandlerResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ public CliActionHandlerResolver(
return _serviceProvider.GetService<GetHtmlActionHandler>();
case RemoveResourceOptions:
return _serviceProvider.GetService<RemoveResourceActionHandler>();
case GenerateDocumentationOptions:
return _serviceProvider.GetService<GenerateDocumentationActionHandler>();
default:
throw new NotImplementedException();
}
Expand Down
4 changes: 0 additions & 4 deletions EpubCore.Cli/CliErrorHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,5 @@ public class CliErrorHandler : ICliErrorHandler
{
public void HandleError(IEnumerable<Error> errors)
{
//foreach (var error in errors)
//{
// Console.WriteLine(error.);
//}
}
}
111 changes: 13 additions & 98 deletions EpubCore.Cli/CommandHandler.cs
Original file line number Diff line number Diff line change
@@ -1,132 +1,47 @@
using System.IO.Abstractions;
using System.Reflection;
using System.Text;
using System.Reflection;
using CommandLine;
using DotMarkdown;
using EpubCore.Cli.ActionHandlers;
using EpubCore.Cli.Models;

namespace EpubCore.Cli;

public class CommandHandler : ICommandHandler
{
private readonly ICliErrorHandler _cliErrorHandler;
private readonly ICliActionHandlerResolver _cliActionHandlerResolver;
private readonly IFileSystem _fileSystem;
private readonly IConsoleWriter _consoleWriter;


public CommandHandler(
ICliErrorHandler cliErrorHandler,
ICliActionHandlerResolver cliActionHandlerResolver,
IFileSystem fileSystem,
IConsoleWriter consoleWriter)
ICliActionHandlerResolver cliActionHandlerResolver)
{
_cliErrorHandler = cliErrorHandler;
_cliActionHandlerResolver = cliActionHandlerResolver;
_fileSystem = fileSystem;
_consoleWriter = consoleWriter;
}

public async Task<int> ExecuteAsync(string[] args)
public int Execute(string[] args)
{
var types = LoadVerbs();

var parser = new Parser(with =>
{
with.HelpWriter = Console.Out;
//ignore case for enum values
with.CaseInsensitiveEnumValues = true;
});

var initialParseResult = parser.ParseArguments(args, types);
if (initialParseResult.Value is GenerateDocumentationOptions genDocOptions)
{
_fileSystem.File.Delete(genDocOptions.DocumentationOutputFilePath);

var helpDocEntries = new List<HelpDocEntry>();

foreach (var curType in types)
{
var verbAttribute = curType.GetCustomAttribute<VerbAttribute>();
if(verbAttribute == null) continue;

var helpDocEntry = new HelpDocEntry
{
VerbName = verbAttribute.Name,
HelpText = verbAttribute.HelpText
};

var properties = curType.GetProperties();
foreach (var curProperty in properties)
{
var optionVal = curProperty.GetCustomAttribute<OptionAttribute>();

if (optionVal == null) continue;
var helpParam = new HelpParamEntry
{
HelpText = optionVal.HelpText,
ShortName = optionVal.ShortName,
LongName = optionVal.LongName,
Required = optionVal.Required
};

helpDocEntry.Parameters.Add(helpParam);
}
});

helpDocEntries.Add(helpDocEntry);
}

WriteMarkdownDocumentation(genDocOptions.DocumentationOutputFilePath, helpDocEntries);

return 0;
}
else
{

var result = parser.ParseArguments(args, types)
.WithParsed(Run)
.WithNotParsed(_cliErrorHandler.HandleError);
if (result.Tag == ParserResultType.Parsed)
return await Task.FromResult(0);
return -1;
}
var result = parser.ParseArguments(args, types)
.WithParsed(Run);
if (result.Tag == ParserResultType.Parsed)
return 0;
return -1;
}

private void WriteMarkdownDocumentation(string docFileName, List<HelpDocEntry> helpDocEntries)
{
var sb = new StringBuilder();

using var writer = MarkdownWriter.Create(sb);
writer.WriteHeading1("EpubCore.Cli Verbs");
foreach (var helpDocEntry in helpDocEntries.OrderBy(g => g.VerbName))
{
writer.WriteHeading2(helpDocEntry.VerbName);
writer.WriteLinkOrText(helpDocEntry.HelpText);
if (helpDocEntry.Parameters.Any())
{
writer.WriteLinkOrText("");
writer.WriteFencedCodeBlock($"epub {helpDocEntry.VerbName} <parameters>");
writer.WriteHeading3("Parameters");
}
foreach (var curParameter in helpDocEntry.Parameters.OrderByDescending(g => g.Required).ThenBy(g => g.LongName))
{
var requiredString = curParameter.Required ? "[required]" : "";
writer.WriteHeading4($" --{curParameter.LongName} ( -{curParameter.ShortName} ) {requiredString}");

writer.WriteLinkOrText($"{curParameter.HelpText}");
}
}

_fileSystem.File.WriteAllText(docFileName, sb.ToString());
_consoleWriter.WriteSuccess($"epub cli documentation written to {docFileName}");
}


private void Run(object obj)
{
var handler = _cliActionHandlerResolver.Resolve(obj);
handler!.HandleCliAction(obj);
}

private static Type[] LoadVerbs()
public static Type[] LoadVerbs()
{
return Assembly.GetExecutingAssembly().GetTypes()
.Where(t => t.GetCustomAttribute<VerbAttribute>() != null).ToArray();
Expand Down
4 changes: 2 additions & 2 deletions EpubCore.Cli/EpubCore.Cli.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<PackAsTool>true</PackAsTool>
Expand All @@ -13,7 +13,7 @@
<Copyright>Copyright 2023</Copyright>
<PackageProjectUrl>https://github.com/domingoladron/EpubCore</PackageProjectUrl>
<RepositoryUrl>https://github.com/domingoladron/EpubCore</RepositoryUrl>
<Version>0.8.0.0</Version>
<Version>0.9.0.0</Version>
</PropertyGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion EpubCore.Cli/ICommandHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

public interface ICommandHandler
{
Task<int> ExecuteAsync(string[] args);
int Execute(string[] args);
}
4 changes: 1 addition & 3 deletions EpubCore.Cli/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ static void Main(string[] args)
using var host = CreateHostBuilder(args).Build();

var result = host.Services.GetService<ICommandHandler>()!
.ExecuteAsync(args).Result;


.Execute(args);
}

private static IHostBuilder CreateHostBuilder(string[] args)
Expand Down
2 changes: 1 addition & 1 deletion EpubCore.Cli/Properties/launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"profiles": {
"EpubCore.Cli": {
"commandName": "Project",
"commandLineArgs": "gen-cli-docs --out docs/wiki/epub-cli-docs.md"
"commandLineArgs": "--version"
}
}
}
2 changes: 1 addition & 1 deletion docs/wiki/epub-cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ In order to make it as easy as possible to manipulate epub files with little / n

## Prerequisites

`.NET 6.0` or greater
`.NET 7.0` or greater

## How to install from the interwebs

Expand Down

0 comments on commit 6a7f87e

Please sign in to comment.