Skip to content

Commit

Permalink
bind invokable on miru cli
Browse files Browse the repository at this point in the history
  • Loading branch information
joaofx committed Apr 19, 2023
1 parent 306f247 commit 071b93c
Show file tree
Hide file tree
Showing 8 changed files with 167 additions and 116 deletions.
48 changes: 24 additions & 24 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,31 +22,31 @@ jobs:
- name: Setup .NET
uses: actions/setup-dotnet@v1
with:
dotnet-version: "7.0.202"
dotnet-version: "7.0.203"

- name: Build & Test
run: ./build test-ci

page-test:
strategy:
fail-fast: false
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
with:
fetch-depth: 0

- name: Setup .NET
uses: actions/setup-dotnet@v1
with:
dotnet-version: "7.0.202"

- name: Setup Firefox
uses: browser-actions/setup-firefox@latest

- name: Setup Chrome
uses: browser-actions/setup-chrome@latest

- name: Build & Test
run: ./build page-test-ci
# page-test:
# strategy:
# fail-fast: false
# runs-on: ubuntu-latest
#
# steps:
# - uses: actions/checkout@v2
# with:
# fetch-depth: 0
#
# - name: Setup .NET
# uses: actions/setup-dotnet@v1
# with:
# dotnet-version: "7.0.203"
#
# - name: Setup Firefox
# uses: browser-actions/setup-firefox@latest
#
# - name: Setup Chrome
# uses: browser-actions/setup-chrome@latest
#
# - name: Build & Test
# run: ./build page-test-ci
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
- name: Setup .NET
uses: actions/setup-dotnet@v1
with:
dotnet-version: "7.0.202"
dotnet-version: "7.0.203"

- name: Build & Publish
env:
Expand Down
2 changes: 1 addition & 1 deletion global.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"sdk": {
"version": "7.0.202"
"version": "7.0.203"
}
}
4 changes: 1 addition & 3 deletions src/Miru/Consolables/CliMiruHost.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
using System;
using System.Collections.Generic;
using System.CommandLine;
using System.CommandLine.NamingConventionBinder;
using System.CommandLine.Parsing;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Miru.Foundation.Bootstrap;
using Miru.Hosting;

Expand Down Expand Up @@ -72,7 +70,7 @@ private void AddConsolables(RootCommand rootCommand)
}
}

private static RootCommand CreateRootCommand()
public static RootCommand CreateRootCommand()
{
var rootCommand = new RootCommand
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System.Data;
using FluentMigrator.Builders.Alter.Column;
using FluentMigrator.Builders.Alter.Table;
using FluentMigrator.Builders.Create.Table;

namespace Miru.Databases.Migrations.FluentMigrator;

public static class ForeignKeyReferenceExtensions
{
public static ICreateTableColumnOptionOrForeignKeyCascadeOrWithColumnSyntax AsForeignKeyReference(
this ICreateTableColumnAsTypeSyntax column,
string foreignTable,
string idColumn = "Id",
bool deleteOnCascade = false)
{
var attrs = column.AsInt64().Indexed().ForeignKey(foreignTable, idColumn);

return deleteOnCascade ? attrs.OnDelete(Rule.Cascade) : attrs;
}

public static IAlterTableColumnOptionOrAddColumnOrAlterColumnSyntax AsForeignKeyReference(
this IAlterTableColumnAsTypeSyntax column,
string foreignTable,
string idColumn = "Id",
bool deleteOnCascade = false)
{
var attrs = column.AsInt64().Indexed().ForeignKey(foreignTable, idColumn);

return deleteOnCascade ? attrs.OnDelete(Rule.Cascade) : attrs;
}

public static IAlterColumnOptionOrForeignKeyCascadeSyntax AsForeignKeyReference(
this IAlterColumnAsTypeOrInSchemaSyntax column,
string foreignTable,
string idColumn = "Id",
bool deleteOnCascade = false)
{
var attrs = column.AsInt64().Indexed().ForeignKey(foreignTable, idColumn);

return deleteOnCascade ? attrs.OnDelete(Rule.Cascade) : attrs;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using System.Data;
using FluentMigrator.Builders.Alter.Table;
using FluentMigrator.Builders.Create.Table;

namespace Miru.Databases.Migrations.FluentMigrator;
Expand All @@ -20,43 +18,4 @@ public static ICreateTableWithColumnSyntax WithId(this ICreateTableWithColumnSyn
{
return table.WithColumn("Id").AsId();
}

// public static ICreateTableColumnOptionOrForeignKeyCascadeOrWithColumnSyntax AsForeignKeyReference(
// this ICreateTableColumnAsTypeSyntax column,
// string foreignTable,
// string idColumn = "Id",
// bool indexed = true,
// bool deleteOnCascade = true)
// {
// var attrs = column.AsInt64();
//
// if (indexed)
// attrs = attrs.Indexed();
//
// var attrs2 = attrs.ForeignKey(foreignTable, idColumn);
//
// return deleteOnCascade ? attrs2.OnDelete(Rule.Cascade) : attrs2;
// }

public static ICreateTableColumnOptionOrForeignKeyCascadeOrWithColumnSyntax AsForeignKeyReference(
this ICreateTableColumnAsTypeSyntax column,
string foreignTable,
string idColumn = "Id",
bool deleteOnCascade = false)
{
var attrs = column.AsInt64().Indexed().ForeignKey(foreignTable, idColumn);

return deleteOnCascade ? attrs.OnDelete(Rule.Cascade) : attrs;
}

public static IAlterTableColumnOptionOrAddColumnOrAlterColumnSyntax AsForeignKeyReference(
this IAlterTableColumnAsTypeSyntax column,
string foreignTable,
string idColumn = "Id",
bool deleteOnCascade = false)
{
var attrs = column.AsInt64().Indexed().ForeignKey(foreignTable, idColumn);

return deleteOnCascade ? attrs.OnDelete(Rule.Cascade) : attrs;
}
}
114 changes: 85 additions & 29 deletions src/Miru/Pipeline/InvokeConsolable.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
using System;
using System.Collections.Generic;
using System.CommandLine;
using System.CommandLine.Binding;
using System.CommandLine.Invocation;
using System.CommandLine.NamingConventionBinder;
using System.CommandLine.Parsing;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Miru.Consolables;
using Miru.Core;
using Miru.Foundation.Bootstrap;

namespace Miru.Pipeline;
Expand All @@ -35,39 +33,36 @@ public ConsolableHandler(IMiruApp app, ArgsConfiguration argsConfig)

public async Task Execute()
{
var type = App.Assembly
.ExportedTypes
.SingleOrDefault(x =>
x.Name == "Command"
&& x.DeclaringType != null
&& x.DeclaringType.Name == Name);
var type = SearchTypeByInvokableName(Name);

if (type == null )
if (type is null)
{
Console2.RedLine($"Type {Name}.Command not found in assembly {App.Assembly.FullName}");
return;
}

var instance = Activator.CreateInstance(type);

if (instance is RootCommand rootCommand)
{
rootCommand.TreatUnmatchedTokensAsErrors = false;

var binder = new ModelBinder(instance.GetType())
{
EnforceExplicitBinding = false
};

// excludes from args 'invoke FeatureName'
var result = rootCommand.Parse(_argsConfig.CliArgs[2..]);
var invocationContext = new InvocationContext(result);
var bindingContext = invocationContext.BindingContext;

binder.UpdateInstance(instance, bindingContext);
}
var invokableRequest = BuildInvokableRequest(type, _argsConfig.CliArgs[2..]);

if (instance is IInvokable request)
// var instance = Activator.CreateInstance(type);
//
// if (instance is RootCommand rootCommand)
// {
// rootCommand.TreatUnmatchedTokensAsErrors = false;
//
// var binder = new ModelBinder(instance.GetType())
// {
// EnforceExplicitBinding = false
// };
//
// // excludes from args 'invoke FeatureName'
// var result = rootCommand.Parse(_argsConfig.CliArgs[2..]);
// var invocationContext = new InvocationContext(result);
// var bindingContext = invocationContext.BindingContext;
//
// binder.UpdateInstance(instance, bindingContext);
// }

if (invokableRequest is IInvokable request)
{
await _app.ScopedSendAsync(request);

Expand All @@ -78,5 +73,66 @@ public async Task Execute()

Console2.RedLine($"Type {Name}.Command is not a IInvokable");
}

/// <summary>
/// Not very proud of this method but is very handy
/// Builds a yml string and use YamlDotNet to create an instance of type
/// </summary>
private object BuildInvokableRequest(Type type, string[] cliArgs)
{
var rootCommand = CliMiruHost.CreateRootCommand();
var parseResult = rootCommand.Parse(cliArgs);

if (cliArgs.Length == 0)
return Activator.CreateInstance(type);

var yml = parseResult.Tokens
.Select((x, i) => new { Index = i, Item = x })
.GroupBy(x => x.Index / 2)
.Select(x => $"{x.At(0).Item.Value[2..]}: {x.At(1).Item.Value}")
.Join(Environment.NewLine);

return new YamlDotNet.Serialization.Deserializer().Deserialize(yml, type);
}

private Type SearchTypeByInvokableName(string invokableName)
{
return App.Assembly
.ExportedTypes
.SingleOrDefault(x =>
x.Name == "Command"
&& x.DeclaringType != null
&& x.DeclaringType.Name == invokableName);
}
}
}

public static class EnumerableExtensions
{
public static IEnumerable<(T, T)> Pairwise<T>(this IEnumerable<T> source)
{
var previous = default(T);
using (var it = source.GetEnumerator())
{
if (it.MoveNext())
previous = it.Current;

while (it.MoveNext())
yield return (previous, previous = it.Current);
}
}

public static IEnumerable<TResult> Pairwise<TSource, TResult>(this IEnumerable<TSource> source, Func<TSource, TSource, TResult> resultSelector)
{
TSource previous = default(TSource);

using (var it = source.GetEnumerator())
{
if (it.MoveNext())
previous = it.Current;

while (it.MoveNext())
yield return resultSelector(previous, previous = it.Current);
}
}
}
30 changes: 13 additions & 17 deletions tests/Miru.Tests/Pipelines/InvokeConsolableTest.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.CommandLine;
using System.IO;
using System.Threading;
using MediatR;
Expand Down Expand Up @@ -41,10 +40,11 @@ public void Teardown()
public async Task Should_run_invokable_without_args()
{
// arrange
var host = MiruHost.CreateMiruHost("miru", "invoke", "OrderExport").ConfigureServices(x => x
.AddMiruApp<InvokableConsolableTest>()
.AddConsolables<ConfigShowConsolable>()
.AddPipeline<InvokableConsolableTest>());
var host = MiruHost.CreateMiruHost("miru", "invoke", "OrderExport")
.ConfigureServices(x => x
.AddMiruApp<InvokableConsolableTest>()
.AddConsolables<ConfigShowConsolable>()
.AddPipeline<InvokableConsolableTest>());

// act
await host.RunMiruAsync();
Expand All @@ -61,10 +61,12 @@ public async Task Should_run_invokable_without_args()
public async Task Should_run_invokable_parsing_args_to_create_command_instance()
{
// arrange
var host = MiruHost.CreateMiruHost("miru", "invoke", "OrderArchive", "--orderId", "123", "--archiveType", "Temporarily").ConfigureServices(x => x
.AddMiruApp<InvokableConsolableTest>()
.AddConsolables<ConfigShowConsolable>()
.AddPipeline<InvokableConsolableTest>());
var host = MiruHost.CreateMiruHost(
"miru", "invoke", "OrderArchive", "--OrderId", "123", "--ArchiveType", "Temporarily", "-e", "Production")
.ConfigureServices(x => x
.AddMiruApp<InvokableConsolableTest>()
.AddConsolables<ConfigShowConsolable>()
.AddPipeline<InvokableConsolableTest>());

// act
await host.RunMiruAsync();
Expand All @@ -77,18 +79,12 @@ public async Task Should_run_invokable_parsing_args_to_create_command_instance()

public class OrderArchive
{
public class Command : Invokable, IRequest<Command>
public class Command : IRequest<Command>, IInvokable
{
public Command()
{
Add(new Option<long>("--orderId"));
Add(new Option<ArchiveType>("--archiveType"));
}

public long OrderId { get; set; }
public ArchiveType ArchiveType { get; set; }
}

public class Handler : IRequestHandler<Command, Command>
{
public async Task<Command> Handle(Command request, CancellationToken cancellationToken)
Expand Down

0 comments on commit 071b93c

Please sign in to comment.