diff --git a/src/app/FakeLib/FileHelper.fs b/src/app/FakeLib/FileHelper.fs index 5aae0ada8e9..cd23526bcf9 100644 --- a/src/app/FakeLib/FileHelper.fs +++ b/src/app/FakeLib/FileHelper.fs @@ -96,20 +96,6 @@ let (|FileInfoFullName|) (f : FileInfo) = f.FullName /// Active Pattern for determining FileInfoNameSections. let (|FileInfoNameSections|) (f : FileInfo) = (f.Name, f.Extension, f.FullName) -/// Copies a single file to a relative subfolder of the target. -/// ## Parameters -/// -/// - `target` - The target directory -/// - `fileName` - The fileName -let CopyFileIntoSubFolder target fileName = - let relative = (toRelativePath fileName).TrimStart '.' - let fi = fileInfo fileName - let targetName = target + relative - let target = fileInfo targetName - ensureDirExists target.Directory - logVerbosefn "Copy %s to %s" fileName targetName - fi.CopyTo(targetName, true) |> ignore - /// Copies a single file to the target and overwrites the existing file. /// ## Parameters /// @@ -127,6 +113,46 @@ let CopyFile target fileName = f.CopyTo(targetName, true) |> ignore | Directory _ -> logVerbosefn "Ignoring %s, because it is a directory." fileName +let private DoCopyFile targetName fileName = + let fi = fileInfo fileName + let target = fileInfo targetName + ensureDirExists target.Directory + logVerbosefn "Copy %s to %s" fileName targetName + fi.CopyTo(targetName, true) |> ignore + +/// Copies a single file to a relative subfolder of the target. +/// ## Parameters +/// +/// - `target` - The target directory +/// - `fileName` - The fileName +let CopyFileIntoSubFolder target fileName = + let relative = (toRelativePath fileName).TrimStart '.' + DoCopyFile (target + relative) fileName + +/// Copies a single file to the target folder preserving the folder structure +/// starting from the specified base folder. +/// ## Parameters +/// +/// - `baseDir` - The base directory. +/// - `target` - The target directory. +/// - `fileName` - The file name. +let CopyFileWithSubfolder baseDir target fileName = + let fileName = FullName fileName + let baseDir = FullName baseDir + let relative = (ProduceRelativePath baseDir fileName).TrimStart '.' + DoCopyFile (target + relative) fileName + +/// Copies several file groups, each represented by a FileIncludes object, +/// to the target folder preserving the folder structure +/// starting from the BaseDirectory of each FileIncludes. +/// ## Parameters +/// +/// - `target` - The target directory. +/// - `files` - A sequence of file groups. +let CopyWithSubfoldersTo target files = + let copyFiles dir inc = Seq.iter (CopyFileWithSubfolder dir target) inc + Seq.iter (fun inc -> copyFiles inc.BaseDirectory inc) files + /// Copies the files to the target. /// ## Parameters /// diff --git a/src/test/Test.FAKECore/FileHandling/CopyFileSpecs.cs b/src/test/Test.FAKECore/FileHandling/CopyFileSpecs.cs new file mode 100644 index 00000000000..596f7df635d --- /dev/null +++ b/src/test/Test.FAKECore/FileHandling/CopyFileSpecs.cs @@ -0,0 +1,36 @@ +using System.IO; +using Fake; +using Machine.Specifications; + +namespace Test.FAKECore.FileHandling +{ + public class when_copying_file_with_subfolders : BaseFunctions + { + private static readonly string DestinationDir = Path.Combine(TestData.TestDir, "destination"); + + Establish context = CreateTestFileStructure; + Because of = () => FileHelper.CopyFileWithSubfolder( + TestData.TestDir + "/Dir6", + DestinationDir, + Path.Combine(TestData.TestDir, "Dir6/Sub1/file2.nat")); + It should_find_file_in_destination_subfolder = () => + File.Exists(DestinationDir + "/Sub1/file2.nat").ShouldBeTrue(); + } + + public class when_copying_group_of_files_with_subfolders : BaseFunctions + { + private static readonly string DestinationDir = Path.Combine(TestData.TestDir, "destination"); + + Establish context = CreateTestFileStructure; + Because of = () => FileHelper.CopyWithSubfoldersTo( + DestinationDir, + new [] + { + FileSystem.SetBaseDir(TestData.TestDir + "/Dir7", FileSystem.Include("**/*.nat")) + }); + It should_find_file_in_destination_folder = () => + File.Exists(DestinationDir + "/file2.nat").ShouldBeTrue(); + It should_find_file_in_destination_subfolder = () => + File.Exists(DestinationDir + "/Sub1/file2.nat").ShouldBeTrue(); + } +} diff --git a/src/test/Test.FAKECore/Test.FAKECore.csproj b/src/test/Test.FAKECore/Test.FAKECore.csproj index 3a86bf0ae61..83949f98b36 100644 --- a/src/test/Test.FAKECore/Test.FAKECore.csproj +++ b/src/test/Test.FAKECore/Test.FAKECore.csproj @@ -93,6 +93,7 @@ + @@ -387,4 +388,4 @@ --> - + \ No newline at end of file