From: <nan...@na...> - 2012-04-09 23:27:04
|
From: Ryan Boggs <rm...@gm...> --- src/NAnt.Core/Tasks/CopyTask.cs | 230 ++++++++++++++++++++------------------- src/NAnt.Core/Tasks/MoveTask.cs | 185 ++++++++++++++++--------------- 2 files changed, 215 insertions(+), 200 deletions(-) diff --git a/src/NAnt.Core/Tasks/CopyTask.cs b/src/NAnt.Core/Tasks/CopyTask.cs index 4c1b1dc..10a9dc6 100644 --- a/src/NAnt.Core/Tasks/CopyTask.cs +++ b/src/NAnt.Core/Tasks/CopyTask.cs @@ -588,131 +588,139 @@ namespace NAnt.Core.Tasks { /// Actually does the file copies. /// </summary> protected virtual void DoFileOperations() { - if (OperationMap.Count > 0) + // If the operation map is empty, exit (return) the method. + if (OperationMap.Count <= 0) { - // Get the number of file and directory copies to display to - // the user. - int fileMovements = OperationMap.CountFileOperations(); - int dirMovements = OperationMap.CountDirectoryOperations(); + return; + } - // Output the number of file copies - if (fileMovements > 0) - { - if (ToFile != null) { - Log(Level.Info, "Copying {0} file{1} to '{2}'.", - fileMovements, (fileMovements != 1) ? "s" : "", ToFile); - } else { - Log(Level.Info, "Copying {0} file{1} to '{2}'.", - fileMovements, (fileMovements != 1) ? "s" : "", ToDirectory); - } + // Get the number of file and directory copies to display to + // the user. + int fileMovements = OperationMap.CountFileOperations(); + int dirMovements = OperationMap.CountDirectoryOperations(); + + // Output the number of file copies + if (fileMovements > 0) + { + if (ToFile != null) { + Log(Level.Info, "Copying {0} file{1} to '{2}'.", + fileMovements, (fileMovements != 1) ? "s" : "", ToFile); + } else { + Log(Level.Info, "Copying {0} file{1} to '{2}'.", + fileMovements, (fileMovements != 1) ? "s" : "", ToDirectory); + } + } + + // Output the number of directory copies + if (dirMovements > 0) + { + if (ToFile != null) { + Log(Level.Info, "Copying {0} {1} to '{2}'.", + dirMovements, (dirMovements != 1) ? "directories" : "directory", + ToFile); + } else { + Log(Level.Info, "Copying {0} {1} to '{2}'.", + dirMovements, (dirMovements != 1) ? "directories" : "directory", + ToDirectory); } + } - // Output the number of directory copies - if (dirMovements > 0) + // loop thru our file list + for (int i = 0; i < OperationMap.Count; i++) + { + // Setup a temporary var to hold the current file operation + // details. + FileOperation currentOperation = OperationMap[i]; + if (currentOperation.SourceEqualsTarget()) { - if (ToFile != null) { - Log(Level.Info, "Copying {0} {1} to '{2}'.", - dirMovements, (dirMovements != 1) ? "directories" : "directory", - ToFile); - } else { - Log(Level.Info, "Copying {0} {1} to '{2}'.", - dirMovements, (dirMovements != 1) ? "directories" : "directory", - ToDirectory); - } + Log(Level.Verbose, "Skipping self-copy of '{0}'.", + currentOperation.Source); + continue; } - // loop thru our file list - for (int i = 0; i < OperationMap.Count; i++) + try { - // Setup a temporary var to hold the current file operation - // details. - FileOperation currentOperation = OperationMap[i]; - if (currentOperation.SourceEqualsTarget()) - { - Log(Level.Verbose, "Skipping self-copy of '{0}'.", - currentOperation.Source); - continue; - } + Log(Level.Verbose, "Copying {0}.", currentOperation.ToString()); - try + switch (currentOperation.OperationType) { - Log(Level.Verbose, "Copying {0}.", currentOperation.ToString()); - - switch (currentOperation.OperationType) - { - case OperationType.FileToFile: - // create directory if not present - string destinationDirectory = - Path.GetDirectoryName(currentOperation.Target); - if (!Directory.Exists(destinationDirectory)) - { - Directory.CreateDirectory(destinationDirectory); - Log(Level.Verbose, "Created directory '{0}'.", - destinationDirectory); - } + case OperationType.FileToFile: + // create directory if not present + string destinationDirectory = + Path.GetDirectoryName(currentOperation.Target); + if (!Directory.Exists(destinationDirectory)) + { + Directory.CreateDirectory(destinationDirectory); + Log(Level.Verbose, "Created directory '{0}'.", + destinationDirectory); + } - // Ensure the target file is removed before - // attempting to copy. - if (File.Exists(currentOperation.Target)) - { - File.Delete(currentOperation.Target); - } - - // copy the file with filters - FileUtils.CopyFile(currentOperation.Source, - currentOperation.Target, Filters, - InputEncoding, OutputEncoding); - break; - case OperationType.FileToDirectory: - // Setup a local var that combines the directory - // of the target path with the source file name. - string targetFile = Path.Combine(currentOperation.Target, - Path.GetFileName(currentOperation.Source)); - // create directory if not present - if (!Directory.Exists(currentOperation.Target)) - { - Directory.CreateDirectory(currentOperation.Target); - Log(Level.Verbose, "Created directory '{0}'.", - currentOperation.Target); - } + // Ensure the target file is removed before + // attempting to copy. + if (File.Exists(currentOperation.Target)) + { + File.Delete(currentOperation.Target); + } + + // copy the file with filters + FileUtils.CopyFile(currentOperation.Source, + currentOperation.Target, Filters, + InputEncoding, OutputEncoding); + break; + case OperationType.FileToDirectory: + // Setup a local var that combines the directory + // of the target path with the source file name. + string targetFile = Path.Combine(currentOperation.Target, + Path.GetFileName(currentOperation.Source)); + // create directory if not present + if (!Directory.Exists(currentOperation.Target)) + { + Directory.CreateDirectory(currentOperation.Target); + Log(Level.Verbose, "Created directory '{0}'.", + currentOperation.Target); + } - // Ensure the target file is removed before - // attempting to copy. - if (File.Exists(targetFile)) - { - File.Delete(targetFile); - } - - // copy the file with filters - FileUtils.CopyFile(currentOperation.Source, - targetFile, Filters, InputEncoding, OutputEncoding); - break; - case OperationType.DirectoryToDirectory: - // Throw a build exception if the target directory - // already exists. - if (Directory.Exists(currentOperation.Target)) - { - throw new BuildException( - string.Format(CultureInfo.InvariantCulture, - "Failed to copy {0}. Directory '{1}' already exists.", - currentOperation.ToString(), - currentOperation.Target)); - } - - // Copy over the entire directory with filters - FileUtils.CopyDirectory(currentOperation.Source, - currentOperation.Target, Filters, InputEncoding, - OutputEncoding); - break; - } - } - catch (Exception ex) - { - throw new BuildException(string.Format(CultureInfo.InvariantCulture, - "Cannot copy {0}.", currentOperation.ToString()), - Location, ex); + // Ensure the target file is removed before + // attempting to copy. + if (File.Exists(targetFile)) + { + File.Delete(targetFile); + } + + // copy the file with filters + FileUtils.CopyFile(currentOperation.Source, + targetFile, Filters, InputEncoding, OutputEncoding); + break; + case OperationType.DirectoryToDirectory: + // Throw a build exception if the target directory + // already exists. + if (Directory.Exists(currentOperation.Target)) + { + throw new BuildException( + string.Format(CultureInfo.InvariantCulture, + "Failed to copy {0}. Directory '{1}' already exists.", + currentOperation.ToString(), + currentOperation.Target)); + } + + // Copy over the entire directory with filters + FileUtils.CopyDirectory(currentOperation.Source, + currentOperation.Target, Filters, InputEncoding, + OutputEncoding); + break; + default: + throw new + BuildException("Unrecognized copy operation. " + + "The copy task can only copy a file to file, " + + "file to directory, or directory to directory."); } } + catch (Exception ex) + { + throw new BuildException(string.Format(CultureInfo.InvariantCulture, + "Cannot copy {0}.", currentOperation.ToString()), + Location, ex); + } } } diff --git a/src/NAnt.Core/Tasks/MoveTask.cs b/src/NAnt.Core/Tasks/MoveTask.cs index 5158535..3480dec 100644 --- a/src/NAnt.Core/Tasks/MoveTask.cs +++ b/src/NAnt.Core/Tasks/MoveTask.cs @@ -214,116 +214,123 @@ namespace NAnt.Core.Tasks { /// Actually does the file moves. /// </summary> protected override void DoFileOperations() { - if (OperationMap.Count > 0) + // If the operation map is empty, exit (return) the method. + if (OperationMap.Count <= 0) { + return; + } - // loop thru our file list - for (int i = 0; i < OperationMap.Count; i++) + // loop thru our file list + for (int i = 0; i < OperationMap.Count; i++) + { + // Setup a temporary var to hold the current file operation + // details. + FileOperation currentOperation = OperationMap[i]; + if (currentOperation.SourceIsIdenticalToTarget()) { - // Setup a temporary var to hold the current file operation - // details. - FileOperation currentOperation = OperationMap[i]; - if (currentOperation.SourceIsIdenticalToTarget()) - { - Log(Level.Warning, String.Format("Skipping self-move of {0}.", - currentOperation.Source)); - continue; - } + Log(Level.Warning, String.Format("Skipping self-move of {0}.", + currentOperation.Source)); + continue; + } - try - { - Log(Level.Verbose, "Moving {0}.", currentOperation.ToString()); + try + { + Log(Level.Verbose, "Moving {0}.", currentOperation.ToString()); - string destinationDirectory = null; + string destinationDirectory = null; - switch (currentOperation.OperationType) - { - case OperationType.FileToFile: - // Setup the dest directory var - destinationDirectory = - Path.GetDirectoryName(currentOperation.Target); + switch (currentOperation.OperationType) + { + case OperationType.FileToFile: + // Setup the dest directory var + destinationDirectory = + Path.GetDirectoryName(currentOperation.Target); - // create directory if not present - if (!Directory.Exists(destinationDirectory)) - { - Directory.CreateDirectory(destinationDirectory); - Log(Level.Verbose, "Created directory '{0}'.", - destinationDirectory); - } + // create directory if not present + if (!Directory.Exists(destinationDirectory)) + { + Directory.CreateDirectory(destinationDirectory); + Log(Level.Verbose, "Created directory '{0}'.", + destinationDirectory); + } - // Ensure the target file is removed before - // attempting to move. - if (File.Exists(currentOperation.Target)) - { - File.Delete(currentOperation.Target); - } + // Ensure the target file is removed before + // attempting to move. + if (File.Exists(currentOperation.Target)) + { + File.Delete(currentOperation.Target); + } - // move the file with filters - FileUtils.MoveFile(currentOperation.Source, - currentOperation.Target, Filters, - InputEncoding, OutputEncoding); + // move the file with filters + FileUtils.MoveFile(currentOperation.Source, + currentOperation.Target, Filters, + InputEncoding, OutputEncoding); - break; - case OperationType.FileToDirectory: - // Setup the dest directory var - destinationDirectory = currentOperation.Target; + break; + case OperationType.FileToDirectory: + // Setup the dest directory var + destinationDirectory = currentOperation.Target; - // Setup a local var that combines the directory - // of the target path with the source file name. - string targetFile = Path.Combine(destinationDirectory, - Path.GetFileName(currentOperation.Source)); + // Setup a local var that combines the directory + // of the target path with the source file name. + string targetFile = Path.Combine(destinationDirectory, + Path.GetFileName(currentOperation.Source)); - // create directory if not present - if (!Directory.Exists(destinationDirectory)) - { - Directory.CreateDirectory(destinationDirectory); - Log(Level.Verbose, "Created directory '{0}'.", - destinationDirectory); - } + // create directory if not present + if (!Directory.Exists(destinationDirectory)) + { + Directory.CreateDirectory(destinationDirectory); + Log(Level.Verbose, "Created directory '{0}'.", + destinationDirectory); + } - // Ensure the target file is removed before - // attempting to move. - if (File.Exists(targetFile)) - { - File.Delete(targetFile); - } + // Ensure the target file is removed before + // attempting to move. + if (File.Exists(targetFile)) + { + File.Delete(targetFile); + } - // move the file with filters - FileUtils.MoveFile(currentOperation.Source, - targetFile, Filters, InputEncoding, OutputEncoding); + // move the file with filters + FileUtils.MoveFile(currentOperation.Source, + targetFile, Filters, InputEncoding, OutputEncoding); - break; - case OperationType.DirectoryToDirectory: + break; + case OperationType.DirectoryToDirectory: - // Move over the entire directory with filters - FileUtils.MoveDirectory(currentOperation.Source, - currentOperation.Target, Filters, InputEncoding, - OutputEncoding); - break; - } - } - catch (IOException ex) - { - throw new BuildException(string.Format(CultureInfo.InvariantCulture, - "Failed to move {0}.", currentOperation.ToString()), - Location, ex); + // Move over the entire directory with filters + FileUtils.MoveDirectory(currentOperation.Source, + currentOperation.Target, Filters, InputEncoding, + OutputEncoding); + break; + default: + throw new + BuildException("Unrecognized move operation. " + + "The move task can only move a file to file, " + + "file to directory, or directory to directory."); } } - - int fileMovements = OperationMap.CountFileOperations(); - int dirMovements = OperationMap.CountDirectoryOperations(); - - if (fileMovements > 0) + catch (IOException ex) { - Log(Level.Info, "{0} file{1} moved.", fileMovements, - fileMovements != 1 ? "s" : ""); - } - if (dirMovements > 0) - { - Log(Level.Info, "{0} {1} moved.", dirMovements, - dirMovements != 1 ? "directories" : "directory"); + throw new BuildException(string.Format(CultureInfo.InvariantCulture, + "Failed to move {0}.", currentOperation.ToString()), + Location, ex); } } + + int fileMovements = OperationMap.CountFileOperations(); + int dirMovements = OperationMap.CountDirectoryOperations(); + + if (fileMovements > 0) + { + Log(Level.Info, "{0} file{1} moved.", fileMovements, + fileMovements != 1 ? "s" : ""); + } + if (dirMovements > 0) + { + Log(Level.Info, "{0} {1} moved.", dirMovements, + dirMovements != 1 ? "directories" : "directory"); + } } protected override BuildException CreateSourceFileNotFoundException (string sourceFile) { -- 1.7.7 |