Пример #1
0
        /// <summary>
        /// Checks if a reference is already added. The method parses all references and compares the Url.
        /// </summary>
        /// <returns>true if the assembly has already been added.</returns>
        public virtual bool IsAlreadyAdded(out ReferenceNode existingNode)
        {
            ReferenceContainerNode referencesFolder = this.ProjectMgr.FindChild(ReferenceContainerNode.ReferencesNodeVirtualName) as ReferenceContainerNode;

            Debug.Assert(referencesFolder != null, "Could not find the References node");

            for (HierarchyNode n = referencesFolder.FirstChild; n != null; n = n.NextSibling)
            {
                ReferenceNode referenceNode = n as ReferenceNode;
                if (null != referenceNode)
                {
                    if (!string.IsNullOrEmpty(referenceNode.SimpleName) && 0 == string.CompareOrdinal(referenceNode.SimpleName, this.SimpleName))
                    {
                        existingNode = referenceNode;
                        return(true);
                    }
                }
            }

            existingNode = null;
            return(false);
        }
Пример #2
0
        /// <summary>
        /// Checks if a reference is already added. The method parses all references and compares the the FinalItemSpec and the Guid.
        /// </summary>
        /// <returns>true if the assembly has already been added.</returns>
        public override bool IsAlreadyAdded(out ReferenceNode existingNode)
        {
            ReferenceContainerNode referencesFolder = this.ProjectMgr.FindChild(ReferenceContainerNode.ReferencesNodeVirtualName) as ReferenceContainerNode;

            Debug.Assert(referencesFolder != null, "Could not find the References node");

            for (HierarchyNode n = referencesFolder.FirstChild; n != null; n = n.NextSibling)
            {
                if (n is ComReferenceNode)
                {
                    ComReferenceNode referenceNode = n as ComReferenceNode;

                    // We check if the name and guids are the same
                    if (referenceNode.TypeGuid == this.TypeGuid && String.Compare(referenceNode.Caption, this.Caption, StringComparison.OrdinalIgnoreCase) == 0)
                    {
                        existingNode = referenceNode;
                        return(true);
                    }
                }
            }

            existingNode = null;
            return(false);
        }
        internal static bool ContainsUsagesOfTargetFSharpCoreVersionProperty(ReferenceNode node)
        {
            Debug.Assert(IsFSharpCoreReference(node));
            if (node.ItemNode.Item.UnevaluatedInclude.Contains(ProjectFileConstants.TargetFSharpCoreVersionProperty))
                return true;

            var hintPath = node.ItemNode.Item.GetMetadata(ProjectFileConstants.HintPath);
            return hintPath != null && hintPath.UnevaluatedValue.Contains(ProjectFileConstants.TargetFSharpCoreVersionProperty);
        }
 internal static bool IsFSharpCoreReference(ReferenceNode node)
 {
     var isFSharpCore = false;
     try
     {
         var assemblyName = new AssemblyName(node.ItemNode.Item.EvaluatedInclude);
         isFSharpCore = assemblyName.Name == "FSharp.Core";
     }
     catch (FileLoadException)
     {
         // constructor of AssemblyName raises FileNotFoundException if supplied name cannot be parsed as valid assembly name
     }
     return isFSharpCore;
 }
Пример #5
0
        /// <summary>
        /// Checks if a reference is already added. The method parses all references and compares the Url.
        /// </summary>
        /// <returns>true if the assembly has already been added.</returns>
        public virtual bool IsAlreadyAdded(out ReferenceNode existingNode)
        {
            ReferenceContainerNode referencesFolder = this.ProjectMgr.FindChild(ReferenceContainerNode.ReferencesNodeVirtualName) as ReferenceContainerNode;
            Debug.Assert(referencesFolder != null, "Could not find the References node");

            for (HierarchyNode n = referencesFolder.FirstChild; n != null; n = n.NextSibling)
            {
                ReferenceNode referenceNode = n as ReferenceNode;
                if (null != referenceNode)
                {
                    if (!string.IsNullOrEmpty(referenceNode.SimpleName) && 0==string.CompareOrdinal(referenceNode.SimpleName, this.SimpleName))
                    {
                        existingNode = referenceNode;
                        return true;
                    }
                }
            }

            existingNode = null;
            return false;
        }
        /// <summary>
        /// Adds a reference to this container using the selector data structure to identify it.
        /// </summary>
        /// <param name="selectorData">data describing selected component</param>
        /// <returns>Reference in case of a valid reference node has been created or already existed. Otherwise null</returns>
        public ReferenceNode AddReferenceFromSelectorData(VSCOMPONENTSELECTORDATA selectorData)
        {
            //Make sure we can edit the project file
            if (!this.ProjectMgr.QueryEditProjectFile(false))
            {
                throw Marshal.GetExceptionForHR(VSConstants.OLE_E_PROMPTSAVECANCELLED);
            }

            //Create the reference node
            ReferenceNode node = null;

            try
            {
                node = CreateReferenceNode(selectorData);
            }
            catch (ArgumentException)
            {
                // Some selector data was not valid.
            }

            //Add the reference node to the project if we have a valid reference node
            if (node != null)
            {
                ReferenceNode existingNode;
                if (!node.IsAlreadyAdded(out existingNode))
                {
                    // Try to add
                    node.AddReference();
                    if (null == node.Parent)
                    {
                        // The reference was not added, so we can not return this item because it
                        // is not inside the project.
                        return(null);
                    }
                }
                else
                {
                    IVsDetermineWizardTrust wizardTrust = this.GetService(typeof(SVsDetermineWizardTrust)) as IVsDetermineWizardTrust;
                    var isWizardRunning = 0;
                    if (wizardTrust != null)
                    {
                        ErrorHandler.ThrowOnFailure(wizardTrust.IsWizardRunning(out isWizardRunning));
                    }

                    // if assembly already exists in project and we are running under the wizard - do not raise error
                    if (isWizardRunning != 0)
                    {
                        return(existingNode);
                    }
                    else
                    {
                        string message = string.IsNullOrEmpty(node.SimpleName)
                            ? String.Format(CultureInfo.CurrentCulture, SR.GetString(SR.ReferenceAlreadyExists, CultureInfo.CurrentUICulture), node.Caption, existingNode.Caption)
                            : String.Format(CultureInfo.CurrentCulture, SR.GetString(SR.ReferenceWithAssemblyNameAlreadyExists, CultureInfo.CurrentUICulture), node.Caption, node.SimpleName, existingNode.Caption);
                        throw new InvalidOperationException(message);
                    }
                }
            }

            return(node);
        }
        /// <summary>
        /// Adds references to this container from a MSBuild project.
        /// </summary>
        public void LoadReferencesFromBuildProject(Microsoft.Build.Evaluation.Project buildProject)
        {
            Build.Execution.ProjectInstance projectInstanceToSearchExpandedReferences = null;
            // if project uses implicitly expanded list of references,
            // evaluate ImplicitlyExpandTargetFramework target and collect all resolved 'ReferencePath' items
            // later we'll group them and create special reference nodes that represent assembly groupings
            if (ProjectMgr.ImplicitlyExpandTargetFramework)
            {
                var res = ProjectMgr.Build(MsBuildTarget.ImplicitlyExpandTargetFramework);
                if (res.IsSuccessful)
                {
                    projectInstanceToSearchExpandedReferences = res.ProjectInstance;
                }
            }

            // collect groupled framework references
            if (projectInstanceToSearchExpandedReferences != null)
            {
                // fetch all 'ReferencePath' items that were resolved from implicitly expanded references (metadata#ResolvedFrom = ImplicitlyExpandTargetFramework)
                var groupings =
                    projectInstanceToSearchExpandedReferences
                    .GetItems(ProjectFileConstants.ReferencePath)
                    .Where(item => string.Equals(item.GetMetadataValue(ProjectFileConstants.ResolvedFrom), MsBuildTarget.ImplicitlyExpandTargetFramework, StringComparison.OrdinalIgnoreCase))
                    .Select(
                        item =>
                        new
                {
                    referenceGroupingDisplayName = item.GetMetadataValue("ReferenceGroupingDisplayName"),
                    referenceGrouping            = item.GetMetadataValue("ReferenceGrouping"),
                    file = item.EvaluatedInclude
                }
                        )
                    .Where(r =>
                           !string.IsNullOrEmpty(r.referenceGrouping) &&
                           !string.IsNullOrEmpty(r.referenceGroupingDisplayName) &&
                           !string.IsNullOrEmpty(r.file) &&
                           File.Exists(r.file)
                           )
                    .GroupBy(r => r.referenceGrouping);

                foreach (var grouping in groupings)
                {
                    var version = ParseVersionFromReferenceGrouping(grouping.Key);
                    if (version == null)
                    {
                        continue;
                    }
                    // pick property values from the first item - they should be the same for all elements in the grouping
                    var first        = grouping.First();
                    var groupedFiles = grouping.Select(x => x.file).ToArray();

                    var versonText = string.Format(
                        "{0}.{1}.{2}.{3}",
                        version.Major,
                        version.Minor,
                        version.Build != -1 ? version.Build : 0,
                        version.Revision != -1 ? version.Revision : 0
                        );

                    var node = new GroupingReferenceNode(ProjectMgr, first.referenceGroupingDisplayName, first.referenceGrouping, Path.GetDirectoryName(first.file), versonText, groupedFiles);
                    AddChild(node);
                }
            }

            BuildResult buildResult = this.ProjectMgr.Build(MsBuildTarget.ResolveAssemblyReferences);

            foreach (string referenceType in SupportedReferenceTypes)
            {
                bool isAssemblyReference = referenceType == ProjectFileConstants.Reference;
                if (isAssemblyReference && !buildResult.IsSuccessful)
                {
                    continue;
                }

                foreach (var item in MSBuildProject.GetItems(buildProject, referenceType))
                {
                    ProjectElement element = new ProjectElement(this.ProjectMgr, item, false);

                    ReferenceNode node = CreateReferenceNode(referenceType, element, buildResult);
                    if (node != null)
                    {
                        this.AddChild(node);
                    }
                    if (isAssemblyReference)
                    {
                        ProjectMgr.UpdateValueOfCanUseTargetFSharpCoreReferencePropertyIfNecessary(node);
                    }
                }
            }
        }
Пример #8
0
        /// <summary>
        /// Checks if a reference is already added. The method parses all references and compares the the FinalItemSpec and the Guid.
        /// </summary>
        /// <returns>true if the assembly has already been added.</returns>
        /*protected, but public for FSharp.Project.dll*/
        public override bool IsAlreadyAdded(out ReferenceNode existingNode)
        {
            ReferenceContainerNode referencesFolder = this.ProjectMgr.FindChild(ReferenceContainerNode.ReferencesNodeVirtualName) as ReferenceContainerNode;
            Debug.Assert(referencesFolder != null, "Could not find the References node");

            for (HierarchyNode n = referencesFolder.FirstChild; n != null; n = n.NextSibling)
            {
                if (n is ComReferenceNode)
                {
                    ComReferenceNode referenceNode = n as ComReferenceNode;

                    // We check if the name and guids are the same
                    if (referenceNode.TypeGuid == this.TypeGuid && String.Compare(referenceNode.Caption, this.Caption, StringComparison.OrdinalIgnoreCase) == 0)
                    {
                        existingNode = referenceNode;
                        return true;
                    }
                }
            }

            existingNode = null;
            return false;
        }