add: CLI support.
All checks were successful
Build blog docker image / Build-Blog-Image (push) Successful in 1m35s
All checks were successful
Build blog docker image / Build-Blog-Image (push) Successful in 1m35s
This commit is contained in:
parent
7b7a63481a
commit
e6ed407285
30
YaeBlog/Commands/BlogOptionsBinder.cs
Normal file
30
YaeBlog/Commands/BlogOptionsBinder.cs
Normal file
|
@ -0,0 +1,30 @@
|
|||
using System.CommandLine.Binding;
|
||||
using System.Text.Json;
|
||||
using YaeBlog.Core.Models;
|
||||
|
||||
namespace YaeBlog.Commands;
|
||||
|
||||
public sealed class BlogOptionsBinder : BinderBase<BlogOptions>
|
||||
{
|
||||
protected override BlogOptions GetBoundValue(BindingContext bindingContext)
|
||||
{
|
||||
FileInfo settings = new(Path.Combine(Environment.CurrentDirectory, "appsettings.json"));
|
||||
if (!settings.Exists)
|
||||
{
|
||||
throw new InvalidOperationException("Failed to load YaeBlog configurations.");
|
||||
}
|
||||
|
||||
using StreamReader reader = settings.OpenText();
|
||||
using JsonDocument document = JsonDocument.Parse(reader.ReadToEnd());
|
||||
JsonElement root = document.RootElement;
|
||||
JsonElement optionSection = root.GetProperty(BlogOptions.OptionName);
|
||||
|
||||
BlogOptions? result = optionSection.Deserialize<BlogOptions>();
|
||||
if (result is null)
|
||||
{
|
||||
throw new InvalidOperationException("Failed to load YaeBlog configuration in appsettings.json.");
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
90
YaeBlog/Commands/CommandExtensions.cs
Normal file
90
YaeBlog/Commands/CommandExtensions.cs
Normal file
|
@ -0,0 +1,90 @@
|
|||
using System.CommandLine;
|
||||
using YaeBlog.Components;
|
||||
using YaeBlog.Core.Extensions;
|
||||
|
||||
namespace YaeBlog.Commands;
|
||||
|
||||
public static class CommandExtensions
|
||||
{
|
||||
public static Command AddServeCommand(this RootCommand rootCommand)
|
||||
{
|
||||
Command serveCommand = new("serve", "Start http server.");
|
||||
rootCommand.AddCommand(serveCommand);
|
||||
|
||||
serveCommand.SetHandler(async context =>
|
||||
{
|
||||
WebApplicationBuilder builder = WebApplication.CreateBuilder();
|
||||
|
||||
builder.Services.AddRazorComponents()
|
||||
.AddInteractiveServerComponents();
|
||||
builder.Services.AddControllers();
|
||||
builder.Services.AddBlazorBootstrap();
|
||||
builder.AddYaeBlog();
|
||||
|
||||
WebApplication application = builder.Build();
|
||||
|
||||
application.UseStaticFiles();
|
||||
application.UseAntiforgery();
|
||||
application.UseYaeBlog();
|
||||
|
||||
application.MapRazorComponents<App>()
|
||||
.AddInteractiveServerRenderMode();
|
||||
application.MapControllers();
|
||||
|
||||
CancellationToken token = context.GetCancellationToken();
|
||||
await application.RunAsync(token);
|
||||
});
|
||||
|
||||
return rootCommand;
|
||||
}
|
||||
|
||||
public static Command AddNewCommand(this RootCommand rootCommand)
|
||||
{
|
||||
Command newCommand = new("new", "Create a new blog file and image directory.");
|
||||
rootCommand.AddCommand(newCommand);
|
||||
|
||||
Argument<string> filenameArgument = new(name: "blog name", description: "The created blog filename.");
|
||||
newCommand.AddArgument(filenameArgument);
|
||||
|
||||
newCommand.SetHandler(async (file, blogOptions) =>
|
||||
{
|
||||
string fileWithExtension;
|
||||
if (file.EndsWith(".md"))
|
||||
{
|
||||
fileWithExtension = file;
|
||||
file = fileWithExtension[..fileWithExtension.LastIndexOf('.')];
|
||||
}
|
||||
else
|
||||
{
|
||||
fileWithExtension = file + ".md";
|
||||
}
|
||||
|
||||
DirectoryInfo rootDir = new(Path.Combine(Environment.CurrentDirectory, blogOptions.Root));
|
||||
if (!rootDir.Exists)
|
||||
{
|
||||
throw new InvalidOperationException($"Blog source directory '{blogOptions.Root} doesn't exist.");
|
||||
}
|
||||
|
||||
if (rootDir.EnumerateFiles().Any(f => f.Name == fileWithExtension))
|
||||
{
|
||||
throw new InvalidOperationException($"Target blog '{file}' has been created!");
|
||||
}
|
||||
|
||||
FileInfo newBlogFile = new(Path.Combine(rootDir.FullName, fileWithExtension));
|
||||
await using StreamWriter newStream = newBlogFile.CreateText();
|
||||
|
||||
await newStream.WriteAsync($"""
|
||||
---
|
||||
title: {file}
|
||||
tags:
|
||||
---
|
||||
<!--more-->
|
||||
""");
|
||||
|
||||
Console.WriteLine($"Created new blog '{file}.");
|
||||
}, filenameArgument, new BlogOptionsBinder());
|
||||
|
||||
|
||||
return newCommand;
|
||||
}
|
||||
}
|
|
@ -5,4 +5,4 @@ COPY bin/Release/net8.0/publish/ ./
|
|||
COPY source/ ./source/
|
||||
COPY appsettings.json .
|
||||
|
||||
ENTRYPOINT ["dotnet", "YaeBlog.dll"]
|
||||
ENTRYPOINT ["dotnet", "YaeBlog.dll", "serve"]
|
||||
|
|
|
@ -1,22 +1,9 @@
|
|||
using YaeBlog.Components;
|
||||
using YaeBlog.Core.Extensions;
|
||||
using System.CommandLine;
|
||||
using YaeBlog.Commands;
|
||||
|
||||
WebApplicationBuilder builder = WebApplication.CreateBuilder(args);
|
||||
RootCommand rootCommand = new("YaeBlog CLI");
|
||||
|
||||
builder.Services.AddRazorComponents()
|
||||
.AddInteractiveServerComponents();
|
||||
builder.Services.AddControllers();
|
||||
builder.Services.AddBlazorBootstrap();
|
||||
builder.AddYaeBlog();
|
||||
rootCommand.AddServeCommand();
|
||||
rootCommand.AddNewCommand();
|
||||
|
||||
WebApplication application = builder.Build();
|
||||
|
||||
application.UseStaticFiles();
|
||||
application.UseAntiforgery();
|
||||
application.UseYaeBlog();
|
||||
|
||||
application.MapRazorComponents<App>()
|
||||
.AddInteractiveServerRenderMode();
|
||||
application.MapControllers();
|
||||
|
||||
await application.RunAsync();
|
||||
await rootCommand.InvokeAsync(args);
|
||||
|
|
|
@ -12,7 +12,7 @@
|
|||
"http": {
|
||||
"commandName": "Project",
|
||||
"dotnetRunMessages": true,
|
||||
"launchBrowser": true,
|
||||
"launchBrowser": false,
|
||||
"applicationUrl": "http://localhost:5275",
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
|
|
|
@ -6,6 +6,7 @@
|
|||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Blazor.Bootstrap" Version="3.0.0-preview.2" />
|
||||
<PackageReference Include="System.CommandLine" Version="2.0.0-beta4.22272.1" />
|
||||
</ItemGroup>
|
||||
|
||||
<PropertyGroup>
|
||||
|
|
Loading…
Reference in New Issue
Block a user