private void CloneFolderFiles(DirectoryInfo sourceFolderPath, FileSystemInfo sourceRootPath, string sourceNamespace, string targetPath, string targetNamespace) { foreach (var sourceFilePath in Directory.GetFiles(sourceFolderPath.ToString())) { CurrentPath = sourceFilePath; var sourceFileName = new FileInfo(sourceFilePath).Name; if (cloningBehavior.ShouldExcludeFile(sourceFileName)) { continue; } var adjustedTargetPath = GetAdjustedTargetPath(sourceFilePath, sourceRootPath, sourceNamespace, targetNamespace); var targetFilePath = Path.Combine(targetPath, adjustedTargetPath); if (cloningBehavior.ShouldPlainCopyFile(sourceFileName)) { File.Copy(sourceFilePath, targetFilePath, true); continue; } //Detect the encoding and then apply it again when writing the new file var sourceEncoding = fileEncodingDetector.ResolveFileEncoding(sourceFilePath); var sourceStream = new StreamReader(sourceFilePath, sourceEncoding); var sourceContent = sourceStream.ReadToEnd(); var targetEncoding = sourceStream.CurrentEncoding; sourceStream.Close(); if (!sourceContent.Contains(sourceNamespace)) { File.Copy(sourceFilePath, targetFilePath, true); continue; } var targetStream = new StreamWriter(new FileStream(targetFilePath, FileMode.CreateNew), targetEncoding); targetStream.Write(ReplaceNamespace(sourceContent, sourceNamespace, targetNamespace)); targetStream.Close(); } }
public void ShouldPlainCopyFilePath_ShouldReturnFalseForNoMatchInPlainCopyCollection() { Assert.That(behavior.ShouldPlainCopyFile("C:\\foo\\bar\\foo.rtf"), Is.False); }