private static void launchFromContext(LaunchContext context) { LaunchOptions options = LaunchOptions.FromContext(context); switch (options.Mode) { case LaunchOptions.LaunchMode.DiffTool: onLaunchFromDiffTool(options); break; case LaunchOptions.LaunchMode.Register: if (registerCustomProtocol()) { integrateInGitExtensions(); integrateInSourceTree(); } break; case LaunchOptions.LaunchMode.Unregister: unregisterCustomProtocol(); break; case LaunchOptions.LaunchMode.Normal: if (context.IsRunningSingleInstance) { onLaunchMainInstance(options); } else { onLaunchAnotherInstance(context); } break; default: Debug.Assert(false); break; } }
private static void onLaunchMainInstance(LaunchOptions options) { cleanUpOldFiles(); if (ApplicationUpdateHelper.ShowCheckForUpdatesDialog()) { Trace.TraceInformation("Application is exiting to install a new version"); return; } bool runningAsUwp = new DesktopBridge.Helpers().IsRunningAsUwp(); Trace.TraceInformation(String.Format("Running as UWP = {0}", runningAsUwp ? "Yes" : "No")); if (runningAsUwp) { revertOldInstallations(); } string path = runningAsUwp ? Constants.UWP_Launcher_Name : Process.GetCurrentProcess().MainModule.FileName; if (!prepareGitEnvironment() || !integrateInDiffTool(path)) { return; } bool integratedInGitExtensions = integrateInGitExtensions(); bool integratedInSourceTree = integrateInSourceTree(); Version osVersion = Environment.OSVersion.Version; Trace.TraceInformation(String.Format("OS version is {0}", osVersion.ToString())); Version clrVersion = Environment.Version; Trace.TraceInformation(String.Format("CLR version is {0}", clrVersion.ToString())); Trace.TraceInformation(String.Format(".NET Framework version is {0}", typeof(string).Assembly.ImageRuntimeVersion)); LaunchOptions.NormalModeOptions normalOptions = options.SpecialOptions as LaunchOptions.NormalModeOptions; Application.Run(new MainForm(normalOptions.StartMinimized, runningAsUwp, normalOptions.StartUrl, integratedInGitExtensions, integratedInSourceTree)); }
private static void onLaunchFromDiffTool(LaunchOptions launchOptions) { LaunchContext context = (launchOptions.SpecialOptions as LaunchOptions.DiffToolModeOptions).LaunchContext; if (context.IsRunningSingleInstance) { Trace.TraceWarning("Merge Request Helper is not running"); MessageBox.Show("Merge Request Helper is not running. Discussion cannot be created", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return; } IntPtr concurrentDiscussionWindow = context.GetWindowByCaption(Constants.StartNewThreadCaption, false); if (concurrentDiscussionWindow != IntPtr.Zero) { Trace.TraceWarning(String.Format("Found a concurrent {0} window", Constants.StartNewThreadCaption)); Win32Tools.ForceWindowIntoForeground(concurrentDiscussionWindow); return; } int parentToolPID = -1; try { string diffToolName = Path.GetFileNameWithoutExtension(createDiffTool().GetToolCommand()); StorageSupport.LocalCommitStorageType type = ConfigurationHelper.GetPreferredStorageType(Settings); string toolProcessName = type == StorageSupport.LocalCommitStorageType.FileStorage ? diffToolName : "git"; parentToolPID = getParentProcessId(context.CurrentProcess, toolProcessName); } catch (ArgumentException ex) { ExceptionHandlers.Handle("Cannot obtain diff tool file name", ex); } catch (Win32Exception ex) { ExceptionHandlers.Handle("Cannot find parent diff tool process", ex); } if (parentToolPID == -1) { Trace.TraceError("Cannot find parent diff tool process"); MessageBox.Show( "Cannot find parent diff tool process. Discussion cannot be created. Is Merge Request Helper running?", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return; } string[] argumentsEx = new string[context.Arguments.Length + 1]; Array.Copy(context.Arguments, 0, argumentsEx, 0, context.Arguments.Length); argumentsEx[argumentsEx.Length - 1] = parentToolPID.ToString(); string message = String.Join("|", argumentsEx.Skip(1)); // skip executable path IntPtr mainWindow = context.GetWindowByCaption(Constants.MainWindowCaption, true); if (mainWindow == IntPtr.Zero) { Debug.Assert(false); Trace.TraceWarning("Cannot find Main Window"); return; } Win32Tools.SendMessageToWindow(mainWindow, message); }
private static void Main() { using (LaunchContext context = new LaunchContext()) { Application.ThreadException += (sender, e) => HandleUnhandledException(e.Exception); try { string currentLogFileName = getLogFileName(context); CustomTraceListener listener = null; try { listener = new CustomTraceListener(currentLogFileName, String.Format("Merge Request Helper {0} started. PID {1}", Application.ProductVersion, Process.GetCurrentProcess().Id)); Trace.Listeners.Add(listener); } catch (ArgumentException) { // Cannot do anything good here return; } Directory.SetCurrentDirectory(Path.GetDirectoryName(context.CurrentProcess.MainModule.FileName)); ServiceManager = new ServiceManager(); FeedbackReporter = new FeedbackReporter( () => { listener.Flush(); listener.Close(); Trace.Listeners.Remove(listener); }, () => { try { listener = new CustomTraceListener(currentLogFileName, null); Trace.Listeners.Add(listener); } catch (Exception) { // Cannot do anything good here } }, getApplicationDataPath()); LaunchOptions options = LaunchOptions.FromContext(context); switch (options.Mode) { case LaunchOptions.LaunchMode.DiffTool: onLaunchFromDiffTool(options); break; case LaunchOptions.LaunchMode.Register: if (registerCustomProtocol()) { integrateInGitUI(); } break; case LaunchOptions.LaunchMode.Unregister: unregisterCustomProtocol(); break; case LaunchOptions.LaunchMode.Normal: if (context.IsRunningSingleInstance) { onLaunchMainInstace(options); } else { onLaunchAnotherInstance(options, context); } break; default: Debug.Assert(false); break; } } catch (Exception ex) // whatever unhandled exception { HandleUnhandledException(ex); } } }