Пример #1
0
        public void Item_SystemXmlWhenProjectHasSystemXmlReference_OneReferenceReturned()
        {
            CreateReferences();
            msbuildProject.AddReference("System.Xml");

            global::EnvDTE.Reference reference = references.Item("System.Xml");

            Assert.AreEqual("System.Xml", reference.Name);
        }
Пример #2
0
        public static void AddProjectReferencesPathsIntoHost(References references, MetadataReaderHost host)
        {
            Contract.Requires(host != null);

            if (references == null)
            {
                return;
            }

            for (int i = 1; i <= references.Count; i++)
            {
                var tempRef = references.Item(i);
                if (tempRef == null)
                {
                    continue;
                }

                var refPath = tempRef.Path;
                if (!String.IsNullOrEmpty(refPath))
                {
                    var refDir = Path.GetDirectoryName(refPath);
                    if (refDir != null)
                    {
                        host.AddLibPath(refDir);
                        var referenceAssemblyPath = Path.Combine(refDir, "CodeContracts");
                        if (System.IO.Directory.Exists(referenceAssemblyPath))
                        {
                            host.AddLibPath(referenceAssemblyPath);
                        }
                    }
                }
            }
        }
Пример #3
0
        /// <summary>
        /// WORKAROUND:
        /// This override is in place to handle the case-sensitive call to Project.Object.References.Item
        /// There are certain assemblies where the AssemblyName and Assembly file name do not match in case
        /// And, this causes a mismatch. For more information, Refer to the RemoveReference of the base class
        /// </summary>
        /// <param name="name"></param>
        public override async Task RemoveReferenceAsync(string name)
        {
            await NuGetUIThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();

            try
            {
                var referenceName = Path.GetFileNameWithoutExtension(name);

                var reference = References.Item(referenceName);

                if (reference == null)
                {
                    // No exact match found for referenceName. Trying case-insensitive search
                    NuGetProjectContext.Log(ProjectManagement.MessageLevel.Warning, Strings.Warning_NoExactMatchForReference, referenceName);
                    foreach (var r in References.Cast <Reference>())
                    {
                        if (string.Equals(referenceName, r.Name, StringComparison.OrdinalIgnoreCase))
                        {
                            if (reference == null)
                            {
                                reference = r;
                            }
                            else
                            {
                                var message = string.Format(CultureInfo.CurrentCulture, Strings.FailedToRemoveReference, referenceName);
                                NuGetProjectContext.Log(ProjectManagement.MessageLevel.Error, message);
                                throw new InvalidOperationException(message);
                            }
                        }
                    }
                }

                // At this point, the necessary case-sensitive and case-insensitive search are performed
                if (reference != null)
                {
                    reference.Remove();
                    NuGetProjectContext.Log(ProjectManagement.MessageLevel.Debug, Strings.Debug_RemoveReference, name, ProjectName);
                }
                else
                {
                    NuGetProjectContext.Log(ProjectManagement.MessageLevel.Warning, Strings.Warning_FailedToFindMatchForRemoveReference, referenceName);
                }
            }
            catch (Exception e)
            {
                NuGetProjectContext.Log(ProjectManagement.MessageLevel.Warning, e.Message);
            }
        }
        private void RemoveReferenceCore(string name, References references)
        {
            Debug.Assert(ThreadHelper.CheckAccess());

            try
            {
                var referenceName = Path.GetFileNameWithoutExtension(name);

                Reference reference = references.Item(referenceName);

                if (reference == null)
                {
                    // No exact match found for referenceName. Trying case-insensitive search
                    NuGetProjectContext.Log(ProjectManagement.MessageLevel.Warning, Strings.Warning_NoExactMatchForReference, referenceName);
                    foreach (Reference r in references)
                    {
                        if (String.Equals(referenceName, r.Name, StringComparison.OrdinalIgnoreCase))
                        {
                            if (reference == null)
                            {
                                reference = r;
                            }
                            else
                            {
                                var message = String.Format(CultureInfo.CurrentCulture, Strings.FailedToRemoveReference, referenceName);
                                NuGetProjectContext.Log(ProjectManagement.MessageLevel.Error, message);
                                throw new InvalidOperationException(message);
                            }
                        }
                    }
                }

                // At this point, the necessary case-sensitive and case-insensitive search are performed
                if (reference != null)
                {
                    reference.Remove();
                    NuGetProjectContext.Log(ProjectManagement.MessageLevel.Debug, Strings.Debug_RemoveReference, name, ProjectName);
                }
                else
                {
                    NuGetProjectContext.Log(ProjectManagement.MessageLevel.Warning, Strings.Warning_FailedToFindMatchForRemoveReference, referenceName);
                }
            }
            catch (Exception e)
            {
                NuGetProjectContext.Log(ProjectManagement.MessageLevel.Warning, e.Message);
            }
        }
Пример #5
0
        /// <summary>
        /// We find the collection of references needed by this project.
        /// </summary>
        private void parseReferences()
        {
            // We loop through the collection of references, adding
            // them to the project. (There is a second pass later to
            // resolve references to other projects that may not have
            // been parsed yet.)
            References references    = Utils.call(() => (m_vsProject.References));
            int        numReferences = Utils.call(() => (references.Count));

            for (int i = 1; i <= numReferences; ++i)
            {
                Reference reference = Utils.call(() => references.Item(i));
                string    fullPath  = Utils.call(() => (reference.Path));
                bool      copyLocal = Utils.call(() => (reference.CopyLocal));
                m_projectInfo.addReference(fullPath, copyLocal);
            }
        }
Пример #6
0
        internal void RemoveReferenceCore(string name, References references)
        {
            try
            {
                var referenceName = System.IO.Path.GetFileNameWithoutExtension(name);

                Reference reference = references.Item(referenceName);

                if (reference == null)
                {
                    // No exact match found for referenceName. Trying case-insensitive search
                    Logger.Log(MessageLevel.Warning, VsResources.Warning_NoExactMatchForReference, referenceName);
                    foreach (Reference r in references)
                    {
                        if (String.Equals(referenceName, r.Name, StringComparison.OrdinalIgnoreCase))
                        {
                            if (reference == null)
                            {
                                reference = r;
                            }
                            else
                            {
                                var message = String.Format(CultureInfo.CurrentCulture, VsResources.FailedToRemoveReference, referenceName);
                                Logger.Log(MessageLevel.Error, message);
                                throw new InvalidOperationException(message);
                            }
                        }
                    }
                }

                // At this point, the necessary case-sensitive and case-insensitive search are performed
                if (reference != null)
                {
                    reference.Remove();
                    Logger.Log(MessageLevel.Debug, VsResources.Debug_RemoveReference, name, ProjectName);
                }
                else
                {
                    Logger.Log(MessageLevel.Warning, VsResources.Warning_FailedToFindMatchForRemoveReference, referenceName);
                }
            }
            catch (Exception e)
            {
                Logger.Log(MessageLevel.Warning, e.Message);
            }
        }
        internal void RemoveReferenceCore(string name, References references)
        {
            try
            {
                var referenceName = System.IO.Path.GetFileNameWithoutExtension(name);

                Reference reference = references.Item(referenceName);

                if (reference == null)
                {
                    // No exact match found for referenceName. Trying case-insensitive search
                    Logger.Log(MessageLevel.Warning, VsResources.Warning_NoExactMatchForReference, referenceName);
                    foreach (Reference r in references)
                    {
                        if (String.Equals(referenceName, r.Name, StringComparison.OrdinalIgnoreCase))
                        {
                            if (reference == null)
                            {
                                reference = r;
                            }
                            else
                            {
                                var message = String.Format(CultureInfo.CurrentCulture, VsResources.FailedToRemoveReference, referenceName);
                                Logger.Log(MessageLevel.Error, message);
                                throw new InvalidOperationException(message);
                            }
                        }
                    }
                }

                // At this point, the necessary case-sensitive and case-insensitive search are performed
                if (reference != null)
                {
                    reference.Remove();
                    Logger.Log(MessageLevel.Debug, VsResources.Debug_RemoveReference, name, ProjectName);
                }
                else
                {
                    Logger.Log(MessageLevel.Warning, VsResources.Warning_FailedToFindMatchForRemoveReference, referenceName);
                }
            }
            catch (Exception e)
            {
                Logger.Log(MessageLevel.Warning, e.Message);
            }
        }
Пример #8
0
        /// <summary>
        /// We find the collection of references needed by this project.
        /// </summary>
        private void parseReferences()
        {
            // We loop through the collection of references, adding
            // them to the project. (There is a second pass later to
            // resolve references to other projects that may not have
            // been parsed yet.)
            References references    = Utils.call(() => (m_vsProject.References));
            int        numReferences = Utils.call(() => (references.Count));

            for (int i = 1; i <= numReferences; ++i)
            {
                Reference reference = Utils.call(() => references.Item(i));
                string    fullPath  = Utils.call(() => (reference.Path));
                bool      copyLocal = Utils.call(() => (reference.CopyLocal));

                // Some projects can have bogus references.
                // It's not good but we shouldn't choke on them either.
                if (!string.IsNullOrEmpty(fullPath))
                {
                    m_projectInfo.addReference(fullPath, copyLocal);
                }
            }
        }
Пример #9
0
    public static void AddProjectReferencesPathsIntoHost(References references, MetadataReaderHost host) {
      Contract.Requires(host != null);

      if (references == null) return;
      
      for (int i = 1; i <= references.Count; i++) {
        var tempRef = references.Item(i);
        if (tempRef == null) continue;

        var refPath = tempRef.Path;
        if (!String.IsNullOrEmpty(refPath)) {
          var refDir = Path.GetDirectoryName(refPath);
          if (refDir != null) {
            host.AddLibPath(refDir);
            var referenceAssemblyPath = Path.Combine(refDir, "CodeContracts");
            if (System.IO.Directory.Exists(referenceAssemblyPath))
              host.AddLibPath(referenceAssemblyPath);

          }
        }
      }
    }
Пример #10
0
        /// <summary>
        /// Load the project settings for a specific transform file and return it
        /// </summary>
        /// <param name="sourceFile">The full path to the data file</param>
        /// <param name="baseDirectory">The path for the base directory (with a trailing \)</param>
        /// <param name="arguments">Argument list to pass to the returned transform</param>
        /// <param name="project">The context project</param>
        /// <param name="automationObjectName">The name of an automation object supported by the live document. Used
        /// to get a live version of the file stream.</param>
        /// <returns>The transform file name. Existence will have been verified.</returns>
        private string LoadProjectSettings(string sourceFile, string baseDirectory, XsltArgumentList arguments, EnvDTE.Project project, out string automationObjectName)
        {
                        #if VerifyUIThread
            Microsoft.VisualStudio.Shell.ThreadHelper.ThrowIfNotOnUIThread();
                        #endif

            Debug.Assert(arguments != null);             // Allocate before call
            string transformFile = null;
            automationObjectName = null;
            string plixProjectSettingsFile = baseDirectory + PlixProjectSettingsFile;
            if (File.Exists(plixProjectSettingsFile))
            {
                // Use the text from the live document if possible
                string          liveText    = null;
                EnvDTE.Document settingsDoc = null;
                try
                {
                    settingsDoc = project.DTE.Documents.Item(plixProjectSettingsFile);
                }
                catch (ArgumentException)
                {
                    // swallow
                }
                catch (InvalidCastException)
                {
                    // swallow
                }
                if (settingsDoc != null)
                {
                    EnvDTE.TextDocument textDoc = settingsDoc.Object("TextDocument") as EnvDTE.TextDocument;
                    if (textDoc != null)
                    {
                        liveText = textDoc.StartPoint.CreateEditPoint().GetText(textDoc.EndPoint);
                    }
                }
                string sourceFileIdentifier = sourceFile.Substring(baseDirectory.Length);
                PlixLoaderNameTable names   = PlixLoaderSchema.Names;
                using (FileStream plixSettingsStream = (liveText == null) ? new FileStream(plixProjectSettingsFile, FileMode.Open, FileAccess.Read) : null)
                {
                    using (XmlTextReader settingsReader = new XmlTextReader((liveText == null) ? new StreamReader(plixSettingsStream) as TextReader : new StringReader(liveText), names))
                    {
                        using (XmlReader reader = XmlReader.Create(settingsReader, PlixLoaderSchema.ReaderSettings))
                        {
                            References references = null;
                            bool       finished   = false;
                            while (!finished && reader.Read())
                            {
                                if (reader.NodeType == XmlNodeType.Element)
                                {
                                    if (!reader.IsEmptyElement)
                                    {
                                        while (reader.Read())
                                        {
                                            XmlNodeType nodeType = reader.NodeType;
                                            if (nodeType == XmlNodeType.Element)
                                            {
                                                Debug.Assert(XmlUtility.TestElementName(reader.LocalName, names.SourceFileElement));                                                 // Only value allowed by the validating loader
                                                string testFileName = reader.GetAttribute(names.FileAttribute);
                                                if (0 == string.Compare(testFileName, sourceFileIdentifier, true, CultureInfo.CurrentCulture))
                                                {
                                                    finished = true;                                                     // Stop looking
                                                    string attrValue = reader.GetAttribute(names.TransformFileAttribute);
                                                    if (attrValue != null && attrValue.Length != 0)
                                                    {
                                                        transformFile = baseDirectory + attrValue;
                                                    }
                                                    attrValue = reader.GetAttribute(names.LiveDocumentObjectAttribute);
                                                    if (attrValue != null && attrValue.Length != 0)
                                                    {
                                                        automationObjectName = attrValue;
                                                    }
                                                    if (!reader.IsEmptyElement)
                                                    {
                                                        while (reader.Read())
                                                        {
                                                            nodeType = reader.NodeType;
                                                            if (nodeType == XmlNodeType.Element)
                                                            {
                                                                string localName = reader.LocalName;
                                                                if (XmlUtility.TestElementName(localName, names.TransformParameterElement))
                                                                {
                                                                    // Add an argument for the transform
                                                                    arguments.AddParam(reader.GetAttribute(names.NameAttribute), "", reader.GetAttribute(names.ValueAttribute));
                                                                }
                                                                else if (XmlUtility.TestElementName(localName, names.ExtensionClassElement))
                                                                {
                                                                    // Load an extension class and associate it with an extension namespace
                                                                    // used by the transform
                                                                    arguments.AddExtensionObject(reader.GetAttribute(names.XslNamespaceAttribute), Type.GetType(reader.GetAttribute(names.ClassNameAttribute), true, false).GetConstructor(Type.EmptyTypes).Invoke(new object[0]));
                                                                }
                                                                else if (XmlUtility.TestElementName(localName, names.ProjectReferenceElement))
                                                                {
                                                                    // The generated code requires project references, add them
                                                                    if (null == references)
                                                                    {
                                                                        references = ((VSProject)project.Object).References;
                                                                    }
                                                                    if (references.Item(reader.GetAttribute(names.NamespaceAttribute)) == null)
                                                                    {
                                                                        references.Add(reader.GetAttribute(names.AssemblyAttribute));
                                                                    }
                                                                }
                                                                else
                                                                {
                                                                    Debug.Assert(false);                                                                     // Not allowed by schema definition
                                                                }
                                                                XmlUtility.PassEndElement(reader);
                                                            }
                                                            else if (nodeType == XmlNodeType.EndElement)
                                                            {
                                                                break;
                                                            }
                                                        }
                                                    }
                                                    break;
                                                }
                                                XmlUtility.PassEndElement(reader);
                                            }
                                            else if (nodeType == XmlNodeType.EndElement)
                                            {
                                                break;
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            bool verifiedExistence = false;
            if (transformFile == null)
            {
                string fileBase = sourceFile.Substring(0, sourceFile.LastIndexOf('.'));
                transformFile = fileBase + ".xslt";
                if (File.Exists(transformFile))
                {
                    verifiedExistence = true;
                }
                else
                {
                    transformFile = fileBase + ".xsl";
                }
            }
            if (!verifiedExistence && !File.Exists(transformFile))
            {
                transformFile = null;
            }
            return(transformFile);
        }