From ce28876f1d9284c9d327466527f257a8c973e612 Mon Sep 17 00:00:00 2001 From: Elias Stepanik <40958815+eliasstepanik@users.noreply.github.com> Date: Thu, 18 Nov 2021 22:36:47 +0100 Subject: [PATCH] Program --- NTOB/NTOB.csproj | 11 +++ NTOB/Program.cs | 180 ++++++++++++++++++++++++++++++++++++++- NTOB/appsettings.json | 22 ++++- NTOB/logic/BlazorPage.cs | 56 +++++++++++- 4 files changed, 265 insertions(+), 4 deletions(-) diff --git a/NTOB/NTOB.csproj b/NTOB/NTOB.csproj index 9590466..dd10863 100644 --- a/NTOB/NTOB.csproj +++ b/NTOB/NTOB.csproj @@ -3,6 +3,17 @@ Exe net5.0 + latest + + + + + + + + + + diff --git a/NTOB/Program.cs b/NTOB/Program.cs index 84f0e37..b19088a 100644 --- a/NTOB/Program.cs +++ b/NTOB/Program.cs @@ -1,12 +1,188 @@ using System; +using System.CommandLine; +using System.CommandLine.Invocation; +using System.IO; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using HtmlAgilityPack; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Logging; +using NTOB.logic; +using Serilog; +using Serilog.Extensions.Logging; namespace NTOB { class Program { - static void Main(string[] args) + private static async Task Main(string[] args) { - Console.WriteLine("Hello World!"); + var Configuration = new ConfigurationBuilder() + .SetBasePath(Directory.GetCurrentDirectory()) + .AddJsonFile(AppDomain.CurrentDomain.BaseDirectory + "\\appsettings.json", optional: true, reloadOnChange: true) + .AddEnvironmentVariables() + .Build(); + + + Log.Logger = new LoggerConfiguration() + .ReadFrom.Configuration(Configuration) + .Enrich.FromLogContext() + .CreateLogger(); + + var builder = new HostBuilder() + .ConfigureServices((hostContext, services) => + { + services.AddLogging(config => + { + config.ClearProviders(); + config.AddProvider(new SerilogLoggerProvider(Log.Logger)); + var minimumLevel = Configuration.GetSection("Serilog:MinimumLevel")?.Value; + if (!string.IsNullOrEmpty(minimumLevel)) + { + config.SetMinimumLevel(Enum.Parse(minimumLevel)); + } + }); + + }); + + + var rootCommand = new RootCommand + { + new Option( + "--input-path", + "This is the Path where the Nicepage generated Project is located."), + new Option( + "--output-path", + "This is the Path where the Blazor Project should be generated."), + new Option( + "--debug", + "Turn Debug on or off") + }; + + rootCommand.Description = "My sample app"; + + // Note that the parameters of the handler method are matched according to the names of the options + rootCommand.Handler = CommandHandler.Create((inputPath, outputPath, debug) => + { + if(debug) + Log.Logger.Debug("Debug is on"); + + + if (inputPath == null) + throw new ArgumentNullException(nameof(inputPath)); + + if(outputPath == null) + throw new ArgumentNullException(nameof(outputPath)); + + + ConvertToBlazor(inputPath,outputPath); + + }); + + // Parse the incoming args and invoke the handler + return await rootCommand.InvokeAsync(args); + } + + + public static void ConvertToBlazor(string inputPath, string outputPath) + { + + BlazorPage blazorPage= new BlazorPage("TestApp", outputPath); + blazorPage.CreatePage(); + + File.Delete(Path.Combine(inputPath, "index.html")); + + Directory.CreateDirectory(Path.Combine(outputPath, "wwwroot", "nicepage")); + Directory.CreateDirectory(Path.Combine(outputPath, "wwwroot", "nicepage", "css")); + Directory.CreateDirectory(Path.Combine(outputPath, "wwwroot", "nicepage", "js")); + + File.Move(Path.Combine(inputPath, "nicepage.css"), Path.Combine(outputPath, "wwwroot", "nicepage", "css", "nicepage.css")); + File.Move(Path.Combine(inputPath, "nicepage.js"), Path.Combine(outputPath, "wwwroot", "nicepage", "js", "nicepage.js")); + File.Move(Path.Combine(inputPath, "jquery.js"), Path.Combine(outputPath, "wwwroot", "nicepage", "js", "jquery.js")); + + Directory.Move(Path.Combine(inputPath, "images"), Path.Combine(outputPath, "wwwroot", "nicepage", "images")); + + + Directory.CreateDirectory(Path.Combine(outputPath, "Pages", "nicepage")); + + var files = Directory.GetFiles(inputPath).ToList(); + + + foreach (var file in files) + { + + string str = File.ReadAllText(file); + str = str.Replace("images/","nicepage/images/"); + File.WriteAllText(file, str); + + int i = files.IndexOf(file); + FileInfo fileInfo = new FileInfo(file); + string fileContont = File.ReadAllText(file); + + if(fileInfo.Extension == ".html") + { + HtmlDocument doc = new HtmlDocument(); + doc.Load(file); + + var body = doc.DocumentNode.SelectSingleNode("//body"); + var header = doc.DocumentNode.SelectSingleNode("//header"); + var head = doc.DocumentNode.SelectSingleNode("//head"); + if (i == 1) + { + StringBuilder sb = new StringBuilder(); + StringBuilder bodyClasses = new StringBuilder(); + foreach (var classes in body.GetClasses()) + { + bodyClasses.Append($"{classes} "); + } + + sb.AppendLine("@inherits LayoutComponentBase"); + sb.AppendLine($"
"); + sb.Append(header.OuterHtml); + sb.AppendLine("@Body"); + sb.AppendLine("
"); + + + File.WriteAllText (Path.Combine(outputPath, "Shared", "MainLayout.razor"), sb.ToString()); + + var content = File.ReadAllLines(Path.Combine(outputPath, "Pages", "_Layout.cshtml")).ToList(); + content.Insert(12, ""); + File.WriteAllLines(Path.Combine(outputPath, "Pages", "_Layout.cshtml"), content); + } + + body.SelectSingleNode("//header").Remove(); + + StringBuilder fText = new StringBuilder(); + + fText.AppendLine("@page " + '"' + "/" + Path.GetFileName(file).Replace(".html","") + '"'); + fText.AppendLine(body.InnerHtml); + + File.WriteAllText(file, fText.ToString()); + + var fileName = Path.GetFileName(file); + var newFileName = fileName.Replace(".html", ".razor"); + + File.Move(file, Path.Combine(outputPath, "Pages", "nicepage", newFileName)); + } + else if (fileInfo.Extension == ".css") + { + var fileName = Path.GetFileName(file); + var newFileName = fileName.Replace(".css", ".razor.css"); + + File.Move(file, Path.Combine(outputPath, "Pages", "nicepage", newFileName)); + } + + + + + } + + + } + } } \ No newline at end of file diff --git a/NTOB/appsettings.json b/NTOB/appsettings.json index 29091fa..764bc0d 100644 --- a/NTOB/appsettings.json +++ b/NTOB/appsettings.json @@ -1,3 +1,23 @@ { - + "Serilog": { + "MinimumLevel": "Debug", + "WriteTo": [ + { + "Name": "Console", + "Args": { + "theme": "Serilog.Sinks.SystemConsole.Themes.AnsiConsoleTheme::Code, Serilog.Sinks.Console", + "outputTemplate": "===> {Timestamp:HH:mm:ss.fff zzz} [{Level:w3}] {Message:lj}{NewLine}{Exception}" + } + + }, + { + "Name": "File", + "Args": { + "path": "C:\\Users\\elias\\OneDrive\\Dokumente\\NTOB\\log\\NTOB.log", + "rollingInterval": "Day", + "outputTemplate": "===> {Timestamp:HH:mm:ss.fff zzz} [{Level:w3}] {Message:lj}{NewLine}{Exception}" + } + } + ] + } } \ No newline at end of file diff --git a/NTOB/logic/BlazorPage.cs b/NTOB/logic/BlazorPage.cs index 002322d..0b8d9b4 100644 --- a/NTOB/logic/BlazorPage.cs +++ b/NTOB/logic/BlazorPage.cs @@ -1,7 +1,61 @@ -namespace NTOB.logic +using System; +using System.Diagnostics; +using System.Threading.Tasks; + +namespace NTOB.logic { public class BlazorPage { + public string Title { get; set; } + public string Path { get; set; } + + + public BlazorPage(string title, string path) + { + Title = title; + Path = path; + } + + public void CreatePage(bool https = false, AuthType authType = AuthType.Individual, bool debug = false) + { + + + string command = "dotnet new blazorserver" + + " -o " + '"' +Path + '"' + + " -n " + Title + + (https ? " " : " --no-https ") + + "-au " + authType.ToString(); + + + Process cmd = new Process(); + cmd.StartInfo.FileName = "cmd.exe"; + cmd.StartInfo.RedirectStandardInput = true; + cmd.StartInfo.RedirectStandardOutput = true; + cmd.StartInfo.CreateNoWindow = true; + cmd.StartInfo.UseShellExecute = false; + cmd.Start(); + + cmd.StandardInput.WriteLine(command); + cmd.StandardInput.Flush(); + cmd.StandardInput.Close(); + cmd.WaitForExit(); + if (debug) + { + Console.WriteLine(cmd.StandardOutput.ReadToEnd()); + } + + + } + } + + public enum AuthType + { + None, + Individual, + IndividualB2C, + SingleOrg, + MultiOrg, + Windows } } \ No newline at end of file