Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/ardacetinkaya/Blazor.Console
A simple component to mock CLI for ASP.NET Core Blazor applications to execute some custom commands for an application
https://github.com/ardacetinkaya/Blazor.Console
asp-net-core aspnetcore blazor blazor-component blazor-server commandline dotnet-core dotnetcore
Last synced: 3 months ago
JSON representation
A simple component to mock CLI for ASP.NET Core Blazor applications to execute some custom commands for an application
- Host: GitHub
- URL: https://github.com/ardacetinkaya/Blazor.Console
- Owner: ardacetinkaya
- License: mit
- Created: 2019-11-14T17:49:04.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2020-11-24T17:46:14.000Z (about 4 years ago)
- Last Synced: 2024-06-29T04:33:33.032Z (7 months ago)
- Topics: asp-net-core, aspnetcore, blazor, blazor-component, blazor-server, commandline, dotnet-core, dotnetcore
- Language: C#
- Homepage:
- Size: 1.49 MB
- Stars: 67
- Watchers: 3
- Forks: 10
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Blazor.CommandLine (a.k.a Blazor.Console)
[![Actions Status](https://github.com/ardacetinkaya/Blazor.Console/workflows/Build/badge.svg)](https://github.com/ardacetinkaya/Blazor.Console/actions)
[![NuGet version (Blzr.Components.CommandLine)](https://img.shields.io/nuget/v/Blzr.Components.CommandLine.svg)](https://www.nuget.org/packages/Blzr.Components.CommandLine/)Development: ![Build](https://github.com/ardacetinkaya/Blazor.Console/workflows/Build/badge.svg?branch=development)
### What and why... ###
This is a simple console component for ASP.NET Core 3.0 Blazor Server application model. The motivation behind this simple component is to provide simple command-line interface to manage some Web API. With this component it is easy to execute some business related commands.*
Real life scenario examples;
- Clear response cache
- Export log files
- Change run-time settings
- Monitor application resourcesThis Blazor component is based on ```System.CommandLine``` API and reflects standart command-line features to be more reliable.
### Usage ###
Check Demo project(BlazorConsoleDemo.csproj) for usage;
Add following extensions to services and application within Startup.cs
```cs
public void ConfigureServices(IServiceCollection services)
{
services.AddCommandLine();
}public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseCommandLine(env.WebRootPath);
}
```Add tag into needed view file(ex: Index.razor)
```html
@page "/"
@using Blazor.CommandLine
@using Blazor.Components.CommandLine
@using Blazor.Components.CommandLine.ConsoleHello, world!
```
And to have fancy UI add CSS to host file, _HOST.cshtml
```html
```
### Commands ###
Adding commands to Blazor.CommandLine is not complicated. First create a command class, implement it with ```BaseCommand``` . Then add the command(s) to the BlazorCommandLine's Commands as below;
Within custom command's constructor it is easy to add options to the command. Also command arguments can be enabled for the command as ```System.CommandLine```
To implement the command's main execution just override Execute() or ExecuteAsync() method.
```cshtml
@page "/"
@using Blazor.CommandLine
@using Blazor.Components.CommandLine
@using Blazor.Components.CommandLine.Console@inject IRunningCommand RunningCommand
Hello, world!
@code
{
BlazorCommandLine console;protected override Task OnAfterRenderAsync(bool firstRender)
{
console.Commands.Add(new CommandExample("simple","Description of command"));
console.Commands.Add(new LongCommand("lng","Description of long-running command"));return base.OnAfterRenderAsync(firstRender);
}public class CommandExample : BaseCommand
{
public CommandExample(string name,string description):base(name,description)
{
base.AddOption("-t","Description of option -t");
base.AddOption("-ar","Description of option -ar");base.UseArguments($"Some extra arguments for {name}");
}public override bool Execute(DefaultStreamWriter console,string option1,string option2,string option3,string option4,List arguments)
{
console.Write("This is output of a simple command");
return true;
}
}public class LongCommand : BaseCommand
{
public LongCommand(string name, string description):base(name,description,true)
{
}public override async Task ExecuteAsync(DefaultStreamWriter console, string option1,string option2,string option3,string optionArgument4,List arguments)
{
var i = 0;while (i < 10)
{
await Task.Delay(550);
i++;
}
console.Write("This was a long running command");
return true;
}
}
}
```
### References ###
- Some commands might be long running tasks, Blazor.CommandLine use some features of a great component for this kind of requirement. Please also check https://github.com/h3x4d3c1m4l/Blazor.LoadingIndicator if you need a loading indicator for a Blazor app.