Skip to content

Commit

Permalink
♻️ Refactor Source Code Generators and Migrate to net8.0
Browse files Browse the repository at this point in the history
- Source genereators use IIncrementalGenerator
- Added API Routes Generator
- Added Mardown Support (Page.md)
  • Loading branch information
koeeenig committed Dec 13, 2023
1 parent d952697 commit afed1db
Show file tree
Hide file tree
Showing 114 changed files with 2,006 additions and 760 deletions.
23 changes: 15 additions & 8 deletions BlazeKit.sln
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ VisualStudioVersion = 17.7.34009.444
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BlazeKit", "src\BlazeKit\BlazeKit.csproj", "{93775A2B-DB61-4195-9A38-D0C6BEF80267}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BlazeKit.Reactive", "src\BlazeKit.Reactive\BlazeKit.Reactive.csproj", "{1C08F700-1DC7-4B33-842E-2AFD3BA76B38}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BlazeKit.Deployment.Vercel", "src\BlazeKit.Deployment.Vercel\BlazeKit.Deployment.Vercel.csproj", "{CB3D0D42-28F1-4AFA-8BC9-37A1523A4583}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BlazeKit.Abstraction", "src\BlazeKit.Abstraction\BlazeKit.Abstraction.csproj", "{DD6DE816-D517-47D6-9EDB-3A30E0213BEC}"
Expand All @@ -27,22 +25,20 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BlazeKit.Reactive.Tests", "
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "_build", "build\_build.csproj", "{6B6EFC67-31B4-4D2E-98D5-1A731AF98B0B}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BlazeKit.Reactivity", "src\BlazeKit.Reactivity\BlazeKit.Reactivity.csproj", "{3CD31A61-9A86-4D79-A7A6-B6A4AD0BF7D8}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BlazeKit.Routing.TestApp", "tests\BlazeKit.Routing.TestApp\BlazeKit.Routing.TestApp.csproj", "{196C36B5-D580-4C2D-BDFF-AFD530255F2E}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{6B6EFC67-31B4-4D2E-98D5-1A731AF98B0B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{6B6EFC67-31B4-4D2E-98D5-1A731AF98B0B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{93775A2B-DB61-4195-9A38-D0C6BEF80267}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{93775A2B-DB61-4195-9A38-D0C6BEF80267}.Debug|Any CPU.Build.0 = Debug|Any CPU
{93775A2B-DB61-4195-9A38-D0C6BEF80267}.Release|Any CPU.ActiveCfg = Release|Any CPU
{93775A2B-DB61-4195-9A38-D0C6BEF80267}.Release|Any CPU.Build.0 = Release|Any CPU
{1C08F700-1DC7-4B33-842E-2AFD3BA76B38}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1C08F700-1DC7-4B33-842E-2AFD3BA76B38}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1C08F700-1DC7-4B33-842E-2AFD3BA76B38}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1C08F700-1DC7-4B33-842E-2AFD3BA76B38}.Release|Any CPU.Build.0 = Release|Any CPU
{CB3D0D42-28F1-4AFA-8BC9-37A1523A4583}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{CB3D0D42-28F1-4AFA-8BC9-37A1523A4583}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CB3D0D42-28F1-4AFA-8BC9-37A1523A4583}.Release|Any CPU.ActiveCfg = Release|Any CPU
Expand Down Expand Up @@ -71,6 +67,16 @@ Global
{4D98C97D-CFCC-4473-BFD8-B28DA3199EF6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4D98C97D-CFCC-4473-BFD8-B28DA3199EF6}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4D98C97D-CFCC-4473-BFD8-B28DA3199EF6}.Release|Any CPU.Build.0 = Release|Any CPU
{6B6EFC67-31B4-4D2E-98D5-1A731AF98B0B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{6B6EFC67-31B4-4D2E-98D5-1A731AF98B0B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3CD31A61-9A86-4D79-A7A6-B6A4AD0BF7D8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3CD31A61-9A86-4D79-A7A6-B6A4AD0BF7D8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3CD31A61-9A86-4D79-A7A6-B6A4AD0BF7D8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3CD31A61-9A86-4D79-A7A6-B6A4AD0BF7D8}.Release|Any CPU.Build.0 = Release|Any CPU
{196C36B5-D580-4C2D-BDFF-AFD530255F2E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{196C36B5-D580-4C2D-BDFF-AFD530255F2E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{196C36B5-D580-4C2D-BDFF-AFD530255F2E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{196C36B5-D580-4C2D-BDFF-AFD530255F2E}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -79,6 +85,7 @@ Global
{CB3D0D42-28F1-4AFA-8BC9-37A1523A4583} = {B5C21DC5-2745-4530-A317-F4CD52DBE22E}
{D466841A-00F1-4E37-BEB3-F7DEAF7EABB1} = {952B525D-297C-4F66-85E2-967C9982771F}
{4D98C97D-CFCC-4473-BFD8-B28DA3199EF6} = {952B525D-297C-4F66-85E2-967C9982771F}
{196C36B5-D580-4C2D-BDFF-AFD530255F2E} = {952B525D-297C-4F66-85E2-967C9982771F}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {8F0560BE-EE70-4DE3-8ACA-590A2B56C96A}
Expand Down
32 changes: 32 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,35 @@ BlazeKit aims to provide Meta-framework-like Features for Blazor which has been
- 🪄 Auto-Generated Route Parameters

Some more infos can be found at [blazekit.dev](https://blazekit.dev) but keep in mind BlazeKit is work in progress.

# 🚀 Getting Started
Before creating your first BlazeKit project, you should ensure that your local machine has:
- .NET 8 - you can find the download [here](https://dotnet.microsoft.com/en-us/download)

That's it 👍

## ✨ Create an app using the CLI
After you have installed the required dependencies, the easiest way to get a project up an running is by using the BlazeKit CLI.
```ps
# Install the BlazeKit CLI
dotnet tool install --global BlazeKit.CLI --version 0.1.0-alpha.2
```
Now simply create your first BlazeKit project by running the following command
```ps
bkit new NextUnicorn
```
You will be prompted to choose one of the Blazor Hosting Models such as **Blazor WebAssembly** or **Blazor Server**.
Afterwards a BlazeKit app will be created with the choosen hosting model.
Next **_cd_** into your project folder and start .NET's local development server using the **_dotnet watch_** command
```ps
cd NextUnicorn
dotnet watch
```
And here it is, your first BalzeKit project 🎉

## 🔄️ Updating the BlazeKit CLI
The easiest way to update the BlazeKit CLI is to simply uninstall and reinstall it.
```ps
dotnet tool uninstall -g BlazeKit.CLI
dotnet tool install -g BlazeKit.CLI --version 0.1.0-alpha.2
```
1 change: 1 addition & 0 deletions src/BlazeKit.Abstraction/BlazeKit.Abstraction.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<LangVersion>latest</LangVersion>
</PropertyGroup>

</Project>
12 changes: 12 additions & 0 deletions src/BlazeKit.Abstraction/IDeleteRequest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;

namespace BlazeKit.Abstraction;
/// <summary>
/// Interface for Delete Request
/// </summary>
public interface IDeleteRequest
{
Delegate Delete();
}


9 changes: 9 additions & 0 deletions src/BlazeKit.Abstraction/IGetRequest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System;

namespace BlazeKit.Abstraction;
public interface IGetRequest
{
Delegate Get();
}


12 changes: 12 additions & 0 deletions src/BlazeKit.Abstraction/IPatchRequest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;

namespace BlazeKit.Abstraction;
/// <summary>
/// Interface for Patch Request
/// </summary>
public interface IPatchRequest
{
Delegate Patch();
}


10 changes: 10 additions & 0 deletions src/BlazeKit.Abstraction/IPostRequest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System;

namespace BlazeKit.Abstraction;
/// <summary>
/// Interface for Post Request
/// </summary>
public interface IPostRequest
{
Delegate Post();
}
12 changes: 12 additions & 0 deletions src/BlazeKit.Abstraction/IPutRequest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;

namespace BlazeKit.Abstraction;
/// <summary>
/// Interface for Put Request
/// </summary>
public interface IPutRequest
{
Delegate Put();
}


2 changes: 1 addition & 1 deletion src/BlazeKit.CLI/BlazeKit.CLI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<PackAsTool>true</PackAsTool>
Expand Down
Binary file not shown.
Binary file not shown.
178 changes: 146 additions & 32 deletions src/BlazeKit.CLI/Commands/New/NewCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
using Spectre.Console.Cli;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.IO.Compression;
using Yaapii.Atoms.IO;

namespace BlazeKit.CLI.Commands.New
{
Expand All @@ -21,56 +19,172 @@ public override async Task<int> ExecuteAsync([NotNull] CommandContext context, [
return 1;
}

Directory.CreateDirectory(projectDir);

// Ask for the user which template to use
// Ask for the user which template to use
var blazorAppType = AnsiConsole.Prompt(
new SelectionPrompt<string>()
.Title("Hosting model?")
.Title("Choose which type of Blazor App you would like to scaffold:")
.PageSize(10)
.MoreChoicesText("[grey](Move up and down to reveal more fruits)[/]")
.AddChoices(new[] {
"Blazor WebAssembly",
"Blazor Server"
"Blazor WebApp"
}));

var template = "TPL_NEW_PROJECT.zip";
switch (blazorAppType) {
switch (blazorAppType) {
case "Blazor WebAssembly":
template = "TPL_NEW_PROJECT.zip";
AnsiConsole.Status()
.Start("Creating Blazor WebAssembly project...", ctx =>
{
ctx.Spinner(Spinner.Known.Star);
ctx.SpinnerStyle(Style.Parse("green"));
CreateWasmProject(settings);
});
break;
case "Blazor Server":
template = "TPL_NEW_PROJECT_SERVER.zip";
case "Blazor WebApp":
AnsiConsole.Status()
.Start("Creating Blazor WebApp project...", ctx =>
{
ctx.Spinner(Spinner.Known.Star);
ctx.SpinnerStyle(Style.Parse("green"));
CreateWebAppProject(settings);
});
break;
default:
AnsiConsole.MarkupLine($"[red]Error:[/] Unknown Blazor App type [bold]{blazorAppType}[/].");
return 1;
}

using (var zip = new ZipArchive(new ResourceOf($"Commands/New/Assets/{template}", this.GetType()).Stream()))
{
zip.ExtractToDirectory(projectDir);
}

foreach (var file in Directory.GetFiles(projectDir, "*.*",SearchOption.AllDirectories))
{
File.WriteAllText(file, File.ReadAllText(file).Replace("[PROJECTNAME]", settings.Name));
}
// rename the csproj file
var csproj = Directory.GetFiles(projectDir, "*.csproj", SearchOption.TopDirectoryOnly);
if(csproj.Length == 1)
// list all created files in the project directory
AnsiConsole.MarkupLine("[yellow]Created files:[/]");
foreach(var file in Directory.GetFiles(projectDir, "*", SearchOption.AllDirectories))
{
File.Move(csproj[0], Path.Combine(projectDir,$"{settings.Name}.csproj"));
}

// print a list of created files
AnsiConsole.MarkupLine($"[bold]Created files:[/]");
foreach (var file in Directory.GetFiles(projectDir, "*.*", SearchOption.AllDirectories))
{
AnsiConsole.MarkupLine($"[green]{file.Replace(projectDir, "").TrimStart('/','\\')}[/]");
AnsiConsole.MarkupLine($"[green]{file.Replace(projectDir, "")}[/]");
}

AnsiConsole.WriteLine();
AnsiConsole.MarkupLine($"[green]Project [bold]{settings.Name}[/] created.[/]");

return 0;
}

private async void CreateWebAppProject(NewSettings settings)
{
var projectDir = Path.Combine(Directory.GetCurrentDirectory(), settings.Name);
// call dotnet new
DotNetNewProject(settings, "blazor");

AnsiConsole.MarkupLine("[yellow]Scaffolding BlazeKit into the project...[/]");
// remove the Component/Layout folder
Directory.Delete(Path.Combine(projectDir, "Components", "Layout"), true);
// remove the first line of the Components/Pages.Error.razor file
var lines = File.ReadAllLines(Path.Combine(projectDir, "Components", "Pages", "Error.razor"));

new FileInfo(Path.Combine(projectDir, "Routes", "Error", "Page.razor")).Directory.Create();
File.WriteAllLines(Path.Combine(projectDir, "Routes", "Error", "Page.razor"), lines.Skip(1).ToArray());

// move the Components/Pages/App.razor and Components/Pages/_Imports.razor to the project directory
File.Move(Path.Combine(projectDir, "Components", "App.razor"), Path.Combine(projectDir, "App.razor"));
File.Move(Path.Combine(projectDir, "Components", "_Imports.razor"), Path.Combine(projectDir, "_Imports.razor"));
// replace Layout.MainLayout with Layout int Routes.razor
var lines2 = File.ReadAllLines(Path.Combine(projectDir, "Components", "Routes.razor"));
File.WriteAllLines(Path.Combine(projectDir, "Components", "Routes.razor"), lines2.Select(l => l.Replace("Layout.MainLayout", $"{new SanititizedNamespace(settings.Name).Value}.Routes.Layout")).ToArray());

// remove the Components/Pages folder
Directory.Delete(Path.Combine(projectDir, "Components", "Pages"), true);

// create a layout.razor in the Routes directory with the following content:
// @inherits LayoutComponentBase
// @Body
File.WriteAllText(Path.Combine(projectDir, "Routes", "Layout.razor"), "@inherits LayoutComponentBase\n@Body");

// replace first line in Program.cs with using BlazKit
var lines3 = File.ReadAllLines(Path.Combine(projectDir, "Program.cs"));
File.WriteAllLines(Path.Combine(projectDir, "Program.cs"), lines3.Select((l, i) => i == 0 ? $"using {new SanititizedNamespace(settings.Name).Value};" : l).ToArray());

// add BlazeKit packages with dotnet add packe
AddNuGetPackage(projectDir, "BlazeKit");
AddNuGetPackage(projectDir, "BlazeKit.Reactivity");

// add a Routes folder in the porject directory using System.IO
Directory.CreateDirectory(Path.Combine(projectDir, "Routes"));
// add a Page.razor file in the Routes folder using System.IO
File.WriteAllText(Path.Combine(projectDir, "Routes", "Page.razor"), "<h1>Welcome to BlazeKit 👋</h1>");
}

private static void DotNetNewProject(NewSettings settings, string template)
{
var process =
Process.Start(
new ProcessStartInfo("dotnet", $"new {template} -e -o {settings.Name}")
{
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
CreateNoWindow = true,
// WorkingDirectory = Directory.GetCurrentDirectory()
}

);

process.OutputDataReceived += (sender, args) => AnsiConsole.MarkupLine(args.Data);
process.ErrorDataReceived += (sender, args) => AnsiConsole.MarkupLine($"[red]{args.Data}[/]");

process.WaitForExit();
}

private static void AddNuGetPackage(string projectDir, string package)
{
var process =
Process.Start(
new ProcessStartInfo("dotnet", $"add package {package}")
{
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
CreateNoWindow = true,
WorkingDirectory = projectDir
}
);

process.OutputDataReceived += (sender, args) => AnsiConsole.MarkupLine(args.Data);
process.ErrorDataReceived += (sender, args) => AnsiConsole.MarkupLine($"[red]{args.Data}[/]");
process.WaitForExit();
}

private void CreateWasmProject(NewSettings settings)
{
var projectDir = Path.Combine(Directory.GetCurrentDirectory(), settings.Name);
// call dotnet new
DotNetNewProject(settings, "blazorwasm");

AnsiConsole.MarkupLine("[yellow]Scaffolding BlazeKit into the project...[/]");
// remove the PAges directory
Directory.Delete(Path.Combine(projectDir, "Pages"), true);
// remove the layout directory
Directory.Delete(Path.Combine(projectDir, "Layout"), true);
// remove the last line of the _Imports.razor file
var lines = File.ReadAllLines(Path.Combine(projectDir, "_Imports.razor"));
File.WriteAllLines(Path.Combine(projectDir, "_Imports.razor"), lines.Take(lines.Length - 1).ToArray());

// replace Layout.MainLayout with Layout int Routes.razor
var lines2 = File.ReadAllLines(Path.Combine(projectDir, "App.razor"));
File.WriteAllLines(Path.Combine(projectDir, "App.razor"), lines2.Select(l => l.Replace("MainLayout", $"{new SanititizedNamespace(settings.Name).Value}.Routes.Layout")).ToArray());

// add a Routes folder in the porject directory using System.IO
Directory.CreateDirectory(Path.Combine(projectDir, "Routes"));
// add a Page.razor file in the Routes folder using System.IO
File.WriteAllText(Path.Combine(projectDir, "Routes", "Page.razor"),"<h1>Welcome to BlazeKit 👋</h1>");

// create a layout.razor in the Routes directory with the following content:
// @inherits LayoutComponentBase
// @Body
File.WriteAllText(Path.Combine(projectDir, "Routes", "Layout.razor"), "@inherits LayoutComponentBase\n@Body");



// add BlazeKit packages with dotnet add packe
AddNuGetPackage(projectDir, "BlazeKit");
AddNuGetPackage(projectDir, "BlazeKit.Reactivity");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using System.Linq;
using System.Text.RegularExpressions;

namespace BlazeKit
namespace BlazeKit.CLI
{
/// <summary>
/// Sanitizes a namespace string to be a valid C# namespace.
Expand Down
Loading

0 comments on commit afed1db

Please sign in to comment.