private void OnBuildDone(EnvDTE.vsBuildScope Scope, EnvDTE.vsBuildAction Action)
        {
            // detach from the event
            buildEvents.OnBuildDone -= OnBuildDone;

            // rollback the properties changes
            Rollback();

            EnvDTE80.ErrorList errorList = ((EnvDTE80.DTE2) this.project.DTE).ToolWindows.ErrorList;
            // ShowResult if there is any WCF rule violation
            int ruleWarnings = RuleWarningsCount(errorList);

            if (ruleWarnings == 0)
            {
                Logger.Write(
                    Resources.CodeAnalysisSuccess, string.Empty, TraceEventType.Information, 1);
            }
            else
            {
                Logger.Write(
                    string.Format(CultureInfo.CurrentCulture, Resources.CodeAnalysisWarnings, ruleWarnings));
                // We may force the Show Warnings button to be ON so the user will always see any
                // warning message from the result of the code analysis run.
                errorList.ShowWarnings = true;
            }
            this.project = null;
        }
        private int RuleWarningsCount(EnvDTE80.ErrorList errorList)
        {
            int warningsCount = 0;

            for (int index = 1; index <= errorList.ErrorItems.Count; index++)
            {
                // check only on warnings
                if (errorList.ErrorItems.Item(index).ErrorLevel ==
                    EnvDTE80.vsBuildErrorLevel.vsBuildErrorLevelLow)
                {
                    if (errorList.ErrorItems.Item(index).Description.StartsWith(
                            RuleCheckIdPrefix, StringComparison.OrdinalIgnoreCase))
                    {
                        warningsCount++;
                    }
                }
            }
            return(warningsCount);
        }
        /// <summary>
        /// Get the errors in the Error list window
        /// Picks up errors with "The name 'ABCPercentA' does not denote a class, a table, or an extended data type"
        /// as this denotes that there is a missing module reference
        /// </summary>
        /// <returns>True is a reference was added</returns>
        public bool CheckErrorsAndAddReference()
        {
            // Most of the code comes from this stackoverflow conversation
            // https://stackoverflow.com/questions/36834038/visual-studio-2015-envdte-read-errorlist

            bool moduleReferenceAdded = false;

            // get the list of errors from the Error list wondow
            EnvDTE.Window      window = Common.CommonUtil.DTE.Windows.Item(vsWindowKindErrorList);
            EnvDTE80.ErrorList sel    = (EnvDTE80.ErrorList)window.Selection;
            var errorItemsDte         = sel.ErrorItems;

            for (int i = 1; i <= errorItemsDte.Count; i++)
            {
                var errorItem = errorItemsDte.Item(i);
                if (errorItem.ErrorLevel == EnvDTE80.vsBuildErrorLevel.vsBuildErrorLevelHigh &&
                    errorItem.Description.Contains("does not denote a class, a table, or an extended data type"))
                // The name 'ABCPercentA' does not denote a class, a table, or an extended data type
                {
                    //System.Windows.Forms.MessageBox.Show($"Error found: {errorItem.ErrorLevel}; {errorItem.Description}");

                    //now find the element - check if it is an EDT, Table, Class
                    string elementName           = this.GetElementNameFromError(errorItem.Description);
                    var    moduleNameToReference = this.GetModuleFromElement(elementName);
                    if (String.IsNullOrEmpty(moduleNameToReference))
                    {
                        throw new Exception($"No matching element found for {elementName} in EDT's, tables, classes");
                    }
                    // Then find the model

                    // then add the model to the current models reference
                    var currentModel = Common.CommonUtil.GetCurrentModel();
                    // Update the module references
                    if (currentModel.Readonly || currentModel.ModuleReferences.IsReadOnly)
                    {
                        //you cant add to the list as it is read only.
                        // So copy the list into a new one, add the module & updated the model with this list as moduleReference
                        List <string> modules = new List <string>(currentModel.ModuleReferences);
                        if (modules.Contains(moduleNameToReference) == false)
                        {
                            modules.Add(moduleNameToReference);
                            currentModel.ModuleReferences = modules;

                            Common.CommonUtil.GetModelSaveService().UpdateModel(currentModel);
                            this.ReferencesAdded.Add(moduleNameToReference);
                            moduleReferenceAdded = true;
                        }
                        else
                        {
                            // Something else is going on,so break;
                            // User has to manually refresh models and build
                            break;
                        }
                    }
                    //else
                    //{
                    //    // Dont think this will ever go here
                    //    currentModel.ModuleReferences.Add(moduleNameToReference); // this cannot be updated as it is readonly
                    //}

                    // break; // there is usually only one error list this at a time
                }
            }

            return(moduleReferenceAdded);
        }