diff --git a/src/Microsoft.Content.Build.Code2Yaml.Steps/GenerateServiceMappingFile.cs b/src/Microsoft.Content.Build.Code2Yaml.Steps/GenerateServiceMappingFile.cs index 160a695..3b57949 100644 --- a/src/Microsoft.Content.Build.Code2Yaml.Steps/GenerateServiceMappingFile.cs +++ b/src/Microsoft.Content.Build.Code2Yaml.Steps/GenerateServiceMappingFile.cs @@ -47,24 +47,35 @@ group article.Uid by serviceCategory into g select g ).ToDictionary(g => g.Key, g => g.ToList()); - List others = new List(); - Dictionary serviceHrefMapping = new Dictionary(); + ServiceMappingItem other = new ServiceMappingItem + { + name = "Other", + landingPageType = LandingPageTypeService, + uid = "azure.java.sdk.landingpage.services.other", + children = new List { "*" }, + }; + Dictionary hrefMapping = new Dictionary(); if (File.Exists(outputPath)) { using (var reader = new StreamReader(outputPath)) { var oldMapping = new YamlDeserializer().Deserialize(reader); - var oldservices = (from m in oldMapping[0].items - from sm in m.items ?? Enumerable.Empty() - select new { SM = sm, Name = m.name } - ).ToDictionary(i => new ServiceCategory { Service = i.Name, Category = i.SM.name }, i => i.SM.children?.ToList() ?? new List()); - Merge(newservices, oldservices); - var other = oldMapping[0].items.SingleOrDefault(i => i.name == "Other"); - if (other != null) + foreach (var m in oldMapping[0].items) { - others.Add(other); + if (m.name == "Other") + { + other = m; + continue; + } + hrefMapping[m.name] = m.href; + foreach (var c in m.items ?? Enumerable.Empty()) + { + var sc = new ServiceCategory { Service = m.name, Category = c.name }; + Merge(newservices, sc, c.children?.ToList() ?? new List()); + hrefMapping[GetKey(m.name, c.name)] = c.href; + } } - serviceHrefMapping = oldMapping[0].items.ToDictionary(i => i.name, i => i.href); + } } @@ -86,31 +97,34 @@ group v.Value by v.Key.Category into g0 { new ServiceMappingItem() { - uid = "landingpage.reference", + uid = "azure.java.sdk.landingpage.reference", name = "Reference", landingPageType = "Root", items = new ServiceMapping((from pair in services let service = pair.Key - let hrefAndType = GetServiceHrefAndType(serviceHrefMapping, service) + let hrefAndType = GetHrefAndType(hrefMapping, service) select new ServiceMappingItem() { name = service, href = hrefAndType.Item1, landingPageType = hrefAndType.Item2, - uid = "landingpage.services." + service, + uid = "azure.java.sdk.landingpage.services." + service, items = new ServiceMapping(from item in pair.Value let category = item.Category + let chrefAndType = GetHrefAndType(hrefMapping, GetKey(service, category)) select new ServiceMappingItem() { name = item.Category, - uid = "landingpage.services." + service + "." + category, - landingPageType = LandingPageTypeService, + href = chrefAndType.Item1, + landingPageType = chrefAndType.Item2, + uid = "azure.java.sdk.landingpage.services." + service + "." + category, children = item.Uids.ToList() }) }).OrderBy(s => s.name)) } }; - mapping[0].items.AddRange(others); + mapping[0].items.Add(other); + using (var writer = new StreamWriter(outputPath)) { new YamlSerializer().Serialize(writer, mapping); @@ -143,24 +157,21 @@ private static string FormatPath(string path) return path.Replace('\\', '/'); } - private static void Merge(Dictionary> a, Dictionary> b) + private static void Merge(Dictionary> a, ServiceCategory sc, List uids) { - foreach (var pair in b) + List value; + if (!a.TryGetValue(sc, out value)) { - List value; - if (!a.TryGetValue(pair.Key, out value)) - { - a[pair.Key] = new List(); - } - // to-do: when product repo's mapping file is ready, should change the behavior to overwrite. - a[pair.Key].AddRange(pair.Value); + a[sc] = new List(); } + // to-do: when product repo's mapping file is ready, should change the behavior to overwrite. + a[sc].AddRange(uids); } - private static Tuple GetServiceHrefAndType(Dictionary mapping, string service) + private static Tuple GetHrefAndType(Dictionary mapping, string key) { string href; - if (mapping.TryGetValue(service, out href)) + if (mapping.TryGetValue(key, out href) && !string.IsNullOrEmpty(href)) { return Tuple.Create(href, null); } @@ -169,5 +180,10 @@ private static Tuple GetServiceHrefAndType(Dictionary(null, LandingPageTypeService); } } + + private static string GetKey(string service, string category) + { + return service + "." + category; + } } }