GithubHelp home page GithubHelp logo

wanghao-skight / blazor.console Goto Github PK

View Code? Open in Web Editor NEW

This project forked from ardacetinkaya/blazor.console

0.0 0.0 0.0 1.51 MB

A simple component to mock CLI for ASP.NET Core 3.0 Blazor applications to execute some custom commands for an application

HTML 5.23% C# 84.82% CSS 9.95%

blazor.console's Introduction

Blazor.CommandLine (a.k.a Blazor.Console)

Actions Status NuGet version (Blzr.Components.CommandLine)

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 resources

This 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

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)

@page "/"
@using Blazor.CommandLine
@using Blazor.Components.CommandLine
@using Blazor.Components.CommandLine.Console

<h1>Hello, world!</h1>

<BlazorCommandLine @ref="console" Name="Some Demo App v1.0.0"/>

And to have fancy UI add CSS to host file, _HOST.cshtml

<link href="Blazor.CommandLine/styles.css" rel="stylesheet" />

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.

@page "/"
@using Blazor.CommandLine
@using Blazor.Components.CommandLine
@using Blazor.Components.CommandLine.Console

@inject IRunningCommand RunningCommand

<h1>Hello, world!</h1>

<BlazorCommandLine @ref="console" />

@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<string> 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<bool> ExecuteAsync(DefaultStreamWriter console, string option1,string option2,string option3,string optionArgument4,List<string> arguments)
        {
            var i = 0;

            while (i < 10)
            {
                await Task.Delay(550);
                i++;
            }
            
            console.Write("This was a long running command");
            return true;
        }
    }
}

References

blazor.console's People

Contributors

ardacetinkaya avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.