-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathApp.xaml.cs
151 lines (130 loc) · 5.51 KB
/
App.xaml.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
using System;
using Serilog;
using System.Windows;
using System.IO;
using System.Threading;
namespace SSHMan
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
public static string DataPath { get; private set; }
public static string ScriptPath { get; private set; }
public static string LogPath { get; private set; }
public static string WtSettings { get; private set; }
public static string WtPreviewSettings { get; private set; }
static readonly Mutex singleAppMutex = new Mutex(true, "{529A6125-B42E-49A8-B289-216D8FFE45B8}");
public static void Panic(string message)
{
_ = MessageBox.Show($"PANIC: {message}", "Panic", MessageBoxButton.OK, MessageBoxImage.Error);
Environment.Exit(22);
}
public static void EnsureDirectory(string path)
{
if (!Directory.Exists(path))
{
_ = Directory.CreateDirectory(path);
}
}
public static string BackupFile(string file, string prefix = "")
{
var backupPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), prefix + Path.GetFileName(file) + ".bak");
File.Copy(file, backupPath, true);
return backupPath;
}
private static void InstallIfNotExists(string file, byte[] data)
{
if (File.Exists(file)) return;
Log.Information("Installing {file}", file);
File.WriteAllBytes(file, data);
}
private static void InstallAssets()
{
var modDir = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "PowerShell", "Modules", "ReadNamedPipe");
var modDefinition = Path.Combine(modDir, "ReadNamedPipe.psd1");
var modAssembly = Path.Combine(modDir, "ReadNamedPipeCmdlet.dll");
EnsureDirectory(modDir);
InstallIfNotExists(ScriptPath, Scripts.sshchild);
InstallIfNotExists(modDefinition, Scripts.ModuleDefinition);
InstallIfNotExists(modAssembly, Scripts.ReadNamedPipeCmdlet);
}
private void Application_Startup(object sender, StartupEventArgs e)
{
AppDomain.CurrentDomain.ProcessExit += CurrentDomain_ProcessExit;
if (singleAppMutex.WaitOne(TimeSpan.Zero, true))
{
DataPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "SSHMan");
ScriptPath = Path.Combine(DataPath, "sshchild.ps1");
LogPath = Path.Combine(DataPath, "sshman.log");
WtSettings = Path.Join(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Packages", "Microsoft.WindowsTerminal_8wekyb3d8bbwe", "LocalState", "settings.json");
WtPreviewSettings = Path.Join(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Packages", "Microsoft.WindowsTerminalPreview_8wekyb3d8bbwe", "LocalState", "settings.json");
EnsureDirectory(DataPath);
if (e.Args.Length > 0)
{
switch (e.Args[0])
{
case "--debugdev":
DebugLogger();
break;
case "--debug":
ExternalDebugLogger();
break;
}
}
else
{
StandardLogger();
}
InstallAssets();
}
else
{
var hwnd = NativeMethods.FindWindow(null, "SSHMan");
NativeMethods.ShowWindow(hwnd, NativeMethods.Win32ShowCmd.Restore);
NativeMethods.SetForegroundWindow(hwnd);
this.Shutdown();
}
}
internal static void AnnounceSessionEnd(Guid workId)
{
throw new NotImplementedException();
}
private void CurrentDomain_ProcessExit(object sender, EventArgs e)
{
// If we exit because of any other reason than the happy path the mutex might still exist so we better kill it here
singleAppMutex.Close();
singleAppMutex.Dispose();
}
private static void ExternalDebugLogger()
{
if (!NativeMethods.AllocConsole())
{
Panic("Failed to allocate console");
}
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Debug()
.WriteTo.Console()
.WriteTo.File(LogPath, rollingInterval: RollingInterval.Day)
.CreateLogger();
Log.Information("Using user debug console");
}
private static void DebugLogger()
{
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Debug()
.WriteTo.Debug()
.WriteTo.File(LogPath, rollingInterval: RollingInterval.Day)
.CreateLogger();
Log.Information("Using developer debug console");
}
private static void StandardLogger()
{
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Information()
.WriteTo.File(LogPath, rollingInterval: RollingInterval.Day)
.CreateLogger();
}
}
}