Ejemplo n.º 1
0
        /// <summary>
        /// Creates a set of complicated item metadata and properties, and items to exercise
        /// the Expander class.  The data here contains escaped characters, metadata that
        /// references properties, properties that reference items, and other complex scenarios.
        /// </summary>
        /// <param name="pg"></param>
        /// <param name="primaryItemsByName"></param>
        /// <param name="secondaryItemsByName"></param>
        /// <param name="itemMetadata"></param>
        private void CreateComplexPropertiesItemsMetadata
            (
            out ReadOnlyLookup readOnlyLookup,
            out StringMetadataTable itemMetadata
            )
        {
            ProjectInstance project = ProjectHelpers.CreateEmptyProjectInstance();
            Dictionary<string, string> itemMetadataTable = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
            itemMetadataTable["Culture"] = "abc%253bdef;$(Gee_Aych_Ayee)";
            itemMetadataTable["Language"] = "english";
            itemMetadata = new StringMetadataTable(itemMetadataTable);

            PropertyDictionary<ProjectPropertyInstance> pg = new PropertyDictionary<ProjectPropertyInstance>();
            pg.Set(ProjectPropertyInstance.Create("Gee_Aych_Ayee", "ghi"));
            pg.Set(ProjectPropertyInstance.Create("OutputPath", @"\jk ; l\mno%253bpqr\stu"));
            pg.Set(ProjectPropertyInstance.Create("TargetPath", "@(IntermediateAssembly->'%(RelativeDir)')"));

            List<ProjectItemInstance> intermediateAssemblyItemGroup = new List<ProjectItemInstance>();
            ProjectItemInstance i1 = new ProjectItemInstance(project, "IntermediateAssembly", @"subdir1\engine.dll", project.FullPath);
            intermediateAssemblyItemGroup.Add(i1);
            i1.SetMetadata("aaa", "111");
            ProjectItemInstance i2 = new ProjectItemInstance(project, "IntermediateAssembly", @"subdir2\tasks.dll", project.FullPath);
            intermediateAssemblyItemGroup.Add(i2);
            i2.SetMetadata("bbb", "222");

            List<ProjectItemInstance> contentItemGroup = new List<ProjectItemInstance>();
            ProjectItemInstance i3 = new ProjectItemInstance(project, "Content", "splash.bmp", project.FullPath);
            contentItemGroup.Add(i3);
            i3.SetMetadata("ccc", "333");

            List<ProjectItemInstance> resourceItemGroup = new List<ProjectItemInstance>();
            ProjectItemInstance i4 = new ProjectItemInstance(project, "Resource", "string$(p).resx", project.FullPath);
            resourceItemGroup.Add(i4);
            i4.SetMetadata("ddd", "444");
            ProjectItemInstance i5 = new ProjectItemInstance(project, "Resource", "dialogs%253b.resx", project.FullPath);
            resourceItemGroup.Add(i5);
            i5.SetMetadata("eee", "555");

            List<ProjectItemInstance> contentItemGroup2 = new List<ProjectItemInstance>();
            ProjectItemInstance i6 = new ProjectItemInstance(project, "Content", "about.bmp", project.FullPath);
            contentItemGroup2.Add(i6);
            i6.SetMetadata("fff", "666");

            ItemDictionary<ProjectItemInstance> secondaryItemsByName = new ItemDictionary<ProjectItemInstance>();
            secondaryItemsByName.ImportItems(resourceItemGroup);
            secondaryItemsByName.ImportItems(contentItemGroup2);

            Lookup lookup = new Lookup(secondaryItemsByName, pg, null);

            // Add primary items
            lookup.EnterScope("x");
            lookup.PopulateWithItems("IntermediateAssembly", intermediateAssemblyItemGroup);
            lookup.PopulateWithItems("Content", contentItemGroup);

            readOnlyLookup = new ReadOnlyLookup(lookup);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Creates an expander populated with some ProjectPropertyInstances and ProjectPropertyItems.
        /// </summary>
        /// <returns></returns>
        private Expander<ProjectPropertyInstance, ProjectItemInstance> CreateItemFunctionExpander()
        {
            ProjectInstance project = ProjectHelpers.CreateEmptyProjectInstance();
            PropertyDictionary<ProjectPropertyInstance> pg = new PropertyDictionary<ProjectPropertyInstance>();
            pg.Set(ProjectPropertyInstance.Create("p", "v0"));
            pg.Set(ProjectPropertyInstance.Create("p", "v1"));
            pg.Set(ProjectPropertyInstance.Create("Val", "2"));
            pg.Set(ProjectPropertyInstance.Create("a", "filename"));

            ItemDictionary<ProjectItemInstance> ig = new ItemDictionary<ProjectItemInstance>();

            for (int n = 0; n < 10; n++)
            {
                ProjectItemInstance pi = new ProjectItemInstance(project, "i", "i" + n.ToString(), project.FullPath);
                for (int m = 0; m < 5; m++)
                {
                    pi.SetMetadata("Meta" + m.ToString(), @"c:\firstdirectory\seconddirectory\file" + m.ToString() + ".ext");
                }
                pi.SetMetadata("Meta9", @"seconddirectory\file.ext");
                pi.SetMetadata("Meta10", @";someo%3bherplace\foo.txt;secondd%3brectory\file.ext;");
                pi.SetMetadata("MetaBlank", @"");

                if (n % 2 > 0)
                {
                    pi.SetMetadata("Even", "true");
                    pi.SetMetadata("Odd", "false");
                }
                else
                {
                    pi.SetMetadata("Even", "false");
                    pi.SetMetadata("Odd", "true");
                }
                ig.Add(pi);
            }

            Dictionary<string, string> itemMetadataTable = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
            itemMetadataTable["Culture"] = "abc%253bdef;$(Gee_Aych_Ayee)";
            itemMetadataTable["Language"] = "english";
            IMetadataTable itemMetadata = new StringMetadataTable(itemMetadataTable);

            Expander<ProjectPropertyInstance, ProjectItemInstance> expander = new Expander<ProjectPropertyInstance, ProjectItemInstance>(pg, ig, itemMetadata);

            return expander;
        }
Ejemplo n.º 3
0
        public void ModifyItemTwiceInSameScope2()
        {
            ProjectInstance project = ProjectHelpers.CreateEmptyProjectInstance();
            ItemDictionary<ProjectItemInstance> table1 = new ItemDictionary<ProjectItemInstance>();
            Lookup lookup = LookupHelpers.CreateLookup(table1);

            // Add an item with m=m1 and n=n1 and o=o1
            ProjectItemInstance item1 = new ProjectItemInstance(project, "i1", "a2", project.FullPath);
            item1.SetMetadata("m", "m1");
            item1.SetMetadata("n", "n1");
            item1.SetMetadata("o", "o1");
            lookup.PopulateWithItem(item1);

            Lookup.Scope enteredScope = lookup.EnterScope("x");

            // It's still m=m1, n=n1, o=o1
            ICollection<ProjectItemInstance> group = lookup.GetItems("i1");
            Assert.Equal(1, group.Count);
            Assert.Equal("m1", group.First().GetMetadataValue("m"));
            Assert.Equal("n1", group.First().GetMetadataValue("n"));
            Assert.Equal("o1", group.First().GetMetadataValue("o"));

            // Make a modification to the item to be m=m2 and n=n2
            Lookup.MetadataModifications newMetadata = new Lookup.MetadataModifications(keepOnlySpecified: false);
            newMetadata.Add("m", "m2");
            newMetadata.Add("n", "n2");
            group = new List<ProjectItemInstance>();
            group.Add(item1);
            lookup.ModifyItems("i1", group, newMetadata);

            // It's now m=m2, n=n2, o=o1
            ICollection<ProjectItemInstance> foundGroup = lookup.GetItems("i1");
            Assert.Equal(1, foundGroup.Count);
            Assert.Equal("m2", foundGroup.First().GetMetadataValue("m"));
            Assert.Equal("n2", foundGroup.First().GetMetadataValue("n"));
            Assert.Equal("o1", foundGroup.First().GetMetadataValue("o"));

            // Make a modification to the item to be n=n3 
            newMetadata = new Lookup.MetadataModifications(keepOnlySpecified: false);
            newMetadata.Add("n", "n3");
            lookup.ModifyItems("i1", group, newMetadata);

            // It's now m=m2, n=n3, o=o1
            foundGroup = lookup.GetItems("i1");
            Assert.Equal(1, foundGroup.Count);
            Assert.Equal("m2", foundGroup.First().GetMetadataValue("m"));
            Assert.Equal("n3", foundGroup.First().GetMetadataValue("n"));
            Assert.Equal("o1", foundGroup.First().GetMetadataValue("o"));

            // But the original item hasn't changed yet
            Assert.Equal("m1", item1.GetMetadataValue("m"));
            Assert.Equal("n1", item1.GetMetadataValue("n"));
            Assert.Equal("o1", item1.GetMetadataValue("o"));

            enteredScope.LeaveScope();

            // It's still m=m2, n=n3, o=o1
            foundGroup = lookup.GetItems("i1");
            Assert.Equal(1, foundGroup.Count);
            Assert.Equal("m2", foundGroup.First().GetMetadataValue("m"));
            Assert.Equal("n3", foundGroup.First().GetMetadataValue("n"));
            Assert.Equal("o1", foundGroup.First().GetMetadataValue("o"));

            // And the original item has changed
            Assert.Equal("m2", item1.GetMetadataValue("m"));
            Assert.Equal("n3", item1.GetMetadataValue("n"));
            Assert.Equal("o1", item1.GetMetadataValue("o"));
        }
Ejemplo n.º 4
0
        public void ModifyItemThatWasAddedInSameScope()
        {
            ProjectInstance project = ProjectHelpers.CreateEmptyProjectInstance();
            ItemDictionary<ProjectItemInstance> table1 = new ItemDictionary<ProjectItemInstance>();
            Lookup lookup = LookupHelpers.CreateLookup(table1);

            Lookup.Scope enteredScope = lookup.EnterScope("x");

            // Add an item with m=m1
            ProjectItemInstance item1 = new ProjectItemInstance(project, "i1", "a2", project.FullPath);
            item1.SetMetadata("m", "m1");
            lookup.AddNewItem(item1);

            // Change the item to be m=m2
            Lookup.MetadataModifications newMetadata = new Lookup.MetadataModifications(keepOnlySpecified: false);
            newMetadata.Add("m", "m2");
            ICollection<ProjectItemInstance> group = new List<ProjectItemInstance>();
            group.Add(item1);
            lookup.ModifyItems(item1.ItemType, group, newMetadata);

            // Now it has m=m2
            group = lookup.GetItems("i1");
            Assert.Equal(1, group.Count);
            Assert.Equal("m2", group.First().GetMetadataValue("m"));

            // But the original item hasn't changed yet
            Assert.Equal("m1", item1.GetMetadataValue("m"));

            enteredScope.LeaveScope();

            // It still has m=m2
            group = lookup.GetItems("i1");
            Assert.Equal(1, group.Count);
            Assert.Equal("m2", group.First().GetMetadataValue("m"));

            // But now the original item has changed as well
            Assert.Equal("m2", item1.GetMetadataValue("m"));
        }
Ejemplo n.º 5
0
        public void KeepMetadataOnlySpecifiedNoneSpecified()
        {
            ProjectInstance project = ProjectHelpers.CreateEmptyProjectInstance();
            ItemDictionary<ProjectItemInstance> table1 = new ItemDictionary<ProjectItemInstance>();
            Lookup lookup = LookupHelpers.CreateLookup(table1);

            Lookup.Scope enteredScope = lookup.EnterScope("x");

            // Add an item with m=m1
            ProjectItemInstance item1 = new ProjectItemInstance(project, "i1", "a2", project.FullPath);
            item1.SetMetadata("m1", "m1");
            item1.SetMetadata("m2", "m2");
            lookup.AddNewItem(item1);

            Lookup.Scope enteredScope2 = lookup.EnterScope("x");

            // Test keeping only specified metadata
            Lookup.MetadataModifications newMetadata = new Lookup.MetadataModifications(keepOnlySpecified: true);
            ICollection<ProjectItemInstance> group = lookup.GetItems(item1.ItemType);
            lookup.ModifyItems(item1.ItemType, group, newMetadata);

            group = lookup.GetItems("i1");
            Assert.Equal(1, group.Count);

            // m1 and m2 are gone.
            Assert.Equal(String.Empty, group.First().GetMetadataValue("m1"));
            Assert.Equal(String.Empty, group.First().GetMetadataValue("m2"));

            enteredScope2.LeaveScope();

            group = lookup.GetItems("i1");
            Assert.Equal(1, group.Count);

            // m1 and m2 are gone.
            Assert.Equal(String.Empty, group.First().GetMetadataValue("m1"));
            Assert.Equal(String.Empty, group.First().GetMetadataValue("m2"));

            enteredScope.LeaveScope();

            group = lookup.GetItems("i1");
            Assert.Equal(1, group.Count);

            // m1 and m2 are gone.
            Assert.Equal(String.Empty, group.First().GetMetadataValue("m1"));
            Assert.Equal(String.Empty, group.First().GetMetadataValue("m2"));
        }
Ejemplo n.º 6
0
        public void ModifyItemTwiceInSameScope1()
        {
            ProjectInstance project = ProjectHelpers.CreateEmptyProjectInstance();
            ItemDictionary<ProjectItemInstance> table1 = new ItemDictionary<ProjectItemInstance>();
            Lookup lookup = LookupHelpers.CreateLookup(table1);

            // Add an item with m=m1 and n=n1 
            ProjectItemInstance item1 = new ProjectItemInstance(project, "i1", "a2", project.FullPath);
            item1.SetMetadata("m", "m1");
            lookup.PopulateWithItem(item1);

            Lookup.Scope enteredScope = lookup.EnterScope("x");

            // Make a modification to the item to be m=m2
            Lookup.MetadataModifications newMetadata = new Lookup.MetadataModifications(keepOnlySpecified: false);
            newMetadata.Add("m", "m2");
            ICollection<ProjectItemInstance> group = new List<ProjectItemInstance>();
            group.Add(item1);
            lookup.ModifyItems(item1.ItemType, group, newMetadata);

            // Make an unrelated modification to the item
            newMetadata = new Lookup.MetadataModifications(keepOnlySpecified: false);
            newMetadata.Add("n", "n1");
            lookup.ModifyItems(item1.ItemType, group, newMetadata);

            // It's now m=m2
            group = lookup.GetItems("i1");
            Assert.Equal(1, group.Count);
            Assert.Equal("m2", group.First().GetMetadataValue("m"));
        }
Ejemplo n.º 7
0
        public void RemoveItemFromProjectPreviouslyModifiedAndGottenThroughGetItem()
        {
            ProjectInstance project = ProjectHelpers.CreateEmptyProjectInstance();
            // Create some project state with an item with m=m1 and n=n1 
            ItemDictionary<ProjectItemInstance> table1 = new ItemDictionary<ProjectItemInstance>();
            ProjectItemInstance item1 = new ProjectItemInstance(project, "i1", "a2", project.FullPath);
            item1.SetMetadata("m", "m1");
            table1.Add(item1);

            Lookup lookup = LookupHelpers.CreateLookup(table1);

            Lookup.Scope enteredScope = lookup.EnterScope("x");

            // Make a modification to the item to be m=m2
            Lookup.MetadataModifications newMetadata = new Lookup.MetadataModifications(keepOnlySpecified: false);
            newMetadata.Add("m", "m2");
            List<ProjectItemInstance> group = new List<ProjectItemInstance>();
            group.Add(item1);
            lookup.ModifyItems(item1.ItemType, group, newMetadata);

            // Get the item (under the covers, it cloned it in order to apply the modification)
            ICollection<ProjectItemInstance> group2 = lookup.GetItems(item1.ItemType);
            Assert.Equal(1, group2.Count);
            ProjectItemInstance item1b = group2.First();

            // Remove the item
            lookup.RemoveItem(item1b);

            // There's now no items at all
            ICollection<ProjectItemInstance> group3 = lookup.GetItems(item1.ItemType);
            Assert.Equal(0, group3.Count);

            // Leave scope
            enteredScope.LeaveScope();

            // And now none left in the project either
            Assert.Equal(0, table1["i1"].Count);
        }
Ejemplo n.º 8
0
        public void KeepMetadataOnlySpecifiedPropagate4()
        {
            ProjectInstance project = ProjectHelpers.CreateEmptyProjectInstance();
            ItemDictionary<ProjectItemInstance> table1 = new ItemDictionary<ProjectItemInstance>();
            Lookup lookup = LookupHelpers.CreateLookup(table1);

            Lookup.Scope enteredScope = lookup.EnterScope("x");

            // Add an item with m=m1
            ProjectItemInstance item1 = new ProjectItemInstance(project, "i1", "a2", project.FullPath);
            item1.SetMetadata("m1", "m1");
            item1.SetMetadata("m2", "m2");
            lookup.AddNewItem(item1);

            Lookup.Scope enteredScope2 = lookup.EnterScope("x");

            // Get rid of all of the metadata, then add m3
            Lookup.MetadataModifications newMetadata = new Lookup.MetadataModifications(keepOnlySpecified: true);
            newMetadata.Add("m3", "m3");
            ICollection<ProjectItemInstance> group = lookup.GetItems(item1.ItemType);
            lookup.ModifyItems(item1.ItemType, group, newMetadata);

            group = lookup.GetItems("i1");
            Assert.Equal(1, group.Count);

            // m1 and m2 are gone.
            Assert.Equal(String.Empty, group.First().GetMetadataValue("m1"));
            Assert.Equal(String.Empty, group.First().GetMetadataValue("m2"));

            // m3 is still there.
            Assert.Equal("m3", group.First().GetMetadataValue("m3"));

            enteredScope2.LeaveScope();

            // Keep m3.
            Lookup.MetadataModifications newMetadata2 = new Lookup.MetadataModifications(keepOnlySpecified: true);
            newMetadata2["m3"] = Lookup.MetadataModification.CreateFromNoChange();
            group = lookup.GetItems(item1.ItemType);
            lookup.ModifyItems(item1.ItemType, group, newMetadata2);

            group = lookup.GetItems("i1");
            Assert.Equal(1, group.Count);

            // m1 and m2 are gone
            Assert.Equal(String.Empty, group.First().GetMetadataValue("m1"));
            Assert.Equal(String.Empty, group.First().GetMetadataValue("m2"));

            // m3 is still there
            Assert.Equal("m3", group.First().GetMetadataValue("m3"));

            enteredScope.LeaveScope();

            group = lookup.GetItems("i1");
            Assert.Equal(1, group.Count);

            // m1 and m2 are gone
            Assert.Equal(String.Empty, group.First().GetMetadataValue("m1"));
            Assert.Equal(String.Empty, group.First().GetMetadataValue("m2"));

            // m3 is still there.
            Assert.Equal("m3", group.First().GetMetadataValue("m3"));
        }
Ejemplo n.º 9
0
        public void InvalidUnqualifiedMetadataReference()
        {
            ProjectInstance project = ProjectHelpers.CreateEmptyProjectInstance();
            List<string> parameters = new List<string>();
            parameters.Add("@(File)");
            parameters.Add("%(Culture)");

            ItemDictionary<ProjectItemInstance> itemsByType = new ItemDictionary<ProjectItemInstance>();

            List<ProjectItemInstance> items = new List<ProjectItemInstance>();

            ProjectItemInstance a = new ProjectItemInstance(project, "File", "a.foo", project.FullPath);
            items.Add(a);
            ProjectItemInstance b = new ProjectItemInstance(project, "File", "b.foo", project.FullPath);
            items.Add(b);
            a.SetMetadata("Culture", "fr-fr");
            itemsByType.ImportItems(items);

            PropertyDictionary<ProjectPropertyInstance> properties = new PropertyDictionary<ProjectPropertyInstance>();

            // This is expected to throw because not all items contain a value for metadata "Culture".
            // Only a.foo has a Culture metadata.  b.foo does not.
            List<ItemBucket> buckets = BatchingEngine.PrepareBatchingBuckets(parameters, CreateLookup(itemsByType, properties), MockElementLocation.Instance);
        }
Ejemplo n.º 10
0
        public void ModifyItemInProjectPreviouslyModifiedAndGottenThroughGetItem()
        {
            ProjectInstance project = ProjectHelpers.CreateEmptyProjectInstance();
            // Create some project state with an item with m=m1 and n=n1 
            ItemDictionary<ProjectItemInstance> table1 = new ItemDictionary<ProjectItemInstance>();
            ProjectItemInstance item1 = new ProjectItemInstance(project, "i1", "a2", project.FullPath);
            item1.SetMetadata("m", "m1");
            table1.Add(item1);

            Lookup lookup = LookupHelpers.CreateLookup(table1);

            Lookup.Scope enteredScope = lookup.EnterScope("x");

            // Make a modification to the item to be m=m2
            Lookup.MetadataModifications newMetadata = new Lookup.MetadataModifications(keepOnlySpecified: false);
            newMetadata.Add("m", "m2");
            List<ProjectItemInstance> group = new List<ProjectItemInstance>();
            group.Add(item1);
            lookup.ModifyItems(item1.ItemType, group, newMetadata);

            // Get the item (under the covers, it cloned it in order to apply the modification)
            ICollection<ProjectItemInstance> group2 = lookup.GetItems(item1.ItemType);
            Assert.Equal(1, group2.Count);
            ProjectItemInstance item1b = group2.First();

            // Modify to m=m3
            Lookup.MetadataModifications newMetadata2 = new Lookup.MetadataModifications(keepOnlySpecified: false);
            newMetadata2.Add("m", "m3");
            List<ProjectItemInstance> group3 = new List<ProjectItemInstance>();
            group3.Add(item1b);
            lookup.ModifyItems(item1b.ItemType, group3, newMetadata2);

            // Modifications are visible
            ICollection<ProjectItemInstance> group4 = lookup.GetItems(item1b.ItemType);
            Assert.Equal(1, group4.Count);
            Assert.Equal("m3", group4.First().GetMetadataValue("m"));

            // Leave scope
            enteredScope.LeaveScope();

            // Still visible
            ICollection<ProjectItemInstance> group5 = lookup.GetItems(item1b.ItemType);
            Assert.Equal(1, group5.Count);
            Assert.Equal("m3", group5.First().GetMetadataValue("m"));

            // And the one in the project is changed
            Assert.Equal("m3", item1.GetMetadataValue("m"));
        }
Ejemplo n.º 11
0
        public void ValidUnqualifiedMetadataReference()
        {
            ProjectInstance project = ProjectHelpers.CreateEmptyProjectInstance();
            List<string> parameters = new List<string>();
            parameters.Add("@(File)");
            parameters.Add("%(Culture)");

            ItemDictionary<ProjectItemInstance> itemsByType = new ItemDictionary<ProjectItemInstance>();

            List<ProjectItemInstance> items = new List<ProjectItemInstance>();

            ProjectItemInstance a = new ProjectItemInstance(project, "File", "a.foo", project.FullPath);
            ProjectItemInstance b = new ProjectItemInstance(project, "File", "b.foo", project.FullPath);
            a.SetMetadata("Culture", "fr-fr");
            b.SetMetadata("Culture", "en-en");
            items.Add(a);
            items.Add(b);
            itemsByType.ImportItems(items);

            PropertyDictionary<ProjectPropertyInstance> properties = new PropertyDictionary<ProjectPropertyInstance>();

            List<ItemBucket> buckets = BatchingEngine.PrepareBatchingBuckets(parameters, CreateLookup(itemsByType, properties), null);
            Assert.AreEqual(2, buckets.Count);
        }
Ejemplo n.º 12
0
        public void KeepMetadataOnlySpecifiedPropagate2()
        {
            ProjectInstance project = ProjectHelpers.CreateEmptyProjectInstance();
            ItemDictionary<ProjectItemInstance> table1 = new ItemDictionary<ProjectItemInstance>();
            Lookup lookup = LookupHelpers.CreateLookup(table1);

            Lookup.Scope enteredScope = lookup.EnterScope("x");

            // Add an item with m=m1
            ProjectItemInstance item1 = new ProjectItemInstance(project, "i1", "a2", project.FullPath);
            item1.SetMetadata("m1", "m1");
            item1.SetMetadata("m2", "m2");
            lookup.AddNewItem(item1);

            Lookup.Scope enteredScope2 = lookup.EnterScope("x");

            // Add m3 metadata
            Lookup.MetadataModifications newMetadata = new Lookup.MetadataModifications(keepOnlySpecified: false);
            newMetadata.Add("m3", "m3");
            ICollection<ProjectItemInstance> group = lookup.GetItems(item1.ItemType);
            lookup.ModifyItems(item1.ItemType, group, newMetadata);

            group = lookup.GetItems("i1");
            Assert.AreEqual(1, group.Count);

            // All metadata are present
            Assert.AreEqual("m1", group.First().GetMetadataValue("m1"));
            Assert.AreEqual("m2", group.First().GetMetadataValue("m2"));
            Assert.AreEqual("m3", group.First().GetMetadataValue("m3"));

            enteredScope2.LeaveScope();

            // Now clear metadata
            Lookup.MetadataModifications newMetadata2 = new Lookup.MetadataModifications(keepOnlySpecified: true);
            group = lookup.GetItems(item1.ItemType);
            lookup.ModifyItems(item1.ItemType, group, newMetadata2);

            group = lookup.GetItems("i1");
            Assert.AreEqual(1, group.Count);

            // All metadata are gone
            Assert.AreEqual(String.Empty, group.First().GetMetadataValue("m1"));
            Assert.AreEqual(String.Empty, group.First().GetMetadataValue("m2"));
            Assert.AreEqual(String.Empty, group.First().GetMetadataValue("m3"));

            enteredScope.LeaveScope();

            group = lookup.GetItems("i1");
            Assert.AreEqual(1, group.Count);

            // All metadata are gone
            Assert.AreEqual(String.Empty, group.First().GetMetadataValue("m1"));
            Assert.AreEqual(String.Empty, group.First().GetMetadataValue("m2"));
            Assert.AreEqual(String.Empty, group.First().GetMetadataValue("m3"));
        }
Ejemplo n.º 13
0
        /// <summary>
        /// Gets task item outputs
        /// </summary>
        private void GatherTaskItemOutputs(bool outputTargetIsItem, string outputTargetName, ITaskItem[] outputs, ElementLocation parameterLocation)
        {
            // if the task has generated outputs (if it didn't, don't do anything)
            if (outputs != null)
            {
                if (outputTargetIsItem)
                {
                    foreach (ITaskItem output in outputs)
                    {
                        // if individual items in the array are null, ignore them
                        if (output != null)
                        {
                            ProjectItemInstance newItem;

                            ProjectItemInstance.TaskItem outputAsProjectItem = output as ProjectItemInstance.TaskItem;
                            string parameterLocationEscaped = EscapingUtilities.EscapeWithCaching(parameterLocation.File);

                            if (outputAsProjectItem != null)
                            {
                                // The common case -- all items involved are Microsoft.Build.Execution.ProjectItemInstance.TaskItems.  
                                // Furthermore, because that is true, we know by definition that they also implement ITaskItem2.
                                newItem = new ProjectItemInstance(_projectInstance, outputTargetName, outputAsProjectItem.IncludeEscaped, parameterLocationEscaped);

                                newItem.SetMetadata(outputAsProjectItem.MetadataCollection); // copy-on-write!
                            }
                            else
                            {
                                ITaskItem2 outputAsITaskItem2 = output as ITaskItem2;

                                if (outputAsITaskItem2 != null)
                                {
                                    // Probably a Microsoft.Build.Utilities.TaskItem.  Not quite as good, but we can still preserve escaping. 
                                    newItem = new ProjectItemInstance(_projectInstance, outputTargetName, outputAsITaskItem2.EvaluatedIncludeEscaped, parameterLocationEscaped);

                                    // It would be nice to be copy-on-write here, but Utilities.TaskItem doesn't know about CopyOnWritePropertyDictionary. 
                                    foreach (DictionaryEntry entry in outputAsITaskItem2.CloneCustomMetadataEscaped())
                                    {
                                        newItem.SetMetadataOnTaskOutput((string)entry.Key, (string)entry.Value);
                                    }
                                }
                                else
                                {
                                    // Not a ProjectItemInstance.TaskItem or even a ITaskItem2, so we have to fake it.  
                                    // Setting an item spec expects the escaped value, as does setting metadata. 
                                    newItem = new ProjectItemInstance(_projectInstance, outputTargetName, EscapingUtilities.Escape(output.ItemSpec), parameterLocationEscaped);

                                    foreach (DictionaryEntry entry in output.CloneCustomMetadata())
                                    {
                                        newItem.SetMetadataOnTaskOutput((string)entry.Key, EscapingUtilities.Escape((string)entry.Value));
                                    }
                                }
                            }

                            _batchBucket.Lookup.AddNewItem(newItem);
                        }
                    }

                    if (_logTaskInputs && !_taskLoggingContext.LoggingService.OnlyLogCriticalEvents && outputs.Length > 0)
                    {
                        string parameterText = ItemGroupLoggingHelper.GetParameterText(
                            ResourceUtilities.FormatResourceString("OutputItemParameterMessagePrefix"),
                            outputTargetName,
                            outputs);

                        _taskLoggingContext.LogCommentFromText(MessageImportance.Low, parameterText);
                    }
                }
                else
                {
                    // to store an ITaskItem array in a property, join all the item-specs with semi-colons to make the
                    // property value, and ignore/discard the attributes on the ITaskItems.
                    //
                    // An empty ITaskItem[] should create a blank value property, for compatibility.                 
                    StringBuilder joinedOutputs = (outputs.Length == 0) ? new StringBuilder() : null;

                    foreach (ITaskItem output in outputs)
                    {
                        // if individual items in the array are null, ignore them
                        if (output != null)
                        {
                            joinedOutputs = joinedOutputs ?? new StringBuilder();

                            if (joinedOutputs.Length > 0)
                            {
                                joinedOutputs.Append(';');
                            }

                            ITaskItem2 outputAsITaskItem2 = output as ITaskItem2;

                            if (outputAsITaskItem2 != null)
                            {
                                joinedOutputs.Append(outputAsITaskItem2.EvaluatedIncludeEscaped);
                            }
                            else
                            {
                                joinedOutputs.Append(EscapingUtilities.Escape(output.ItemSpec));
                            }
                        }
                    }

                    if (joinedOutputs != null)
                    {
                        var outputString = joinedOutputs.ToString();
                        if (_logTaskInputs && !_taskLoggingContext.LoggingService.OnlyLogCriticalEvents)
                        {
                            _taskLoggingContext.LogComment(MessageImportance.Low, "OutputPropertyLogMessage", outputTargetName, outputString);
                        }

                        _batchBucket.Lookup.SetProperty(ProjectPropertyInstance.Create(outputTargetName, outputString, parameterLocation, _projectInstance.IsImmutable));
                    }
                }
            }
        }
Ejemplo n.º 14
0
        /// <summary>
        /// Initialize the host object
        /// </summary>
        /// <param name="throwOnExecute">Should the task throw when executed</param>
        private void InitializeHost(bool throwOnExecute)
        {
            _loggingService = LoggingService.CreateLoggingService(LoggerMode.Synchronous, 1) as ILoggingService;
            _logger = new MockLogger();
            _loggingService.RegisterLogger(_logger);
            _host = new TaskExecutionHost();
            TargetLoggingContext tlc = new TargetLoggingContext(_loggingService, new BuildEventContext(1, 1, BuildEventContext.InvalidProjectContextId, 1));

            // Set up a temporary project and add some items to it.
            ProjectInstance project = CreateTestProject();

            TypeLoader typeLoader = new TypeLoader(new TypeFilter(IsTaskFactoryClass));
            AssemblyLoadInfo loadInfo = AssemblyLoadInfo.Create(Assembly.GetAssembly(typeof(TaskBuilderTestTask.TaskBuilderTestTaskFactory)).FullName, null);
            LoadedType loadedType = new LoadedType(typeof(TaskBuilderTestTask.TaskBuilderTestTaskFactory), loadInfo);

            TaskBuilderTestTask.TaskBuilderTestTaskFactory taskFactory = new TaskBuilderTestTask.TaskBuilderTestTaskFactory();
            taskFactory.ThrowOnExecute = throwOnExecute;
            string taskName = "TaskBuilderTestTask";
            (_host as TaskExecutionHost)._UNITTESTONLY_TaskFactoryWrapper = new TaskFactoryWrapper(taskFactory, loadedType, taskName, null);
            _host.InitializeForTask
                (
                this,
                tlc,
                project,
                taskName,
                ElementLocation.Create("none", 1, 1),
                this,
                false,
                null,
                false,
                CancellationToken.None
                );

            ProjectTaskInstance taskInstance = project.Targets["foo"].Tasks.First();
            TaskLoggingContext talc = tlc.LogTaskBatchStarted(".", taskInstance);

            ItemDictionary<ProjectItemInstance> itemsByName = new ItemDictionary<ProjectItemInstance>();

            ProjectItemInstance item = new ProjectItemInstance(project, "ItemListContainingOneItem", "a.cs", ".");
            item.SetMetadata("Culture", "fr-fr");
            itemsByName.Add(item);
            _oneItem = new ITaskItem[] { new TaskItem(item) };

            item = new ProjectItemInstance(project, "ItemListContainingTwoItems", "b.cs", ".");
            ProjectItemInstance item2 = new ProjectItemInstance(project, "ItemListContainingTwoItems", "c.cs", ".");
            item.SetMetadata("HintPath", "c:\\foo");
            item2.SetMetadata("HintPath", "c:\\bar");
            itemsByName.Add(item);
            itemsByName.Add(item2);
            _twoItems = new ITaskItem[] { new TaskItem(item), new TaskItem(item2) };

            _bucket = new ItemBucket(new string[0], new Dictionary<string, string>(), new Lookup(itemsByName, new PropertyDictionary<ProjectPropertyInstance>(), null), 0);
            _host.FindTask(null);
            _host.InitializeForBatch(talc, _bucket, null);
            _parametersSetOnTask = new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase);
            _outputsReadFromTask = new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase);
        }
Ejemplo n.º 15
0
        private static Lookup GenerateLookup(ProjectInstance project, PropertyDictionary<ProjectPropertyInstance> properties)
        {
            List<ProjectItemInstance> items = new List<ProjectItemInstance>();
            ProjectItemInstance item1 = new ProjectItemInstance(project, "i0", "a1", project.FullPath);
            ProjectItemInstance item2 = new ProjectItemInstance(project, "i0", "a2", project.FullPath);
            ProjectItemInstance item3 = new ProjectItemInstance(project, "i0", "a3", project.FullPath);
            ProjectItemInstance item4 = new ProjectItemInstance(project, "i0", "a4", project.FullPath);
            item1.SetMetadata("m", "m1");
            item1.SetMetadata("n", "n1");
            item2.SetMetadata("m", "m2");
            item2.SetMetadata("n", "n2");
            item3.SetMetadata("m", "m2");
            item3.SetMetadata("n", "n2");
            item4.SetMetadata("m", "m3");
            item4.SetMetadata("n", "n3");
            items.Add(item1);
            items.Add(item2);
            items.Add(item3);
            items.Add(item4);
            ItemDictionary<ProjectItemInstance> itemsByName = new ItemDictionary<ProjectItemInstance>();
            itemsByName.ImportItems(items);

            Lookup lookup = LookupHelpers.CreateLookup(properties, itemsByName);

            return lookup;
        }