From 39a74726dac0a9b98186991e60126cb64c983af5 Mon Sep 17 00:00:00 2001 From: Enis Necipoglu Date: Wed, 17 Apr 2024 14:04:51 +0300 Subject: [PATCH] Add bundle list command --- src/AbpDevTools/Commands/AbpBundleCommand.cs | 37 +++-------- .../Commands/AbpBundleListCommand.cs | 62 +++++++++++++++++++ src/AbpDevTools/Program.cs | 1 + 3 files changed, 72 insertions(+), 28 deletions(-) create mode 100644 src/AbpDevTools/Commands/AbpBundleListCommand.cs diff --git a/src/AbpDevTools/Commands/AbpBundleCommand.cs b/src/AbpDevTools/Commands/AbpBundleCommand.cs index bdbdbdc..89b744f 100644 --- a/src/AbpDevTools/Commands/AbpBundleCommand.cs +++ b/src/AbpDevTools/Commands/AbpBundleCommand.cs @@ -15,6 +15,12 @@ public class AbpBundleCommand : ICommand public bool GraphBuild { get; set; } protected IConsole? console; + protected AbpBundleListCommand listCommand; + + public AbpBundleCommand(AbpBundleListCommand listCommand) + { + this.listCommand = listCommand; + } public async ValueTask ExecuteAsync(IConsole console) { @@ -37,10 +43,7 @@ public async ValueTask ExecuteAsync(IConsole console) await Task.Yield(); - return Directory.EnumerateFiles(WorkingDirectory, "*.csproj", SearchOption.AllDirectories) - .Where(IsCsprojBlazorWasm) - .Select(x => new FileInfo(x)) - .ToArray(); + return listCommand.GetWasmProjects(); }); if (wasmCsprojs.Length == 0) @@ -56,7 +59,8 @@ public async ValueTask ExecuteAsync(IConsole console) { if (GraphBuild) { - var compiled = await AnsiConsole.Status().StartAsync($"[grey]Building {csproj.Name}...[/]", async (ctx) => + var index = Array.IndexOf(wasmCsprojs, csproj) + 1; + var compiled = await AnsiConsole.Status().StartAsync($"[grey]{index/wasmCsprojs.Length} Building {csproj.Name}...[/]", async (ctx) => { ctx.Spinner(Spinner.Known.SimpleDotsScrolling); @@ -107,27 +111,4 @@ public async ValueTask ExecuteAsync(IConsole console) }); } } - - static bool IsCsprojBlazorWasm(string file) - { - using var fileStream = new FileStream(file, FileMode.Open, FileAccess.Read); - using var streamReader = new StreamReader(fileStream, Encoding.UTF8, true); - - for (int i = 0; i < 4; i++) - { - var line = streamReader.ReadLine(); - - if (string.IsNullOrEmpty(line)) - { - continue; - } - - if (line.Contains("Sdk=\"Microsoft.NET.Sdk.BlazorWebAssembly\"")) - { - return true; - } - } - - return false; - } } diff --git a/src/AbpDevTools/Commands/AbpBundleListCommand.cs b/src/AbpDevTools/Commands/AbpBundleListCommand.cs new file mode 100644 index 0000000..d5c47f0 --- /dev/null +++ b/src/AbpDevTools/Commands/AbpBundleListCommand.cs @@ -0,0 +1,62 @@ +using System.Text; +using CliFx.Infrastructure; + +[Command("bundle list", Description = "List projects that needs to run 'abp bundle'.")] +public class AbpBundleListCommand : ICommand +{ + [CommandParameter(0, IsRequired = false, Description = "Working directory for the command. Probably project or solution directory path goes here. Default: . (Current Directory)")] + public string? WorkingDirectory { get; set; } + + public async ValueTask ExecuteAsync(IConsole console) + { + if (string.IsNullOrEmpty(WorkingDirectory)) + { + WorkingDirectory = Directory.GetCurrentDirectory(); + } + + var wasmCsprojs = GetWasmProjects(); + + if (wasmCsprojs.Length == 0) + { + await console.Output.WriteLineAsync("No Blazor WASM projects found. No files to bundle."); + + return; + } + + await console.Output.WriteLineAsync("Blazor WASM projects found:"); + foreach (var csproj in wasmCsprojs) + { + await console.Output.WriteLineAsync($"- {csproj.FullName}"); + } + } + + public FileInfo[] GetWasmProjects(){ + return Directory.EnumerateFiles(WorkingDirectory!, "*.csproj", SearchOption.AllDirectories) + .Where(IsCsprojBlazorWasm) + .Select(x => new FileInfo(x)) + .ToArray(); + } + + static bool IsCsprojBlazorWasm(string file) + { + using var fileStream = new FileStream(file, FileMode.Open, FileAccess.Read); + using var streamReader = new StreamReader(fileStream, Encoding.UTF8, true); + + for (int i = 0; i < 4; i++) + { + var line = streamReader.ReadLine(); + + if (string.IsNullOrEmpty(line)) + { + continue; + } + + if (line.Contains("Sdk=\"Microsoft.NET.Sdk.BlazorWebAssembly\"")) + { + return true; + } + } + + return false; + } +} \ No newline at end of file diff --git a/src/AbpDevTools/Program.cs b/src/AbpDevTools/Program.cs index a7d1ed4..e5dcc66 100644 --- a/src/AbpDevTools/Program.cs +++ b/src/AbpDevTools/Program.cs @@ -54,6 +54,7 @@ public static CliApplicationBuilder BuildServices(this CliApplicationBuilder bui typeof(EnvironmentCommand), typeof(EnvironmentConfigurationCommand), typeof(AbpBundleCommand), + typeof(AbpBundleListCommand), typeof(TestCommand), typeof(UpdateCheckCommand), typeof(CleanCommand),