33using System . IO ;
44using System . Linq ;
55using System . Windows ;
6- using ShecanDesktop . Common . Log ;
76
87namespace ShecanDesktop . Core
98{
109 public class Launcher
1110 {
12- #region Private Members
11+ #region Constructors
1312
14- private bool _isAppReadyToLaunch ;
13+ public Launcher ( AppInfo appInfo )
14+ {
15+ _appInfo = appInfo ;
16+ }
1517
1618 #endregion
1719
18- #region Static Public Members
20+ #region Private Members
1921
20- public static string DateTimeStamp => $ "{ DateTime . Now : yyyy-MM-dd hh-mm-ss} ";
21- public static ILogger Logger ;
22- public static LauncherInfo LauncherInfo ;
22+ private readonly AppInfo _appInfo ;
23+ private bool _isAppReadyToLaunch ;
2324
2425 #endregion
2526
26- #region Non-Static Public Members
27-
28- public virtual void PrepareApplication ( )
27+ #region Public Members
28+
29+ public void Prepare ( )
2930 {
30- // Create a instance of LauncherInfo and puts it in LauncherInfo field.
31- // We can access to LauncherInfo field from anywhere of the application.
32- InitializeLauncher ( ) ;
33-
34- // NOTE: Should be call after LauncherInfo initializing.
35- // Because we need to log directory path that will specify in LauncherInfo.
36- // Create a logger instance and puts it in Logger field.
37- // We can access to Logger field from anywhere of the application.
38- StartLogger ( ) ;
39-
40- // NOTE: Should be call after launcher initializing.
41- // Because we need to primary directories collection that will specify in LauncherInfo.
4231 CheckPrimaryDirectories ( ) ;
43-
44- // Manage app crash details
4532 ManageAppCrash ( ) ;
46-
47- // Make application single instance
4833 CheckInstances ( ) ;
49-
50- // Set application info such as name, version, etc
5134 SetAppInfo ( ) ;
52-
53- // Set app status
5435 _isAppReadyToLaunch = true ;
5536 }
5637
57- public virtual void StartApplication ( )
38+ public void Start ( )
5839 {
5940 if ( ! _isAppReadyToLaunch )
6041 throw new InvalidOperationException ( "App is not ready to launch. You must calling the Prepare method first." ) ;
@@ -66,30 +47,22 @@ public virtual void StartApplication()
6647 // manager in App.xaml.cs (EnableUnhandledExceptionManager method) after
6748 // the application launched, we cannot show the main window using ShowDialog method.
6849 Application . Current . MainWindow . Show ( ) ;
69- }
7050
71- #endregion
72-
73- #region Protected Methods
51+ if ( ! Global . AppInfo . AppCommandLine . Contains ( "-debug" ) ) return ;
7452
75- protected void InitializeLauncher ( )
76- {
77- LauncherInfo = new LauncherInfo ( ) ;
78- LauncherInfo . Initialize ( ) ;
53+ Global . Logger . LogInfo ( $ "Executable Path: { Global . AppInfo . AppFullPath } " ) ;
54+ Global . Logger . LogInfo ( $ "App Version: { Global . AppInfo . AppVersion } " ) ;
55+ Global . Logger . LogInfo ( $ "Command Line: { Global . AppInfo . AppCommandLine } " ) ;
56+ Global . Logger . LogInfo ( $ "Current Os: { Global . GetOsName ( ) } " ) ;
7957 }
8058
81- protected void StartLogger ( )
82- {
83- var logFileName = $ "{ LauncherInfo . AppLogFolder } " +
84- $ "{ DateTimeStamp } .log";
59+ #endregion
8560
86- Logger = new Logger ( ) ;
87- Logger . Start ( logFileName , DateTimeStamp ) ;
88- }
61+ #region Protected Methods
8962
9063 protected void CheckPrimaryDirectories ( )
9164 {
92- var primaryDirectories = LauncherInfo . AppPrimaryDirectories ;
65+ var primaryDirectories = _appInfo . AppPrimaryDirectories ;
9366
9467 foreach ( var primaryDirectory in primaryDirectories )
9568 {
@@ -100,23 +73,23 @@ protected void CheckPrimaryDirectories()
10073 }
10174 }
10275
103- protected static void ManageAppCrash ( )
76+ protected void ManageAppCrash ( )
10477 {
105- if ( ! File . Exists ( LauncherInfo . AppCrashFile ) )
78+ if ( ! File . Exists ( _appInfo . AppCrashFile ) )
10679 return ;
10780
108- var crashDetails = File . ReadAllLines ( LauncherInfo . AppCrashFile ) ;
81+ var crashDetails = File . ReadAllLines ( _appInfo . AppCrashFile ) ;
10982 if ( crashDetails . Length < 0 ) return ;
11083
11184 // Crash file new location
112- var crashFileNewLocation = $ "{ LauncherInfo . AppCrashFolder } { DateTime . Now : yyyy-MM-dd hh-mm-ss} .txt";
85+ var crashFileNewLocation = $ "{ _appInfo . AppCrashFolder } { DateTime . Now : yyyy-MM-dd hh-mm-ss} .txt";
11386
11487 // Prepare crash folder
115- if ( ! Directory . Exists ( LauncherInfo . AppCrashFolder ) )
116- Directory . CreateDirectory ( LauncherInfo . AppCrashFolder ) ;
88+ if ( ! Directory . Exists ( _appInfo . AppCrashFolder ) )
89+ Directory . CreateDirectory ( _appInfo . AppCrashFolder ) ;
11790
11891 // Move crash file to crash folder
119- File . Move ( LauncherInfo . AppCrashFile ,
92+ File . Move ( _appInfo . AppCrashFile ,
12093 crashFileNewLocation ) ;
12194
12295 // Display app last crash details using Notepad
@@ -134,7 +107,7 @@ protected void CheckInstances()
134107
135108 protected void SetAppInfo ( )
136109 {
137- Application . Current . Resources [ "AppVersion" ] = LauncherInfo . AppVersion ;
110+ Application . Current . Resources [ "AppVersion" ] = _appInfo . AppVersion ;
138111 }
139112
140113 #endregion
0 commit comments