Skip to content
This repository has been archived by the owner on Feb 28, 2022. It is now read-only.

Commit

Permalink
add step to generate service mapping toc
Browse files Browse the repository at this point in the history
  • Loading branch information
ansyral committed Jun 9, 2017
1 parent a8790f5 commit 33e02b7
Show file tree
Hide file tree
Showing 9 changed files with 247 additions and 1 deletion.
9 changes: 9 additions & 0 deletions src/Microsoft.Content.Build.Code2Yaml.Constants/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ public static class Constants
public const string OutputPath = "output_path";
public const string ExcludePaths = "exclude_paths";
public const string Language = "language";
public const string ServiceMapping = "service_mapping";
public const string GenerateTocMDFile = "generate_toc_md";
public const string Config = "config";
public const string IndexFileName = "index.xml";
Expand Down Expand Up @@ -36,5 +37,13 @@ public static class Doxyfile
public const string OUTPUT_DIRECTORY = "OUTPUT_DIRECTORY";
public const string EXCLUDE = "EXCLUDE";
}

public static class ServiceMappingConfig
{
public const string Mappings = "mappings";
public const string OutputPath = "service_mapping_output_path";
public const string Service = "service";
public const string Category = "category";
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,8 @@ public class ConfigModel

[JsonProperty(Constants.ExcludePaths)]
public List<string> ExcludePaths { get; set; }

[JsonProperty(Constants.ServiceMapping)]
public ServiceMappingConfig ServiceMappingConfig { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
namespace Microsoft.Content.Build.Code2Yaml.DataContracts
{
using System;
using System.Collections.Generic;

using Microsoft.Content.Build.Code2Yaml.Constants;

using Newtonsoft.Json;

public class ServiceMappingConfig
{
[JsonProperty(Constants.ServiceMappingConfig.Mappings)]
public Dictionary<string, ServiceCategory> Mappings { get; set; }

[JsonProperty(Constants.ServiceMappingConfig.OutputPath)]
public string OutputPath { get; set; }
}

public class ServiceCategory : IEquatable<ServiceCategory>
{
[JsonProperty(Constants.ServiceMappingConfig.Service)]
public string Service { get; set; }

[JsonProperty(Constants.ServiceMappingConfig.Category)]
public string Category { get; set; }

public bool Equals(ServiceCategory other)
{
if (other == null)
{
return false;
}
return Service == other.Service &&
Category == other.Category;
}

public override bool Equals(object obj)
{
return Equals(obj as ServiceCategory);
}

public override int GetHashCode()
{
return Service.GetHashCode() ^ Category.GetHashCode();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,11 @@
</ItemGroup>
<ItemGroup>
<Compile Include="ConfigModel\ConfigModel.cs" />
<Compile Include="ConfigModel\ServiceMappingConfig.cs" />
<Compile Include="Hierarchy\HierarchyChange.cs" />
<Compile Include="Hierarchy\HierarchyType.cs" />
<Compile Include="ServiceMapping\ServiceMapping.cs" />
<Compile Include="ServiceMapping\ServiceMappingItem.cs" />
<Compile Include="ViewModel\ApiParameter.cs" />
<Compile Include="ViewModel\ArticleItemYaml.cs" />
<Compile Include="ViewModel\CrefInfo.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace Microsoft.Content.Build.Code2Yaml.DataContracts
{
using System.Collections.Generic;

public class ServiceMapping : List<ServiceMappingItem>
{
public ServiceMapping(IEnumerable<ServiceMappingItem> items) : base(items)
{
}

public ServiceMapping() : base()
{
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace Microsoft.Content.Build.Code2Yaml.DataContracts
{
using System.Collections.Generic;

public class ServiceMappingItem
{
public string name { get; set; }
public string uid { get; set; }
public string href { get; set; }
public string landingPageType { get; set; }
public List<string> children { get; set; }
public ServiceMapping items { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
namespace Microsoft.Content.Build.Code2Yaml.Steps
{
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;

using Microsoft.Content.Build.Code2Yaml.Common;
using Microsoft.Content.Build.Code2Yaml.Constants;
using Microsoft.Content.Build.Code2Yaml.DataContracts;
using DocAsCode.YamlSerialization;

public class GenerateServiceMappingFile : IStep
{
public string StepName
{
get
{
return "GenerateServiceMappingFile";
}
}

public Task RunAsync(BuildContext context)
{
var config = context.GetSharedObject(Constants.Config) as ConfigModel;
if (config == null)
{
throw new ApplicationException(string.Format("Key: {0} doesn't exist in build context", Constants.Config));
}
var mappingConfig = config.ServiceMappingConfig;
if (mappingConfig == null)
{
return Task.FromResult(0);
}
string outputPath = mappingConfig.OutputPath;
var articlesDict = context.GetSharedObject(Constants.ArticleItemYamlDict) as ConcurrentDictionary<string, ArticleItemYaml>;

var newservices = (from article in articlesDict.Values
where article.Type == MemberType.Namespace
let serviceCategory = Find(mappingConfig.Mappings, FormatPath(article.Source.Path))
where serviceCategory != null
group article.Uid by serviceCategory into g
select g
).ToDictionary(g => g.Key, g => g.ToList());

List<ServiceMappingItem> others = new List<ServiceMappingItem>();
if (File.Exists(outputPath))
{
using (var reader = new StreamReader(outputPath))
{
var oldMapping = new YamlDeserializer().Deserialize<ServiceMapping>(reader);
var oldservices = (from m in oldMapping[0].items
from sm in m.items ?? Enumerable.Empty<ServiceMappingItem>()
select new { SM = sm, Name = m.name }
).ToDictionary(i => new ServiceCategory { Service = i.Name, Category = i.SM.name }, i => i.SM.children.ToList());
Merge(newservices, oldservices);
var other = oldMapping[0].items.SingleOrDefault(i => i.name == "Other");
if (other != null)
{
others.Add(other);
}
}

}
var services = (from item in newservices
group item by item.Key.Service into g
select new
{
Service = g.Key,
Items = (from v in g
group v.Value by v.Key.Category into g0
select new
{
Category = g0.Key,
Uids = g0.SelectMany(i => i).OrderBy(i => i).Distinct().ToList()
}).ToList(),
}).ToDictionary(p => p.Service, p => p.Items);

var mapping = new ServiceMapping()
{
new ServiceMappingItem()
{
uid = "landingpage.reference",
name = "Reference",
landingPageType = "Root",
items = new ServiceMapping((from pair in services
let service = pair.Key
select new ServiceMappingItem()
{
name = service,
href = "~/docs-ref-services/overview/azure/" + service + ".md",
items = new ServiceMapping(from item in pair.Value
let category = item.Category
select new ServiceMappingItem()
{
name = item.Category,
uid = "landingpage.services." + service + "." + category,
landingPageType = "Service",
children = item.Uids.ToList()
})
}).OrderBy(s => s.name))
}
};
mapping[0].items.AddRange(others);
using (var writer = new StreamWriter(outputPath))
{
new YamlSerializer().Serialize(writer, mapping);
}
return Task.FromResult(0);
}

private ServiceCategory Find(Dictionary<string, ServiceCategory> mappings, string path)
{
foreach (var mapping in mappings)
{
if (path.StartsWith(GetPathFromSrcRepository(mapping.Key)))
{
return mapping.Value;
}
}
return null;
}

private static string GetPathFromSrcRepository(string path)
{
var formatted = FormatPath(path);
var parts = formatted.Split('/');
return string.Join("/", parts.Skip(2));
}

private static string FormatPath(string path)
{
if (string.IsNullOrEmpty(path)) return null;
return path.Replace('\\', '/');
}

private static void Merge(Dictionary<ServiceCategory, List<string>> a, Dictionary<ServiceCategory, List<string>> b)
{
foreach (var pair in b)
{
List<string> value;
if (!a.TryGetValue(pair.Key, out value))
{
a[pair.Key] = new List<string>();
}
a[pair.Key].AddRange(pair.Value);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="GenerateArticles.cs" />
<Compile Include="GenerateServiceMappingFile.cs" />
<Compile Include="GenerateToc.cs" />
<Compile Include="IStep.cs" />
<Compile Include="PreprocessXml.cs" />
Expand Down
4 changes: 3 additions & 1 deletion src/code2yaml/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ static void Main(string[] args)
new List<IStep>
{
new GenerateToc { NameGenerator = NameGeneratorFactory.Create(_config.Language) },
new GenerateArticles { Generator = ArticleGeneratorFactory.Create(_config.Language) },
new StepCollection(
new GenerateArticles { Generator = ArticleGeneratorFactory.Create(_config.Language) },
new GenerateServiceMappingFile()),
}));
string status = "Failed";
var watch = Stopwatch.StartNew();
Expand Down

0 comments on commit 33e02b7

Please sign in to comment.