-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathProgram.cs
More file actions
108 lines (94 loc) · 3.76 KB
/
Program.cs
File metadata and controls
108 lines (94 loc) · 3.76 KB
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
using DevExpress.XtraEditors;
using DevExpress.XtraReports.Security;
using Serilog;
using Serilog.Sinks.WinForms.Base;
using System.Diagnostics;
using System.IO;
using Velopack;
using Velopack.Sources;
namespace ERPNext_PowerPlay
{
internal static class Program
{
public static string FrappeURL = "";
public static string FrappeUser = "";
public static string MyAppDir = "";
public static string ApiToken = "";
private static bool _updateChecked = false;
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
// MUST be the very first thing called in Main
VelopackApp.Build().Run();
if (PriorProcess() != null)
{
XtraMessageBox.Show("Another instance of the app is already running.", "ERPNext PowerPlay", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
ConfigureSerilog();
ScriptPermissionManager.GlobalInstance = new ScriptPermissionManager(ExecutionMode.Unrestricted);
ApplicationConfiguration.Initialize();
// Check for updates once the message loop is idle
Application.Idle += OnFirstIdle;
Application.Run(new frmMain());
}
private static async void OnFirstIdle(object? sender, EventArgs e)
{
if (_updateChecked) return;
_updateChecked = true;
Application.Idle -= OnFirstIdle;
await CheckForUpdatesAsync();
}
public static async Task CheckForUpdatesAsync()
{
try
{
var mgr = new UpdateManager(new GithubSource("https://github.com/Cecypo-Tech/ERPNext-PowerPlay", null, false));
var newVersion = await mgr.CheckForUpdatesAsync();
if (newVersion == null) return;
var result = XtraMessageBox.Show(
$"Version {newVersion.TargetFullRelease.Version} is available.\n\nWould you like to update now?",
"Update Available",
MessageBoxButtons.YesNo,
MessageBoxIcon.Information);
if (result == DialogResult.Yes)
{
await mgr.DownloadUpdatesAsync(newVersion);
mgr.ApplyUpdatesAndRestart(newVersion);
}
}
catch (Exception ex)
{
Log.Warning(ex, "Update check failed");
}
}
public static Process? PriorProcess()
// Returns a System.Diagnostics.Process pointing to
// a pre-existing process with the same name as the
// current one, if any; or null if the current process
// is unique.
{
Process curr = Process.GetCurrentProcess();
Process[] procs = Process.GetProcessesByName(curr.ProcessName);
foreach (Process p in procs)
{
if ((p.Id != curr.Id) &&
(p.MainModule!.FileName == curr.MainModule!.FileName))
return p;
}
return null;
}
private static void ConfigureSerilog()
{
MyAppDir = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + Path.DirectorySeparatorChar + Application.ProductName;
Log.Logger = new LoggerConfiguration()
.Enrich.FromLogContext()
.WriteToGridView()
.WriteTo.File(path: Program.MyAppDir + @"\\log.txt", rollingInterval: RollingInterval.Day, rollOnFileSizeLimit: true, retainedFileCountLimit: 90)
.CreateLogger();
}
}
}