Skip to content

Commit

Permalink
Create only those folder that contain files while tranlating in folde…
Browse files Browse the repository at this point in the history
…r to folder mode.
  • Loading branch information
Konard committed Feb 28, 2020
1 parent 9a4955a commit ad2dce3
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public void FolderToFolderTransfomationTest()
File.WriteAllText(Path.Combine(sourceFolderPath, "a.cs"), "a a a");
var aFolderPath = Path.Combine(sourceFolderPath, "A");
Directory.CreateDirectory(aFolderPath);
Directory.CreateDirectory(Path.Combine(sourceFolderPath, "B"));
File.WriteAllText(Path.Combine(aFolderPath, "b.cs"), "b b b");
File.WriteAllText(Path.Combine(sourceFolderPath, "x.txt"), "should not be translated");

Expand All @@ -44,6 +45,7 @@ public void FolderToFolderTransfomationTest()
Assert.True(File.Exists(aCppFile));
Assert.Equal("c c c", File.ReadAllText(aCppFile));
Assert.True(Directory.Exists(Path.Combine(targetFolderPath, "A")));
Assert.False(Directory.Exists(Path.Combine(targetFolderPath, "B")));
var bCppFile = Path.Combine(targetFolderPath, "A", "b.cpp");
Assert.True(File.Exists(bCppFile));
Assert.Equal("c c c", File.ReadAllText(bCppFile));
Expand Down
31 changes: 27 additions & 4 deletions csharp/Platform.RegularExpressions.Transformer/FileTransformer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,23 +86,23 @@ public void Transform(string sourcePath, string targetPath)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
protected virtual void TransformFolder(string sourcePath, string targetPath)
{
var files = Directory.GetFiles(sourcePath);
var directories = Directory.GetDirectories(sourcePath);
if (files.Length == 0 && directories.Length == 0)
if (CountFilesRecursively(sourcePath, SourceFileExtension) == 0)
{
return;
}
EnsureTargetDirectoryExists(targetPath);
var directories = Directory.GetDirectories(sourcePath);
for (var i = 0; i < directories.Length; i++)
{
var relativePath = GetRelativePath(sourcePath, directories[i]);
var newTargetPath = Path.Combine(targetPath, relativePath);
TransformFolder(directories[i], newTargetPath);
}
var files = Directory.GetFiles(sourcePath);
Parallel.For(0, files.Length, i =>
{
var file = files[i];
if (file.EndsWith(SourceFileExtension, StringComparison.OrdinalIgnoreCase))
if (FileExtensionMatches(file, SourceFileExtension))
{
TransformFile(file, GetTargetFileName(file, targetPath));
}
Expand All @@ -129,6 +129,29 @@ protected virtual void TransformFile(string sourcePath, string targetPath)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
protected string GetTargetFileName(string sourcePath, string targetDirectory) => Path.ChangeExtension(Path.Combine(targetDirectory, Path.GetFileName(sourcePath)), TargetFileExtension);

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static long CountFilesRecursively(string path, string extension)
{
var files = Directory.GetFiles(path);
var directories = Directory.GetDirectories(path);
var result = 0L;
for (var i = 0; i < directories.Length; i++)
{
result += CountFilesRecursively(directories[i], extension);
}
for (var i = 0; i < files.Length; i++)
{
if (FileExtensionMatches(files[i], extension))
{
result++;
}
}
return result;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static bool FileExtensionMatches(string file, string extension) => file.EndsWith(extension, StringComparison.OrdinalIgnoreCase);

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static void EnsureTargetFileDirectoryExists(string targetPath)
{
Expand Down

0 comments on commit ad2dce3

Please sign in to comment.