Skip to content

Commit

Permalink
Merge pull request #559 from otto-gebb/copy-with-subfolders
Browse files Browse the repository at this point in the history
Copy with subfolders
  • Loading branch information
forki committed Oct 10, 2014
2 parents 6330546 + 9eefc91 commit c99ea11
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 15 deletions.
54 changes: 40 additions & 14 deletions src/app/FakeLib/FileHelper.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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
///
Expand All @@ -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
///
Expand Down
36 changes: 36 additions & 0 deletions src/test/Test.FAKECore/FileHandling/CopyFileSpecs.cs
Original file line number Diff line number Diff line change
@@ -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();
}
}
3 changes: 2 additions & 1 deletion src/test/Test.FAKECore/Test.FAKECore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@
<ItemGroup>
<Compile Include="AssemblySpecs.cs" />
<Compile Include="CliSpecs.cs" />
<Compile Include="FileHandling\CopyFileSpecs.cs" />
<Compile Include="FixProjectFileSpecs.cs" />
<Compile Include="CompareProjectFilesSpecs.cs" />
<Compile Include="Globbing\SampeWithParens\SampleWithParensSpecs.cs" />
Expand Down Expand Up @@ -387,4 +388,4 @@
<Target Name="AfterBuild">
</Target>
-->
</Project>
</Project>

0 comments on commit c99ea11

Please sign in to comment.