diff --git a/ElectronNET.CLI/Commands/BuildCommand.cs b/ElectronNET.CLI/Commands/BuildCommand.cs index 9e0cc91..329a1a3 100644 --- a/ElectronNET.CLI/Commands/BuildCommand.cs +++ b/ElectronNET.CLI/Commands/BuildCommand.cs @@ -51,16 +51,16 @@ namespace ElectronNET.CLI.Commands Directory.CreateDirectory(tempPath); } - EmbeddedFileHelper.DeployEmbeddedFile(tempPath, "main.js"); - EmbeddedFileHelper.DeployEmbeddedFile(tempPath, "package.json"); - EmbeddedFileHelper.DeployEmbeddedFile(tempPath, "package-lock.json"); + EmbeddedFileHelper.DeployEmbeddedFile(tempPath, "main.js", "ElectronHost."); + EmbeddedFileHelper.DeployEmbeddedFile(tempPath, "package.json", "ElectronHost."); + EmbeddedFileHelper.DeployEmbeddedFile(tempPath, "package-lock.json", "ElectronHost."); string hostApiFolder = Path.Combine(tempPath, "api"); if (Directory.Exists(hostApiFolder) == false) { Directory.CreateDirectory(hostApiFolder); } - EmbeddedFileHelper.DeployEmbeddedFile(hostApiFolder, "ipc.js", "api."); + EmbeddedFileHelper.DeployEmbeddedFile(hostApiFolder, "ipc.js", "ElectronHost.api."); Console.WriteLine("Start npm install..."); ProcessHelper.CmdExecute("npm install", tempPath); diff --git a/ElectronNET.CLI/Commands/InitCommand.cs b/ElectronNET.CLI/Commands/InitCommand.cs new file mode 100644 index 0000000..6cfbfe1 --- /dev/null +++ b/ElectronNET.CLI/Commands/InitCommand.cs @@ -0,0 +1,93 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Runtime.InteropServices; +using System.Threading.Tasks; +using System.Xml.Linq; + +namespace ElectronNET.CLI.Commands +{ + public class InitCommand : ICommand + { + public const string COMMAND_NAME = "init"; + public const string COMMAND_DESCRIPTION = "Creates the needed Electron.NET config for your Electron Application."; + public const string COMMAND_ARGUMENTS = " from ASP.NET Core Project."; + public static IList CommandOptions { get; set; } = new List(); + + private const string ConfigName = "electronnet.json"; + + public Task ExecuteAsync() + { + return Task.Run(() => + { + var currentDirectory = Directory.GetCurrentDirectory(); + + Console.WriteLine("Adding our config file to your project..."); + + var targetFilePath = Path.Combine(currentDirectory, ConfigName); + + if (File.Exists(targetFilePath)) + { + Console.WriteLine("Config file already in your project."); + return false; + } + + // Deploy config file + EmbeddedFileHelper.DeployEmbeddedFile(currentDirectory, ConfigName); + + // search .csproj + Console.WriteLine($"Search your .csproj to add the needed {ConfigName}..."); + var projectFile = Directory.EnumerateFiles(currentDirectory, "*.csproj", SearchOption.TopDirectoryOnly).FirstOrDefault(); + + // update config file with the name of the csproj + // ToDo: If the csproj name != application name, this will fail + string text = File.ReadAllText(targetFilePath); + text = text.Replace("{{executable}}", Path.GetFileNameWithoutExtension(projectFile)); + File.WriteAllText(targetFilePath, text); + + Console.WriteLine($"Found your .csproj: {projectFile} - check for existing config or update it."); + + using (var stream = File.Open(projectFile, FileMode.OpenOrCreate, FileAccess.ReadWrite)) + { + var xmlDocument = XDocument.Load(stream); + + var projectElement = xmlDocument.Descendants("Project").FirstOrDefault(); + if (projectElement == null || projectElement.Attribute("Sdk")?.Value != "Microsoft.NET.Sdk.Web") + { + Console.WriteLine($"Project file is not a compatible type of 'Microsoft.NET.Sdk.Web'. Your project: {projectElement?.Attribute("Sdk")?.Value}"); + return false; + } + + if (xmlDocument.ToString().Contains($"Content Update=\"{ConfigName}\"")) + { + Console.WriteLine($"{ConfigName} already in csproj."); + return false; + } + + Console.WriteLine($"{ConfigName} will be added to csproj."); + + string itemGroupXmlString = "" + + "" + + "PreserveNewest" + + "" + + ""; + + var newItemGroupForConfig = XElement.Parse(itemGroupXmlString); + xmlDocument.Root.Add(newItemGroupForConfig); + + stream.SetLength(0); + stream.Position = 0; + + xmlDocument.Save(stream); + + Console.WriteLine($"{ConfigName} added in csproj - happy electronizing!"); + } + + return true; + }); + } + + + } +} diff --git a/ElectronNET.CLI/Commands/StartElectronCommand.cs b/ElectronNET.CLI/Commands/StartElectronCommand.cs index c0e4995..541d832 100644 --- a/ElectronNET.CLI/Commands/StartElectronCommand.cs +++ b/ElectronNET.CLI/Commands/StartElectronCommand.cs @@ -49,16 +49,16 @@ namespace ElectronNET.CLI.Commands string tempBinPath = Path.Combine(tempPath, "bin"); ProcessHelper.CmdExecute($"dotnet publish -r win10-x64 --output \"{tempBinPath}\"", aspCoreProjectPath); - EmbeddedFileHelper.DeployEmbeddedFile(tempPath, "main.js"); - EmbeddedFileHelper.DeployEmbeddedFile(tempPath, "package.json"); - EmbeddedFileHelper.DeployEmbeddedFile(tempPath, "package-lock.json"); + EmbeddedFileHelper.DeployEmbeddedFile(tempPath, "main.js", "ElectronHost."); + EmbeddedFileHelper.DeployEmbeddedFile(tempPath, "package.json", "ElectronHost."); + EmbeddedFileHelper.DeployEmbeddedFile(tempPath, "package-lock.json", "ElectronHost."); string hostApiFolder = Path.Combine(tempPath, "api"); if (Directory.Exists(hostApiFolder) == false) { Directory.CreateDirectory(hostApiFolder); } - EmbeddedFileHelper.DeployEmbeddedFile(hostApiFolder, "ipc.js", "api."); + EmbeddedFileHelper.DeployEmbeddedFile(hostApiFolder, "ipc.js", "ElectronHost.api."); Console.WriteLine("Start npm install..."); ProcessHelper.CmdExecute("npm install", tempPath); diff --git a/ElectronNET.CLI/ElectronNET.CLI.csproj b/ElectronNET.CLI/ElectronNET.CLI.csproj index 410138e..1078d20 100644 --- a/ElectronNET.CLI/ElectronNET.CLI.csproj +++ b/ElectronNET.CLI/ElectronNET.CLI.csproj @@ -15,6 +15,7 @@ + @@ -24,6 +25,7 @@ + diff --git a/ElectronNET.CLI/EmbeddedFileHelper.cs b/ElectronNET.CLI/EmbeddedFileHelper.cs index bcaa246..aa405e3 100644 --- a/ElectronNET.CLI/EmbeddedFileHelper.cs +++ b/ElectronNET.CLI/EmbeddedFileHelper.cs @@ -19,7 +19,7 @@ namespace ElectronNET.CLI { using (var fileStream = File.Create(Path.Combine(targetPath, file))) { - var streamFromEmbeddedFile = GetTestResourceFileStream("ElectronHost." + namespacePath + file); + var streamFromEmbeddedFile = GetTestResourceFileStream(namespacePath + file); streamFromEmbeddedFile.CopyTo(fileStream); } } diff --git a/ElectronNET.CLI/Program.cs b/ElectronNET.CLI/Program.cs index b851cdf..63caac0 100644 --- a/ElectronNET.CLI/Program.cs +++ b/ElectronNET.CLI/Program.cs @@ -4,9 +4,11 @@ using System.Collections.Generic; using System.Linq; using System.Reflection; using System.Text; +using System.Xml.Linq; namespace ElectronNET.CLI { + class Program { static void Main(string[] args) @@ -28,6 +30,9 @@ namespace ElectronNET.CLI case BuildCommand.COMMAND_NAME: command = new BuildCommand(); break; + case InitCommand.COMMAND_NAME: + command = new InitCommand(); + break; case "--help": case "--h": case "help": diff --git a/ElectronNET.CLI/electronnet.json b/ElectronNET.CLI/electronnet.json new file mode 100644 index 0000000..653cc60 --- /dev/null +++ b/ElectronNET.CLI/electronnet.json @@ -0,0 +1,3 @@ +{ + "executable": "{{executable}}" +} \ No newline at end of file diff --git a/ElectronNET.WebApp/ElectronNET.WebApp.csproj b/ElectronNET.WebApp/ElectronNET.WebApp.csproj index 33d154c..5143f4d 100644 --- a/ElectronNET.WebApp/ElectronNET.WebApp.csproj +++ b/ElectronNET.WebApp/ElectronNET.WebApp.csproj @@ -1,14 +1,12 @@ + - netcoreapp2.0 win10-x64 - - @@ -20,8 +18,12 @@ - - + + + PreserveNewest + + + \ No newline at end of file diff --git a/ElectronNET.WebApp/electronnet.json b/ElectronNET.WebApp/electronnet.json new file mode 100644 index 0000000..4ada330 --- /dev/null +++ b/ElectronNET.WebApp/electronnet.json @@ -0,0 +1,3 @@ +{ + "executable": "ElectronNET.WebApp" +} \ No newline at end of file