Last active
November 13, 2015 19:03
-
-
Save nojaf/11346078 to your computer and use it in GitHub Desktop.
Umbraco extension generator ApiController
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Diagnostics; | |
using System.Linq; | |
using System.Net; | |
using System.Net.Http; | |
using System.Text; | |
using System.Web; | |
using System.Web.Http; | |
using Umbraco.Core.Models; | |
using Umbraco.Web.WebApi; | |
namespace MyApp.Controllers | |
{ | |
/// <summary> | |
/// Umbraco Web API Controller | |
/// This controller will be reachanble at | |
/// http://yoursite.co.uk/umbraco/api/YourControllerName | |
/// | |
/// Documentation | |
/// http://our.umbraco.org/documentation/Reference/WebApi/ | |
/// </summary> | |
public class AliasController : UmbracoApiController | |
{ | |
private const string File = "~/Models/MyAppExtensions.cs"; | |
private const string Namespace = "MyApp.Models"; | |
private const string Classname = "MyAppExtensions"; | |
[HttpGet] | |
public void Generate() | |
{ | |
StringBuilder sb = new StringBuilder(); | |
CreateBegin(sb); | |
CreateBody(sb); | |
CreateEnd(sb); | |
string fileLocation = HttpContext.Current.Server.MapPath(File); | |
System.IO.File.WriteAllText(fileLocation, sb.ToString()); | |
} | |
/// <summary> | |
/// Adds all the using statements, namespace and classname | |
/// </summary> | |
/// <param name="sb">Stringbuilder sb</param> | |
private void CreateBegin(StringBuilder sb) | |
{ | |
sb.AppendLine("using System;"); | |
sb.AppendLine("using System.Collections.Generic;"); | |
sb.AppendLine("using Umbraco.Core.Models;"); | |
sb.AppendLine("using Umbraco.Web;"); | |
sb.AppendLine("namespace " + Namespace); | |
sb.AppendLine("{"); | |
sb.AppendLine("\tpublic static partial class " + Classname); | |
sb.AppendLine("\t{"); | |
} | |
/// <summary> | |
/// Looks for all contentTypes and creates constants and extension methodes | |
/// </summary> | |
/// <param name="sb"></param> | |
private void CreateBody(StringBuilder sb) | |
{ | |
List<string> contentTypeAliases = new List<string>(); | |
List<string> propertyAliases = new List<string>(); | |
var contentTypes = Services.ContentTypeService.GetAllContentTypes(); | |
foreach (var contentType in contentTypes) | |
{ | |
if (!contentTypeAliases.Contains(contentType.Alias)) | |
{ | |
contentTypeAliases.Add(contentType.Alias); | |
} | |
foreach (var property in contentType.PropertyTypes) | |
{ | |
if (!propertyAliases.Contains(property.Alias)) | |
{ | |
propertyAliases.Add(property.Alias); | |
} | |
} | |
} | |
contentTypeAliases = contentTypeAliases.OrderBy(x => x).ToList(); | |
propertyAliases = propertyAliases.OrderBy(x => x).ToList(); | |
sb.AppendLine("\t\t#region ContentTypeAliases"); | |
CreateContentTypeAliasConstants(sb, contentTypeAliases); | |
sb.AppendLine("\t\t#endregion"); | |
sb.AppendLine(string.Empty); | |
sb.AppendLine("\t\t#region PropertyAliases"); | |
CreatePropertyAliasConstants(sb, propertyAliases); | |
sb.AppendLine("\t\t#endregion"); | |
sb.AppendLine(string.Empty); | |
sb.AppendLine("\t\t#region IPublishedContent GenericExtensions"); | |
CreateGenericIPublishedContentExtensions(sb, propertyAliases); | |
sb.AppendLine("\t\t#endregion"); | |
sb.AppendLine(string.Empty); | |
sb.AppendLine("\t\t#region IPublishedContent StringExtensions"); | |
CreateStringIPublishedContentExtensions(sb, propertyAliases); | |
sb.AppendLine("\t\t#endregion"); | |
sb.AppendLine(string.Empty); | |
sb.AppendLine("\t\t#region IContent GenericExtensions"); | |
CreateGenericIContentGetExtensions(sb, propertyAliases); | |
sb.AppendLine(string.Empty); | |
CreateGenericIContentSetExtensions(sb, propertyAliases); | |
sb.AppendLine("\t\t#endregion"); | |
sb.AppendLine(string.Empty); | |
sb.AppendLine("\t\t#region IContent StringExtensions"); | |
CreateStringIContentGetExtensions(sb, propertyAliases); | |
sb.AppendLine("\t\t#endregion"); | |
sb.AppendLine(string.Empty); | |
} | |
/// <summary> | |
/// GetPropertyAliasAsString generation | |
/// </summary> | |
/// <param name="sb">StringBuilder sb</param> | |
/// <param name="propertyAliases">All the aliases</param> | |
private void CreateStringIPublishedContentExtensions(StringBuilder sb, List<string> propertyAliases) | |
{ | |
foreach (var alias in propertyAliases) | |
{ | |
sb.AppendLine("\t\tpublic static string Get" + Capitalize(alias) + "AsString(this IPublishedContent publishedContent)"); | |
sb.AppendLine("\t\t{"); | |
sb.AppendLine("\t\t\tif(!publishedContent.HasProperty(" + Capitalize(alias) + "PropertyAlias))"); | |
sb.AppendLine("\t\t\t{"); | |
sb.AppendLine("\t\t\t\tthrow new Exception(\"the contentType doesn't contain a value for \" + " + Capitalize(alias) + "PropertyAlias);"); | |
sb.AppendLine("\t\t\t}"); | |
sb.AppendLine(string.Empty); | |
sb.AppendLine("\t\t\tif(publishedContent.HasValue("+ Capitalize(alias) + "PropertyAlias))"); | |
sb.AppendLine("\t\t\t{"); | |
sb.AppendLine("\t\t\t\treturn publishedContent.GetPropertyValue<string>("+ Capitalize(alias) + "PropertyAlias);"); | |
sb.AppendLine("\t\t\t}"); | |
sb.AppendLine(string.Empty); | |
sb.AppendLine("\t\t\treturn null;"); | |
sb.AppendLine("\t\t}"); | |
if (propertyAliases.Last() != alias) | |
{ | |
sb.AppendLine(string.Empty); | |
} | |
} | |
} | |
/// <summary> | |
/// GetPropertyAliasAs<T> generation | |
/// </summary> | |
/// <param name="sb">StringBuilder sb</param> | |
/// <param name="propertyAliases">All the aliases</param> | |
private void CreateGenericIPublishedContentExtensions(StringBuilder sb, List<string> propertyAliases) | |
{ | |
foreach (var alias in propertyAliases) | |
{ | |
sb.AppendLine("\t\tpublic static T Get" + Capitalize(alias) + "As<T>(this IPublishedContent publishedContent)"); | |
sb.AppendLine("\t\t{"); | |
sb.AppendLine("\t\t\tif(!publishedContent.HasProperty(" + Capitalize(alias) + "PropertyAlias))"); | |
sb.AppendLine("\t\t\t{"); | |
sb.AppendLine("\t\t\t\tthrow new Exception(\"the contentType doesn't contain a value for \" + " + Capitalize(alias) + "PropertyAlias);"); | |
sb.AppendLine("\t\t\t}"); | |
sb.AppendLine(string.Empty); | |
sb.AppendLine("\t\t\tif(publishedContent.HasValue("+Capitalize(alias) + "PropertyAlias))"); | |
sb.AppendLine("\t\t\t{"); | |
sb.AppendLine("\t\t\t\treturn publishedContent.GetPropertyValue<T>(" + Capitalize(alias) + "PropertyAlias);"); | |
sb.AppendLine("\t\t\t}"); | |
sb.AppendLine(string.Empty); | |
sb.AppendLine("\t\t\treturn default(T);"); | |
sb.AppendLine("\t\t}"); | |
if (propertyAliases.Last() != alias) | |
{ | |
sb.AppendLine(string.Empty); | |
} | |
} | |
} | |
private void CreateGenericIContentSetExtensions(StringBuilder sb, List<string> propertyAliases) | |
{ | |
foreach (var alias in propertyAliases) | |
{ | |
string constant = string.Concat(Capitalize(alias), "PropertyAlias"); | |
sb.AppendLine("\t\tpublic static void Set" + Capitalize(alias) + "(this IContent content, object value)"); | |
sb.AppendLine("\t\t{"); | |
sb.AppendLine("\t\t\tif(!content.HasProperty(" + constant + "))"); | |
sb.AppendLine("\t\t\t{"); | |
sb.AppendLine("\t\t\t\tthrow new Exception(\"the contentType doesn't contain a value for \" + " + constant+");"); | |
sb.AppendLine("\t\t\t}"); | |
sb.AppendLine(string.Empty); | |
sb.AppendLine("\t\t\tcontent.SetValue(" + constant + ", value);"); | |
sb.AppendLine("\t\t}"); | |
if (propertyAliases.Last() != alias) | |
{ | |
sb.AppendLine(string.Empty); | |
} | |
} | |
} | |
private void CreateGenericIContentGetExtensions(StringBuilder sb, List<string> propertyAliases) | |
{ | |
foreach (var alias in propertyAliases) | |
{ | |
string constant = string.Concat(Capitalize(alias), "PropertyAlias"); | |
sb.AppendLine("\t\tpublic static T Get" + Capitalize(alias) + "As<T>(this IContent content)"); | |
sb.AppendLine("\t\t{"); | |
sb.AppendLine("\t\t\tif(!content.HasProperty(" + constant + "))"); | |
sb.AppendLine("\t\t\t{"); | |
sb.AppendLine("\t\t\t\tthrow new Exception(\"the contentType doesn't contain a value for \" + " + constant + ");"); | |
sb.AppendLine("\t\t\t}"); | |
sb.AppendLine(string.Empty); | |
sb.AppendLine("\t\t\treturn content.GetValue<T>(" + constant + ");"); | |
sb.AppendLine("\t\t}"); | |
if (propertyAliases.Last() != alias) | |
{ | |
sb.AppendLine(string.Empty); | |
} | |
} | |
} | |
private void CreateStringIContentGetExtensions(StringBuilder sb, List<string> propertyAliases) | |
{ | |
foreach (var alias in propertyAliases) | |
{ | |
string constant = string.Concat(Capitalize(alias), "PropertyAlias"); | |
sb.AppendLine("\t\tpublic static string Get" + Capitalize(alias) + "AsString(this IContent content)"); | |
sb.AppendLine("\t\t{"); | |
sb.AppendLine("\t\t\tif(!content.HasProperty(" + constant + "))"); | |
sb.AppendLine("\t\t\t{"); | |
sb.AppendLine("\t\t\t\tthrow new Exception(\"the contentType doesn't contain a value for \" + " + constant + ");"); | |
sb.AppendLine("\t\t\t}"); | |
sb.AppendLine(string.Empty); | |
sb.AppendLine("\t\t\treturn content.GetValue<string>(" + constant + ");"); | |
sb.AppendLine("\t\t}"); | |
if (propertyAliases.Last() != alias) | |
{ | |
sb.AppendLine(string.Empty); | |
} | |
} | |
} | |
/// <summary> | |
/// Create const string for all properties | |
/// </summary> | |
/// <param name="sb">StringBuilder sb</param> | |
/// <param name="propertyAliases">All the aliases</param> | |
private void CreatePropertyAliasConstants(StringBuilder sb, List<string> propertyAliases) | |
{ | |
foreach (string alias in propertyAliases) | |
{ | |
sb.AppendLine("\t\tpublic const string " + Capitalize(alias) + "PropertyAlias = \"" + alias + "\";"); | |
} | |
} | |
/// <summary> | |
/// Create const string for all contentTypes | |
/// </summary> | |
/// <param name="sb">StringBuilder sb</param> | |
/// <param name="contentTypeAliases">All the contentType aliases</param> | |
private void CreateContentTypeAliasConstants(StringBuilder sb, List<string> contentTypeAliases) | |
{ | |
foreach (string alias in contentTypeAliases) | |
{ | |
sb.AppendLine("\t\tpublic const string " + Capitalize(alias) + "ContentTypeAlias = \"" + alias + "\";"); | |
} | |
} | |
/// <summary> | |
/// Wrap it up by adding closing brackets | |
/// </summary> | |
/// <param name="sb">StringBuilder sb</param> | |
private void CreateEnd(StringBuilder sb) | |
{ | |
sb.AppendLine("\t}"); | |
sb.AppendLine("}"); | |
} | |
/// <summary> | |
/// HelperMethod | |
/// </summary> | |
/// <param name="word"></param> | |
/// <returns></returns> | |
private string Capitalize(string word) | |
{ | |
if (!string.IsNullOrEmpty(word) && word.Length > 1) | |
{ | |
return word[0].ToString().ToUpper() + word.Substring(1, word.Length - 1); | |
} | |
return word; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment