mirror of
https://github.com/ElectronNET/Electron.NET.git
synced 2026-09-21 14:35:16 +00:00
install command?
This commit is contained in:
77
ElectronNET.CLI/Commands/InstallElectronCommand.cs
Normal file
77
ElectronNET.CLI/Commands/InstallElectronCommand.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
42
ElectronNET.CLI/ElectronHost/main.js
Normal file
42
ElectronNET.CLI/ElectronHost/main.js
Normal 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();
|
||||
});
|
||||
});
|
||||
|
||||
1411
ElectronNET.CLI/ElectronHost/package-lock.json
generated
Normal file
1411
ElectronNET.CLI/ElectronHost/package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
16
ElectronNET.CLI/ElectronHost/package.json
Normal file
16
ElectronNET.CLI/ElectronHost/package.json
Normal 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"
|
||||
}
|
||||
}
|
||||
@@ -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>
|
||||
|
||||
36
ElectronNET.CLI/EmbeddedFileHelper.cs
Normal file
36
ElectronNET.CLI/EmbeddedFileHelper.cs
Normal 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;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -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":
|
||||
|
||||
Reference in New Issue
Block a user