Skip to content

Commit

Permalink
Initial upload
Browse files Browse the repository at this point in the history
  • Loading branch information
YuriyDurov committed Jan 4, 2024
1 parent c72da3a commit f7b6858
Show file tree
Hide file tree
Showing 26 changed files with 591 additions and 2 deletions.
37 changes: 37 additions & 0 deletions .github/workflows/Publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
name: Release BitzArt.Console

on:
repository_dispatch:
push:
tags:
- "BitzArt.Console-v[0-9]+.[0-9]+.[0-9]+*"

env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
NUGET_APIKEY: ${{ secrets.NUGET_APIKEY}}

jobs:

BitzArt-Console:
name: BitzArt.Console
runs-on: ubuntu-latest
steps:

- name: Checkout
uses: actions/checkout@v2

- name: Verify commit
run: |
git fetch --no-tags --prune --depth=1 origin +refs/heads/*:refs/remotes/origin/*
git branch --remote --contains | grep origin/main
- name: Set version
run: echo "VERSION=${GITHUB_REF/refs\/tags\/BitzArt.Console-v/}" >> $GITHUB_ENV

- name: Build
run: |
dotnet build src/BitzArt.Console/BitzArt.Console.csproj --configuration Release /p:Version=${VERSION}
dotnet pack src/BitzArt.Console/BitzArt.Console.csproj --configuration Release /p:Version=${VERSION} --no-build --output .
- name: Push
run: dotnet nuget push BitzArt.Console.${VERSION}.nupkg --source https://api.nuget.org/v3/index.json --api-key ${NUGET_APIKEY}
25 changes: 25 additions & 0 deletions .github/workflows/Tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: Tests

on:
push:
branches:
- main
tags-ignore:
- '*'

jobs:
tests:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2

- name: Install dependencies
run: dotnet restore

- name: Build
run: dotnet build --configuration Release --no-restore

- name: Test
run: dotnet test --no-restore --verbosity normal
57 changes: 57 additions & 0 deletions BitzArt.Console.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.8.34330.188
MinimumVisualStudioVersion = 10.0.40219.1
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{E8566731-C3CC-4CB9-85E0-72357C6BFDCA}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "sample", "sample", "{FF001A93-A89D-483A-B0F7-1D85F419CC86}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tests", "tests", "{9410E858-3224-4A08-9D28-658D5ADA7147}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BitzArt.Console.TestApp", "sample\BitzArt.Console.TestApp\BitzArt.Console.TestApp.csproj", "{B936B06A-384C-43CD-98AA-95233A4042B8}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BitzArt.Console", "src\BitzArt.Console\BitzArt.Console.csproj", "{50E9BDF9-C9D5-455F-BEFD-546F40BA4269}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BitzArt.Console.Tests", "tests\BitzArt.Console.Tests\BitzArt.Console.Tests.csproj", "{7AC4B407-8B8F-4547-A0A9-A45A80B9C4A8}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".github", ".github", "{E4EB97B3-DECB-4C34-B7F4-55EDD0A680AA}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "workflows", "workflows", "{59D0F46E-803A-4616-A5AF-CA2FAFB2E508}"
ProjectSection(SolutionItems) = preProject
.github\workflows\Publish.yml = .github\workflows\Publish.yml
.github\workflows\Tests.yml = .github\workflows\Tests.yml
EndProjectSection
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{B936B06A-384C-43CD-98AA-95233A4042B8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B936B06A-384C-43CD-98AA-95233A4042B8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B936B06A-384C-43CD-98AA-95233A4042B8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B936B06A-384C-43CD-98AA-95233A4042B8}.Release|Any CPU.Build.0 = Release|Any CPU
{50E9BDF9-C9D5-455F-BEFD-546F40BA4269}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{50E9BDF9-C9D5-455F-BEFD-546F40BA4269}.Debug|Any CPU.Build.0 = Debug|Any CPU
{50E9BDF9-C9D5-455F-BEFD-546F40BA4269}.Release|Any CPU.ActiveCfg = Release|Any CPU
{50E9BDF9-C9D5-455F-BEFD-546F40BA4269}.Release|Any CPU.Build.0 = Release|Any CPU
{7AC4B407-8B8F-4547-A0A9-A45A80B9C4A8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{7AC4B407-8B8F-4547-A0A9-A45A80B9C4A8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7AC4B407-8B8F-4547-A0A9-A45A80B9C4A8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7AC4B407-8B8F-4547-A0A9-A45A80B9C4A8}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{B936B06A-384C-43CD-98AA-95233A4042B8} = {FF001A93-A89D-483A-B0F7-1D85F419CC86}
{50E9BDF9-C9D5-455F-BEFD-546F40BA4269} = {E8566731-C3CC-4CB9-85E0-72357C6BFDCA}
{7AC4B407-8B8F-4547-A0A9-A45A80B9C4A8} = {9410E858-3224-4A08-9D28-658D5ADA7147}
{59D0F46E-803A-4616-A5AF-CA2FAFB2E508} = {E4EB97B3-DECB-4C34-B7F4-55EDD0A680AA}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {7B835038-140D-4D0B-8B20-E8931CFA0DE0}
EndGlobalSection
EndGlobal
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,9 @@
# ConsoleApp
A framework for building console apps
@@ -1,10 +0,0 @@
# BitzArt.Console

[![NuGet](https://img.shields.io/nuget/v/BitzArt.Console.svg)](https://www.nuget.org/packages/BitzArt.Console/)
[![NuGet](https://img.shields.io/nuget/dt/BitzArt.Console.svg)](https://www.nuget.org/packages/BitzArt.Console/)

A modern .Net console application framework

> **Note:** This project is in development and not ready for production use.
15 changes: 15 additions & 0 deletions sample/BitzArt.Console.TestApp/BitzArt.Console.TestApp.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<RootNamespace>BitzArt.Console</RootNamespace>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\BitzArt.Console\BitzArt.Console.csproj" />
</ItemGroup>

</Project>
8 changes: 8 additions & 0 deletions sample/BitzArt.Console.TestApp/Menus/MainMenu.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace BitzArt.Console;

internal class MainMenu(IConsoleAppNavigationManager navigation) : ConsoleMenuBase
{
public override string Title => "Main Menu";

public IConsoleAppNavigationManager Navigation { get; } = navigation;
}
6 changes: 6 additions & 0 deletions sample/BitzArt.Console.TestApp/Menus/SubMenu1.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace BitzArt.Console;

internal class SubMenu1 : ConsoleMenuBase
{
public override string Title => "Submenu 1";
}
6 changes: 6 additions & 0 deletions sample/BitzArt.Console.TestApp/Menus/SubMenu2.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace BitzArt.Console;

internal class SubMenu2 : ConsoleMenuBase
{
public override string Title => "Submenu 2";
}
15 changes: 15 additions & 0 deletions sample/BitzArt.Console.TestApp/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace BitzArt.Console;

internal class Program
{
static void Main()
{
var builder = new ConsoleAppBuilder();

builder.Services.AddConsoleMenusFromAssemblyContaining<Program>();

var app = builder.Build();

app.Run<MainMenu>();
}
}
14 changes: 14 additions & 0 deletions src/BitzArt.Console/App/ConsoleApp.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using Microsoft.Extensions.DependencyInjection;

namespace BitzArt.Console;

public class ConsoleApp(ConsoleAppBuilder builder)
{
public IServiceProvider Services { get; private set; } = builder.Services.BuildServiceProvider();

public void Run<TConsoleMenu>(CancellationToken cancellationToken = default) where TConsoleMenu : IConsoleMenu
{
var consoleMenu = Services.GetRequiredService<TConsoleMenu>();
consoleMenu.RunAsync().Wait(cancellationToken);
}
}
17 changes: 17 additions & 0 deletions src/BitzArt.Console/App/ConsoleAppBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using Microsoft.Extensions.DependencyInjection;

namespace BitzArt.Console;

public class ConsoleAppBuilder
{
public IServiceCollection Services { get; private set; }

public ConsoleAppBuilder()
{
Services = new ServiceCollection();

Services.AddSingleton<IConsoleAppNavigationManager, ConsoleAppNavigationManager>();
}

public ConsoleApp Build() => new(this);
}
35 changes: 35 additions & 0 deletions src/BitzArt.Console/BitzArt.Console.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<RootNamespace>BitzArt.Console</RootNamespace>

<PackageId>BitzArt.Console</PackageId>
<Authors>BitzArt</Authors>
<Description></Description>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<RepositoryType>git</RepositoryType>
<RepositoryUrl>https://github.com/BitzArt/Console</RepositoryUrl>
<PackageProjectUrl>https://github.com/BitzArt/Console</PackageProjectUrl>
<PackageReadmeFile>README.md</PackageReadmeFile>
</PropertyGroup>

<ItemGroup>
<None Include="../../README.md" Pack="True" PackagePath="\" Visible="false" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="8.0.0" />
<PackageReference Include="Spectre.Console" Version="0.48.0" />
<PackageReference Include="Spectre.Console.Cli" Version="0.48.0" />
</ItemGroup>

<ItemGroup>
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleTo">
<_Parameter1>BitzArt.Console.Tests</_Parameter1>
</AssemblyAttribute>
</ItemGroup>
</Project>
45 changes: 45 additions & 0 deletions src/BitzArt.Console/Extensions/AddConsoleMenuExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using Microsoft.Extensions.DependencyInjection;
using System.Reflection;

namespace BitzArt.Console;

public static class AddConsoleMenuExtensions
{
public static IServiceCollection AddConsoleMenusFromAssemblyContaining<TAssemblyPointer>(this IServiceCollection services)
=> services.AddConsoleMenusFromAssemblyContaining(typeof(TAssemblyPointer));

public static IServiceCollection AddConsoleMenusFromAssemblyContaining(this IServiceCollection services, Type type)
=> services.AddConsoleMenusFromAssembly(type.Assembly);

public static IServiceCollection AddConsoleMenusFromAssembly(this IServiceCollection services, Assembly assembly)
{
var tools = assembly
.DefinedTypes
.Where(x => x.IsAbstract == false)
.Where(x => x.GetInterfaces().Contains(typeof(IConsoleMenu)));

foreach (var tool in tools) services.AddConsoleMenu(tool);

return services;
}

public static IServiceCollection AddConsoleMenu<TConsoleMenu>(this IServiceCollection services)
where TConsoleMenu : class, IConsoleMenu
{
services.AddTransient<TConsoleMenu>();
services.AddTransient<IConsoleMenu, TConsoleMenu>();

return services;
}

public static IServiceCollection AddConsoleMenu(this IServiceCollection services, Type type)
{
if (type is null) throw new ArgumentException($"{nameof(type)} must not be null");
if (type.IsAssignableTo(typeof(IConsoleMenu)) == false) throw new ArgumentException($"{type.Name} is not assignable to IConsoleMenu");

services.AddTransient(type);
services.AddTransient(x => (IConsoleMenu)x.GetRequiredService(type));

return services;
}
}
23 changes: 23 additions & 0 deletions src/BitzArt.Console/Extensions/RunConsoleMenuExtension.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using Microsoft.Extensions.DependencyInjection;

namespace BitzArt.Console;

public static class RunConsoleMenuExtension
{
public static async Task RunConsoleMenuAsync<TConsoleMenu>(this IServiceProvider serviceProvider)
where TConsoleMenu : IConsoleMenu
{
var tool = serviceProvider.GetRequiredService<TConsoleMenu>();

await tool.RunAsync();
}

public static async Task RunConsoleMenuAsync(this IServiceProvider serviceProvider, Type menuType)
{
if (!menuType.IsAssignableTo(typeof(IConsoleMenu))) throw new ArgumentException($"Menu Type must implement {nameof(IConsoleMenu)}", nameof(menuType));

var tool = (IConsoleMenu)serviceProvider.GetRequiredService(menuType);

await tool.RunAsync();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace BitzArt.Console;

public interface IConsoleAppNavigationManager
{
Task NavigateAsync(Type menuType);
Task NavigateAsync<T>() where T : IConsoleMenu;
}
6 changes: 6 additions & 0 deletions src/BitzArt.Console/Interfaces/IConsoleMenu.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace BitzArt.Console;

public interface IConsoleMenu
{
public Task RunAsync();
}
32 changes: 32 additions & 0 deletions src/BitzArt.Console/Menus/ConsoleMenuBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using Spectre.Console;

namespace BitzArt.Console;

public abstract class ConsoleMenuBase : IConsoleMenu
{
public virtual string Title => "Menu";

public async Task RunAsync()
{
Render();

OnRendered();
await OnRenderedAsync();
}

public virtual void Render()
{
AnsiConsole.Clear();
AnsiConsoleMenu.WriteTitle(Title);
}

public virtual void OnRendered()
{

}

public virtual Task OnRenderedAsync()
{
return Task.CompletedTask;
}
}
8 changes: 8 additions & 0 deletions src/BitzArt.Console/Menus/ConsolePanelMenuBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using Spectre.Console;

namespace BitzArt.Console;

public abstract class ConsolePanelMenuBase(Panel panel) : ConsoleMenuBase
{
public Panel Panel { get; set; } = panel;
}
Loading

0 comments on commit f7b6858

Please sign in to comment.