示例#1
0
        public void InitializeAsync()
        {
            try
            {
                Logger.Info(string.Format("Initializing WakaTime v{0}", Constants.PluginVersion));

                // VisualStudio Object
                _docEvents      = ObjDte.Events.DocumentEvents;
                _windowEvents   = ObjDte.Events.WindowEvents;
                _solutionEvents = ObjDte.Events.SolutionEvents;

                // Settings Form
                _settingsForm              = new SettingsForm();
                _settingsForm.ConfigSaved += SettingsFormOnConfigSaved;

                try
                {
                    // Make sure python is installed
                    if (!Dependencies.IsPythonInstalled())
                    {
                        Dependencies.DownloadAndInstallPython();
                    }

                    if (!Dependencies.DoesCliExist() || !Dependencies.IsCliUpToDate())
                    {
                        Dependencies.DownloadAndInstallCli();
                    }
                }
                catch (WebException ex)
                {
                    Logger.Error("Are you behind a proxy? Try setting a proxy in WakaTime Settings with format https://user:pass@host:port. Exception Traceback:", ex);
                }
                catch (Exception ex)
                {
                    Logger.Error("Error detecting dependencies. Exception Traceback:", ex);
                }

                // Add our command handlers for menu (commands must exist in the .vsct file)
                var mcs = GetService(typeof(IMenuCommandService)) as OleMenuCommandService;
                if (mcs != null)
                {
                    // Create the command for the menu item.
                    var menuCommandId = new CommandID(GuidList.GuidWakaTimeCmdSet, (int)PkgCmdIdList.UpdateWakaTimeSettings);
                    var menuItem      = new MenuCommand(MenuItemCallback, menuCommandId);
                    mcs.AddCommand(menuItem);
                }

                // setup event handlers
                _docEvents.DocumentOpened     += DocEventsOnDocumentOpened;
                _docEvents.DocumentSaved      += DocEventsOnDocumentSaved;
                _windowEvents.WindowActivated += WindowEventsOnWindowActivated;
                _solutionEvents.Opened        += SolutionEventsOnOpened;

                // setup timer to process queued heartbeats every 8 seconds
                timer.Interval = 1000 * 8;
                timer.Elapsed += ProcessHeartbeats;
                timer.Start();

                Logger.Info(string.Format("Finished initializing WakaTime v{0}", Constants.PluginVersion));
            }
            catch (Exception ex)
            {
                Logger.Error("Error Initializing WakaTime", ex);
            }
        }
示例#2
0
        private void ProcessHeartbeats()
        {
            var pythonBinary = Dependencies.GetPython();

            if (pythonBinary != null)
            {
                // get first heartbeat from queue
                Heartbeat heartbeat;
                bool      gotOne = heartbeatQueue.TryDequeue(out heartbeat);
                if (!gotOne)
                {
                    return;
                }

                // remove all extra heartbeats from queue
                ArrayList extraHeartbeats = new ArrayList();
                Heartbeat h;
                while (heartbeatQueue.TryDequeue(out h))
                {
                    extraHeartbeats.Add(new Heartbeat(h));
                }
                bool hasExtraHeartbeats = extraHeartbeats.Count > 0;

                PythonCliParameters.Key                = ApiKey;
                PythonCliParameters.Plugin             = string.Format("{0}/{1} {2}/{3}", Constants.EditorName, Constants.EditorVersion, Constants.PluginName, Constants.PluginVersion);
                PythonCliParameters.File               = heartbeat.entity;
                PythonCliParameters.Time               = heartbeat.timestamp;
                PythonCliParameters.IsWrite            = heartbeat.is_write;
                PythonCliParameters.Project            = heartbeat.project;
                PythonCliParameters.HasExtraHeartbeats = hasExtraHeartbeats;

                string extraHeartbeatsJSON = null;
                if (hasExtraHeartbeats)
                {
                    extraHeartbeatsJSON = new JavaScriptSerializer().Serialize(extraHeartbeats);
                }

                var process = new RunProcess(pythonBinary, PythonCliParameters.ToArray());
                if (Debug)
                {
                    Logger.Debug(string.Format("[\"{0}\", \"{1}\"]", pythonBinary, string.Join("\", \"", PythonCliParameters.ToArray(true))));
                    process.Run(extraHeartbeatsJSON);
                    if (process.Output != null && process.Output != "")
                    {
                        Logger.Debug(process.Output);
                    }
                    if (process.Error != null && process.Error != "")
                    {
                        Logger.Debug(process.Error);
                    }
                }
                else
                {
                    process.RunInBackground(extraHeartbeatsJSON);
                }

                if (!process.Success)
                {
                    Logger.Error("Could not send heartbeat.");
                    if (process.Output != null && process.Output != "")
                    {
                        Logger.Error(process.Output);
                    }
                    if (process.Error != null && process.Error != "")
                    {
                        Logger.Error(process.Error);
                    }
                }
            }
            else
            {
                Logger.Error("Could not send heartbeat because python is not installed");
            }
        }