From ad2dce3ae9547448f08a392e3b2cfbb245fa04df Mon Sep 17 00:00:00 2001 From: Konstantin Diachenko Date: Fri, 28 Feb 2020 18:18:26 +0300 Subject: [PATCH] Create only those folder that contain files while tranlating in folder to folder mode. --- .../FileTransformerTests.cs | 2 ++ .../FileTransformer.cs | 31 ++++++++++++++++--- 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/csharp/Platform.RegularExpressions.Transformer.Tests/FileTransformerTests.cs b/csharp/Platform.RegularExpressions.Transformer.Tests/FileTransformerTests.cs index ba83d34..034a430 100644 --- a/csharp/Platform.RegularExpressions.Transformer.Tests/FileTransformerTests.cs +++ b/csharp/Platform.RegularExpressions.Transformer.Tests/FileTransformerTests.cs @@ -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"); @@ -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)); diff --git a/csharp/Platform.RegularExpressions.Transformer/FileTransformer.cs b/csharp/Platform.RegularExpressions.Transformer/FileTransformer.cs index 77e6959..b69b1f4 100644 --- a/csharp/Platform.RegularExpressions.Transformer/FileTransformer.cs +++ b/csharp/Platform.RegularExpressions.Transformer/FileTransformer.cs @@ -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)); } @@ -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) {