-
Notifications
You must be signed in to change notification settings - Fork 52
/
AvaloniaProcessBridge.cs
103 lines (85 loc) · 2.88 KB
/
AvaloniaProcessBridge.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Management.Automation;
using System.Reflection;
using System.Runtime.InteropServices;
using OutGridView.Models;
namespace OutGridView.Cmdlet
{
class AvaloniaProcessBridge
{
private static Process _process;
public static List<int> SelectedIndexes { get; set; }
public static void Start(ApplicationData applicationData)
{
SelectedIndexes = new List<int>();
_process = new Process();
_process.StartInfo.FileName = GetOutgridViewApplicationLocation();
_process.StartInfo.CreateNoWindow = true;
_process.StartInfo.UseShellExecute = false;
_process.StartInfo.RedirectStandardInput = true;
_process.StartInfo.RedirectStandardOutput = true;
_process.StartInfo.RedirectStandardError = true;
_process.OutputDataReceived += (sender, data) =>
{
if (!string.IsNullOrWhiteSpace(data.Data))
{
SelectedIndexes = Serializers.ObjectFromJson<List<int>>(data.Data);
}
};
_process.ErrorDataReceived += (sender, data) =>
{
Console.WriteLine(data.Data);
};
_process.Start();
_process.BeginOutputReadLine();
_process.BeginErrorReadLine();
var serializedData = Serializers.ObjectToJson(applicationData);
_process.StandardInput.WriteLine(serializedData);
}
public static void WaitForExit()
{
_process.WaitForExit();
}
public static void CloseProcess()
{
_process.Close();
}
public static bool IsClosed()
{
if (_process == null || _process.HasExited)
{
return true;
}
else
{
return false;
}
}
public static string GetOutgridViewApplicationLocation()
{
string osRid;
string executableName;
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
osRid = "win-x64";
executableName = "OutGridView.Gui.exe";
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
{
osRid = "osx-x64";
executableName = "OutGridView.Gui";
}
else
{
osRid = "linux-x64";
executableName = "OutGridView.Gui";
}
return Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "OutGridView.Gui", osRid, executableName);
}
}
}