79 lines
2.6 KiB
C#
79 lines
2.6 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel.DataAnnotations;
|
|
using System.Linq;
|
|
using System.Reflection;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace MontoyaTech.Rest.Net
|
|
{
|
|
/// <summary>
|
|
/// A class that can take a set of routes and generate code
|
|
/// for a client that can be used to interact with them.
|
|
/// </summary>
|
|
public class CodeGenerator
|
|
{
|
|
/// <summary>
|
|
/// Generates a CSharp client for a given set of routes.
|
|
/// </summary>
|
|
/// <param name="routes"></param>
|
|
/// <returns></returns>
|
|
public static string GenerateCSharpClient(IList<Route> routes)
|
|
{
|
|
var writer = new CodeWriter();
|
|
|
|
writer.WriteLine("public class Client").WriteLine("{").Indent();
|
|
writer.WriteSpacer();
|
|
writer.WriteLine("public string BaseUrl = null;");
|
|
writer.WriteSpacer();
|
|
writer.WriteLine("public Client(string baseUrl)").WriteLine("{").Indent();
|
|
writer.WriteLine("this.BaseUrl = baseUrl;");
|
|
writer.Outdent().WriteLine("}");
|
|
|
|
GenerateCSharpRouteClasses(routes, writer);
|
|
|
|
writer.Outdent().WriteLine("}");
|
|
|
|
return writer.ToString();
|
|
}
|
|
|
|
private static void GenerateCSharpRouteClasses(IList<Route> routes, CodeWriter writer)
|
|
{
|
|
var groups = new Dictionary<string, List<Route>>();
|
|
|
|
//Go through all the routes and group them.
|
|
foreach (var route in routes)
|
|
{
|
|
//Get the method that this route is tied to.
|
|
var methodInfo = route.Target.GetMethodInfo();
|
|
|
|
//See if this method defines the group for us.
|
|
var routeGroup = methodInfo.GetCustomAttribute<RouteGroup>();
|
|
|
|
//Group this route.
|
|
string group = (routeGroup != null ? routeGroup.Name : methodInfo.DeclaringType.Name);
|
|
|
|
if (groups.ContainsKey(group))
|
|
groups[group].Add(route);
|
|
else
|
|
groups.Add(group, new List<Route>() { route });
|
|
}
|
|
}
|
|
|
|
private static void GenerateCSharpRouteClass(string name, List<Route> routes, CodeWriter writer)
|
|
{
|
|
}
|
|
|
|
/// <summary>
|
|
/// Generates a Javascript client for a given set of routes.
|
|
/// </summary>
|
|
/// <param name="routes"></param>
|
|
/// <returns></returns>
|
|
public static string GenerateJavascriptClient(IList<Route> routes)
|
|
{
|
|
return null;
|
|
}
|
|
}
|
|
}
|