install command?

This commit is contained in:
Robert Muehsig
2017-10-04 23:11:23 +02:00
parent 8d89ef3e64
commit 606d78ca42
7 changed files with 1594 additions and 0 deletions

View File

@@ -0,0 +1,77 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Threading.Tasks;
namespace ElectronNET.CLI.Commands
{
public class InstallElectronCommand : ICommand
{
public const string COMMAND_NAME = "install";
public const string COMMAND_DESCRIPTION = "install";
public static IList<CommandOption> CommandOptions { get; set; } = new List<CommandOption>();
public bool DisableInteractive { get; set; }
private string[] _args;
public InstallElectronCommand(string[] args)
{
_args = args;
}
public Task<bool> ExecuteAsync()
{
return Task.Run(() =>
{
Console.WriteLine("Install Electron Host...");
string currentPath = Directory.GetCurrentDirectory();
string targetPath = Path.Combine(currentPath, "..", "CLIDeploy");
Console.WriteLine("Target: " + targetPath);
if (Directory.Exists(targetPath) == false)
{
Directory.CreateDirectory(targetPath);
}
DeployEmbeddedFile(targetPath, "main.js");
DeployEmbeddedFile(targetPath, "package.json");
DeployEmbeddedFile(targetPath, "package-lock.json");
Console.WriteLine("Start npm install...");
using (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.StartInfo.WorkingDirectory = targetPath;
cmd.Start();
cmd.StandardInput.WriteLine("npm install");
cmd.StandardInput.Flush();
cmd.StandardInput.Close();
cmd.WaitForExit();
Console.WriteLine(cmd.StandardOutput.ReadToEnd());
}
return true;
});
}
private static void DeployEmbeddedFile(string targetPath, string file)
{
using (var fileStream = File.Create(Path.Combine(targetPath, file)))
{
var streamFromEmbeddedFile = EmbeddedFileHelper.GetTestResourceFileStream("ElectronHost." + file);
streamFromEmbeddedFile.CopyTo(fileStream);
}
}
}
}

View File

@@ -0,0 +1,42 @@
const { app, BrowserWindow, Notification } = require('electron');
const io = require('socket.io')(3000);
const path = require('path');
let window;
let apiProcess;
app.on('ready', () => {
const process = require('child_process').spawn;
// run server
var apipath = path.join(__dirname, '..\\ElectronNET.WebApp\\bin\\dist\\win\\ElectronNET.WebApp.exe');
apiProcess = process(apipath);
apiProcess.stdout.on('data', (data) => {
var text = data.toString();
console.log(`stdout: ${data.toString()}`);
});
});
io.on('connection', (socket) => {
console.log('ASP.NET Core Application connected...');
socket.on('createBrowserWindow', (options) => {
console.log(options);
options.show = true;
window = new BrowserWindow(options);
window.loadURL('http://localhost:5000');
window.on('closed', function () {
mainWindow = null;
apiProcess = null;
});
});
socket.on('createNotification', (options) => {
const notification = new Notification(options);
notification.show();
});
});

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,16 @@
{
"name": "ElectronNET.Host",
"version": "1.0.0",
"description": "",
"main": "main.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"electron": "^1.7.8",
"socket.io": "^2.0.3"
}
}

View File

@@ -12,4 +12,13 @@
<Version>1.0.0</Version>
</PropertyGroup>
<ItemGroup>
<None Remove="ElectronHost\main.js" />
<None Remove="ElectronHost\package-lock.json" />
<None Remove="ElectronHost\package.json" />
</ItemGroup>
<ItemGroup>
</ItemGroup>
</Project>

View File

@@ -0,0 +1,36 @@
using System;
using System.IO;
using System.Reflection;
namespace ElectronNET.CLI
{
public static class EmbeddedFileHelper
{
private const string ResourcePath = "ElectronNET.CLI.{0}";
public static Stream GetTestResourceFileStream(string folderAndFileInProjectPath)
{
var asm = Assembly.GetExecutingAssembly();
var resource = string.Format(ResourcePath, folderAndFileInProjectPath);
return asm.GetManifestResourceStream(resource);
}
public static string GetTestResourceFileContent(string folderAndFileInProjectPath)
{
var asm = Assembly.GetExecutingAssembly();
var resource = string.Format(ResourcePath, folderAndFileInProjectPath);
using (var stream = asm.GetManifestResourceStream(resource))
{
if (stream != null)
{
var reader = new StreamReader(stream);
return reader.ReadToEnd();
}
}
return String.Empty;
}
}
}

View File

@@ -25,6 +25,9 @@ namespace ElectronNET.CLI
case StartElectronCommand.COMMAND_NAME:
command = new StartElectronCommand(args.Skip(1).ToArray());
break;
case InstallElectronCommand.COMMAND_NAME:
command = new InstallElectronCommand(args.Skip(1).ToArray());
break;
case "--help":
case "--h":
case "help":