Esempio n. 1
0
        /// <summary>
        ///     Attempt to load the underlying MSBuild project.
        /// </summary>
        /// <returns>
        ///     <c>true</c>, if the project was successfully loaded; otherwise, <c>false</c>.
        /// </returns>
        protected override bool TryLoadMSBuildProject()
        {
            try
            {
                if (HasMSBuildProject && !IsDirty)
                {
                    return(true);
                }

                if (!MasterProjectDocument.HasMSBuildProject)
                {
                    throw new InvalidOperationException("Parent project does not have an MSBuild project.");
                }

                if (MSBuildProjectCollection == null)
                {
                    MSBuildProjectCollection = MasterProjectDocument.MSBuildProjectCollection;
                }

                if (HasMSBuildProject && IsDirty)
                {
                    using (StringReader reader = new StringReader(Xml.ToFullString()))
                        using (XmlTextReader xmlReader = new XmlTextReader(reader))
                        {
                            MSBuildProject.Xml.ReloadFrom(xmlReader,
                                                          throwIfUnsavedChanges: false,
                                                          preserveFormatting: true
                                                          );
                        }

                    MSBuildProject.ReevaluateIfNecessary();

                    Log.Verbose("Successfully updated MSBuild project '{ProjectFileName}' from in-memory changes.");
                }
                else
                {
                    MSBuildProject = MSBuildProjectCollection.LoadProject(ProjectFile.FullName);
                }

                return(true);
            }
            catch (InvalidProjectFileException invalidProjectFile)
            {
                if (Workspace.Configuration.Logging.IsDebugLoggingEnabled)
                {
                    Log.Error(invalidProjectFile, "Failed to load MSBuild proiect '{ProjectFileName}'.",
                              ProjectFile.FullName
                              );
                }

                AddErrorDiagnostic(invalidProjectFile.BaseMessage,
                                   range: invalidProjectFile.GetRange(XmlLocator),
                                   diagnosticCode: invalidProjectFile.ErrorCode
                                   );
            }
            catch (XmlException invalidProjectXml)
            {
                if (Workspace.Configuration.Logging.IsDebugLoggingEnabled)
                {
                    Log.Error(invalidProjectXml, "Failed to parse XML for project '{ProjectFileName}'.",
                              ProjectFile.FullName
                              );
                }

                // TODO: Match SourceUri (need overloads of AddXXXDiagnostic for reporting diagnostics for other files).
                AddErrorDiagnostic(invalidProjectXml.Message,
                                   range: invalidProjectXml.GetRange(XmlLocator),
                                   diagnosticCode: "MSBuild.InvalidXML"
                                   );
            }
            catch (Exception loadError)
            {
                Log.Error(loadError, "Error loading MSBuild project '{ProjectFileName}'.", ProjectFile.FullName);
            }

            return(false);
        }
        /// <summary>
        ///     Attempt to load the underlying MSBuild project.
        /// </summary>
        /// <returns>
        ///     <c>true</c>, if the project was successfully loaded; otherwise, <c>false</c>.
        /// </returns>
        protected override bool TryLoadMSBuildProject()
        {
            try
            {
                if (HasMSBuildProject && !IsDirty)
                {
                    return(true);
                }

                if (MSBuildProjectCollection == null)
                {
                    MSBuildProjectCollection = MSBuildHelper.CreateProjectCollection(ProjectFile.Directory.FullName);
                }

                if (HasMSBuildProject && IsDirty)
                {
                    using (StringReader reader = new StringReader(Xml.ToFullString()))
                        using (XmlTextReader xmlReader = new XmlTextReader(reader))
                        {
                            MSBuildProject.Xml.ReloadFrom(xmlReader,
                                                          throwIfUnsavedChanges: false,
                                                          preserveFormatting: true
                                                          );
                        }

                    MSBuildProject.ReevaluateIfNecessary();

                    Log.Verbose("Successfully updated MSBuild project '{ProjectFileName}' from in-memory changes.");
                }
                else
                {
                    MSBuildProject = MSBuildProjectCollection.LoadProject(ProjectFile.FullName);
                }

                return(true);
            }
            catch (InvalidProjectFileException invalidProjectFile)
            {
                Log.Verbose(invalidProjectFile, "Failed to load MSBuild proiect '{ProjectFileName}' because the project file is invalid. {ErrorMessage}",
                            ProjectFile.FullName,
                            invalidProjectFile.Message
                            );

                AddErrorDiagnostic(invalidProjectFile.BaseMessage,
                                   range: invalidProjectFile.GetRange(XmlLocator),
                                   diagnosticCode: invalidProjectFile.ErrorCode
                                   );
            }
            catch (XmlException invalidProjectXml)
            {
                Log.Verbose(invalidProjectXml, "Failed to load MSBuild proiect '{ProjectFileName}' because the project XML is invalid. {ErrorMessage}",
                            ProjectFile.FullName,
                            invalidProjectXml.Message
                            );

                // TODO: Match SourceUri (need overloads of AddXXXDiagnostic for reporting diagnostics for other files).
                AddErrorDiagnostic(invalidProjectXml.Message,
                                   range: invalidProjectXml.GetRange(XmlLocator),
                                   diagnosticCode: "MSBuild.InvalidXML"
                                   );
            }
            catch (Exception loadError)
            {
                Log.Error(loadError, "Error loading MSBuild project '{ProjectFileName}'.", ProjectFile.FullName);
            }

            return(false);
        }