Browse Source

Merge pull request #4633 from abpframework/maliming/cli-snake-case

Rename the text "MY_PROJECT_NAME" when generating a new project
pull/4728/head
Halil İbrahim Kalkan 6 years ago
committed by GitHub
parent
commit
e281c362d1
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 3
      framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/Building/Steps/SolutionRenameStep.cs
  2. 72
      framework/src/Volo.Abp.Core/System/AbpStringExtensions.cs
  3. 12
      framework/test/Volo.Abp.Core.Tests/System/StringExtensions_Tests.cs

3
framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/Building/Steps/SolutionRenameStep.cs

@ -64,7 +64,8 @@ namespace Volo.Abp.Cli.ProjectBuilding.Building.Steps
RenameHelper.RenameAll(_entries, _projectNamePlaceHolder, _projectName);
RenameHelper.RenameAll(_entries, _projectNamePlaceHolder.ToCamelCase(), _projectName.ToCamelCase());
RenameHelper.RenameAll(_entries, _projectNamePlaceHolder.ToKebabCase(), _projectName.ToKebabCase());
RenameHelper.RenameAll(_entries, _projectNamePlaceHolder.ToSnakeCase().ToUpper(), _projectName.ToSnakeCase().ToUpper());
}
}
}
}
}

72
framework/src/Volo.Abp.Core/System/AbpStringExtensions.cs

@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Globalization;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
@ -312,6 +313,75 @@ namespace System
: Regex.Replace(str, "[a-z][A-Z]", m => m.Value[0] + "-" + char.ToLowerInvariant(m.Value[1]));
}
/// <summary>
/// Converts given PascalCase/camelCase string to snake case.
/// Example: "ThisIsSampleSentence" is converted to "this_is_a_sample_sentence".
/// https://github.com/npgsql/npgsql/blob/dev/src/Npgsql/NameTranslation/NpgsqlSnakeCaseNameTranslator.cs#L51
/// </summary>
/// <param name="str">String to convert.</param>
/// <returns></returns>
public static string ToSnakeCase(this string str)
{
if (string.IsNullOrWhiteSpace(str))
{
return str;
}
var builder = new StringBuilder(str.Length + Math.Min(2, str.Length / 5));
var previousCategory = default(UnicodeCategory?);
for (var currentIndex = 0; currentIndex < str.Length; currentIndex++)
{
var currentChar = str[currentIndex];
if (currentChar == '_')
{
builder.Append('_');
previousCategory = null;
continue;
}
var currentCategory = char.GetUnicodeCategory(currentChar);
switch (currentCategory)
{
case UnicodeCategory.UppercaseLetter:
case UnicodeCategory.TitlecaseLetter:
if (previousCategory == UnicodeCategory.SpaceSeparator ||
previousCategory == UnicodeCategory.LowercaseLetter ||
previousCategory != UnicodeCategory.DecimalDigitNumber &&
previousCategory != null &&
currentIndex > 0 &&
currentIndex + 1 < str.Length &&
char.IsLower(str[currentIndex + 1]))
{
builder.Append('_');
}
currentChar = char.ToLower(currentChar);
break;
case UnicodeCategory.LowercaseLetter:
case UnicodeCategory.DecimalDigitNumber:
if (previousCategory == UnicodeCategory.SpaceSeparator)
{
builder.Append('_');
}
break;
default:
if (previousCategory != null)
{
previousCategory = UnicodeCategory.SpaceSeparator;
}
continue;
}
builder.Append(currentChar);
previousCategory = currentCategory;
}
return builder.ToString();
}
/// <summary>
/// Converts string to enum value.
/// </summary>
@ -476,4 +546,4 @@ namespace System
return encoding.GetBytes(str);
}
}
}
}

12
framework/test/Volo.Abp.Core.Tests/System/StringExtensions_Tests.cs

@ -83,6 +83,16 @@ namespace System
"ThisIsSampleText".ToKebabCase().ShouldBe("this-is-sample-text");
}
[Fact]
public void ToSnakeCase_Test()
{
(null as string).ToSnakeCase().ShouldBe(null);
"helloMoon".ToSnakeCase().ShouldBe("hello_moon");
"HelloWorld".ToSnakeCase().ShouldBe("hello_world");
"HelloIsparta".ToSnakeCase().ShouldBe("hello_isparta");
"ThisIsSampleText".ToSnakeCase().ShouldBe("this_is_sample_text");
}
[Fact]
public void ToSentenceCase_Test()
{
@ -247,4 +257,4 @@ namespace System
_cultureScope.Dispose();
}
}
}
}

Loading…
Cancel
Save