Skip to content

Commit

Permalink
Support Python and TypeScript (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
ejball authored Apr 29, 2019
2 parents c836ec4 + 7c9ee52 commit a4581da
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ Thumbs.db
*.ncrunchsolution
nCrunchTemp*
_ReSharper*
launchSettings.json
5 changes: 5 additions & 0 deletions VersionHistory.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ Prefix the description of the change with `[major]`, `[minor]` or `[patch]` in a

## Released

### 1.1.0

* Find and replace `.js`, `.py`, and `.ts` files.
* Support snake case and kebab case, i.e. when replacing `SourceText` with `TargetText`, replace `source_text` with `target_text` and `source-text` with `target-text`.

### 1.0.0

* Initial release.
2 changes: 1 addition & 1 deletion src/FindReplaceCode/FindReplaceCode.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.1</TargetFramework>
<VersionPrefix>1.0.0</VersionPrefix>
<VersionPrefix>1.1.0</VersionPrefix>
<PackAsTool>true</PackAsTool>
<ToolCommandName>findreplacecode</ToolCommandName>
<PackageId>Faithlife.FindReplaceCode.Tool</PackageId>
Expand Down
20 changes: 20 additions & 0 deletions src/FindReplaceCode/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,14 @@ private IEnumerable<KeyValuePair<string, string>> GetEnhancedFindReplacePairs(Ke
enhancedKeyValuePair = TryGetEnhancedFindReplacePairs(searchReplacePair, x => x.ToUpperInvariant());
if (enhancedKeyValuePair != null)
yield return enhancedKeyValuePair.Value;

enhancedKeyValuePair = TryGetEnhancedFindReplacePairs(searchReplacePair, x => ToSnakeCase(x, '_'));
if (enhancedKeyValuePair != null)
yield return enhancedKeyValuePair.Value;

enhancedKeyValuePair = TryGetEnhancedFindReplacePairs(searchReplacePair, x => ToSnakeCase(x, '-'));
if (enhancedKeyValuePair != null)
yield return enhancedKeyValuePair.Value;
}

private static KeyValuePair<string, string>? TryGetEnhancedFindReplacePairs(KeyValuePair<string, string> searchReplacePair, Func<string, string> transform)
Expand Down Expand Up @@ -286,6 +294,17 @@ private static string RenderMatchingGuid(Match match, Guid value)
return oldText != oldText.ToUpperInvariant() ? newText : newText.ToUpperInvariant();
}

private static string ToSnakeCase(string value, char separator)
{
if (value == null)
return null;

var words = GetWords(value);
return string.Join(separator.ToString(), words.Select(x => x.ToLowerInvariant()));
}

private static string[] GetWords(string value) => s_word.Matches(value).Select(x => x.ToString()).ToArray();

private const string c_guidPattern = @"\{[0-9a-zA-Z]{8}-[0-9a-zA-Z]{4}-[0-9a-zA-Z]{4}-[0-9a-zA-Z]{4}-[0-9a-zA-Z]{12}\}";

private static readonly string s_fullUsageMessage = string.Join(Environment.NewLine, new[]
Expand All @@ -294,6 +313,7 @@ private static string RenderMatchingGuid(Match match, Guid value)
});

private static readonly Regex s_hiddenDirectoryRegex = new Regex(@"[\\/]\..*[\\/]", RegexOptions.CultureInvariant);
private static readonly Regex s_word = new Regex("[A-Z]([A-Z]*(?![a-z])|[a-z]*)|[a-z]+|[0-9]+", RegexOptions.CultureInvariant | RegexOptions.ExplicitCapture);

private readonly string m_folderPath;
private readonly ReadOnlyCollection<KeyValuePair<string, string>> m_searchReplaceArgs;
Expand Down
3 changes: 3 additions & 0 deletions src/FindReplaceCode/ProgramSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,17 @@ public static class ProgramSettings
".csproj",
".fsd",
".html",
".js",
".json",
".md",
".nuspec",
".props",
".proto",
".ps1",
".py",
".settings",
".sln",
".ts",
".xaml",
".yml",
};
Expand Down

0 comments on commit a4581da

Please sign in to comment.