Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added helper to support plain robocopy and mirroring. #988

Merged
merged 3 commits into from
Nov 4, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/app/FakeLib/FakeLib.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@
<Compile Include="DynamicsNavHelper.fs" />
<Compile Include="DynamicsNavFile.fs" />
<Compile Include="SCPHelper.fs" />
<Compile Include="XCopyHelper.fs" />
<Compile Include="RoboCopyHelper.fs" />
<Compile Include="XpkgHelper.fs" />
<Compile Include="VersionHelper.fs" />
<Compile Include="MSBuild\ProjectSystem.fs" />
Expand Down Expand Up @@ -163,6 +163,7 @@
<None Include="app.config" />
<Compile Include="RaygunHelper.fs" />
<Compile Include="SquirrelHelper.fs" />
<Compile Include="XCopyHelper.fs" />
</ItemGroup>
<ItemGroup>
<Reference Include="ICSharpCode.SharpZipLib">
Expand Down
54 changes: 54 additions & 0 deletions src/app/FakeLib/RoboCopyHelper.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/// Contains a task to use [robocopy](https://en.wikipedia.org/wiki/Robocopy) on Windows.
module Fake.RoboCopyHelper

type RoboCopyOptions = { Mirror: bool }

/// Executes a RoboCopy command with options
/// ## Parameters
/// - `source` - The source directory
/// - `destination` - The target directory
/// - `options` - The options to pass to robocopy
let private roboCopyWithOptions (source:string) (destination:string) (options:RoboCopyOptions) =
let args =
"/D /c robocopy " +
(source.TrimEnd('\\') |> FullName |> toParam) +
(destination.TrimEnd('\\') |> FullName |> toParam) +
if options.Mirror then " /MIR /IT"
else " /IT"

let exitcode = ExecProcess (fun info ->
info.FileName <- "CMD.exe"
info.Arguments <- args) System.TimeSpan.MaxValue

// Exit codes from https://support.microsoft.com/en-us/kb/954404
let exitCodeWithMessage =
match exitcode with
| 0 -> (exitcode, "No files were copied. No failure was encountered. No files were mismatched. The files already exist in the destination directory; therefore, the copy operation was skipped.")
| 1 -> (exitcode, "All files were copied successfully.")
| 2 -> (exitcode, "There are some additional files in the destination directory that are not present in the source directory. No files were copied.")
| 3 -> (exitcode, "Some files were copied. Additional files were present. No failure was encountered.")
| 4 -> (exitcode, "Some Mismatched files or directories were detected. Examine the output log. Housekeeping might be required.")
| 5 -> (exitcode, "Some files were copied. Some files were mismatched. No failure was encountered.")
| 6 -> (exitcode, "Additional files and mismatched files exist. No files were copied and no failures were encountered. This means that the files already exist in the destination directory. ")
| 7 -> (exitcode, "Files were copied, a file mismatch was present, and additional files were present.")
| 8 -> (exitcode, "Several files did not copy.")
| _ -> (exitcode, "UNKNOWN ERROR")

match exitCodeWithMessage with
| (exitcode, message) when exitcode < 2 -> tracefn "Succeeded in RoboCopy From: %s To: %s \n%s\n" source destination message |> ignore
| (exitcode, message) when exitcode < 8 -> traceImportant <| sprintf "Important notice in RoboCopy From: %s To: %s \n%s\n" source destination message |> ignore
| (_, _) -> failwithf "Error during RoboCopy From: %s To: %s" source destination

/// Executes a RoboCopy command
/// ## Parameters
/// - `source` - The source directory
/// - `destination` - The target directory
let RoboCopy (source:string) (destination:string) =
roboCopyWithOptions source destination { Mirror = false }

/// Executes a RoboCopy mirror command (potentially destructive)
/// ## Parameters
/// - `source` - The source directory
/// - `destination` - The target directory
let RoboCopyMirror (source:string) (destination:string) =
roboCopyWithOptions source destination { Mirror = true }