Skip to content

Commit

Permalink
Folder to folder transformation logic fix.
Browse files Browse the repository at this point in the history
  • Loading branch information
Konard committed Feb 28, 2020
1 parent 0384ddc commit 6bfb04c
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using System.IO;
using Xunit;

namespace Platform.RegularExpressions.Transformer.Tests
{
public class FileTransformerTests
{
[Fact]
public void FolderToFolderTransfomationTest()
{
var tempPath = Path.GetTempPath();
var sourceFolderPath = Path.Combine(tempPath, "FileTransformerTestsFolderToFolderTransfomationTestSourceFolder");
var targetFolderPath = Path.Combine(tempPath, "FileTransformerTestsFolderToFolderTransfomationTestTargetFolder");

var baseTransformer = new TextTransformer(new SubstitutionRule[]
{
("a", "b"),
("b", "c")
});
var fileTransformer = new FileTransformer(baseTransformer, ".cs", ".cpp");

// Delete before creation (if previous test failed)
Directory.Delete(sourceFolderPath, true);
Directory.Delete(targetFolderPath, true);

Directory.CreateDirectory(sourceFolderPath);
Directory.CreateDirectory(targetFolderPath);

File.WriteAllText(Path.Combine(sourceFolderPath, "a.cs"), "a a a");
var aFolderPath = Path.Combine(sourceFolderPath, "A");
Directory.CreateDirectory(aFolderPath);
File.WriteAllText(Path.Combine(aFolderPath, "b.cs"), "b b b");
File.WriteAllText(Path.Combine(sourceFolderPath, "x.txt"), "should not be translated");

fileTransformer.Transform(sourceFolderPath, $"{targetFolderPath}{Path.DirectorySeparatorChar}");

var aCppFile = Path.Combine(targetFolderPath, "a.cpp");
Assert.True(File.Exists(aCppFile));
Assert.Equal("c c c", File.ReadAllText(aCppFile));
Assert.True(Directory.Exists(Path.Combine(targetFolderPath, "A")));
var bCppFile = Path.Combine(targetFolderPath, "A", "b.cpp");
Assert.True(File.Exists(bCppFile));
Assert.Equal("c c c", File.ReadAllText(bCppFile));
Assert.False(File.Exists(Path.Combine(targetFolderPath, "x.txt")));
Assert.False(File.Exists(Path.Combine(targetFolderPath, "x.cpp")));

Directory.Delete(sourceFolderPath, true);
Directory.Delete(targetFolderPath, true);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace Platform.RegularExpressions.Transformer.Tests
{
public class TransformersTests
public class TextTransformerTests
{
[Fact]
public void DebugOutputTest()
Expand Down
27 changes: 19 additions & 8 deletions csharp/Platform.RegularExpressions.Transformer/FileTransformer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,21 +60,20 @@ public void Transform(string sourcePath, string targetPath)
{
return;
}
EnsureTargetDirectoryExists(targetDirectoryExists, targetPath);
TransformFolder(sourcePath, targetPath);
}
else if (!(sourceIsDirectory || targetIsDirectory))
{
// File -> File
EnsureSourceFileExists(sourcePath);
EnsureTargetDirectoryExists(targetPath);
EnsureTargetFileDirectoryExists(targetPath);
TransformFile(sourcePath, targetPath);
}
else if (targetIsDirectory)
{
// File -> Folder
EnsureSourceFileExists(sourcePath);
EnsureTargetDirectoryExists(targetDirectoryExists, targetPath);
EnsureTargetDirectoryExists(targetPath, targetDirectoryExists);
TransformFile(sourcePath, GetTargetFileName(sourcePath, targetPath));
}
else
Expand All @@ -87,17 +86,26 @@ 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)
{
return;
}
EnsureTargetDirectoryExists(targetPath);
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 =>
{
TransformFile(files[i], GetTargetFileName(files[i], targetPath));
var file = files[i];
if (file.EndsWith(SourceFileExtension, StringComparison.OrdinalIgnoreCase))
{
TransformFile(file, GetTargetFileName(file, targetPath));
}
});
}

Expand All @@ -122,7 +130,7 @@ protected virtual void TransformFile(string sourcePath, string targetPath)
protected string GetTargetFileName(string sourcePath, string targetDirectory) => Path.ChangeExtension(Path.Combine(targetDirectory, Path.GetFileName(sourcePath)), TargetFileExtension);

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static void EnsureTargetDirectoryExists(string targetPath)
private static void EnsureTargetFileDirectoryExists(string targetPath)
{
if (!File.Exists(targetPath))
{
Expand All @@ -131,7 +139,10 @@ private static void EnsureTargetDirectoryExists(string targetPath)
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static void EnsureTargetDirectoryExists(bool targetDirectoryExists, string targetPath)
private static void EnsureTargetDirectoryExists(string targetPath) => EnsureTargetDirectoryExists(targetPath, DirectoryExists(targetPath));

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static void EnsureTargetDirectoryExists(string targetPath, bool targetDirectoryExists)
{
if (!targetDirectoryExists)
{
Expand Down Expand Up @@ -160,7 +171,7 @@ private static string GetRelativePath(string rootPath, string fullPath)
{
throw new Exception("Could not find rootPath in fullPath when calculating relative path.");
}
return fullPath.Substring(rootPath.Length);
return fullPath.Substring(rootPath.Length + 1);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
Expand Down

0 comments on commit 6bfb04c

Please sign in to comment.