예제 #1
0
        /// <summary>
        ///
        /// </summary>
        /// <returns>false if it couldn't start</returns>
        public bool Start()
        {
            try
            {
                foreach (var hg in Process.GetProcessesByName("hg"))
                {
                    //EventLog.WriteEntry("Application", "Killing old hg...", EventLogEntryType.Information);
                    hg.Kill();
                    if (!hg.WaitForExit(10000))
                    {
                        //EventLog.WriteEntry("Application", "ChorusHub was unable to stop an old hg from running. It will now give up. You should stop the server and run it again after killing whatever 'hg.exe' process is running.", EventLogEntryType.Error);
                        return(false);
                    }
                }


                //we make directories based on what we see in there, so start it afresh lest we re-create folder names
                //that the user long ago stopped using

                if (File.Exists(AccessLogPath))
                {
                    File.Delete(AccessLogPath);
                }

                if (!Directory.Exists(_rootFolder))
                {
                    Directory.CreateDirectory(_rootFolder);
                }

                //EventLog.WriteEntry("Application", "Starting Mercurial Server", EventLogEntryType.Information);

                WriteConfigFile(_rootFolder);

                var arguments = "serve -A accessLog.txt -E log.txt -p " + Port + " --verbose ";

                //const float kHgVersion = (float)1.5;
                //if (kHgVersion < 1.9)
                //{
                arguments += "--webdir-conf hgweb.config";
                //}
                //else
                //{
                //	arguments += "--web-conf hgweb.config";
                //}

#if CommandWindow
                _hgServeProcess = new Process();
                _hgServeProcess.StartInfo.WorkingDirectory       = _rootFolder;
                _hgServeProcess.StartInfo.FileName               = Chorus.MercurialLocation.PathToHgExecutable;
                _hgServeProcess.StartInfo.Arguments              = arguments;
                _hgServeProcess.StartInfo.ErrorDialog            = true;
                _hgServeProcess.StartInfo.UseShellExecute        = false;
                _hgServeProcess.StartInfo.RedirectStandardOutput = true;
                _hgServeProcess.Start();
#else
                _hgServeThread = new Thread(() =>
                {
                    var commandLineRunner = new CommandLineRunner();
                    try
                    {
                        var progress = new ConsoleProgress();
                        commandLineRunner.Start(
                            Chorus.MercurialLocation.PathToHgExecutable,
                            arguments,
                            Encoding.UTF8, _rootFolder, -1,
                            progress, s => progress.WriteMessage(s));
                    }
                    catch (ThreadAbortException)
                    {
                        //Progress.WriteVerbose("Hg Serve command Thread Aborting (that's normal when stopping)");
                        try
                        {
                            if (!commandLineRunner.Abort(1))
                            {
                                //EventLog.WriteEntry("Application", "Hg Serve might not have closed down.", EventLogEntryType.Information);
                            }
                        }
                        catch { }
                    }
                });
                _hgServeThread.Start();
#endif

                return(true);
            }
            catch
            {
                //EventLog.WriteEntry("Application", error.Message, EventLogEntryType.Error);
                return(false);
            }
        }