1 // Copyright (c) Microsoft. All rights reserved.
2 // Licensed under the MIT license. See LICENSE file in the project root for full license information.
3 
4 using System;
5 using System.IO;
6 using System.Diagnostics;
7 using System.Resources;
8 using System.Reflection;
9 using Microsoft.Build.Framework;
10 using Microsoft.Build.Utilities;
11 using Microsoft.Build.Shared;
12 
13 namespace Microsoft.Build.Tasks
14 {
15     /// <summary>
16     /// Returns paths to the frameworks SDK.
17     /// </summary>
18     public class GetFrameworkSdkPath : TaskExtension
19     {
20         #region Properties
21 
22         private static string s_path;
23         private static string s_version20Path;
24         private static string s_version35Path;
25         private static string s_version40Path;
26         private static string s_version45Path;
27         private static string s_version451Path;
28         private static string s_version46Path;
29         private static string s_version461Path;
30 
31         /// <summary>
32         /// The path to the latest .NET SDK if it could be found. It will be String.Empty if the SDK was not found.
33         /// </summary>
34         [Output]
35         public string Path
36         {
37             get
38             {
39                 if (s_path == null)
40                 {
41                     s_path = ToolLocationHelper.GetPathToDotNetFrameworkSdk(TargetDotNetFrameworkVersion.Latest, VisualStudioVersion.VersionLatest);
42 
43                     if (String.IsNullOrEmpty(s_path))
44                     {
45                         Log.LogMessageFromResources(
46                             MessageImportance.High,
47                             "GetFrameworkSdkPath.CouldNotFindSDK",
48                             ToolLocationHelper.GetDotNetFrameworkSdkInstallKeyValue(TargetDotNetFrameworkVersion.Latest, VisualStudioVersion.VersionLatest),
49                             ToolLocationHelper.GetDotNetFrameworkSdkRootRegistryKey(TargetDotNetFrameworkVersion.Latest, VisualStudioVersion.VersionLatest)
50                         );
51 
52                         s_path = String.Empty;
53                     }
54                     else
55                     {
56                         s_path = FileUtilities.EnsureTrailingSlash(s_path);
57                         Log.LogMessageFromResources(MessageImportance.Low, "GetFrameworkSdkPath.FoundSDK", s_path);
58                     }
59                 }
60 
61                 return s_path;
62             }
63             set
64             {
65                 // Does nothing; for backwards compatibility only
66             }
67         }
68 
69         /// <summary>
70         /// The path to the v2.0 .NET SDK if it could be found. It will be String.Empty if the SDK was not found.
71         /// </summary>
72         [Output]
73         public string FrameworkSdkVersion20Path
74         {
75             get
76             {
77                 if (s_version20Path == null)
78                 {
79                     s_version20Path = ToolLocationHelper.GetPathToDotNetFrameworkSdk(TargetDotNetFrameworkVersion.Version20);
80 
81                     if (String.IsNullOrEmpty(s_version20Path))
82                     {
83                         Log.LogMessageFromResources(
84                             MessageImportance.High,
85                             "GetFrameworkSdkPath.CouldNotFindSDK",
86                             ToolLocationHelper.GetDotNetFrameworkSdkInstallKeyValue(TargetDotNetFrameworkVersion.Version20),
87                             ToolLocationHelper.GetDotNetFrameworkSdkRootRegistryKey(TargetDotNetFrameworkVersion.Version20)
88                         );
89 
90                         s_version20Path = String.Empty;
91                     }
92                     else
93                     {
94                         s_version20Path = FileUtilities.EnsureTrailingSlash(s_version20Path);
95                         Log.LogMessageFromResources(MessageImportance.Low, "GetFrameworkSdkPath.FoundSDK", s_version20Path);
96                     }
97                 }
98 
99                 return s_version20Path;
100             }
101         }
102 
103         /// <summary>
104         /// The path to the v3.5 .NET SDK if it could be found. It will be String.Empty if the SDK was not found.
105         /// </summary>
106         [Output]
107         public string FrameworkSdkVersion35Path
108         {
109             get
110             {
111                 if (s_version35Path == null)
112                 {
113                     s_version35Path = ToolLocationHelper.GetPathToDotNetFrameworkSdk(TargetDotNetFrameworkVersion.Version35, VisualStudioVersion.VersionLatest);
114 
115                     if (String.IsNullOrEmpty(s_version35Path))
116                     {
117                         Log.LogMessageFromResources(
118                             MessageImportance.High,
119                             "GetFrameworkSdkPath.CouldNotFindSDK",
120                             ToolLocationHelper.GetDotNetFrameworkSdkInstallKeyValue(TargetDotNetFrameworkVersion.Version35, VisualStudioVersion.VersionLatest),
121                             ToolLocationHelper.GetDotNetFrameworkSdkRootRegistryKey(TargetDotNetFrameworkVersion.Version35, VisualStudioVersion.VersionLatest)
122                         );
123 
124                         s_version35Path = String.Empty;
125                     }
126                     else
127                     {
128                         s_version35Path = FileUtilities.EnsureTrailingSlash(s_version35Path);
129                         Log.LogMessageFromResources(MessageImportance.Low, "GetFrameworkSdkPath.FoundSDK", s_version35Path);
130                     }
131                 }
132 
133                 return s_version35Path;
134             }
135         }
136 
137         /// <summary>
138         /// The path to the v4.0 .NET SDK if it could be found. It will be String.Empty if the SDK was not found.
139         /// </summary>
140         [Output]
141         public string FrameworkSdkVersion40Path
142         {
143             get
144             {
145                 if (s_version40Path == null)
146                 {
147                     s_version40Path = ToolLocationHelper.GetPathToDotNetFrameworkSdk(TargetDotNetFrameworkVersion.Version40, VisualStudioVersion.VersionLatest);
148 
149                     if (String.IsNullOrEmpty(s_version40Path))
150                     {
151                         Log.LogMessageFromResources(
152                             MessageImportance.High,
153                             "GetFrameworkSdkPath.CouldNotFindSDK",
154                             ToolLocationHelper.GetDotNetFrameworkSdkInstallKeyValue(TargetDotNetFrameworkVersion.Version40, VisualStudioVersion.VersionLatest),
155                             ToolLocationHelper.GetDotNetFrameworkSdkRootRegistryKey(TargetDotNetFrameworkVersion.Version40, VisualStudioVersion.VersionLatest)
156                         );
157 
158                         s_version40Path = String.Empty;
159                     }
160                     else
161                     {
162                         s_version40Path = FileUtilities.EnsureTrailingSlash(s_version40Path);
163                         Log.LogMessageFromResources(MessageImportance.Low, "GetFrameworkSdkPath.FoundSDK", s_version40Path);
164                     }
165                 }
166 
167                 return s_version40Path;
168             }
169         }
170 
171         /// <summary>
172         /// The path to the v4.5 .NET SDK if it could be found. It will be String.Empty if the SDK was not found.
173         /// </summary>
174         [Output]
175         public string FrameworkSdkVersion45Path
176         {
177             get
178             {
179                 if (s_version45Path == null)
180                 {
181                     s_version45Path = ToolLocationHelper.GetPathToDotNetFrameworkSdk(TargetDotNetFrameworkVersion.Version45, VisualStudioVersion.VersionLatest);
182 
183                     if (String.IsNullOrEmpty(s_version45Path))
184                     {
185                         Log.LogMessageFromResources(
186                             MessageImportance.High,
187                             "GetFrameworkSdkPath.CouldNotFindSDK",
188                             ToolLocationHelper.GetDotNetFrameworkSdkInstallKeyValue(TargetDotNetFrameworkVersion.Version45, VisualStudioVersion.VersionLatest),
189                             ToolLocationHelper.GetDotNetFrameworkSdkRootRegistryKey(TargetDotNetFrameworkVersion.Version45, VisualStudioVersion.VersionLatest)
190                         );
191 
192                         s_version45Path = String.Empty;
193                     }
194                     else
195                     {
196                         s_version45Path = FileUtilities.EnsureTrailingSlash(s_version45Path);
197                         Log.LogMessageFromResources(MessageImportance.Low, "GetFrameworkSdkPath.FoundSDK", s_version45Path);
198                     }
199                 }
200 
201                 return s_version45Path;
202             }
203         }
204 
205         /// <summary>
206         /// The path to the v4.5.1 .NET SDK if it could be found. It will be String.Empty if the SDK was not found.
207         /// </summary>
208         [Output]
209         public string FrameworkSdkVersion451Path
210         {
211             get
212             {
213                 if (s_version451Path == null)
214                 {
215                     s_version451Path = ToolLocationHelper.GetPathToDotNetFrameworkSdk(TargetDotNetFrameworkVersion.Version451, VisualStudioVersion.VersionLatest);
216 
217                     if (String.IsNullOrEmpty(s_version451Path))
218                     {
219                         Log.LogMessageFromResources(
220                             MessageImportance.High,
221                             "GetFrameworkSdkPath.CouldNotFindSDK",
222                             ToolLocationHelper.GetDotNetFrameworkSdkInstallKeyValue(TargetDotNetFrameworkVersion.Version451, VisualStudioVersion.VersionLatest),
223                             ToolLocationHelper.GetDotNetFrameworkSdkRootRegistryKey(TargetDotNetFrameworkVersion.Version451, VisualStudioVersion.VersionLatest)
224                         );
225 
226                         s_version451Path = String.Empty;
227                     }
228                     else
229                     {
230                         s_version451Path = FileUtilities.EnsureTrailingSlash(s_version451Path);
231                         Log.LogMessageFromResources(MessageImportance.Low, "GetFrameworkSdkPath.FoundSDK", s_version451Path);
232                     }
233                 }
234 
235                 return s_version451Path;
236             }
237         }
238 
239         /// <summary>
240         /// The path to the v4.6 .NET SDK if it could be found. It will be String.Empty if the SDK was not found.
241         /// </summary>
242         [Output]
243         public string FrameworkSdkVersion46Path
244         {
245             get
246             {
247                 if (s_version46Path == null)
248                 {
249                     s_version46Path = ToolLocationHelper.GetPathToDotNetFrameworkSdk(TargetDotNetFrameworkVersion.Version46, VisualStudioVersion.VersionLatest);
250 
251                     if (String.IsNullOrEmpty(s_version46Path))
252                     {
253                         Log.LogMessageFromResources(
254                             MessageImportance.High,
255                             "GetFrameworkSdkPath.CouldNotFindSDK",
256                             ToolLocationHelper.GetDotNetFrameworkSdkInstallKeyValue(TargetDotNetFrameworkVersion.Version46, VisualStudioVersion.VersionLatest),
257                             ToolLocationHelper.GetDotNetFrameworkSdkRootRegistryKey(TargetDotNetFrameworkVersion.Version46, VisualStudioVersion.VersionLatest)
258                         );
259 
260                         s_version46Path = String.Empty;
261                     }
262                     else
263                     {
264                         s_version46Path = FileUtilities.EnsureTrailingSlash(s_version46Path);
265                         Log.LogMessageFromResources(MessageImportance.Low, "GetFrameworkSdkPath.FoundSDK", s_version46Path);
266                     }
267                 }
268 
269                 return s_version46Path;
270             }
271         }
272 
273         /// <summary>
274         /// The path to the v4.6.1 .NET SDK if it could be found. It will be String.Empty if the SDK was not found.
275         /// </summary>
276         [Output]
277         public string FrameworkSdkVersion461Path
278         {
279             get
280             {
281                 if (s_version461Path == null)
282                 {
283                     s_version461Path = ToolLocationHelper.GetPathToDotNetFrameworkSdk(TargetDotNetFrameworkVersion.Version461, VisualStudioVersion.VersionLatest);
284 
285                     if (String.IsNullOrEmpty(s_version461Path))
286                     {
287                         Log.LogMessageFromResources(
288                             MessageImportance.High,
289                             "GetFrameworkSdkPath.CouldNotFindSDK",
290                             ToolLocationHelper.GetDotNetFrameworkSdkInstallKeyValue(TargetDotNetFrameworkVersion.Version461, VisualStudioVersion.VersionLatest),
291                             ToolLocationHelper.GetDotNetFrameworkSdkRootRegistryKey(TargetDotNetFrameworkVersion.Version461, VisualStudioVersion.VersionLatest)
292                         );
293 
294                         s_version461Path = String.Empty;
295                     }
296                     else
297                     {
298                         s_version461Path = FileUtilities.EnsureTrailingSlash(s_version461Path);
299                         Log.LogMessageFromResources(MessageImportance.Low, "GetFrameworkSdkPath.FoundSDK", s_version461Path);
300                     }
301                 }
302 
303                 return s_version461Path;
304             }
305         }
306 
307         #endregion
308 
309         #region ITask Members
310 
311         /// <summary>
312         /// Get the SDK.
313         /// </summary>
314         /// <returns>true</returns>
Execute()315         public override bool Execute()
316         {
317             //Does Nothing: getters do all the work
318 
319             return true;
320         }
321 
322         #endregion
323     }
324 }
325