Exemple #1
0
 public RainbowIterator(string rootPath, YamlSerializationFormatter itemFormatter)
 {
     _rootPath          = Path.GetFullPath(rootPath);
     this.itemFormatter = itemFormatter;
     _currentPosition   = 0;
     InitStack();
 }
        public static IItemData DeserializeYaml(string yaml, string itemId)
        {
            var formatter = new YamlSerializationFormatter(null, null);

            if (yaml != null)
            {
                using (var ms = new MemoryStream())
                {
                    IItemData itemData = null;
                    try
                    {
                        var bytes = Encoding.UTF8.GetBytes(yaml);
                        ms.Write(bytes, 0, bytes.Length);

                        ms.Seek(0, SeekOrigin.Begin);
                        itemData = formatter.ReadSerializedItem(ms, itemId);
                    }
                    catch (Exception e)
                    {
                        Log.Error("Problem reading yaml from remote server", e, typeof(RemoteContentService));
                    }
                    if (itemData != null)
                    {
                        return(itemData);
                    }
                }
            }
            return(null);
        }
        public static DbItem DeserializeItem(FileInfo file, string language)
        {
            DbItem deserialized = null;
            var    formatter    = new YamlSerializationFormatter(null, null);

            using (StreamReader sr = new StreamReader(file.FullName))
            {
                try
                {
                    IItemData item = formatter.ReadSerializedItem(sr.BaseStream, file.Name);
                    if (item != null)
                    {
                        deserialized          = new DbItem(item.Name, new ID(item.Id), new ID(item.TemplateId));
                        deserialized.FullPath = item.Path;
                        deserialized.BranchId = new ID(item.BranchId);
                        deserialized.ParentID = new ID(item.ParentId);

                        foreach (var sharedField in item.SharedFields)
                        {
                            deserialized.Fields.Add(new DbField(new ID(sharedField.FieldId))
                            {
                                Name = sharedField.NameHint, Value = sharedField.Value
                            });
                        }

                        foreach (var unversionedField in item.UnversionedFields)
                        {
                            if (unversionedField != null && unversionedField.Language.Name == language)
                            {
                                var field = unversionedField.Fields.Last();
                                deserialized.Fields.Add(new DbField(new ID(field.FieldId))
                                {
                                    Name = field.NameHint, Value = field.Value
                                });
                            }
                        }

                        if (item.Versions != null && item.Versions.Count() > 0 && item.Versions.Any(v => v.Language.Name == language))
                        {
                            foreach (var field in item.Versions.Where(v => v.Language.Name == language).Last().Fields)
                            {
                                deserialized.Fields.Add(new DbField(new ID(field.FieldId))
                                {
                                    Name = field.NameHint, Value = field.Value
                                });
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    throw new Exception(string.Format("Unable to deserialize '{0}'", file.FullName), ex);
                }
            }

            return(deserialized);
        }
Exemple #4
0
        public RainbowSerializationProvider(string name) : base(name)
        {
            _name = name;

            var config = (RainbowConfigSection)ConfigurationManager.GetSection("rainbow");
            var rainbowConfigFactory = new RainbowConfigFactory(config);

            _formatter = rainbowConfigFactory.CreateFormatter() as YamlSerializationFormatter;
        }
Exemple #5
0
        public static IList <TemplateItem> Run(AppSettings settings)
        {
            var formatter = new YamlSerializationFormatter(null, null);

            var folder = new DirectoryInfo(settings.Folder);

            var unicornData = new UnicornReader(formatter, settings.Debug, settings.IncludeSitecorePaths).ReadDirectory(folder).ToList();

            var factory = new TemplateFactory();

            return(factory.Create(unicornData));
        }
Exemple #6
0
            public static YamlSerializationFormatterClone Clone(YamlSerializationFormatter obj)
            {
                Assert.ArgumentNotNull(obj, nameof(obj));
                var fieldFilter = typeof(YamlSerializationFormatter)
                                  .GetField("_fieldFilter", BindingFlags.NonPublic | BindingFlags.Instance)
                                  ?.GetValue(obj) as IFieldFilter;

                return(new YamlSerializationFormatterClone(fieldFilter, obj)
                {
                    ParentDataStore = obj.ParentDataStore
                });
            }
        public void YamlFormatter_ReadsItemWithDatabaseName_AsExpected()
        {
            var formatter = new YamlSerializationFormatter(null, null);

            var yml = @"---
            ID: a4f985d9-98b3-4b52-aaaf-4344f6e747c6
            Parent: 001dd393-96c5-490b-924a-b0f25cd9efd8
            Template: 007a464d-5b09-4d0e-8481-cb6a604a5948
            Path: /sitecore/content/test
            DB: master
            SharedFields:
            - ID: 549fa670-79ab-4810-9450-aba0c06a2b87
              # Text Shared
              Value: SHARED
            Languages:
            - Language: en
             Versions:
              - Version: 1
            Fields:
            - ID: 25bed78c-4957-4165-998a-ca1b52f67497
              # __Created
              Value: 20140918T062658:635466184182719253
            ";

            using (var ms = new MemoryStream(Encoding.UTF8.GetBytes(yml)))
            {
                var item = formatter.ReadSerializedItem(ms, "unittest.yml");
                Assert.Equal("unittest.yml", item.SerializedItemId);
                Assert.Equal(new Guid("a4f985d9-98b3-4b52-aaaf-4344f6e747c6"), item.Id);
                Assert.Equal("master", item.DatabaseName);
                Assert.Equal(new Guid("001dd393-96c5-490b-924a-b0f25cd9efd8"), item.ParentId);
                Assert.Equal("/sitecore/content/test", item.Path);

                var shared = item.SharedFields.ToArray();

                Assert.Equal(1, shared.Length);
                Assert.Equal(new Guid("549fa670-79ab-4810-9450-aba0c06a2b87"), shared[0].FieldId);
                Assert.Equal("SHARED", shared[0].Value);

                var versions = item.Versions.ToArray();

                Assert.Equal(1, versions.Length);
                Assert.Equal(1, versions[0].VersionNumber);
                Assert.Equal(new CultureInfo("en"), versions[0].Language);

                var versionedFields = versions[0].Fields.ToArray();

                Assert.Equal(1, versionedFields.Length);
                Assert.Equal(new Guid("25bed78c-4957-4165-998a-ca1b52f67497"), versionedFields[0].FieldId);
                Assert.Equal("20140918T062658:635466184182719253", versionedFields[0].Value);
            }
        }
Exemple #8
0
        public void YamlFormatter_WritesItem_WithFieldFormatter_AsExpected()
        {
            var xmlConfigNode = @"<serializationFormatter>
						<fieldFormatter type=""Rainbow.Formatting.FieldFormatters.MultilistFormatter, Rainbow"" />
					</serializationFormatter>"                    ;

            var configDoc = new XmlDocument();

            configDoc.LoadXml(xmlConfigNode);

            var formatter = new YamlSerializationFormatter(configDoc.DocumentElement, null);

            var item = new FakeItem(
                id: new Guid("a4f985d9-98b3-4b52-aaaf-4344f6e747c6"),
                parentId: new Guid("001dd393-96c5-490b-924a-b0f25cd9efd8"),
                templateId: new Guid("007a464d-5b09-4d0e-8481-cb6a604a5948"),
                path: "/sitecore/content/test",
                sharedFields: new[]
            {
                new FakeFieldValue("{35633C96-8494-4C05-A62A-954E2B401A4D}|{1486633B-3A5D-40D6-99D5-920FDFA63617}", "Multilist", new Guid("549fa670-79ab-4810-9450-aba0c06a2b87"), "Multilist Field")
            },
                name: "test");

            var expectedYml = @"---
ID: a4f985d9-98b3-4b52-aaaf-4344f6e747c6
Parent: 001dd393-96c5-490b-924a-b0f25cd9efd8
Template: 007a464d-5b09-4d0e-8481-cb6a604a5948
Path: /sitecore/content/test
DB: master
SharedFields:
- ID: 549fa670-79ab-4810-9450-aba0c06a2b87
  Hint: Multilist Field
  Type: Multilist
  Value: |
    {35633C96-8494-4C05-A62A-954E2B401A4D}
    {1486633B-3A5D-40D6-99D5-920FDFA63617}
";

            using (var ms = new MemoryStream())
            {
                formatter.WriteSerializedItem(item, ms);

                ms.Seek(0, SeekOrigin.Begin);

                using (var reader = new StreamReader(ms))
                {
                    var yml = reader.ReadToEnd();

                    Assert.Equal(expectedYml, yml);
                }
            }
        }
Exemple #9
0
        public void YamlFormatter_ReadsItem_AsExpected()
        {
            var formatter = new YamlSerializationFormatter(null, null);

            var yml = @"---
ID: a4f985d9-98b3-4b52-aaaf-4344f6e747c6
Parent: 001dd393-96c5-490b-924a-b0f25cd9efd8
Template: 007a464d-5b09-4d0e-8481-cb6a604a5948
Path: /sitecore/content/test
SharedFields:
- ID: 549fa670-79ab-4810-9450-aba0c06a2b87
  # Text Shared
  Value: SHARED
Languages:
- Language: en
  Versions:
  - Version: 1
    Fields:
    - ID: 25bed78c-4957-4165-998a-ca1b52f67497
      # __Created
      Value: 20140918T062658:635466184182719253
";

            using (var ms = new MemoryStream(Encoding.UTF8.GetBytes(yml)))
            {
                var item = formatter.ReadSerializedItem(ms, "unittest.yml");
                Assert.Equal("unittest.yml", item.SerializedItemId);
                Assert.Equal(new Guid("a4f985d9-98b3-4b52-aaaf-4344f6e747c6"), item.Id);
                Assert.Equal(null, item.DatabaseName);
                Assert.Equal(new Guid("001dd393-96c5-490b-924a-b0f25cd9efd8"), item.ParentId);
                Assert.Equal("/sitecore/content/test", item.Path);

                var shared = item.SharedFields.ToArray();

                Assert.Equal(1, shared.Length);
                Assert.Equal(new Guid("549fa670-79ab-4810-9450-aba0c06a2b87"), shared[0].FieldId);
                Assert.Equal("SHARED", shared[0].Value);

                var versions = item.Versions.ToArray();

                Assert.Equal(1, versions.Length);
                Assert.Equal(1, versions[0].VersionNumber);
                Assert.Equal(new CultureInfo("en"), versions[0].Language);

                var versionedFields = versions[0].Fields.ToArray();

                Assert.Equal(1, versionedFields.Length);
                Assert.Equal(new Guid("25bed78c-4957-4165-998a-ca1b52f67497"), versionedFields[0].FieldId);
                Assert.Equal("20140918T062658:635466184182719253", versionedFields[0].Value);
            }
        }
Exemple #10
0
        public static IItemData GetRemoteItemData(RemoteContentPullArgs args, string itemId)
        {
            try
            {
                WebClient wc = new WebClient {
                    Encoding = Encoding.UTF8
                };
                string yamlList = wc.UploadString($"{args.server}/scs/cmcontenttreegetitem.scsvc", "POST",
                                                  args.GetSerializedData(itemId, false));
                string yaml      = JsonConvert.DeserializeObject <List <string> >(yamlList).FirstOrDefault();
                var    formatter = new YamlSerializationFormatter(null, null);
                if (yaml != null)
                {
                    using (var ms = new MemoryStream())
                    {
                        IItemData itemData = null;
                        try
                        {
                            var bytes = Encoding.UTF8.GetBytes(yaml);
                            ms.Write(bytes, 0, bytes.Length);

                            ms.Seek(0, SeekOrigin.Begin);
                            itemData = formatter.ReadSerializedItem(ms, itemId);
                            itemData.DatabaseName = args.database;
                        }
                        catch (Exception e)
                        {
                            Log.Error("Problem reading yaml from remote server", e, args);
                        }
                        if (itemData != null)
                        {
                            return(itemData);
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Log.Error("Error getting remote item data for " + itemId, e, args);
            }
            return(null);
        }
		public void YamlFormatter_ReadsMetadata_AsExpected()
		{
			var formatter = new YamlSerializationFormatter(null, null);

			var metadata = @"---
ID: a4f985d9-98b3-4b52-aaaf-4344f6e747c6
Parent: 001dd393-96c5-490b-924a-b0f25cd9efd8
Template: 007a464d-5b09-4d0e-8481-cb6a604a5948
Path: /sitecore/content/test
";

			using (var ms = new MemoryStream(Encoding.UTF8.GetBytes(metadata)))
			{
				var item = formatter.ReadSerializedItemMetadata(ms, "unittest.yml");
				Assert.Equal(new Guid("a4f985d9-98b3-4b52-aaaf-4344f6e747c6"), item.Id);
				Assert.Equal(new Guid("001dd393-96c5-490b-924a-b0f25cd9efd8"), item.ParentId);
				Assert.Equal("unittest.yml", item.SerializedItemId);
				Assert.Equal("/sitecore/content/test", item.Path);
			}
		}
Exemple #12
0
        public void YamlFormatter_ReadsMetadata_AsExpected()
        {
            var formatter = new YamlSerializationFormatter(null, null);

            var metadata = @"---
ID: a4f985d9-98b3-4b52-aaaf-4344f6e747c6
Parent: 001dd393-96c5-490b-924a-b0f25cd9efd8
Template: 007a464d-5b09-4d0e-8481-cb6a604a5948
Path: /sitecore/content/test
";

            using (var ms = new MemoryStream(Encoding.UTF8.GetBytes(metadata)))
            {
                var item = formatter.ReadSerializedItemMetadata(ms, "unittest.yml");
                Assert.Equal(new Guid("a4f985d9-98b3-4b52-aaaf-4344f6e747c6"), item.Id);
                Assert.Equal(new Guid("001dd393-96c5-490b-924a-b0f25cd9efd8"), item.ParentId);
                Assert.Equal("unittest.yml", item.SerializedItemId);
                Assert.Equal("/sitecore/content/test", item.Path);
            }
        }
Exemple #13
0
        private IEnumerable <IItemData> GetRemoteItemData(RemoteContentPullArgs args)
        {
            List <IItemData> ret      = new List <IItemData>();
            bool             children = args.children;

            args.children = false;
            Queue <string> pullableItems = new Queue <string>();
            WebClient      wc            = new WebClient {
                Encoding = Encoding.UTF8
            };

            pullableItems.Enqueue(args.id);
            while (pullableItems.Any())
            {
                string cur = pullableItems.Dequeue();
                args.id = cur;
                string yamlList = null;
                try
                {
                    yamlList = wc.UploadString($"{args.server}/scs/cmcontenttreegetitem.scsvc", "POST",
                                               args.GetSerializedData());
                }
                catch (Exception e)
                {
                    Log.Error("Problem reading remote server item data", e, this);
                }
                if (string.IsNullOrWhiteSpace(yamlList))
                {
                    yield return(new ErrorItemData()
                    {
                        Id = Guid.Parse(args.id), Path = "Problem reading remote server item data"
                    });

                    continue;
                }
                List <string> remoteItems = JsonConvert.DeserializeObject <List <string> >(yamlList);
                var           formatter   = new YamlSerializationFormatter(null, null);
                foreach (string yaml in remoteItems)
                {
                    using (var ms = new MemoryStream())
                    {
                        IItemData itemData = null;
                        try
                        {
                            var bytes = Encoding.UTF8.GetBytes(yaml);
                            ms.Write(bytes, 0, bytes.Length);

                            ms.Seek(0, SeekOrigin.Begin);
                            itemData = formatter.ReadSerializedItem(ms, args.id);
                            itemData.DatabaseName = args.database;
                        }
                        catch (Exception e)
                        {
                            Log.Error("Problem reading yaml from remote server", e, this);
                        }
                        if (itemData == null)
                        {
                            yield return(new ErrorItemData()
                            {
                                Id = Guid.Parse(args.id), Path = "Problem reading yaml from remote server"
                            });

                            continue;
                        }
                        yield return(itemData);
                    }
                }
                if (children)
                {
                    ContentTreeNode node = null;
                    try
                    {
                        node = JsonConvert.DeserializeObject <ContentTreeNode>(wc.UploadString($"{args.server}/scs/cmcontenttree.scsvc", "POST",
                                                                                               $@"{{ ""id"": ""{cur}"", ""database"": ""{args.database}"", ""server"": ""{args.server}"" }}"));
                        foreach (ContentTreeNode child in node.Nodes)
                        {
                            pullableItems.Enqueue(child.Id);
                        }
                    }
                    catch (Exception e)
                    {
                        Log.Error("Problem getting children of node " + cur, e, this);
                    }
                    if (node == null)
                    {
                        yield return new ErrorItemData()
                               {
                                   Id = Guid.Parse(cur), Path = "Problem getting children of node"
                               }
                    }
                    ;
                }
            }
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="RainbowDataItem" /> class.
 /// </summary>
 /// <param name="rootPath">The root path.</param>
 /// <param name="relatedPath">The related path.</param>
 /// <param name="name">The name.</param>
 /// <param name="formatter">The formatter.</param>
 public RainbowDataItem(string rootPath, string relatedPath, string name, YamlSerializationFormatter formatter)
     : base(rootPath, relatedPath, name)
 {
     _formatter = formatter;
 }
        protected override SyncItem GetItem(FileInfo itemFile, Func <IItemData, bool> mustMatch)
        {
            var formatter = new YamlSerializationFormatter(null, null);

            //Checking path is a template, Helix structure means have to traverse more directories bringing in serialized roles and other yml files.
            if (!itemFile.FullName.Contains(@"\serialization\Templates"))
            {
                return(null);
            }

            SyncItem syncItem = null;

            using (StreamReader sr = new StreamReader(itemFile.FullName))
            {
                try
                {
                    IItemData item = formatter.ReadSerializedItem(sr.BaseStream, itemFile.Name);
                    if (item == null || (mustMatch != null && !mustMatch(item)))
                    {
                        return(null);
                    }

                    syncItem = new SyncItem()
                    {
                        Name         = item.Name,
                        BranchId     = new ID(item.BranchId).ToString(),
                        DatabaseName = item.DatabaseName,
                        ID           = new ID(item.Id).ToString(),
                        ItemPath     = item.Path,
                        TemplateID   = new ID(item.TemplateId).ToString(),
                        ParentID     = new ID(item.ParentId).ToString()
                    };
                    foreach (var version in item.Versions)
                    {
                        var syncVersion = syncItem.AddVersion(version.Language.Name, version.VersionNumber.ToString(),
                                                              version.VersionNumber.ToString());
                        foreach (var field in version.Fields)
                        {
                            syncVersion.AddField(new ID(field.FieldId).ToString(), field.NameHint, field.NameHint, field.Value, true);
                        }
                    }

                    foreach (var sharedField in item.SharedFields)
                    {
                        syncItem.AddSharedField(new ID(sharedField.FieldId).ToString(), sharedField.NameHint,
                                                sharedField.NameHint, sharedField.Value, true);
                    }

                    foreach (var unversionedField in item.UnversionedFields)
                    {
                        foreach (var version in syncItem.Versions.Where(v => v.Language == unversionedField.Language.Name))
                        {
                            foreach (var itemFieldValue in unversionedField.Fields)
                            {
                                version.AddField(new ID(itemFieldValue.FieldId).ToString(), itemFieldValue.NameHint,
                                                 itemFieldValue.NameHint, itemFieldValue.Value, true);
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    throw new Exception(string.Format("Unable to deserialize '{0}'", itemFile.FullName), ex);
                }
            }
            return(syncItem);
        }
Exemple #16
0
        public void YamlFormatter_ReadsItemWithDatabaseName_AsExpected()
        {
            var formatter = new YamlSerializationFormatter(null, null);

            var yml = @"---
ID: a4f985d9-98b3-4b52-aaaf-4344f6e747c6
Parent: 001dd393-96c5-490b-924a-b0f25cd9efd8
Template: 007a464d-5b09-4d0e-8481-cb6a604a5948
Path: /sitecore/content/test
DB: master
SharedFields:
- ID: 549fa670-79ab-4810-9450-aba0c06a2b87
  Hint: Text Shared
  Value: SHARED
Languages:
- Language: en
  Fields:
  - ID: ffffd78c-4957-4165-998a-ca1b52f67497
    Hint: Unversioned
    Value: unversioned
  Versions:
  - Version: 1
    Fields:
    - ID: 25bed78c-4957-4165-998a-ca1b52f67497
      Hint: __Created
      Value: 20140918T062658:635466184182719253
";

            using (var ms = new MemoryStream(Encoding.UTF8.GetBytes(yml)))
            {
                var item = formatter.ReadSerializedItem(ms, "unittest.yml");
                Assert.Equal("unittest.yml", item.SerializedItemId);
                Assert.Equal(new Guid("a4f985d9-98b3-4b52-aaaf-4344f6e747c6"), item.Id);
                Assert.Equal("master", item.DatabaseName);
                Assert.Equal(new Guid("001dd393-96c5-490b-924a-b0f25cd9efd8"), item.ParentId);
                Assert.Equal("/sitecore/content/test", item.Path);

                var shared = item.SharedFields.ToArray();

                // verify shared values
                shared.Length.Should().Be(1);
                shared[0].FieldId.Should().Be(new Guid("549fa670-79ab-4810-9450-aba0c06a2b87"));
                shared[0].Value.Should().Be("SHARED");
                shared[0].NameHint.Should().Be("Text Shared");

                var unversioned = item.UnversionedFields.ToArray();

                // verify unversioned values
                unversioned.Length.Should().Be(1);
                unversioned[0].Language.Should().Be(new CultureInfo("en"));

                var unversionedFields = unversioned.First().Fields.ToArray();

                // verify unversioned field values
                unversionedFields.Length.Should().Be(1);
                unversionedFields[0].NameHint.Should().Be("Unversioned");
                unversionedFields[0].FieldId.Should().Be(new Guid("ffffd78c-4957-4165-998a-ca1b52f67497"));
                unversionedFields[0].Value.Should().Be("unversioned");

                var versions = item.Versions.ToArray();

                // verify version values
                versions.Length.Should().Be(1);
                versions[0].VersionNumber.Should().Be(1);
                versions[0].Language.Should().Be(new CultureInfo("en"));

                var versionedFields = versions[0].Fields.ToArray();

                // verify versioned fields values
                versionedFields.Length.Should().Be(1);
                versionedFields[0].FieldId.Should().Be(new Guid("25bed78c-4957-4165-998a-ca1b52f67497"));
                versionedFields[0].NameHint.Should().Be("__Created");
                versionedFields[0].Value.Should().Be("20140918T062658:635466184182719253");
            }
        }
Exemple #17
0
 private YamlSerializationFormatterClone(
     IFieldFilter fieldFilter,
     YamlSerializationFormatter formatter)
     : base(fieldFilter, formatter.FieldFormatters.ToArray())
 {
 }
Exemple #18
0
 public static YamlSerializationFormatter Clone(this YamlSerializationFormatter obj)
 {
     return(YamlSerializationFormatterClone.Clone(obj));
 }
Exemple #19
0
        public void YamlFormatter_WritesItem_AsExpected()
        {
            var formatter = new YamlSerializationFormatter(null, null);

            var item = new FakeItem(
                id: new Guid("a4f985d9-98b3-4b52-aaaf-4344f6e747c6"),
                parentId: new Guid("001dd393-96c5-490b-924a-b0f25cd9efd8"),
                templateId: new Guid("007a464d-5b09-4d0e-8481-cb6a604a5948"),
                path: "/sitecore/content/test",
                sharedFields: new[]
            {
                new FakeFieldValue("SHARED", string.Empty, new Guid("549fa670-79ab-4810-9450-aba0c06a2b87"), "Text Shared")
            },
                name: "test",
                branchId: new Guid("25bed78c-4957-4165-998a-ca1b52f67497"),
                versions: new[]
            {
                new FakeItemVersion(1, "en", new FakeFieldValue("20140918T062658:635466184182719253", string.Empty, new Guid("25bed78c-4957-4165-998a-ca1b52f67497"), "__Created")),
            },
                unversionedFields: new[]
            {
                new ProxyItemLanguage(new CultureInfo("en"))
                {
                    Fields = new[] { new FakeFieldValue("unversioned", string.Empty, new Guid("ffffd78c-4957-4165-998a-ca1b52f67497"), "Unversioned") }
                }
            });

            var expectedYml = @"---
ID: a4f985d9-98b3-4b52-aaaf-4344f6e747c6
Parent: 001dd393-96c5-490b-924a-b0f25cd9efd8
Template: 007a464d-5b09-4d0e-8481-cb6a604a5948
Path: /sitecore/content/test
DB: master
BranchID: 25bed78c-4957-4165-998a-ca1b52f67497
SharedFields:
- ID: 549fa670-79ab-4810-9450-aba0c06a2b87
  Hint: Text Shared
  Value: SHARED
Languages:
- Language: en
  Fields:
  - ID: ffffd78c-4957-4165-998a-ca1b52f67497
    Hint: Unversioned
    Value: unversioned
  Versions:
  - Version: 1
    Fields:
    - ID: 25bed78c-4957-4165-998a-ca1b52f67497
      Hint: __Created
      Value: 20140918T062658:635466184182719253
";

            using (var ms = new MemoryStream())
            {
                formatter.WriteSerializedItem(item, ms);

                ms.Seek(0, SeekOrigin.Begin);

                using (var reader = new StreamReader(ms))
                {
                    var yml = reader.ReadToEnd();

                    Assert.Equal(expectedYml, yml);
                }
            }
        }
		public void YamlFormatter_WritesItem_WithFieldFormatter_AsExpected()
		{
			var xmlConfigNode = @"<serializationFormatter>
						<fieldFormatter type=""Rainbow.Formatting.FieldFormatters.MultilistFormatter, Rainbow"" />
					</serializationFormatter>";

			var configDoc = new XmlDocument();
			configDoc.LoadXml(xmlConfigNode);

			var formatter = new YamlSerializationFormatter(configDoc.DocumentElement, null);

			var item = new FakeItem(
				id: new Guid("a4f985d9-98b3-4b52-aaaf-4344f6e747c6"),
				parentId: new Guid("001dd393-96c5-490b-924a-b0f25cd9efd8"),
				templateId: new Guid("007a464d-5b09-4d0e-8481-cb6a604a5948"),
				path: "/sitecore/content/test",
				sharedFields: new[]
				{
					new FakeFieldValue("{35633C96-8494-4C05-A62A-954E2B401A4D}|{1486633B-3A5D-40D6-99D5-920FDFA63617}", "Multilist", new Guid("549fa670-79ab-4810-9450-aba0c06a2b87"), "Multilist Field")
				},
				name: "test");

			var expectedYml = @"---
ID: a4f985d9-98b3-4b52-aaaf-4344f6e747c6
Parent: 001dd393-96c5-490b-924a-b0f25cd9efd8
Template: 007a464d-5b09-4d0e-8481-cb6a604a5948
Path: /sitecore/content/test
DB: master
SharedFields:
- ID: 549fa670-79ab-4810-9450-aba0c06a2b87
  Hint: Multilist Field
  Type: Multilist
  Value: |
    {35633C96-8494-4C05-A62A-954E2B401A4D}
    {1486633B-3A5D-40D6-99D5-920FDFA63617}
";

			using (var ms = new MemoryStream())
			{
				formatter.WriteSerializedItem(item, ms);

				ms.Seek(0, SeekOrigin.Begin);

				using (var reader = new StreamReader(ms))
				{
					var yml = reader.ReadToEnd();

					Assert.Equal(expectedYml, yml);
				}
			}
		}
		public void YamlFormatter_WritesItem_AsExpected()
		{
			var formatter = new YamlSerializationFormatter(null, null);

			var item = new FakeItem(
				id: new Guid("a4f985d9-98b3-4b52-aaaf-4344f6e747c6"),
				parentId: new Guid("001dd393-96c5-490b-924a-b0f25cd9efd8"),
				templateId: new Guid("007a464d-5b09-4d0e-8481-cb6a604a5948"),
				path: "/sitecore/content/test",
				sharedFields: new[]
				{
					new FakeFieldValue("SHARED", string.Empty, new Guid("549fa670-79ab-4810-9450-aba0c06a2b87"), "Text Shared")
				},
				name: "test",
				branchId: new Guid("25bed78c-4957-4165-998a-ca1b52f67497"),
				versions: new[]
				{
					new FakeItemVersion(1, "en", new FakeFieldValue("20140918T062658:635466184182719253", string.Empty, new Guid("25bed78c-4957-4165-998a-ca1b52f67497"), "__Created")),
				},
				unversionedFields: new[]
				{
					new ProxyItemLanguage(new CultureInfo("en")) { Fields = new[] { new FakeFieldValue("unversioned", string.Empty, new Guid("ffffd78c-4957-4165-998a-ca1b52f67497"), "Unversioned") } }
				});

			var expectedYml = @"---
ID: a4f985d9-98b3-4b52-aaaf-4344f6e747c6
Parent: 001dd393-96c5-490b-924a-b0f25cd9efd8
Template: 007a464d-5b09-4d0e-8481-cb6a604a5948
Path: /sitecore/content/test
DB: master
BranchID: 25bed78c-4957-4165-998a-ca1b52f67497
SharedFields:
- ID: 549fa670-79ab-4810-9450-aba0c06a2b87
  Hint: Text Shared
  Value: SHARED
Languages:
- Language: en
  Fields:
  - ID: ffffd78c-4957-4165-998a-ca1b52f67497
    Hint: Unversioned
    Value: unversioned
  Versions:
  - Version: 1
    Fields:
    - ID: 25bed78c-4957-4165-998a-ca1b52f67497
      Hint: __Created
      Value: 20140918T062658:635466184182719253
";

			using (var ms = new MemoryStream())
			{
				formatter.WriteSerializedItem(item, ms);

				ms.Seek(0, SeekOrigin.Begin);

				using (var reader = new StreamReader(ms))
				{
					var yml = reader.ReadToEnd();

					Assert.Equal(expectedYml, yml);
				}
			}

		}
Exemple #22
0
        protected override List <SyncItem> GetAllItems(DirectoryInfo folder, string db, string[] includePaths)
        {
            var formatter = new YamlSerializationFormatter(null, null);

            List <SyncItem> result = new List <SyncItem>();

            foreach (FileInfo itemFile in folder.GetFiles("*.yml", SearchOption.AllDirectories))
            {
                using (StreamReader sr = new StreamReader(itemFile.FullName))
                {
                    try
                    {
                        var item = formatter.ReadSerializedItem(sr.BaseStream, itemFile.Name);
                        if (item == null || !includePaths.Any(p => item.Path.StartsWith(p)))
                        {
                            continue;
                        }

                        var syncItem = new SyncItem()
                        {
                            Name         = item.Name,
                            BranchId     = new ID(item.BranchId).ToString(),
                            DatabaseName = item.DatabaseName,
                            ID           = new ID(item.Id).ToString(),
                            ItemPath     = item.Path,
                            TemplateID   = new ID(item.TemplateId).ToString(),
                            ParentID     = new ID(item.ParentId).ToString()
                        };
                        foreach (var version in item.Versions)
                        {
                            var syncVersion = syncItem.AddVersion(version.Language.Name, version.VersionNumber.ToString(), version.VersionNumber.ToString());
                            foreach (var field in version.Fields)
                            {
                                syncVersion.AddField(new ID(field.FieldId).ToString(), field.NameHint, field.NameHint, field.Value, true);
                            }
                        }

                        foreach (var sharedField in item.SharedFields)
                        {
                            syncItem.AddSharedField(new ID(sharedField.FieldId).ToString(), sharedField.NameHint, sharedField.NameHint, sharedField.Value, true);
                        }

                        foreach (var unversionedField in item.UnversionedFields)
                        {
                            foreach (var version in syncItem.Versions.Where(v => v.Language == unversionedField.Language.Name))
                            {
                                foreach (var itemFieldValue in unversionedField.Fields)
                                {
                                    version.AddField(new ID(itemFieldValue.FieldId).ToString(), itemFieldValue.NameHint, itemFieldValue.NameHint, itemFieldValue.Value, true);
                                }
                            }
                        }

                        result.Add(syncItem);
                    }
                    catch (Exception ex)
                    {
                        throw new Exception(string.Format("Unable to deserialize '{0}'", itemFile.FullName), ex);
                    }
                }
            }
            return(result);
        }