- using System;
- using System.Activities;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- namespace BuildTasks.Activities
- {
- public class CopyFiles : CodeActivity
- {
- [RequiredArgument]
- public InArgument<IEnumerable<string>> SourceFileMasks { get; set; }
- [RequiredArgument]
- public InArgument<string> SourceDirectory { get; set; }
- [RequiredArgument]
- public InArgument<string> DestinationDirectory { get; set; }
- public InArgument<bool> OverwriteFiles { get; set; }
- protected override void Execute(CodeActivityContext context)
- {
- var sourceFileMasks = context.GetValue(SourceFileMasks);
- if (sourceFileMasks == null || sourceFileMasks.Count() == 0) return;
- var sourceDirectory = new DirectoryInfo(context.GetValue(SourceDirectory));
- if (!sourceDirectory.Exists) return;
- var destinationDirectory = context.GetValue(DestinationDirectory);
- if (!Directory.Exists(destinationDirectory)) return;
- var overwrite = context.GetValue(OverwriteFiles);
- foreach (var sourceFileMask in sourceFileMasks)
- {
- var sourceFiles = sourceDirectory.GetFiles(sourceFileMask);
- foreach (var sourceFile in sourceFiles)
- {
- sourceFile.CopyTo(Path.Combine(destinationDirectory, sourceFile.Name), overwrite);
- }
- }
- }
- }
- }
The SourceFileMasks property allows you to pass in a list of file name masks that match the files you would like to copy. For example, if you needed to copy YourApp.exe, YourApp.exe.config, YourApp.pdb, Dependency.dll, Dependency.pdb, you could specify that with the following list:
{"YourApp.*", "Dependency.*"}
The screen shot below shows a simplified workflow using the CopyFiles activity to copy files from the build drop folder to a network share at “\\MyServer\Destination”. Also of note is the OverwriteFiles property where you can specify if any existing files should be overwritten.
I put together this code rather quickly, so let me know if you see anything overtly incorrect about it. The code is available as a gist at https://gist.github.com/935122.
0 comments:
Post a Comment