Skip to content

Commit

Permalink
Cleaning up LCG code to minimum
Browse files Browse the repository at this point in the history
  • Loading branch information
rappen committed Jan 28, 2023
1 parent 57a4329 commit af01381
Show file tree
Hide file tree
Showing 6 changed files with 9 additions and 160 deletions.
21 changes: 2 additions & 19 deletions FetchXmlBuilder/Converters/LCG/CommonSettings.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System.Collections.Generic;

namespace Rappen.XTB.LCG
namespace Rappen.XTB.LCG
{
public class CommonSettings
{
Expand All @@ -12,20 +10,5 @@ public CommonSettings()
internal string InlineConfigEnd = @"\***** LCG-configuration-END *****/";
public string[] CamelCaseWords { get; set; } = new string[] { "parent", "customer", "owner", "state", "status", "name", "phone", "address", "code", "postal", "mail", "modified", "created", "permission", "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", "team" };
public string[] CamelCaseWordEnds { get; set; } = new string[] { "id" };
public string[] InternalAttributes { get; set; } = new string[] { "importsequencenumber", "owneridname", "owneridtype", "owneridyominame", "createdbyname", "createdbyyominame", "createdonbehalfby", "createdonbehalfbyname", "createdonbehalfbyyominame", "modifiedbyname", "modifiedbyyominame", "modifiedonbehalfby", "modifiedonbehalfbyname", "modifiedonbehalfbyyominame", "overriddencreatedon", "owningbusinessunit", "owningteam", "owninguser", "regardingobjectidname", "regardingobjectidyominame", "regardingobjecttypecode", "timezoneruleversionnumber", "transactioncurrencyidname", "utcconversiontimezonecode", "versionnumber" };
}
}

/*
FileContainer
FileHeader
DataContainer
EntityContainer
EntityDetails
Attributes
Relationships
OptionSets
OptionSetValues
*/
}
110 changes: 0 additions & 110 deletions FetchXmlBuilder/Converters/LCG/Extensions.cs
Original file line number Diff line number Diff line change
@@ -1,96 +1,10 @@
using Microsoft.Xrm.Sdk.Metadata;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Rappen.XTB.LCG
{
public static class Extensions
{
public static AttributeMetadata GetAttribute(this Dictionary<string, EntityMetadata> entities, string entity, string attribute)
{
if (entities == null
|| !entities.TryGetValue(entity, out var metadata)
|| metadata.Attributes == null)
{
return null;
}

return metadata.Attributes.FirstOrDefault(metaattribute => metaattribute.LogicalName == attribute);
}

private static string BeautifyContent(this string content, string indentstr)
{
var fixedcontent = new StringBuilder();
var lines = content.Split('\n').ToList();
var lastline = string.Empty;
var indent = 0;
foreach (var line in lines.Select(l => l.Trim()).Where(l => !string.IsNullOrEmpty(l)))
{
if (AddBlankLineBetween(lastline, line))
{
fixedcontent.AppendLine();
}
if (lastline.EndsWith("{"))
{
indent++;
}
if (line.Equals("}") && indent > 0)
{
indent--;
}
fixedcontent.AppendLine(string.Concat(Enumerable.Repeat(indentstr, indent)) + line);
lastline = line;
}
return fixedcontent.ToString();
}

private static bool AddBlankLineBetween(string lastline, string line)
{
if (string.IsNullOrWhiteSpace(lastline) || lastline.Equals("{"))
{ // Never two empty lines after each other
return false;
}
if (lastline.StartsWith("#region") || line.StartsWith("#region") || line.StartsWith("#endregion"))
{ // Empty lines around region statements
return true;
}
if (lastline.StartsWith("using ") && !line.StartsWith("using "))
{ // Empty lines after usings
return true;
}
if (line.StartsWith("namespace "))
{ // Empty lines before namespace
return true;
}
if (line.StartsWith("public enum"))
{ // Never empty line before enums, we keep it compact
return false;
}
if (lastline.Equals("}") && !line.Equals("}") && !string.IsNullOrWhiteSpace(line))
{ // Never empty line between end blocks
return true;
}
// Following rules are UML specific
if (line.StartsWith("@startuml") || lastline.StartsWith("@startuml") || line.StartsWith("@enduml"))
{
return true;
}
if (line.StartsWith("title") || line.StartsWith("header") || line.StartsWith("footer "))
{
return true;
}
if (line.StartsWith("skinparam") && !lastline.StartsWith("skinparam"))
{
return true;
}
if (line.StartsWith("entity "))
{
return true;
}
return false;
}

public static string CamelCaseIt(this string name, Settings settings)
{
if (!settings.ConstantCamelCased || settings.ConstantName == NameType.DisplayName)
Expand Down Expand Up @@ -153,30 +67,6 @@ bool WordBeginOrEnd(string text, int i)
return result;
}

public static string GetNonDisplayName(this Settings settings, string name)
{
if (settings.DoStripPrefix && !string.IsNullOrEmpty(settings.StripPrefix))
{
foreach (var prefix in settings.StripPrefix.Split(',')
.Select(p => p.Trim())
.Where(p => !string.IsNullOrWhiteSpace(p)
&& name.ToLowerInvariant().StartsWith(p)))
{
name = name.Substring(prefix.Length);
}
}
if (settings.ConstantCamelCased)
{
name = name.CamelCaseIt(settings);
}
return name;
}

public static string ReplaceIfNotEmpty(this string template, string oldValue, string newValue)
{
return string.IsNullOrEmpty(template) ? newValue : template.Replace(oldValue, newValue);
}

public static string StripPrefix(this string name, Settings settings)
{
if (!settings.DoStripPrefix || string.IsNullOrEmpty(settings.StripPrefix) || settings.ConstantName == NameType.DisplayName)
Expand Down
8 changes: 0 additions & 8 deletions FetchXmlBuilder/Converters/LCG/LCG.cs

This file was deleted.

5 changes: 1 addition & 4 deletions FetchXmlBuilder/Converters/LCG/Settings.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using System.Collections.Generic;
using System.IO;

namespace Rappen.XTB.LCG
namespace Rappen.XTB.LCG
{
public class Settings
{
Expand Down
24 changes: 6 additions & 18 deletions FetchXmlBuilder/Converters/LCG/UnicodeCharacterUtilities.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
// Stolen with love from
// Stolen with love from
// https://github.com/dotnet/roslyn/blob/master/src/Compilers/Core/Portable/InternalUtilities/UnicodeCharacterUtilities.cs

// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Linq;

Expand Down Expand Up @@ -148,7 +147,7 @@ public static string MakeValidIdentifier(string name, bool checkkeywords)
private static bool IsLetterChar(UnicodeCategory cat)
{
// letter-character:
// A Unicode character of classes Lu, Ll, Lt, Lm, Lo, or Nl
// A Unicode character of classes Lu, Ll, Lt, Lm, Lo, or Nl
// A Unicode-escape-sequence representing a character of classes Lu, Ll, Lt, Lm, Lo, or Nl

switch (cat)
Expand All @@ -168,7 +167,7 @@ private static bool IsLetterChar(UnicodeCategory cat)
private static bool IsCombiningChar(UnicodeCategory cat)
{
// combining-character:
// A Unicode character of classes Mn or Mc
// A Unicode character of classes Mn or Mc
// A Unicode-escape-sequence representing a character of classes Mn or Mc

switch (cat)
Expand All @@ -184,39 +183,28 @@ private static bool IsCombiningChar(UnicodeCategory cat)
private static bool IsDecimalDigitChar(UnicodeCategory cat)
{
// decimal-digit-character:
// A Unicode character of the class Nd
// A Unicode character of the class Nd
// A unicode-escape-sequence representing a character of the class Nd

return cat == UnicodeCategory.DecimalDigitNumber;
}

private static bool IsConnectingChar(UnicodeCategory cat)
{
// connecting-character:
// connecting-character:
// A Unicode character of the class Pc
// A unicode-escape-sequence representing a character of the class Pc

return cat == UnicodeCategory.ConnectorPunctuation;
}

/// <summary>
/// Returns true if the Unicode character is a formatting character (Unicode class Cf).
/// </summary>
/// <param name="ch">The Unicode character.</param>
internal static bool IsFormattingChar(char ch)
{
// There are no FormattingChars in ASCII range

return ch > 127 && IsFormattingChar(CharUnicodeInfo.GetUnicodeCategory(ch));
}

/// <summary>
/// Returns true if the Unicode character is a formatting character (Unicode class Cf).
/// </summary>
/// <param name="cat">The Unicode character.</param>
private static bool IsFormattingChar(UnicodeCategory cat)
{
// formatting-character:
// formatting-character:
// A Unicode character of the class Cf
// A unicode-escape-sequence representing a character of the class Cf

Expand Down
1 change: 0 additions & 1 deletion FetchXmlBuilder/FetchXmlBuilder.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,6 @@
<Compile Include="Converters\LCG\ConfigurationUtils.cs" />
<Compile Include="Converters\LCG\Extensions.cs" />
<Compile Include="Converters\LCG\FabienDehopreIdentifier.cs" />
<Compile Include="Converters\LCG\LCG.cs" />
<Compile Include="Converters\LCG\Settings.cs" />
<Compile Include="Converters\LCG\UnicodeCharacterUtilities.cs" />
<Compile Include="Converters\QExParse.cs" />
Expand Down

0 comments on commit af01381

Please sign in to comment.