2 // Copy.cs: Task that can copy files
5 // Marek Sieradzki (marek.sieradzki@gmail.com)
7 // (C) 2005 Marek Sieradzki
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 using System.Collections;
32 using System.Collections.Generic;
34 using Microsoft.Build.Framework;
35 using Microsoft.Build.Utilities;
37 namespace Microsoft.Build.Tasks {
38 public class Copy : TaskExtension {
40 ITaskItem[] copiedFiles;
41 ITaskItem[] destinationFiles;
42 ITaskItem destinationFolder;
43 bool skipUnchangedFiles;
44 ITaskItem[] sourceFiles;
50 public override bool Execute ()
53 List <ITaskItem> temporaryCopiedFiles = new List <ITaskItem> ();
55 if (sourceFiles != null && destinationFiles != null &&
56 sourceFiles.Length != destinationFiles.Length)
57 throw new Exception ("Number of source files is different than number of destination files.");
58 if (destinationFiles != null && destinationFolder != null)
59 throw new Exception ("You must specify only one attribute from DestinationFiles and DestinationFolder");
60 if (destinationFiles != null) {
61 for (int i = 0; i < sourceFiles.Length; i ++) {
62 ITaskItem sourceItem = sourceFiles [i];
63 ITaskItem destinationItem = destinationFiles [i];
64 string sourceFile = sourceItem.GetMetadata ("FullPath");
65 string destinationFile = destinationItem.GetMetadata ("FullPath");
67 if (!skipUnchangedFiles || HasFileChanged (sourceFile, destinationFile))
68 CopyFile (sourceFile, destinationFile, true);
70 sourceItem.CopyMetadataTo (destinationItem);
71 temporaryCopiedFiles.Add (destinationItem);
74 } else if (destinationFolder != null) {
75 List<ITaskItem> temporaryDestinationFiles = new List<ITaskItem> ();
76 string destinationDirectory = destinationFolder.GetMetadata ("FullPath");
77 bool directoryCreated = CreateDirectoryIfRequired (destinationDirectory);
79 foreach (ITaskItem sourceItem in sourceFiles) {
80 string sourceFile = sourceItem.GetMetadata ("FullPath");
81 string filename = sourceItem.GetMetadata ("Filename") + sourceItem.GetMetadata ("Extension");
82 string destinationFile = Path.Combine (destinationDirectory,filename);
84 if (!skipUnchangedFiles || directoryCreated ||
85 HasFileChanged (sourceFile, destinationFile))
86 CopyFile (sourceFile, destinationFile, false);
88 temporaryCopiedFiles.Add (new TaskItem (
89 Path.Combine (destinationFolder.GetMetadata ("Identity"), filename),
90 sourceItem.CloneCustomMetadata ()));
92 temporaryDestinationFiles.Add (new TaskItem (
93 Path.Combine (destinationFolder.GetMetadata ("Identity"), filename),
94 sourceItem.CloneCustomMetadata ()));
96 destinationFiles = temporaryDestinationFiles.ToArray ();
98 throw new Exception ("You must specify DestinationFolder or DestinationFiles attribute.");
101 copiedFiles = temporaryCopiedFiles.ToArray ();
105 catch (Exception ex) {
106 Log.LogErrorFromException (ex);
112 public ITaskItem[] CopiedFiles {
119 public ITaskItem[] DestinationFiles {
121 return destinationFiles;
124 destinationFiles = value;
128 public ITaskItem DestinationFolder {
130 return destinationFolder;
133 destinationFolder = value;
137 public bool SkipUnchangedFiles {
139 return skipUnchangedFiles;
142 skipUnchangedFiles = value;
147 public ITaskItem[] SourceFiles {
156 // returns whether directory was created or not
157 bool CreateDirectoryIfRequired (string name)
159 if (Directory.Exists (name))
162 Log.LogMessage ("Creating directory '{0}'", name);
163 Directory.CreateDirectory (name);
167 void CopyFile (string source, string dest, bool create_dir)
170 CreateDirectoryIfRequired (Path.GetDirectoryName (dest));
171 Log.LogMessage ("Copying file from '{0}' to '{1}'", source, dest);
172 File.Copy (source, dest, true);
175 bool HasFileChanged (string source, string dest)
177 if (!File.Exists (dest))
180 FileInfo sourceInfo = new FileInfo (source);
181 FileInfo destinationInfo = new FileInfo (dest);
183 return !(sourceInfo.Length == destinationInfo.Length &&
184 File.GetLastWriteTime(source) <= File.GetLastWriteTime (dest));