public void UpdateMetadataImported()
        {
            Assert.Throws <InvalidOperationException>(() =>
            {
                string file = null;

                try
                {
                    file = Microsoft.Build.Shared.FileUtilities.GetTemporaryFile();
                    ProjectRootElement import = ProjectRootElement.Create(file);
                    import.AddItemDefinitionGroup().AddItemDefinition("i").AddMetadata("m", "m0");
                    import.Save();

                    ProjectRootElement main = ProjectRootElement.Create();
                    Project project         = new Project(main);
                    main.AddImport(file);
                    project.ReevaluateIfNecessary();

                    ProjectItemDefinition definition = project.ItemDefinitions["i"];
                    definition.SetMetadataValue("m", "m1");
                }
                finally
                {
                    File.Delete(file);
                }
            }
                                                      );
        }
Esempio n. 2
0
        // ==========================================================================================

        /// <summary>
        /// Extract input and output properties from MSBuild.
        /// </summary>
        private void GatherNativeInputsAndOutputs()
        {
            m_extension = this.GetPropertyValue("TargetExt");
            //Debug.Assert(String.IsNullOrEmpty(m_extension) == false);
            m_output_binary = (this.GetPropertyValue("TargetName") + m_extension).ToLower();
            //Debug.Assert(String.IsNullOrEmpty(m_extension) == false);
            m_output_binary_path = this.GetPropertyValue("TargetPath");

            ProjectItemDefinition LinkDef = null;

            if (String.Compare(".lib", m_extension, true) == 0)
            {
                LinkDef = this.ItemDefinitions["Lib"];
            }
            else
            {
                LinkDef = this.ItemDefinitions["Link"];
            }

            //Debug.Assert(LinkDef != null);
            if (LinkDef != null)
            {
                String importLibPath = LinkDef.GetMetadataValue("ImportLibrary");
                //Debug.Assert(importLibPath != null);
                if (String.IsNullOrEmpty(importLibPath) == false)
                {
                    m_Import_Library_FullPath = importLibPath.ToLower();
                }
                m_import_library = Path.GetFileName(importLibPath).ToLower();
                if (String.Compare(".lib", m_extension, true) == 0)
                {
                    m_import_library = m_output_binary;
                }

                String additionalDependencies = LinkDef.GetMetadataValue("AdditionalDependencies");
                //Debug.Assert(additionalDependencies != null);
                ResolveInputLibraries(additionalDependencies);

                String PDB = LinkDef.GetMetadataValue("ProgramDatabaseFile");
                //Debug.Assert(PDB != null);
                m_pdb_filename = Path.GetFileName(PDB).ToLower();
            }

            ProjectItemDefinition MidlDef = this.ItemDefinitions["Midl"];

            if (MidlDef != null)
            {
                ProjectMetadata metaTypeLibraryName = MidlDef.GetMetadata("TypeLibraryName");
                if (metaTypeLibraryName.IsImported == false)
                {
                    String typeLibrary = metaTypeLibraryName.EvaluatedValue;
                    String tname       = Path.GetFileName(this.ExpandString(typeLibrary)).ToLower();
                    m_typelibrary = tname;
                }
            }
        }
        /// <summary>
        /// Called when a ProjectInstance is created.
        /// </summary>
        /// <remarks>
        /// Assumes that the itemType string originated in a ProjectItemDefinitionElement and therefore
        /// was already validated.
        /// </remarks>
        internal ProjectItemDefinitionInstance(ProjectInstance projectInstance, ProjectItemDefinition itemDefinition)
            : this(projectInstance, itemDefinition.ItemType)
        {
            if (itemDefinition.MetadataCount > 0)
            {
                _metadata = new CopyOnWritePropertyDictionary <ProjectMetadataInstance>(itemDefinition.MetadataCount);
            }

            foreach (ProjectMetadata originalMetadata in itemDefinition.Metadata)
            {
                _metadata.Set(new ProjectMetadataInstance(originalMetadata));
            }
        }
        public void EmptyMetadataCollection()
        {
            ProjectRootElement xml = ProjectRootElement.Create();

            xml.AddItemDefinitionGroup().AddItemDefinition("i");
            Project project = new Project(xml);

            ProjectItemDefinition         itemDefinition     = project.ItemDefinitions["i"];
            IEnumerable <ProjectMetadata> metadataCollection = itemDefinition.Metadata;

            List <ProjectMetadata> metadataList = Helpers.MakeList(metadataCollection);

            Assert.Equal(0, metadataList.Count);

            Assert.Equal(null, itemDefinition.GetMetadata("m"));
        }
        public static String GetEvaluatedMetadata(Microsoft.Build.Evaluation.Project project, String type, String name)
        {
            ProjectItemDefinition item     = null;
            ProjectMetadata       metadata = null;

            if (project.ItemDefinitions.TryGetValue("IceBuilder", out item))
            {
                metadata = item.Metadata.FirstOrDefault(m => m.Name.Equals(name));
            }
            else
            {
                metadata =
                    project.AllEvaluatedItemDefinitionMetadata.FirstOrDefault(
                        m => m.ItemType.Equals("IceBuilder") && m.Name.Equals(name));
            }
            return(metadata == null ? String.Empty : metadata.EvaluatedValue);
        }
        public void SetMetadataImported()
        {
            string file = null;

            try
            {
                file = Microsoft.Build.Shared.FileUtilities.GetTemporaryFile();
                ProjectRootElement import = ProjectRootElement.Create(file);
                import.AddItemDefinitionGroup().AddItemDefinition("i").AddMetadata("m", "m0");
                import.Save();

                ProjectRootElement main    = ProjectRootElement.Create();
                Project            project = new Project(main);
                main.AddImport(file);
                project.ReevaluateIfNecessary();

                ProjectItemDefinition definition = project.ItemDefinitions["i"];
                definition.SetMetadataValue("n", "n0");

                string expected = String.Format
                                  (
                    ObjectModelHelpers.CleanupFileContents(
                        @"<Project ToolsVersion=""msbuilddefaulttoolsversion"" xmlns=""msbuildnamespace"">
  <ItemDefinitionGroup>
    <i>
      <n>n0</n>
    </i>
  </ItemDefinitionGroup>
  <Import Project=""{0}"" />
</Project>"),
                    file
                                  );

                Helpers.VerifyAssertProjectContent(expected, project.Xml);
            }
            finally
            {
                File.Delete(file);
            }
        }
        // public static void Verify(ProjectItemDefinition view, ProjectItemDefinition real) => Verify(view, real, null);
        public static void Verify(ProjectItemDefinition view, ProjectItemDefinition real, ValidationContext context)
        {
            if (view == null && real == null)
            {
                return;
            }
            VerifyLinkedNotNull(view);
            VerifyNotLinkedNotNull(real);

            // note ItemDefinition does not have a XML element
            // this is since it is [or can be] a aggregation of multiple ProjectItemDefinitionElement's.
            // This is somewhat of deficiency of MSBuild API.
            // (for example SetMetadata will always create a new ItemDefinitionElement because of that, for new metadata).

            Assert.Equal(real.ItemType, view.ItemType);
            Assert.Equal(real.MetadataCount, view.MetadataCount);

            Verify(view.Metadata, real.Metadata, Verify, context);

            foreach (var rm in real.Metadata)
            {
                var rv = real.GetMetadataValue(rm.Name);
                var vv = view.GetMetadataValue(rm.Name);
                Assert.Equal(rv, vv);

                var grm = real.GetMetadata(rm.Name);
                var gvm = view.GetMetadata(rm.Name);

                Verify(gvm, grm, context);
            }

            VerifyLinkedNotNull(view.Project);
            VerifyNotLinkedNotNull(real.Project);
            if (context?.Pair != null)
            {
                Assert.Same(context?.Pair.View, view.Project);
                Assert.Same(context?.Pair.Real, real.Project);
            }
        }
Esempio n. 8
0
        public ProjectItem(ProjectItemDefinition projectItemDefintion)
        {
            _commandService = IoC.Get <ICommandService>();

            ProjectItemDefintion = projectItemDefintion;
        }
        public void MigrateProjectReferences()
        {
            List <ProjectItem> allProjReferences = new List <ProjectItem>();

            foreach (ProjectItem i in project_.GetItemsIgnoringCondition("ProjectReference"))
            {
                allProjReferences.Add(i);
                string refProj = i.GetMetadataValue("FullPath");
                if (string.IsNullOrWhiteSpace(refProj) || !File.Exists(refProj))
                {
                    logger_.LogWarning($"File not found for project reference {i.UnevaluatedInclude}");
                    continue;
                }

                // Resolve project to package alias
                string    alias       = Path.GetFileNameWithoutExtension(refProj);
                ITaskItem refProjItem = allProjects_.FirstOrDefault((ii) => refProj.Equals(ii.GetMetadata("FullPath")));
                if (refProjItem != null)
                {
                    alias = PackagesConfig.GetProjectId(refProjItem);
                }

                logger_.LogMessage($"Project {projectItem_.ItemSpec}- Converting library reference '{refProj}' to Nuget package '{PackageIdPrefix + alias}' version {packageVersion_}");
                packagesConfig_.Add(PackageIdPrefix + alias, packageVersion_);
            }

            foreach (ProjectItem i in project_.GetItemsIgnoringCondition("Link"))
            {
                string   libDependencies = i.GetMetadataValue("AdditionalDependencies");
                string[] libs            = libDependencies.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
                foreach (string l in libs)
                {
                    string li = Path.GetFileNameWithoutExtension(l);
                    ConvertLibToPkg(li);
                }
            }

            foreach (ProjectMetadata i in project_.AllEvaluatedItemDefinitionMetadata)
            {
                if (i.ItemType.Equals("Link") && i.Name.Equals("AdditionalDependencies"))
                {
                    string   libDependencies = i.EvaluatedValue;
                    string[] libs            = libDependencies.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
                    foreach (string l in libs)
                    {
                        string li = Path.GetFileNameWithoutExtension(l);
                        ConvertLibToPkg(li);
                    }
                }
            }

            ProjectItemDefinition pid = project_.ItemDefinitions["Link"];

            if (pid != null)
            {
                string   libDependencies = pid.GetMetadataValue("AdditionalDependencies");
                string[] libs            = libDependencies.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
                foreach (string l in libs)
                {
                    string li = Path.GetFileNameWithoutExtension(l);
                    ConvertLibToPkg(li);
                }
            }

            project_.RemoveItems(allProjReferences);
            packagesConfig_.Save();
            project_.Save();
        }