public string GetEvaluatedMetadata(string attributeName)
        {
            if (this.IsVirtual)
            {
                // For virtual items, use our virtual property collection
                if (!virtualProperties.ContainsKey(attributeName))
                {
                    return(String.Empty);
                }
                return(virtualProperties[attributeName]);
            }

            // cannot ask MSBuild for Include, so intercept it and return the corresponding property
            if (String.Compare(attributeName, ProjectFileConstants.Include, StringComparison.OrdinalIgnoreCase) == 0)
            {
                return(item.EvaluatedInclude);
            }

            // Build Action is the type, not a property, so intercept this one as well
            if (String.Compare(attributeName, ProjectFileConstants.BuildAction, StringComparison.OrdinalIgnoreCase) == 0)
            {
                return(item.ItemType);
            }

            return(item.GetMetadataValue(attributeName));
        }
        /// <summary>
        /// Helper for metadata escaping tests
        /// </summary>
        private void SpecialCharactersInMetadataValueTests(Microsoft.Build.Evaluation.ProjectItem item)
        {
            Assert.AreEqual("%3B", item.GetMetadata("EscapedSemicolon").UnevaluatedValue);
            Assert.AreEqual(";", item.GetMetadata("EscapedSemicolon").EvaluatedValue);
            Assert.AreEqual(";", item.GetMetadataValue("EscapedSemicolon"));

            Assert.AreEqual("%24", item.GetMetadata("EscapedDollarSign").UnevaluatedValue);
            Assert.AreEqual("$", item.GetMetadata("EscapedDollarSign").EvaluatedValue);
            Assert.AreEqual("$", item.GetMetadataValue("EscapedDollarSign"));
        }
Example #3
0
        /// <summary>
        /// Set an attribute on the project element
        /// </summary>
        /// <param name="attributeName">Name of the attribute to set</param>
        /// <param name="attributeValue">Value to give to the attribute.  Use <c>null</c> to delete the metadata definition.</param>
        public void SetMetadata(string attributeName, string attributeValue)
        {
            Debug.Assert(String.Compare(attributeName, ProjectFileConstants.Include, StringComparison.OrdinalIgnoreCase) != 0, "Use rename as this won't work");

            if (this.IsVirtual)
            {
                // For virtual node, use our virtual property collection
                if (virtualProperties.ContainsKey(attributeName))
                {
                    virtualProperties.Remove(attributeName);
                }
                virtualProperties.Add(attributeName, attributeValue);
                return;
            }

            // Build Action is the type, not a property, so intercept
            if (String.Equals(attributeName, ProjectFileConstants.BuildAction, StringComparison.OrdinalIgnoreCase))
            {
                item.ItemType = attributeValue;
                return;
            }

            String currentValue = item.GetMetadataValue(attributeName);
            bool   changed;

            if (String.IsNullOrEmpty(currentValue) && String.IsNullOrEmpty(attributeValue))
            {
                changed = false;
            }
            else
            {
                changed = String.Compare(currentValue, attributeValue) != 0;
            }
            if (changed)
            {
                // Check out the project file.
                if (!this.itemProject.QueryEditProjectFile(false))
                {
                    throw Marshal.GetExceptionForHR(VSConstants.OLE_E_PROMPTSAVECANCELLED);
                }

                if (attributeValue == null)
                {
                    item.RemoveMetadata(attributeName);
                }
                else
                {
                    item.SetMetadataValue(attributeName, attributeValue);
                }
                itemProject.SetProjectFileDirty(true);
            }
        }
        public static NuGetReference AsNuGetReference(this MBuild.ProjectItem item)
        {
            var packageName    = item.EvaluatedInclude;
            var packageVersion = item.GetMetadataValue(MSBuildConstants.VersionElementName) ?? "0.0.0"; // Package references without versions will resolve to the lowest stable version

            return(new NuGetReference(packageName, packageVersion));
        }
 private ProjectReferenceAdapter GetProjectReferenceAdapter(ProjectItem r, bool conditionTrue)
 {
     var projectGuid = r.GetMetadataValue("Project");
     var csprojRelativePath = r.EvaluatedInclude;
     var absoluteProjectPath = Path.Combine(ProjectDirectory.FullName, csprojRelativePath);
     var referencedProjectAdapter = _projectLoader.GetProject(Guid.Parse(projectGuid), absoluteProjectPath);
     return new ProjectReferenceAdapter(referencedProjectAdapter, () => _project.RemoveItem(r), AddBinaryReferenceIfNotExists, conditionTrue);
 }
Example #6
0
        public void how_to_convert_DTE_ProjectItem_to_MSBuild_ProjectItem()
        {
            // Say you got a DTE project item somehow.
            EnvDTE.ProjectItem dteItem = this.DteLibrary.ProjectItems.OfType <EnvDTE.ProjectItem>().First(pi => pi.Name == "Class1.cs");

            Microsoft.Build.Evaluation.ProjectItem item = dteItem.Adapt().AsMsBuildItem();

            Assert.IsNotNull(item);

            // Now use MSBuild to set/get custom metadata on the item
            item.SetMetadataValue("Identifier", "Foo");
            string identifier = item.GetMetadataValue("Identifier");

            Assert.AreEqual("Foo", identifier);
        }
Example #7
0
        /// <summary>
        /// Get the value of an attribute on a project element
        /// </summary>
        /// <param name="attributeName">Name of the attribute to get the value for</param>
        /// <returns>Value of the attribute</returns>
        public override string GetMetadata(string attributeName)
        {
            // cannot ask MSBuild for Include, so intercept it and return the corresponding property
            if (String.Compare(attributeName, ProjectFileConstants.Include, StringComparison.OrdinalIgnoreCase) == 0)
            {
                return(_item.EvaluatedInclude);
            }

            // Build Action is the type, not a property, so intercept this one as well
            if (String.Compare(attributeName, ProjectFileConstants.BuildAction, StringComparison.OrdinalIgnoreCase) == 0)
            {
                return(_item.ItemType);
            }

            return(_item.GetMetadataValue(attributeName));
        }
Example #8
0
 private string GetTargetPath(ProjectItem item)
 {
     string path = item.UnevaluatedInclude;
     if (item.HasMetadata("Link"))
     {
         path = item.GetMetadataValue("Link");
     }
     return Normalize(path);
 }
        /// <summary>
        /// Helper for SpecialCharactersInMetadataValue tests
        /// </summary>
        internal static void SpecialCharactersInMetadataValueTests(ProjectItem item)
        {
            Assert.Equal("%3B", item.GetMetadata("EscapedSemicolon").UnevaluatedValue);
            Assert.Equal("%3B", item.GetMetadata("EscapedSemicolon").EvaluatedValueEscaped);
            Assert.Equal(";", item.GetMetadata("EscapedSemicolon").EvaluatedValue);
            Assert.Equal("%3B", Project.GetMetadataValueEscaped(item, "EscapedSemicolon"));
            Assert.Equal(";", item.GetMetadataValue("EscapedSemicolon"));

            Assert.Equal("%24", item.GetMetadata("EscapedDollarSign").UnevaluatedValue);
            Assert.Equal("%24", item.GetMetadata("EscapedDollarSign").EvaluatedValueEscaped);
            Assert.Equal("$", item.GetMetadata("EscapedDollarSign").EvaluatedValue);
            Assert.Equal("%24", Project.GetMetadataValueEscaped(item, "EscapedDollarSign"));
            Assert.Equal("$", item.GetMetadataValue("EscapedDollarSign"));
        }
 public static string GetMetadataValue(MSBuild.ProjectItem item, string name)
 {
     return(item.GetMetadataValue(name));
 }
Example #11
0
 public AssemblyReference(ProjectItem item)
 {
     _name = item.EvaluatedInclude;
     _hintPath = item.GetMetadataValue("HintPath");
 }
Example #12
0
        private bool ResolveProjectReferenceItemByAssemblyName(ProjectItem reference, string mapping)
        {
            if (reference.HasMetadata("HintPath"))
            {
                var hintpath = reference.GetMetadataValue("HintPath");
                var fileInfo = new FileInfo(hintpath);
                return fileInfo.Name.Equals(mapping, StringComparison.OrdinalIgnoreCase);
            }

            return false;
        }
 public string GetEvaluatedMetadata(string name)
 {
     return(item.GetMetadataValue(name));
 }
Example #14
0
 public static string GetMetadataValue(Microsoft.Build.Evaluation.ProjectItem item, string name)
 {
     return(item.GetMetadataValue(name));
 }
Example #15
0
        Asset ToAsset(FileInfo file, ProjectItem item)
        {
            var buildAction = (AssetBuildActionKind) Enum.Parse(typeof(AssetBuildActionKind), item.ItemType);
            var name = item.GetMetadataValue("Name");
            var importer = item.GetMetadataValue("Importer");
            var processor = item.GetMetadataValue("Processor");
            if (processor == "PassThroughProcessor") processor = null;

            return new Asset(this, file, buildAction, name, importer, processor);
        }
Example #16
0
 public ProjectDescription(Microsoft.Build.Evaluation.ProjectItem x, string dllPath)
 {
     Name     = x.EvaluatedInclude;
     FileName = Name.Split(',').First();
     Path     = System.IO.Path.Combine(dllPath, x.GetMetadataValue("HintPath"));
 }