* AssemblyResolver.cs:
[mono.git] / mcs / class / Microsoft.Build.Tasks / Microsoft.Build.Tasks / ResolveAssemblyReference.cs
1 //
2 // ResolveAssemblyReference.cs: Searches for assembly files.
3 //
4 // Author:
5 //   Marek Sieradzki (marek.sieradzki@gmail.com)
6 // 
7 // (C) 2006 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.Generic;
32 using System.IO;
33 using System.Reflection;
34 using System.Security;
35 using Microsoft.Build.Framework;
36 using Microsoft.Build.Utilities;
37
38 namespace Microsoft.Build.Tasks {
39         public class ResolveAssemblyReference : TaskExtension {
40         
41                 bool            autoUnify;
42                 ITaskItem[]     assemblyFiles;
43                 ITaskItem[]     assemblies;
44                 string          appConfigFile;
45                 string[]        allowedAssemblyExtensions;
46                 string[]        allowedRelatedFileExtensions;
47                 string[]        candidateAssemblyFiles;
48                 ITaskItem[]     copyLocalFiles;
49                 ITaskItem[]     filesWritten;
50                 bool            findDependencies;
51                 bool            findRelatedFiles;
52                 bool            findSatellites;
53                 bool            findSerializationAssemblies;
54                 string[]        installedAssemblyTables;
55                 ITaskItem[]     relatedFiles;
56                 ITaskItem[]     resolvedDependencyFiles;
57                 ITaskItem[]     resolvedFiles;
58                 ITaskItem[]     satelliteFiles;
59                 ITaskItem[]     scatterFiles;
60                 string[]        searchPaths;
61                 ITaskItem[]     serializationAssemblyFiles;
62                 bool            silent;
63                 string          stateFile;
64                 ITaskItem[]     suggestedRedirects;
65                 string[]        targetFrameworkDirectories;
66                 string          targetProcessorArchitecture;
67
68                 AssemblyResolver        assembly_resolver;
69
70                 public ResolveAssemblyReference ()
71                 {
72                         assembly_resolver = new AssemblyResolver ();
73                 }
74
75                 public override bool Execute ()
76                 {
77                         assembly_resolver.Log = Log;
78                         List <ITaskItem> tempResolvedFiles = new List <ITaskItem> ();
79                 
80                         foreach (ITaskItem item in assemblies) {
81                                 assembly_resolver.ResetSearchLogger ();
82                                 //FIXME: Copy local
83                                 string resolved = null;
84                                 foreach (string spath in searchPaths) {
85                                         bool specific_version;
86                                         if (!TryGetSpecificVersionValue (item, out specific_version))
87                                                 return false;
88
89                                         assembly_resolver.SearchLogger.WriteLine ("For searchpath {0}", spath);
90
91                                         if (String.Compare (spath, "{HintPathFromItem}") == 0) {
92                                                 resolved = assembly_resolver.ResolveHintPathReference (item, specific_version);
93                                         } else if (String.Compare (spath, "{TargetFrameworkDirectory}") == 0) {
94                                                 foreach (string fpath in targetFrameworkDirectories) {
95                                                         resolved = assembly_resolver.FindInTargetFramework (item,
96                                                                         fpath, specific_version);
97                                                         if (resolved != null)
98                                                                 break;
99                                                 }
100                                         } else if (String.Compare (spath, "{GAC}") == 0) {
101                                                 resolved = assembly_resolver.ResolveGacReference (item, specific_version);
102                                         } else if (String.Compare (spath, "{RawFileName}") == 0) {
103                                                 //FIXME: identify assembly names, as extract the name, and try with that?
104                                                 if (assembly_resolver.GetAssemblyNameFromFile (item.ItemSpec) != null)
105                                                         resolved = item.ItemSpec;
106                                         } else {
107                                                 resolved = assembly_resolver.FindInDirectory (item, spath);
108                                         }
109
110                                         if (resolved != null)
111                                                 break;
112                                 }
113
114                                 if (resolved == null) {
115                                         Log.LogWarning ("Reference '{0}' not resolved", item.ItemSpec);
116                                         Log.LogMessage ("{0}", assembly_resolver.SearchLogger.ToString ());
117                                 } else {
118                                         Log.LogMessage (MessageImportance.Low, "Reference {0} resolved to {1}", item.ItemSpec, resolved);
119                                         tempResolvedFiles.Add (new TaskItem (resolved));
120                                 }
121                         }
122                         
123                         resolvedFiles = tempResolvedFiles.ToArray ();
124
125                         return true;
126                 }
127
128                 bool TryGetSpecificVersionValue (ITaskItem item, out bool specific_version)
129                 {
130                         specific_version = true;
131                         string value = item.GetMetadata ("SpecificVersion");
132                         if (String.IsNullOrEmpty (value)) {
133                                 AssemblyName name = new AssemblyName (item.ItemSpec);
134                                 // If SpecificVersion is not specified, then
135                                 // it is true if the Include is a strong name else false
136                                 specific_version = assembly_resolver.IsStrongNamed (name);
137                                 return true;
138                         }
139
140                         if (Boolean.TryParse (value, out specific_version))
141                                 return true;
142
143                         Log.LogError ("Item '{0}' has attribute SpecificVersion with invalid value '{1}' " +
144                                         "which could not be converted to a boolean.", item.ItemSpec, value);
145                         return false;
146                 }
147                 
148                 public bool AutoUnify {
149                         get { return autoUnify; }
150                         set { autoUnify = value; }
151                 }
152                 
153                 public ITaskItem[] AssemblyFiles {
154                         get { return assemblyFiles; }
155                         set { assemblyFiles = value; }
156                 }
157                 
158                 public ITaskItem[] Assemblies {
159                         get { return assemblies; }
160                         set { assemblies = value; }
161                 }
162                 
163                 public string AppConfigFile {
164                         get { return appConfigFile; }
165                         set { appConfigFile = value; }
166                 }
167                 
168                 public string[] AllowedAssemblyExtensions {
169                         get { return allowedAssemblyExtensions; }
170                         set { allowedAssemblyExtensions = value; }
171                 }
172
173                 public string[] AllowedRelatedFileExtensions {
174                         get { return allowedRelatedFileExtensions; }
175                         set { allowedRelatedFileExtensions = value; }
176                 }
177                 
178                 public string[] CandidateAssemblyFiles {
179                         get { return candidateAssemblyFiles; }
180                         set { candidateAssemblyFiles = value; }
181                 }
182                 
183                 [Output]
184                 public ITaskItem[] CopyLocalFiles {
185                         get { return copyLocalFiles; }
186                 }
187                 
188                 [Output]
189                 public ITaskItem[] FilesWritten {
190                         get { return filesWritten; }
191                         set { filesWritten = value; }
192                 }
193                 
194                 public bool FindDependencies {
195                         get { return findDependencies; }
196                         set { findDependencies = value; }
197                 }
198                 
199                 public bool FindRelatedFiles {
200                         get { return findRelatedFiles; }
201                         set { findRelatedFiles = value; }
202                 }
203                 
204                 public bool FindSatellites {
205                         get { return findSatellites; }
206                         set { findSatellites = value; }
207                 }
208                 
209                 public bool FindSerializationAssemblies {
210                         get { return findSerializationAssemblies; }
211                         set { findSerializationAssemblies = value; }
212                 }
213                 
214                 public string[] InstalledAssemblyTables {
215                         get { return installedAssemblyTables; }
216                         set { installedAssemblyTables = value; }
217                 }
218                 
219                 [Output]
220                 public ITaskItem[] RelatedFiles {
221                         get { return relatedFiles; }
222                 }
223                 
224                 [Output]
225                 public ITaskItem[] ResolvedDependencyFiles {
226                         get { return resolvedDependencyFiles; }
227                 }
228                 
229                 [Output]
230                 public ITaskItem[] ResolvedFiles {
231                         get { return resolvedFiles; }
232                 }
233                 
234                 [Output]
235                 public ITaskItem[] SatelliteFiles {
236                         get { return satelliteFiles; }
237                 }
238                 
239                 [Output]
240                 public ITaskItem[] ScatterFiles {
241                         get { return scatterFiles; }
242                 }
243                 
244                 [Required]
245                 public string[] SearchPaths {
246                         get { return searchPaths; }
247                         set { searchPaths = value; }
248                 }
249                 
250                 [Output]
251                 public ITaskItem[] SerializationAssemblyFiles {
252                         get { return serializationAssemblyFiles; }
253                 }
254                 
255                 public bool Silent {
256                         get { return silent; }
257                         set { silent = value; }
258                 }
259                 
260                 public string StateFile {
261                         get { return stateFile; }
262                         set { stateFile = value; }
263                 }
264                 
265                 [Output]
266                 public ITaskItem[] SuggestedRedirects {
267                         get { return suggestedRedirects; }
268                 }
269                 
270                 public string[] TargetFrameworkDirectories {
271                         get { return targetFrameworkDirectories; }
272                         set { targetFrameworkDirectories = value; }
273                 }
274                 
275                 public string TargetProcessorArchitecture {
276                         get { return targetProcessorArchitecture; }
277                         set { targetProcessorArchitecture = value; }
278                 }
279         }
280 }
281
282 #endif