Exemple #1
0
 /// <summary>
 /// Initializes a new instance of the <see cref="NodeCollection"/> class.
 /// </summary>
 /// <param name="parent">The collection's parent folder node.</param>
 /// <param name="comparer">A comparer to use for the collection.</param>
 protected NodeCollection(FolderNode parent, IComparer comparer) : base(comparer)
 {
     Tracer.VerifyNonNullArgument(parent, "parent");
     this.parent = parent;
 }
Exemple #2
0
        //==========================================================================================
        // Constructors
        //==========================================================================================

        /// <summary>
        /// Initializes a new instance of the <see cref="NodeProperties"/> class.
        /// </summary>
        protected NodeProperties(Node node)
        {
            Tracer.VerifyNonNullArgument(node, "node");
            this.node = node;
        }
Exemple #3
0
        public static void TraceRunningDocuments()
        {
            // Get the RDT (Running Document Table)
            IVsRunningDocumentTable rdt = Package.Instance.Context.ServiceProvider.GetService(typeof(IVsRunningDocumentTable)) as IVsRunningDocumentTable;

            if (rdt == null)
            {
                Tracer.WriteLineWarning(classType, "TraceRunningDocuments", "Cannot get an instance of IVsRunningDocumentTable to use for enumerating the running documents.");
                return;
            }

            // Get the enumerator for the currently running documents.
            IEnumRunningDocuments enumerator;
            int hr = rdt.GetRunningDocumentsEnum(out enumerator);

            if (NativeMethods.Failed(hr))
            {
                Tracer.WriteLineWarning(classType, "TraceRunningDocuments", "Cannot get an instance of IEnumRunningDocuments to use for enumerating the running documents.");
                return;
            }

            // Enumerate.
            StringCollection traceLines = new StringCollection();

            uint[] cookies = new uint[1];
            uint   fetchCount;

            while (true)
            {
                hr = enumerator.Next(1, cookies, out fetchCount);
                if (NativeMethods.Failed(hr))
                {
                    Tracer.WriteLineWarning(classType, "TraceRunningDocuments", "The enumeration failed for the running documents. Hr=0x{0:X}", hr);
                    return;
                }

                if (fetchCount == 0)
                {
                    break;
                }

                uint cookie = cookies[0];

                // We shouldn't be getting a nil cookie.
                if (cookie == DocumentInfo.NullCookie)
                {
                    Tracer.WriteLineWarning(classType, "TraceRunningDocuments", "There is a null cookie value in the RDT, which shouldn't be happening.");
                }
                else
                {
                    // Now we have a document cookie, so let's get some information about it.
                    DocumentInfo docInfo = Package.Instance.Context.RunningDocumentTable.FindByCookie(cookie);
                    string       traceMessage;
                    if (docInfo == null)
                    {
                        traceMessage = PackageUtility.SafeStringFormatInvariant("The document with cookie '{0}' could not be found in the RDT. There's something weird going on.", cookie);
                    }
                    else
                    {
                        // Here's where we actually do the trace finally.
                        traceMessage = PackageUtility.SafeStringFormatInvariant("RDT document: Cookie={0} Path={1} IsOpen={2} IsDirty={3}", docInfo.Cookie, docInfo.AbsolutePath, docInfo.IsOpen, docInfo.IsDirty);
                    }

                    // We don't want to trace immediately because we want all of these lines to appear together. If we
                    // trace immediately, then the messages will be split up.
                    traceLines.Add(traceMessage);
                }
            }

            // Now trace all of the messages at once.
            foreach (string traceMessage in traceLines)
            {
                Tracer.WriteLine(classType, "TraceRunningDocuments", Tracer.Level.Information, traceMessage);
            }
        }
Exemple #4
0
        /// <summary>
        /// Reads the &lt;WindowsInstallerXml&gt; node.
        /// </summary>
        /// <param name="node">The <see cref="XmlNode"/> to parse.</param>
        /// <returns>true if the node was read correctly; otherwise, false.</returns>
        protected bool ReadProjectNode(XmlNode node)
        {
            if (!this.VerifyNode(node, this.ProjectElementName))
            {
                return(false);
            }

            // Read and validate all of the required attributes.
            // -------------------------------------------------

            // SchemaVersion
            Version fileSchemaVersion = this.SchemaVersion;
            string  schemaString      = XmlHelperMethods.GetAttributeString(node, AttributeNames.SchemaVersion, this.SchemaVersion.ToString());

            try
            {
                fileSchemaVersion = new Version(schemaString);
            }
            catch (Exception e)
            {
                Tracer.WriteLineWarning(classType, "ReadProjectNode", "Cannot parse the SchemaVersion attribute {0}: {1}.", schemaString, e.ToString());
            }
            if (fileSchemaVersion < this.SchemaVersion)
            {
                // Right now we only support version 1.0 schemas, but if we ever change the schema
                // with new versions then we would need to ask the user if he/she wants to upgrade
                // the project to the new schema version.
            }
            else if (fileSchemaVersion > this.SchemaVersion)
            {
                string projectFileName = Path.GetFileName(this.Project.FilePath);
                string message         = Package.Instance.Context.NativeResources.GetString(ResId.IDS_E_PROJECTFILENEWERVERSION, projectFileName);
                Package.Instance.Context.ShowErrorMessageBox(message);
                return(false);
            }

            // ProjectGuid
            this.Project.ProjectGuid = XmlHelperMethods.GetAttributeGuid(node, AttributeNames.ProjectGuid, Guid.NewGuid());

            // Read the rest of the nodes in the project file in any order.
            // -----------------------------------------------------------
            foreach (XmlNode childNode in node.ChildNodes)
            {
                bool success = true;

                switch (childNode.Name)
                {
                case ElementNames.BuildSettings:
                    success = this.ReadBuildSettingsNode(childNode);
                    break;

                case ElementNames.Configurations:
                    success = this.ReadCollectionNode(childNode, ElementNames.Configurations, ElementNames.Configuration, new ReadCollectionItem(this.ReadConfigurationNode));
                    break;

                case ElementNames.Files:
                    success = this.ReadCollectionNode(childNode, ElementNames.Files, ElementNames.File, new ReadCollectionItem(this.ReadFileNode));
                    break;
                }

                // We can't have this in the switch block because the reference node is not a constant string value.
                if (childNode.Name == this.ReferencesElementName)
                {
                    success = this.ReadCollectionNode(childNode, this.ReferencesElementName, this.ReferenceElementName, new ReadCollectionItem(this.ReadLibraryReferenceNode));
                }

                if (!success)
                {
                    this.Project.Unavailable = true;
                    return(false);
                }
            }

            return(true);
        }
Exemple #5
0
        /// <summary>
        /// Saves the attached project in the specified encoding.
        /// </summary>
        /// <param name="encoding">The encoding of the file. If null, <see cref="Encoding.UTF8"/> is used.</param>
        /// <param name="forceSave">Indicates whether to ignore the attached project's dirty flag when determining whether to save.</param>
        /// <returns>true if successful; otherwise, false.</returns>
        public bool Save(Encoding encoding, bool forceSave)
        {
            if (encoding == null)
            {
                encoding = Encoding.UTF8;
            }

            // If a project hasn't been attached yet, there's nothing to save.
            if (this.Project == null)
            {
                return(false);
            }

            // Check the dirty state of the project to see if we even need to save.
            if (!this.Project.IsDirty && !forceSave)
            {
                Tracer.WriteLineInformation(classType, "Save", "The project doesn't need to be saved.");
                return(true);
            }

            // At this point we know we have to save the project.
            string filePath = this.Project.FilePath;

            try
            {
                using (StreamWriter streamWriter = new StreamWriter(filePath, false, encoding))
                {
                    ProjectFileXmlWriter writer = new ProjectFileXmlWriter(streamWriter);
                    writer.WriteStartDocument();

                    // <VisualStudioProject>
                    writer.WriteStartElement(ElementNames.VisualStudioProject);

                    // <Project>
                    writer.WriteStartElement(this.ProjectElementName);
                    this.WriteProjectAttributes(writer);

                    // <BuildSettings>
                    BuildSettings buildSettings = this.Project.BuildSettings;
                    writer.WriteStartElement(ElementNames.BuildSettings);
                    this.WriteBuildSettingsAttributes(writer);
                    writer.WriteEndElement();

                    // <Configurations>
                    writer.WriteStartElement(ElementNames.Configurations);
                    foreach (ProjectConfiguration config in this.Project.ConfigurationProvider.ProjectConfigurations)
                    {
                        this.WriteConfigurationNode(writer, config);
                    }
                    writer.WriteEndElement();

                    // <References>
                    writer.WriteStartElement(this.ReferencesElementName);
                    foreach (ReferenceFileNode reference in this.Project.ReferencesNode.Children)
                    {
                        writer.WriteStartElement(this.ReferenceElementName);
                        writer.WriteAttributeString(AttributeNames.RelativePath, reference.RelativePath);
                        writer.WriteEndElement();
                    }
                    writer.WriteEndElement();

                    // <Files>
                    writer.WriteStartElement(ElementNames.Files);
                    this.WriteFilesInNode(writer, this.Project.RootNode);
                    writer.WriteEndElement();

                    writer.WriteEndDocument();

                    // Clear the project's dirty state.
                    this.Project.ClearDirty();
                }
            }
            catch (Exception e)
            {
                if (!this.SilentFailures)
                {
                    string title   = Package.Instance.Context.NativeResources.GetString(ResId.IDS_E_PROJECTFILESAVE_TITLE, filePath);
                    string message = Package.Instance.Context.NativeResources.GetString(ResId.IDS_E_PROJECTFILESAVE, e.Message);
                    Package.Instance.Context.ShowErrorMessageBox(title, message);
                }
                Tracer.Fail("There was an error in saving the file {0}: {1}", filePath, e.ToString());
                return(false);
            }

            return(true);
        }
Exemple #6
0
        /// <summary>
        /// Loads a project file from disk.
        /// </summary>
        /// <param name="filePath">The absolute path of the project file to load.</param>
        /// <returns>true if the project file was loaded correctly; otherwise, false.</returns>
        public bool Load(string filePath)
        {
            Tracer.VerifyStringArgument(filePath, "filePath");

            // Create a new project.
            this.project = this.CreateProject(this);

            // Set the project's file path to the one being loaded in. Do this first in case we have
            // to make the project unavailable it can still display the correct caption in Solution Explorer.
            this.Project.FilePath = filePath;

            // Make sure the file exists.
            if (!File.Exists(filePath))
            {
                if (!this.SilentFailures)
                {
                    string message = SconceStrings.FileDoesNotExist(filePath);
                    Package.Instance.Context.ShowErrorMessageBox(message);
                }
                this.Project.Unavailable = true;
                Tracer.WriteLine(classType, "Load", Tracer.Level.Warning, "The project file '{0}' does not exist.", filePath);
                return(false);
            }

            try
            {
                using (StreamReader stream = new StreamReader(filePath))
                {
                    XmlTextReader reader = new XmlTextReader(stream);
                    reader.WhitespaceHandling = WhitespaceHandling.None;
                    XmlDocument xmlDoc = new XmlDocument();
                    xmlDoc.Load(reader);
                    XmlNode node = xmlDoc.DocumentElement;

                    // <VisualStudioProject>
                    if (!this.VerifyNode(node, ElementNames.VisualStudioProject))
                    {
                        this.Project.Unavailable = true;
                        return(false);
                    }
                    node = node.FirstChild;

                    if (!this.ReadProjectNode(node))
                    {
                        this.Project.Unavailable = true;
                        return(false);
                    }
                }
            }
            catch (XmlException e)
            {
                if (!this.SilentFailures)
                {
                    string projectFileName = Path.GetFileName(filePath);
                    string title           = Package.Instance.Context.NativeResources.GetString(ResId.IDS_E_INVALIDPROJECTFILE_TITLE);
                    string message         = Package.Instance.Context.NativeResources.GetString(ResId.IDS_E_INVALIDPROJECTFILE, projectFileName);
                    Package.Instance.Context.ShowErrorMessageBox(title, message);
                }
                this.Project.Unavailable = true;
                Tracer.Fail("There was an error parsing '{0}': {1}", filePath, e.ToString());
                return(false);
            }

            // Once the project has been loaded, it's not dirty anymore.
            this.Project.ClearDirty();

            return(true);
        }