diff --git a/ElectronNET.CLI/Commands/Actions/DirectoryCopy.cs b/ElectronNET.CLI/Commands/Actions/DirectoryCopy.cs new file mode 100644 index 0000000..0e7ce28 --- /dev/null +++ b/ElectronNET.CLI/Commands/Actions/DirectoryCopy.cs @@ -0,0 +1,53 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Text; + +namespace ElectronNET.CLI.Commands.Actions +{ + public static class DirectoryCopy + { + public static void Do(string sourceDirName, string destDirName, bool copySubDirs, List ignoredSubDirs) + { + // Get the subdirectories for the specified directory. + DirectoryInfo dir = new DirectoryInfo(sourceDirName); + + if (!dir.Exists) + { + throw new DirectoryNotFoundException( + "Source directory does not exist or could not be found: " + + sourceDirName); + } + + DirectoryInfo[] dirs = dir.GetDirectories(); + // If the destination directory doesn't exist, create it. + if (!Directory.Exists(destDirName)) + { + Directory.CreateDirectory(destDirName); + } + + // Get the files in the directory and copy them to the new location. + FileInfo[] files = dir.GetFiles(); + foreach (FileInfo file in files) + { + string temppath = Path.Combine(destDirName, file.Name); + file.CopyTo(temppath, false); + } + + // If copying subdirectories, copy them and their contents to new location. + if (copySubDirs) + { + foreach (DirectoryInfo subdir in dirs) + { + if (ignoredSubDirs.Contains(subdir.Name)) + { + continue; + } + + string temppath = Path.Combine(destDirName, subdir.Name); + Do(subdir.FullName, temppath, copySubDirs, ignoredSubDirs); + } + } + } + } +} diff --git a/ElectronNET.CLI/Commands/AddCommand.cs b/ElectronNET.CLI/Commands/AddCommand.cs new file mode 100644 index 0000000..f93ea3f --- /dev/null +++ b/ElectronNET.CLI/Commands/AddCommand.cs @@ -0,0 +1,129 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Runtime.InteropServices; +using System.Text; +using System.Threading.Tasks; +using System.Xml; +using System.Xml.Linq; + +namespace ElectronNET.CLI.Commands +{ + public class AddCommand : ICommand + { + public const string COMMAND_NAME = "add"; + public const string COMMAND_DESCRIPTION = "The add command needs to be invoked via 'add hosthook'. This creates a special folder for your custom npm package installation."; + public const string COMMAND_ARGUMENTS = "hosthook"; + public static IList CommandOptions { get; set; } = new List(); + + + private string[] _args; + + public AddCommand(string[] args) + { + _args = args; + } + + private static string ElectronHostHookFolderName = "ElectronHostHook"; + + public Task ExecuteAsync() + { + return Task.Run(() => + { + if(_args.Length == 0) + { + Console.WriteLine("Specify 'hosthook' to add custom npm packages."); + return false; + } + + if(_args[0].ToLowerInvariant() != "hosthook") + { + Console.WriteLine("Specify 'hosthook' to add custom npm packages."); + return false; + } + + string aspCoreProjectPath = ""; + + // Maybe ToDo: Adding the possiblity to specify a path (like we did in the InitCommand, but this would require a better command args parser) + aspCoreProjectPath = Directory.GetCurrentDirectory(); + + var currentDirectory = aspCoreProjectPath; + + var targetFilePath = Path.Combine(currentDirectory, ElectronHostHookFolderName); + + if(Directory.Exists(targetFilePath)) + { + Console.WriteLine("ElectronHostHook directory already in place. If you want to start over, delete the folder and invoke this command again."); + return false; + } + + Console.WriteLine("Adding the ElectronHostHook folder to your project..."); + + Directory.CreateDirectory(targetFilePath); + + // Deploy related files + EmbeddedFileHelper.DeployEmbeddedFile(targetFilePath, "index.ts", "ElectronHostHook."); + EmbeddedFileHelper.DeployEmbeddedFile(targetFilePath, "connector.ts", "ElectronHostHook."); + EmbeddedFileHelper.DeployEmbeddedFile(targetFilePath, "package.json", "ElectronHostHook."); + EmbeddedFileHelper.DeployEmbeddedFile(targetFilePath, "tsconfig.json", "ElectronHostHook."); + + // search .csproj + Console.WriteLine($"Search your .csproj to add configure CopyToPublishDirectory to 'Never'"); + var projectFile = Directory.EnumerateFiles(currentDirectory, "*.csproj", SearchOption.TopDirectoryOnly).FirstOrDefault(); + + Console.WriteLine($"Found your .csproj: {projectFile} - check for existing CopyToPublishDirectory setting or update it."); + + if (!EditCsProj(projectFile)) return false; + + Console.WriteLine($"Everything done - happy electronizing with your custom npm packages!"); + + return true; + }); + } + + // ToDo: Cleanup this copy/past code. + private static bool EditCsProj(string projectFile) + { + 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; + } + + string itemGroupXmlString = "" + + "" + + "Never" + + "" + + ""; + + var newItemGroupForConfig = XElement.Parse(itemGroupXmlString); + xmlDocument.Root.Add(newItemGroupForConfig); + + stream.SetLength(0); + stream.Position = 0; + + var xws = new XmlWriterSettings + { + OmitXmlDeclaration = true, + Indent = true + }; + using (XmlWriter xw = XmlWriter.Create(stream, xws)) + { + xmlDocument.Save(xw); + } + + } + + Console.WriteLine($"Publish setting added in csproj!"); + return true; + } + + } +} diff --git a/ElectronNET.CLI/Commands/BuildCommand.cs b/ElectronNET.CLI/Commands/BuildCommand.cs index 55d71c1..4a91cd7 100644 --- a/ElectronNET.CLI/Commands/BuildCommand.cs +++ b/ElectronNET.CLI/Commands/BuildCommand.cs @@ -82,6 +82,13 @@ namespace ElectronNET.CLI.Commands return false; } + string electronhosthookDir = Path.Combine(Directory.GetCurrentDirectory(), "ElectronHostHook"); + + if (Directory.Exists(electronhosthookDir)) + { + DirectoryCopy.Do(electronhosthookDir, tempPath, true, new List() { "node_modules" }); + } + DeployEmbeddedElectronFiles.Do(tempPath); var checkForNodeModulesDirPath = Path.Combine(tempPath, "node_modules"); diff --git a/ElectronNET.CLI/Commands/StartElectronCommand.cs b/ElectronNET.CLI/Commands/StartElectronCommand.cs index 51dfb18..8b31c3b 100644 --- a/ElectronNET.CLI/Commands/StartElectronCommand.cs +++ b/ElectronNET.CLI/Commands/StartElectronCommand.cs @@ -59,6 +59,13 @@ namespace ElectronNET.CLI.Commands return false; } + string electronhosthookDir = Path.Combine(aspCoreProjectPath, "ElectronHostHook"); + + if (Directory.Exists(electronhosthookDir)) + { + DirectoryCopy.Do(electronhosthookDir, tempPath, true, new List() { "node_modules" }); + } + DeployEmbeddedElectronFiles.Do(tempPath); var checkForNodeModulesDirPath = Path.Combine(tempPath, "node_modules"); @@ -92,5 +99,7 @@ namespace ElectronNET.CLI.Commands return true; }); } + + } } diff --git a/ElectronNET.CLI/Program.cs b/ElectronNET.CLI/Program.cs index e328d1f..2030030 100644 --- a/ElectronNET.CLI/Program.cs +++ b/ElectronNET.CLI/Program.cs @@ -31,6 +31,9 @@ namespace ElectronNET.CLI case InitCommand.COMMAND_NAME: command = new InitCommand(args.Skip(1).ToArray()); break; + case AddCommand.COMMAND_NAME: + command = new AddCommand(args.Skip(1).ToArray()); + break; case VersionCommand.COMMAND_NAME: command = new VersionCommand(args.Skip(1).ToArray()); break; @@ -83,15 +86,20 @@ namespace ElectronNET.CLI Console.WriteLine($"\t{StartElectronCommand.COMMAND_NAME.PadRight(NAME_WIDTH)} {StartElectronCommand.COMMAND_DESCRIPTION}"); Console.WriteLine("\t"); - Console.WriteLine("Commands to build the Electron Application:"); + Console.WriteLine("Command to build the Electron Application:"); Console.WriteLine("\t"); Console.WriteLine($"\t{BuildCommand.COMMAND_NAME.PadRight(NAME_WIDTH)} {BuildCommand.COMMAND_DESCRIPTION}"); Console.WriteLine("\t"); - Console.WriteLine("Commands to init the Electron Application:"); + Console.WriteLine("Command to init the Electron Application:"); Console.WriteLine("\t"); Console.WriteLine($"\t{InitCommand.COMMAND_NAME.PadRight(NAME_WIDTH)} {InitCommand.COMMAND_DESCRIPTION}"); + Console.WriteLine("\t"); + Console.WriteLine("Command to add a custom npm packages to the Electron Application:"); + Console.WriteLine("\t"); + Console.WriteLine($"\t{AddCommand.COMMAND_NAME.PadRight(NAME_WIDTH)} {AddCommand.COMMAND_DESCRIPTION}"); + Console.WriteLine("\t"); Console.WriteLine("Commands to see the current ElectronNET version number:"); Console.WriteLine("\t"); @@ -116,6 +124,9 @@ namespace ElectronNET.CLI case InitCommand.COMMAND_NAME: PrintUsage(InitCommand.COMMAND_NAME, InitCommand.COMMAND_DESCRIPTION, InitCommand.CommandOptions, InitCommand.COMMAND_ARGUMENTS); break; + case AddCommand.COMMAND_NAME: + PrintUsage(AddCommand.COMMAND_NAME, AddCommand.COMMAND_DESCRIPTION, AddCommand.CommandOptions, AddCommand.COMMAND_ARGUMENTS); + break; case VersionCommand.COMMAND_NAME: PrintUsage(VersionCommand.COMMAND_NAME, VersionCommand.COMMAND_DESCRIPTION, VersionCommand.CommandOptions, VersionCommand.COMMAND_ARGUMENTS); break;