This commit is contained in:
Elias Stepanik 2021-11-18 22:36:47 +01:00
parent 84e6a91dc4
commit ce28876f1d
4 changed files with 265 additions and 4 deletions

View File

@ -3,6 +3,17 @@
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
<LangVersion>latest</LangVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="HtmlAgilityPack" Version="1.11.38" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="6.0.0" />
<PackageReference Include="Serilog.Extensions.Logging" Version="3.1.0" />
<PackageReference Include="Serilog.Settings.Configuration" Version="3.3.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="4.0.0" />
<PackageReference Include="Serilog.Sinks.File" Version="5.0.0" />
<PackageReference Include="System.CommandLine" Version="2.0.0-beta1.21308.1" />
</ItemGroup>
</Project>

View File

@ -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<int> 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<LogLevel>(minimumLevel));
}
});
});
var rootCommand = new RootCommand
{
new Option<string>(
"--input-path",
"This is the Path where the Nicepage generated Project is located."),
new Option<string>(
"--output-path",
"This is the Path where the Blazor Project should be generated."),
new Option<bool>(
"--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<string, string, bool>((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($"<div class='{bodyClasses.ToString()}'>");
sb.Append(header.OuterHtml);
sb.AppendLine("@Body");
sb.AppendLine("</div>");
File.WriteAllText (Path.Combine(outputPath, "Shared", "MainLayout.razor"), sb.ToString());
var content = File.ReadAllLines(Path.Combine(outputPath, "Pages", "_Layout.cshtml")).ToList();
content.Insert(12, "<link href='nicepage/css/nicepage.css' rel='stylesheet' media='screen'/>");
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));
}
}
}
}
}

View File

@ -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}"
}
}
]
}
}

View File

@ -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
}
}