Fix bug #458916.
[mono.git] / mcs / class / Microsoft.Build.Tasks / Microsoft.Build.Tasks / Copy.cs
1 //
2 // Copy.cs: Task that can copy files
3 //
4 // Author:
5 //   Marek Sieradzki (marek.sieradzki@gmail.com)
6 //
7 // (C) 2005 Marek Sieradzki
8 //
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:
16 //
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 //
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.
27
28 #if NET_2_0
29
30 using System;
31 using System.Collections;
32 using System.Collections.Generic;
33 using System.IO;
34 using Microsoft.Build.Framework;
35 using Microsoft.Build.Utilities;
36
37 namespace Microsoft.Build.Tasks {
38         public class Copy : TaskExtension {
39         
40                 ITaskItem[]     copiedFiles;
41                 ITaskItem[]     destinationFiles;
42                 ITaskItem       destinationFolder;
43                 bool            skipUnchangedFiles;
44                 ITaskItem[]     sourceFiles;
45                 
46                 public Copy ()
47                 {
48                 }
49
50                 public override bool Execute ()
51                 {
52                         try {
53                                 List <ITaskItem> temporaryCopiedFiles = new List <ITaskItem> ();
54                         
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");
66
67                                                 if (!skipUnchangedFiles || HasFileChanged (sourceFile, destinationFile))
68                                                         CopyFile (sourceFile, destinationFile, true);
69
70                                                 sourceItem.CopyMetadataTo (destinationItem);
71                                                 temporaryCopiedFiles.Add (destinationItem);
72                                         }
73                                         
74                                 } else if (destinationFolder != null) {
75                                         List<ITaskItem> temporaryDestinationFiles = new List<ITaskItem> ();
76                                         string destinationDirectory = destinationFolder.GetMetadata ("FullPath");
77                                         bool directoryCreated = CreateDirectoryIfRequired (destinationDirectory);
78                                         
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);
83
84                                                 if (!skipUnchangedFiles || directoryCreated ||
85                                                         HasFileChanged (sourceFile, destinationFile))
86                                                         CopyFile (sourceFile, destinationFile, false);
87
88                                                 temporaryCopiedFiles.Add (new TaskItem (
89                                                                 Path.Combine (destinationFolder.GetMetadata ("Identity"), filename),
90                                                                 sourceItem.CloneCustomMetadata ()));
91
92                                                 temporaryDestinationFiles.Add (new TaskItem (
93                                                                 Path.Combine (destinationFolder.GetMetadata ("Identity"), filename),
94                                                                 sourceItem.CloneCustomMetadata ()));
95                                         }
96                                         destinationFiles = temporaryDestinationFiles.ToArray ();
97                                 } else {
98                                         throw new Exception ("You must specify DestinationFolder or DestinationFiles attribute.");
99                                 }
100                                 
101                                 copiedFiles = temporaryCopiedFiles.ToArray ();
102
103                                 return true;
104                         }
105                         catch (Exception ex) {
106                                 Log.LogErrorFromException (ex);
107                                 return false;
108                         }
109                 }
110
111                 [Output]
112                 public ITaskItem[] CopiedFiles {
113                         get {
114                                 return copiedFiles;
115                         }
116                 }
117
118                 [Output]
119                 public ITaskItem[] DestinationFiles {
120                         get {
121                                 return destinationFiles;
122                         }
123                         set {
124                                 destinationFiles = value;
125                         }
126                 }
127
128                 public ITaskItem DestinationFolder {
129                         get {
130                                 return destinationFolder;
131                         }
132                         set {
133                                 destinationFolder = value;
134                         }
135                 }
136
137                 public bool SkipUnchangedFiles {
138                         get {
139                                 return skipUnchangedFiles;
140                         }
141                         set {
142                                 skipUnchangedFiles = value;
143                         }
144                 }
145
146                 [Required]
147                 public ITaskItem[] SourceFiles {
148                         get {
149                                 return sourceFiles;
150                         }
151                         set {
152                                 sourceFiles = value;
153                         }
154                 }
155
156                 // returns whether directory was created or not
157                 bool CreateDirectoryIfRequired (string name)
158                 {
159                         if (Directory.Exists (name))
160                                 return false;
161
162                         Log.LogMessage ("Creating directory '{0}'", name);
163                         Directory.CreateDirectory (name);
164                         return true;
165                 }
166
167                 void CopyFile (string source, string dest, bool create_dir)
168                 {
169                         if (create_dir)
170                                 CreateDirectoryIfRequired (Path.GetDirectoryName (dest));
171                         Log.LogMessage ("Copying file from '{0}' to '{1}'", source, dest);
172                         File.Copy (source, dest, true);
173                 }
174
175                 bool HasFileChanged (string source, string dest)
176                 {
177                         if (!File.Exists (dest))
178                                 return true;
179
180                         FileInfo sourceInfo = new FileInfo (source);
181                         FileInfo destinationInfo = new FileInfo (dest);
182
183                         return !(sourceInfo.Length == destinationInfo.Length &&
184                                         File.GetLastWriteTime(source) <= File.GetLastWriteTime (dest));
185                 }
186
187         }
188 }
189
190 #endif