Ejemplo n.º 1
0
 object ITaskProcessor.Process(CopyTask task) {
     ProcessBaseTask(task);
     task.SourcePath = ProcessString(task.SourcePath);
     task.DestPath = ProcessString(task.DestPath);
     task.Include = ProcessString(task.Include);
     task.Exclude = ProcessString(task.Exclude);
     return null;
 }
Ejemplo n.º 2
0
 object ITaskProcessor.Process(CopyTask task) {
     if(task == null) return null;
     try {
         if(File.Exists(task.SourcePath)) {
             File.Copy(task.SourcePath, task.DestPath, true);
             return null;
         }
         if(Directory.Exists(task.SourcePath)) {
             if((!string.IsNullOrEmpty(task.Exclude) && !string.IsNullOrEmpty(task.Include)) ||
                (!string.IsNullOrEmpty(project.CopyOptionExclude) && !string.IsNullOrEmpty(task.Include)) ||
                 (!string.IsNullOrEmpty(task.Exclude) && !string.IsNullOrEmpty(project.CopyOptionInclude))) {
                     return new InvalidOperationException("Wrong include/exclude copy parameters.");
             }                    
             bool include = false;
             HashSet<string> filterSet = new HashSet<string>();
             if(!string.IsNullOrEmpty(task.Exclude) || !string.IsNullOrEmpty(project.CopyOptionExclude)) {
                 if(task.Exclude != null){
                     foreach(var exItem in task.Exclude.Split(separatorChars, StringSplitOptions.RemoveEmptyEntries)) {
                         filterSet.Add(exItem);
                     }
                 }
                 if(project.CopyOptionExclude != null) {
                     foreach(var exItem in project.CopyOptionExclude.Split(separatorChars, StringSplitOptions.RemoveEmptyEntries)) {
                         filterSet.Add(exItem);
                     }
                 }
             }else if(!string.IsNullOrEmpty(task.Include) || !string.IsNullOrEmpty(project.CopyOptionInclude)) {
                 include = true;
                 if(task.Include != null) {
                     foreach(var inItem in task.Include.Split(separatorChars, StringSplitOptions.RemoveEmptyEntries)) {
                         filterSet.Add(inItem);
                     }
                 }
                 if(project.CopyOptionInclude != null) {
                     foreach(var inItem in project.CopyOptionInclude.Split(separatorChars, StringSplitOptions.RemoveEmptyEntries)) {
                         filterSet.Add(inItem);
                     }
                 }
             }
             List<string> sourceDirectories = new List<string>();
             string[] sourceFiles;
             sourceDirectories.Add(task.SourcePath);
             if(!task.Recursive) {
                 sourceFiles = Directory.GetFiles(task.SourcePath, "*", SearchOption.TopDirectoryOnly);
             } else {
                 sourceDirectories.AddRange(Directory.GetDirectories(task.SourcePath, "*", SearchOption.AllDirectories));
                 sourceFiles = Directory.GetFiles(task.SourcePath, "*", SearchOption.AllDirectories);
             }
             int sourcePathLength = task.SourcePath.Length;
             List<string> directoriesToDelete = new List<string>(sourceDirectories.Count);
             foreach(string dir in sourceDirectories) {
                 string partDir = dir.Remove(0, sourcePathLength).TrimStart('\\');
                 string destWDir = Path.Combine(task.DestPath, partDir);
                 directoriesToDelete.Add(destWDir);
                 if(!Directory.Exists(destWDir)) {
                     Directory.CreateDirectory(destWDir);
                 }
             }
             Console.WriteLine();
             Console.WriteLine("Coping: {0}...", task.SourcePath);
             int lastLeft = Console.CursorLeft;
             int lastTop = Console.CursorTop;
             int fileIndex = 0;
             int fileCount = sourceFiles.Length;
             List<string> filesToDelete = new List<string>(sourceFiles.Length);
             foreach(string file in sourceFiles) {
                 bool foundInFilterSet = filterSet.Contains(Path.GetFileName(file)) || filterSet.Contains(Path.GetExtension(file));
                 if((!foundInFilterSet && include) || (foundInFilterSet && !include))
                     continue;
                 string partFile = file.Remove(0, sourcePathLength).TrimStart('\\');
                 string destFile = Path.Combine(task.DestPath, partFile);
                 File.Copy(file, destFile, true);
                 if(fileIndex > 0 && (fileIndex % 15) == 0) {
                     Console.SetCursorPosition(lastLeft, lastTop);
                     Console.Write("\t{0} %             ", fileIndex * 100 / fileCount);
                 }
                 fileIndex++;
                 filesToDelete.Add(destFile);
             }
             if(!string.IsNullOrEmpty(task.Id)) {
                 processedDirectoriesDict[task.Id] = directoriesToDelete.ToArray();
                 processedFilesDict[task.Id] = filesToDelete.ToArray();
             }
             return null;
         }
     } catch(Exception ex) {
         return ex;
     }
     return new FileNotFoundException(task.SourcePath);
 }