Esempio n. 1
0
        protected override void ResolveAssemblyReference()
        {
            if (this.ProjectMgr == null || this.ProjectMgr.IsClosed)
            {
                return;
            }

            var instance = this.ProjectMgr.ProjectInstance;
            // do not call MsBuild again that will slow down things a lot.
            var group = MSBuildProjectInstance.GetItems(instance, ProjectFileConstants.ReferencePath);

            if (group != null)
            {
                foreach (var item in group)
                {
                    string fullPath = item.GetMetadataValue("fullpath");
                    if (!File.Exists(fullPath))
                    {
                        continue;
                    }
                    AssemblyName name = AssemblyName.GetAssemblyName(fullPath);

                    // Try with full assembly name and then with weak assembly name.
                    if (String.Compare(name.FullName, this.AssemblyName.FullName, StringComparison.OrdinalIgnoreCase) == 0 ||
                        String.Compare(name.Name, this.AssemblyName.Name, StringComparison.OrdinalIgnoreCase) == 0)
                    {
                        if (!NativeMethods.IsSamePath(fullPath, this.AssemblyPath))
                        {
                            // set the full path now.
                            this.AssemblyPath = fullPath;

                            // We have a new item to listen too, since the assembly reference is resolved from a different place.
                            base.ObserveItem(this.AssemblyPath);
                        }
                        this.ResolvedAssembly = name;
                        // No hint path is needed since the assembly path will always be resolved.
                        // cache the propertynames and the item.
                        resolvedProperties = new HashSet <string>(StringComparer.OrdinalIgnoreCase);
                        foreach (var str in item.MetadataNames)
                        {
                            resolvedProperties.Add(str);
                        }
                        prjitem = item;

                        return;
                    }
                }
                // when we get here then the assembly was not resolved by MsBuild. Maybe the reference was not persisted yet ?
                var xnode = ProjectMgr as XSharpProjectNode;
                if (xnode != null && !xnode.IsLoading)
                {
                    base.ResolveAssemblyReference();
                }

                return;
            }
        }
        /// <summary>
        /// Does the actual job of resolving an assembly reference. We need a private method that does not violate
        /// calling virtual method from the constructor.
        /// </summary>
        internal virtual void ResolveAssemblyReference()
        {
            if (this.ProjectMgr == null || this.ProjectMgr.IsClosed)
            {
                return;
            }

            var instance = this.ProjectMgr.ProjectInstance;
            // must call MsBuild again
            var group = MSBuildProjectInstance.GetItems(instance, ProjectFileConstants.ReferencePath).ToArray();

            // RvdH Only call ResolveAsemblyReferences when we cannot find any items
            if (group == null || group.Length == 0)
            {
                BuildInstance(this.ProjectMgr, instance, MsBuildTarget.ResolveAssemblyReferences);
                group = MSBuildProjectInstance.GetItems(instance, ProjectFileConstants.ReferencePath).ToArray();
            }
            if (group != null)
            {
                foreach (var item in group)
                {
                    string fullPath = item.GetMetadataValue("fullpath");
                    if (!File.Exists(fullPath))
                    {
                        continue;
                    }
                    AssemblyName name = AssemblyName.GetAssemblyName(fullPath);

                    // Try with full assembly name and then with weak assembly name.
                    if (String.Compare(name.FullName, this.assemblyName.FullName, StringComparison.OrdinalIgnoreCase) == 0 ||
                        String.Compare(name.Name, this.assemblyName.Name, StringComparison.OrdinalIgnoreCase) == 0)
                    {
                        if (!NativeMethods.IsSamePath(fullPath, this.assemblyPath))
                        {
                            // set the full path now.
                            this.assemblyPath = fullPath;

                            // We have a new item to listen too, since the assembly reference is resolved from a different place.
                            this.fileChangeListener.ObserveItem(this.assemblyPath);
                        }
                        this.resolvedAssemblyName = name;
                        // No hint path is needed since the assembly path will always be resolved.

                        // cache the propertynames and the item.
                        resolvedProperties = new HashSet <string>(StringComparer.OrdinalIgnoreCase);
                        foreach (var str in item.MetadataNames)
                        {
                            resolvedProperties.Add(str);
                        }
                        prjitem = item;
                        return;
                    }
                }
                // when we get here then the assembly was not resolved by MsBuild. Maybe the reference was not persisted yet ?
                return;
            }
        }
 public void SetInvalidEmptyMetadataName()
 {
     Assert.Throws <ArgumentException>(() =>
     {
         ProjectItemInstance item = GetItemInstance();
         item.SetMetadata(String.Empty, "m1");
     }
                                       );
 }
Esempio n. 4
0
        /// <summary>
        /// Get the file location as seen in the IDE, used for ResourceDictionary.Source resolution.
        /// </summary>
        private string GetSourceLink(ProjectItemInstance projectItemInstance)
        {
            if (projectItemInstance.HasMetadata("Link"))
            {
                return(projectItemInstance.GetMetadataValue("Link"));
            }

            return(projectItemInstance.EvaluatedInclude);
        }
Esempio n. 5
0
        /// <summary>
        ///     Returns a value indicating if the specified <see cref="ProjectItemInstance"/>
        ///     originated in an imported file.
        /// </summary>
        /// <returns>
        ///     <see langword="true"/> if <paramref name="item"/> originated in an imported file;
        ///     otherwise, <see langword="false"/> if it was defined in the project being built.
        /// </returns>
        /// <exception cref="ArgumentNullException">
        ///     <paramref name="item"/> is <see langword="null"/>.
        /// </exception>
        public static bool IsImported(this ProjectItemInstance item)
        {
            Requires.NotNull(item, nameof(item));

            string definingProjectFullPath = item.GetMetadataValue("DefiningProjectFullPath");
            string projectFullPath         = item.Project.FullPath; // NOTE: This returns project being built, not owning target

            return(!StringComparers.Paths.Equals(definingProjectFullPath, projectFullPath));
        }
 public void SetInvalidNullMetadataName()
 {
     Assert.Throws <ArgumentNullException>(() =>
     {
         ProjectItemInstance item = GetItemInstance();
         item.SetMetadata(null, "m1");
     }
                                           );
 }
 public void SetInvalidEmptyInclude()
 {
     Assert.Throws <ArgumentException>(() =>
     {
         ProjectItemInstance item = GetItemInstance();
         item.EvaluatedInclude    = String.Empty;
     }
                                       );
 }
 public void SetInvalidNullInclude()
 {
     Assert.Throws <ArgumentNullException>(() =>
     {
         ProjectItemInstance item = GetItemInstance();
         item.EvaluatedInclude    = null;
     }
                                           );
 }
Esempio n. 9
0
        /// <summary>
        /// Get a single metadata instance
        /// </summary>
        private static ProjectMetadataInstance GetMetadataInstance()
        {
            Project                 project         = new Project();
            ProjectInstance         projectInstance = project.CreateProjectInstance();
            ProjectItemInstance     item            = projectInstance.AddItem("i", "i1");
            ProjectMetadataInstance metadata        = item.SetMetadata("m", "m1");

            return(metadata);
        }
        public void CastToITaskItemNoMetadata()
        {
            ProjectItemInstance item = GetItemInstance();

            ITaskItem taskItem = (ITaskItem)item;

            Assert.Equal(0 + BuiltInMetadataCount, taskItem.MetadataCount);
            Assert.Equal(0 + BuiltInMetadataCount, taskItem.MetadataNames.Count);
            Assert.Equal(String.Empty, taskItem.GetMetadata("m"));
        }
Esempio n. 11
0
        public static string SafeGetMetadataValue(this ProjectItemInstance item, string name)
        {
            string result = null;

            try {
                result = item.GetMetadataValue(name);
            }
            catch (Exception) { }
            return(result);
        }
        private static string GetFullPath(string projectPath, ProjectItemInstance reference)
        {
            string otherProjectPath = reference.EvaluatedInclude;

            if (!Path.IsPathRooted(otherProjectPath))
            {
                otherProjectPath = Path.Combine(Path.GetDirectoryName(projectPath), otherProjectPath);
            }

            return(otherProjectPath);
        }
Esempio n. 13
0
        public static bool ShouldCopyToOutputDirectory(this ProjectItemInstance item)
        {
            var copyToOutputDirectoryValue = item.GetMetadataValue("CopyToOutputDirectory");

            if (copyToOutputDirectoryValue.Equals("Always", StringComparison.OrdinalIgnoreCase) ||
                copyToOutputDirectoryValue.Equals("PreserveNewest", StringComparison.OrdinalIgnoreCase))
            {
                return(true);
            }

            return(false);
        }
Esempio n. 14
0
        // Internal for testing
        internal static string[] GetConfiguredExtensionNames(ProjectItemInstance configurationItem)
        {
            var extensionNamesValue = configurationItem.GetMetadataValue(RazorConfigurationItemTypeExtensionsProperty);

            if (string.IsNullOrEmpty(extensionNamesValue))
            {
                return(Array.Empty <string>());
            }

            var configuredExtensionNames = extensionNamesValue.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries);

            return(configuredExtensionNames);
        }
        public void CreateItemWithNullMetadataValue()
        {
            Project         project         = new Project();
            ProjectInstance projectInstance = project.CreateProjectInstance();

            IDictionary <string, string> metadata = new Dictionary <string, string>();

            metadata.Add("m", null);

            ProjectItemInstance item = projectInstance.AddItem("i", "i1", metadata);

            Assert.Equal(String.Empty, item.GetMetadataValue("m"));
        }
        public void AccessorsWithMetadata()
        {
            ProjectItemInstance item = GetItemInstance();

            item.SetMetadata("m1", "v0");
            item.SetMetadata("m1", "v1");
            item.SetMetadata("m2", "v2");

            Assert.Equal("m1", item.GetMetadata("m1").Name);
            Assert.Equal("m2", item.GetMetadata("m2").Name);
            Assert.Equal("v1", item.GetMetadataValue("m1"));
            Assert.Equal("v2", item.GetMetadataValue("m2"));
        }
Esempio n. 17
0
        private static void AddEdgesFromSolution(IReadOnlyDictionary <ConfigurationMetadata, ParsedProject> allParsedProjects, IReadOnlyDictionary <string, IReadOnlyCollection <string> > solutionDependencies, GraphEdges edges)
        {
            var projectsByPath = new Dictionary <string, List <ProjectGraphNode> >();

            foreach (var project in allParsedProjects)
            {
                var projectPath = project.Value.GraphNode.ProjectInstance.FullPath;

                if (projectsByPath.ContainsKey(projectPath))
                {
                    projectsByPath[projectPath].Add(project.Value.GraphNode);
                }
                else
                {
                    projectsByPath[projectPath] = new List <ProjectGraphNode> {
                        project.Value.GraphNode
                    };
                }
            }

            foreach (var solutionDependency in solutionDependencies)
            {
                var referencingProjectPath = solutionDependency.Key;

                ErrorUtilities.VerifyThrow(projectsByPath.ContainsKey(referencingProjectPath), "nodes should include solution projects");

                var referencedNodes = solutionDependency.Value.SelectMany(
                    referencedProjectPath =>
                {
                    ErrorUtilities.VerifyThrow(projectsByPath.ContainsKey(referencedProjectPath), "nodes should include solution projects");

                    return(projectsByPath[referencedProjectPath]);
                }).ToArray();

                var referencingNodes = projectsByPath[referencingProjectPath];

                foreach (var referencingNode in referencingNodes)
                {
                    foreach (var referencedNode in referencedNodes)
                    {
                        var stubItem = new ProjectItemInstance(
                            referencingNode.ProjectInstance,
                            SolutionItemReference,
                            referencedNode.ProjectInstance.FullPath,
                            referencingNode.ProjectInstance.FullPath);

                        referencingNode.AddProjectReference(referencedNode, stubItem, edges);
                    }
                }
            }
        }
Esempio n. 18
0
        /// <summary>
        /// Constructor for IVSOutput2 implementation
        /// </summary>
        /// <param name="projectManager">Project that produce this output</param>
        /// <param name="outputAssembly">MSBuild generated item corresponding to the output assembly (by default, these would be of type MainAssembly</param>
        public Output(ProjectNode projectManager, ProjectItemInstance outputAssembly)
        {
            if (projectManager == null)
            {
                throw new ArgumentNullException("projectManager");
            }
            if (outputAssembly == null)
            {
                throw new ArgumentNullException("outputAssembly");
            }

            project = projectManager;
            output  = outputAssembly;
        }
        private static void AssertExpectedStyleCopSetting(string expectedValue, BuildResult actualResult)
        {
            Assert.IsNotNull(actualResult.ProjectStateAfterBuild, "Test error: ProjectStateAfterBuild should not be null");

            IEnumerable <ProjectItemInstance> matches = actualResult.ProjectStateAfterBuild.GetItemsByItemTypeAndEvaluatedInclude(BuildTaskConstants.SettingItemName, TargetConstants.StyleCopProjectPathItemName);

            Assert.AreNotEqual(0, matches.Count(), "Expected SonarQubeSetting with include value of '{0}' does not exist", TargetConstants.StyleCopProjectPathItemName);
            Assert.AreEqual(1, matches.Count(), "Only expecting one SonarQubeSetting with include value of '{0}' to exist", TargetConstants.StyleCopProjectPathItemName);

            ProjectItemInstance item = matches.Single();
            string value             = item.GetMetadataValue(BuildTaskConstants.SettingValueMetadataName);

            Assert.AreEqual(expectedValue, value, "SonarQubeSetting with include value '{0}' does not have the expected value");
        }
Esempio n. 20
0
        private bool IsValidItem(ProjectItemInstance outputItem)
        {
            Version version  = null;
            Version version1 = null;

            if (Version.TryParse(outputItem.GetMetadataValue("Version"), out version) && Version.TryParse(outputItem.GetMetadataValue("HighestVersionInRedist"), out version1) && version > version1)
            {
                return(false);
            }
            if (!string.IsNullOrEmpty(outputItem.GetMetadataValue("OutOfRangeDependencies")))
            {
                return(false);
            }
            return(true);
        }
Esempio n. 21
0
        public void ShouldCopyToOutputDirectory(string copyToOutputDirectoryValue, bool expectedResult)
        {
            ProjectRootElement projectRootElement = ProjectRootElement.Create();
            ProjectItemElement item = projectRootElement.AddItem("Foo", "Foo.xml");

            if (!string.IsNullOrEmpty(copyToOutputDirectoryValue))
            {
                item.AddMetadata("CopyToOutputDirectory", copyToOutputDirectoryValue);
            }

            ProjectInstance     projectInstance = TestHelpers.CreateProjectInstanceFromRootElement(projectRootElement);
            ProjectItemInstance itemInstance    = projectInstance.GetItems("Foo").Single();

            Assert.Equal(expectedResult, itemInstance.ShouldCopyToOutputDirectory());
        }
        public void CastToITaskItem()
        {
            ProjectItemInstance item = GetItemInstance();

            item.SetMetadata("m", "m1");

            ITaskItem taskItem = (ITaskItem)item;

            Assert.Equal(item.EvaluatedInclude, taskItem.ItemSpec);
            Assert.Equal(1 + BuiltInMetadataCount, taskItem.MetadataCount);
            Assert.Equal(1 + BuiltInMetadataCount, taskItem.MetadataNames.Count);
            Assert.Equal("m1", taskItem.GetMetadata("m"));
            taskItem.SetMetadata("m", "m2");
            Assert.Equal("m2", item.GetMetadataValue("m"));
        }
Esempio n. 23
0
        public void BuiltInMisqualifiedMetadataExpression()
        {
            string content = @"
                    <Project>
                        <ItemGroup>
                            <i Include='i1'>
                                <m>%(j.Identity)</m>
                            </i>
                        </ItemGroup>
                    </Project>
                ";

            ProjectItemInstance item = GetOneItem(content);

            Assert.Equal(String.Empty, item.GetMetadataValue("m"));
        }
Esempio n. 24
0
        public void AddItemWithoutMetadata()
        {
            ProjectInstance p = GetEmptyProjectInstance();

            ProjectItemInstance returned = p.AddItem("i", "i1");

            Assert.Equal("i", returned.ItemType);
            Assert.Equal("i1", returned.EvaluatedInclude);
            Assert.False(returned.Metadata.GetEnumerator().MoveNext());

            foreach (ProjectItemInstance item in p.Items)
            {
                Assert.Equal("i1", item.EvaluatedInclude);
                Assert.False(item.Metadata.GetEnumerator().MoveNext());
            }
        }
        public void BuiltInMisqualifiedMetadataExpression()
        {
            string content = @"
                    <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003' >
                        <ItemGroup>
                            <i Include='i1'>
                                <m>%(j.Identity)</m>
                            </i>
                        </ItemGroup>
                    </Project>
                ";

            ProjectItemInstance item = GetOneItem(content);

            Assert.Equal(String.Empty, item.GetMetadataValue("m"));
        }
Esempio n. 26
0
        public void GetTargetPath(string itemIdentity, string linkValue, string expectedResult)
        {
            ProjectRootElement projectRootElement = ProjectRootElement.Create();

            itemIdentity = itemIdentity.Replace("{ProjectDir}", projectRootElement.DirectoryPath, StringComparison.Ordinal);
            ProjectItemElement item = projectRootElement.AddItem("Foo", itemIdentity);

            if (!string.IsNullOrEmpty(linkValue))
            {
                item.AddMetadata("Link", linkValue);
            }

            ProjectInstance     projectInstance = TestHelpers.CreateProjectInstanceFromRootElement(projectRootElement);
            ProjectItemInstance itemInstance    = projectInstance.GetItems("Foo").Single();

            Assert.Equal(expectedResult, itemInstance.GetTargetPath());
        }
Esempio n. 27
0
        // Internal for testing
        internal static bool TryGetConfigurationItem(
            string configuration,
            IEnumerable <ProjectItemInstance> projectItems,
            out ProjectItemInstance configurationItem)
        {
            foreach (var item in projectItems)
            {
                if (item.ItemType == RazorConfigurationItemType && item.EvaluatedInclude == configuration)
                {
                    configurationItem = item;
                    return(true);
                }
            }

            configurationItem = null;
            return(false);
        }
Esempio n. 28
0
        private void ReportInputs(ProjectPredictionReporter reporter, ProjectItemInstance masmItem, HashSet <string> reportedIncludes)
        {
            reporter.ReportInputFile(masmItem.EvaluatedInclude);

            string[] includePaths = masmItem.GetMetadataValue(IncludePathsMetadata)
                                    .Split(IncludePathsSeparator, StringSplitOptions.RemoveEmptyEntries);

            // Avoid reporting paths that we've already reported for this project.
            foreach (string includePath in includePaths)
            {
                string trimmedPath = includePath.Trim();
                if (!string.IsNullOrEmpty(trimmedPath) && reportedIncludes.Add(trimmedPath))
                {
                    reporter.ReportInputDirectory(trimmedPath);
                }
            }
        }
        public void BuiltInMetadataInMetadataCondition()
        {
            string content = @"
                    <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003' >
                        <ItemGroup>
                            <i Include='i1'>
                                <m Condition=""'%(Identity)'=='i1'"">m1</m>
                                <n Condition=""'%(Identity)'=='i2'"">n1</n>
                            </i>
                        </ItemGroup>
                    </Project>
                ";

            ProjectItemInstance item = GetOneItem(content);

            Assert.Equal("m1", item.GetMetadataValue("m"));
            Assert.Equal(String.Empty, item.GetMetadataValue("n"));
        }
        public void NoMetadata()
        {
            string content = @"
                    <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003' >
                        <ItemGroup>
                            <i Include='i1'/>
                        </ItemGroup>
                    </Project>
                ";

            ProjectItemInstance item = GetOneItem(content);

            Assert.Equal("i", item.ItemType);
            Assert.Equal("i1", item.EvaluatedInclude);
            Assert.Equal(false, item.Metadata.GetEnumerator().MoveNext());
            Assert.Equal(0 + BuiltInMetadataCount, Helpers.MakeList(item.MetadataNames).Count);
            Assert.Equal(0 + BuiltInMetadataCount, item.MetadataCount);
        }