Skip to content

Commit

Permalink
Closed #29 Exposing class templates in common settings file
Browse files Browse the repository at this point in the history
  • Loading branch information
rappen committed May 1, 2019
1 parent d1b3adf commit cf5ff54
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 30 deletions.
26 changes: 2 additions & 24 deletions LateboundConstantGenerator/CSharpUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,32 +10,10 @@ namespace Rappen.XTB.LCG
{
public class CSharpUtils
{
#region Templates

public struct Template
{
public const string IndentStr = " ";
public const string Header1 = @"// *********************************************************************
// Created by: Latebound Constant Generator {version} for XrmToolBox
// Author : Jonas Rapp http://twitter.com/rappen
// Repo : https://github.com/rappen/LateboundConstantGenerator
// Source Org: {organization}";

public const string Header2 = "// *********************************************************************";
public const string Namespace = "namespace {namespace}\n{\n{entities}\n}";
public const string Class = "public static class {classname}\n{\n{entity}\n{attributes}\n{relationships}\n{optionsets}\n}";
public const string Entity = "public const string LogicalName = '{logicalname}';\npublic const string LogicalCollectionName = '{logicalcollectionname}';";
public const string Attribute = "public const string {attribute} = '{logicalname}';";
public const string Relationship = "public const string {relationship} = '{schemaname}';";
public const string OptionSet = "public enum {name}\n{\n{values}\n}";
public const string OptionSetValue = "{name} = {value}";
public const string Region = "#region {region}\n{content}\n#endregion {region}";
}

#endregion Templates

private static Template Template;
public static string GenerateClasses(List<EntityMetadataProxy> entitiesmetadata, Settings settings, IConstantFileWriter fileWriter)
{
Template = settings.commonsettings.Template;
var selectedentities = entitiesmetadata.Where(e => e.Selected).ToList();
var commonentity = GetCommonEntity(selectedentities, settings);
if (commonentity != null)
Expand Down
25 changes: 24 additions & 1 deletion LateboundConstantGenerator/CommonSettings.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
namespace Rappen.XTB.LCG
using System.Collections.Generic;
using System.Linq;

namespace Rappen.XTB.LCG
{
public class CommonSettings
{
Expand All @@ -18,5 +21,25 @@ public class CommonSettings
public bool HeaderLocalPath { get; set; } = true;
public string CamelCaseWords { get; set; } = "parent, customer, owner, state, status, name, phone, address, code, postal, mail, modified, created, type, method, verson, number, first, last, middle, contact, account, system, user, fullname, preferred, processing, annual, plugin, step, key, details, message, description, constructor, execution, secure, configuration, behalf, count, percent, internal, external, trace, entity, primary, secondary, lastused, credit, credited, donot, exchange, import, invoke, invoked, private, market, marketing, revenue, business, price, level, pricelevel, territory, version, conversion, workorder";
public string CamelCaseWordEnds { get; set; } = "id";
public Template Template { get; set; } = new Template();
}

public class Template
{
internal string IndentStr = " ";
internal string Header1 = @"// *********************************************************************
// Created by: Latebound Constant Generator {version} for XrmToolBox
// Author : Jonas Rapp http://twitter.com/rappen
// Repo : https://github.com/rappen/LateboundConstantGenerator
// Source Org: {organization}";
internal string Header2 = "// *********************************************************************";
internal string Namespace = "namespace {namespace}\n{\n{entities}\n}";
public string Class { get; set; } = "public static class {classname}\n{\n{entity}\n{attributes}\n{relationships}\n{optionsets}\n}";
public string Entity { get; set; } = "public const string LogicalName = '{logicalname}';\npublic const string LogicalCollectionName = '{logicalcollectionname}';";
public string Attribute { get; set; } = "public const string {attribute} = '{logicalname}';";
public string Relationship { get; set; } = "public const string {relationship} = '{schemaname}';";
public string OptionSet { get; set; } = "public enum {name}\n{\n{values}\n}";
public string OptionSetValue { get; set; } = "{name} = {value}";
public string Region { get; set; } = "#region {region}\n{content}\n#endregion {region}";
}
}
10 changes: 5 additions & 5 deletions LateboundConstantGenerator/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ public static AttributeMetadata GetAttribute(this Dictionary<string, EntityMetad

public static bool WriteFile(this string content, string filename, string orgurl, Settings settings)
{
content = content.BeautifyContent();
var header = CSharpUtils.Template.Header1
content = content.BeautifyContent(settings.commonsettings.Template.IndentStr);
var header = settings.commonsettings.Template.Header1
.Replace("{version}", Assembly.GetExecutingAssembly().GetName().Version.ToString())
.Replace("{organization}", orgurl);
if (settings.commonsettings.HeaderLocalPath)
Expand All @@ -38,7 +38,7 @@ public static bool WriteFile(this string content, string filename, string orgurl
{
header += "\r\n// Created : " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
}
header += "\r\n" + CSharpUtils.Template.Header2;
header += "\r\n" + settings.commonsettings.Template.Header2;
try
{
File.WriteAllText(filename, header + "\r\n\r\n" + content);
Expand All @@ -51,7 +51,7 @@ public static bool WriteFile(this string content, string filename, string orgurl
}
}

public static string BeautifyContent(this string content)
public static string BeautifyContent(this string content, string indentstr)
{
var fixedcontent = new StringBuilder();
var lines = content.Split('\n').ToList();
Expand All @@ -71,7 +71,7 @@ public static string BeautifyContent(this string content)
{
indent--;
}
fixedcontent.AppendLine(string.Concat(Enumerable.Repeat(CSharpUtils.Template.IndentStr, indent)) + line);
fixedcontent.AppendLine(string.Concat(Enumerable.Repeat(indentstr, indent)) + line);
lastline = line;
}
return fixedcontent.ToString();
Expand Down

0 comments on commit cf5ff54

Please sign in to comment.