1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
|
//
// SdkProjectExtension.cs
//
// Author:
// Matt Ward <matt.ward@microsoft.com>
//
// Copyright (c) 2019 Microsoft
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Threading.Tasks;
using MonoDevelop.Core;
using MonoDevelop.Core.Assemblies;
using MonoDevelop.Core.Execution;
using MonoDevelop.Projects.MSBuild;
namespace MonoDevelop.Projects
{
[ExportProjectModelExtension]
public class SdkProjectExtension : DotNetProjectExtension
{
HashSet<(string Name, string Include)> evaluatedItems;
MSBuildSdkProject msbuildSdkProject = new MSBuildSdkProject ();
string[] cachedBuildActions;
internal static bool IsSupportedProjectFileExtension (FilePath file)
{
return file.HasExtension (".csproj") || file.HasExtension (".fsproj") || file.HasExtension (".vbproj");
}
/// <summary>
/// HACK: Hide certain files in Solution window. The solution's .userprefs
/// file is the only file is included properly with the .NET Core MSBuild
/// targets.
/// </summary>
internal static bool FileShouldBeHidden (FilePath file)
{
return file.HasExtension (".userprefs") || file.HasFileName (".DS_Store");
}
public IEnumerable<string> TargetFrameworks {
get { return msbuildSdkProject.TargetFrameworks; }
}
internal protected override bool SupportsObject (WorkspaceObject item)
{
return base.SupportsObject (item) && IsSdkProject ((DotNetProject)item);
}
bool IsSdkProject (DotNetProject project)
{
return project.MSBuildProject.GetReferencedSDKs ().Length > 0;
}
internal protected override void OnReadProjectHeader (ProgressMonitor monitor, MSBuildProject msproject)
{
// Do not read the project header when re-evaluating to prevent the
// ToolsVersion that was initially read from the project being changed.
if (!Project.IsReevaluating)
msbuildSdkProject.ReadProjectHeader (msproject);
base.OnReadProjectHeader (monitor, msproject);
}
internal protected override void OnReadProject (ProgressMonitor monitor, MSBuildProject msproject)
{
msbuildSdkProject.AddKnownItemAttributes (Project.MSBuildProject);
base.OnReadProject (monitor, msproject);
msbuildSdkProject.ReadProject (msproject, Project.TargetFramework.Id);
if (!msbuildSdkProject.IsOutputTypeDefined)
Project.CompileTarget = msbuildSdkProject.DefaultCompileTarget;
Project.UseAdvancedGlobSupport = true;
Project.UseDefaultMetadataForExcludedExpandedItems = true;
Project.UseFileWatcher = true;
cachedBuildActions = null;
evaluatedItems = null;
}
internal protected override void OnWriteProject (ProgressMonitor monitor, MSBuildProject msproject)
{
base.OnWriteProject (monitor, msproject);
msbuildSdkProject.WriteProject (msproject, Project.TargetFramework.Id);
}
internal protected override void OnPrepareForEvaluation (MSBuildProject project)
{
base.OnPrepareForEvaluation (project);
var referencedSdks = project.GetReferencedSDKs ();
msbuildSdkProject.HasSdk = referencedSdks.Length > 0;
}
internal protected override async Task<ImmutableArray<ProjectFile>> OnGetSourceFiles (ProgressMonitor monitor, ConfigurationSelector configuration)
{
var sourceFiles = await base.OnGetSourceFiles (monitor, configuration);
// Ensure generated assembly info file is available to type system. It is created in the obj
// directory and is excluded from the project with a wildcard exclude but the type system needs it to
// ensure the project's assembly information is correct to prevent diagnostic errors.
var generatedAssemblyInfoFile = GetGeneratedAssemblyInfoFile (configuration);
if (generatedAssemblyInfoFile != null) {
return sourceFiles.Add (generatedAssemblyInfoFile);
}
return sourceFiles;
}
ProjectFile GetGeneratedAssemblyInfoFile (ConfigurationSelector configuration)
{
var projectConfig = configuration.GetConfiguration (Project) as ProjectConfiguration;
if (projectConfig == null)
return null;
bool generateAssemblyInfo = projectConfig.Properties.GetValue ("GenerateAssemblyInfo", true);
FilePath assemblyInfoFile = projectConfig.Properties.GetPathValue ("GeneratedAssemblyInfoFile");
if (generateAssemblyInfo && assemblyInfoFile.IsNotNull)
return new ProjectFile (assemblyInfoFile, BuildAction.Compile);
return null;
}
internal protected override void OnSetFormat (MSBuildFileFormat format)
{
// Do not call base class since the solution's FileFormat will be used which is
// VS 2012 and this will set the ToolsVersion to "4.0" which we are preventing.
// Setting the ToolsVersion to "4.0" can cause the MSBuild tasks such as
// ResolveAssemblyReferences to fail for .NET Core projects when the project
// xml is generated in memory for the project builder at the same time as the
// project file is being saved.
}
bool? isFSharpSdkProject;
bool IsLegacyFSharpSdkProject ()
{
if (isFSharpSdkProject is null) {
isFSharpSdkProject = ContainsFSharpSdk (Project.MSBuildProject.GetReferencedSDKs ());
}
return isFSharpSdkProject.Value;
static bool ContainsFSharpSdk (string[] sdks)
{
for (var i = 0; i < sdks.Length; i++) {
if (sdks [i].Contains ("FSharp"))
return true;
}
return false;
}
}
internal protected override bool OnGetSupportsImportedItem (IMSBuildItemEvaluated buildItem)
{
if (!IsBuildActionSupported (buildItem.Name))
return false;
if (IsLegacyFSharpSdkProject ()) {
// Ignore imported F# files. F# files are defined in the main project.
// This prevents duplicate F# files when a new project is first created.
if (buildItem.Include.EndsWith (".fs", StringComparison.OrdinalIgnoreCase))
return false;
}
if (IsFromSharedProject (buildItem))
return false;
evaluatedItems ??= CreateEvaluatedItemsCache (Project.MSBuildProject);
return evaluatedItems.Contains ((buildItem.Name, buildItem.Include));
static HashSet<(string, string)> CreateEvaluatedItemsCache (MSBuildProject project)
=> new HashSet<(string Name, string Include)> (
project.EvaluatedItems
.Where (x => x.IsImported)
.Select (x => (x.Name, x.Include))
);
}
/// <summary>
/// Cache the build actions here since adding new files on loading the project will clear the cache
/// so we avoid re-building the Project's build action cache potentially multiple times. This local
/// cache is cleared in OnReadProject.
/// </summary>
bool IsBuildActionSupported (string buildAction)
{
if (cachedBuildActions == null)
cachedBuildActions = Project.GetBuildActions ().Where (a => a != "Folder" && a != "--").ToArray ();
return cachedBuildActions.Contains (buildAction);
}
/// <summary>
/// Checks that the project has the HasSharedItems property set to true and the SharedGUID
/// property in its global property group. Otherwise it is not considered to be a shared project.
/// </summary>
bool IsFromSharedProject (IMSBuildItemEvaluated buildItem)
{
var globalGroup = buildItem?.SourceItem?.ParentProject?.GetGlobalPropertyGroup ();
return globalGroup?.GetValue<bool> ("HasSharedItems") == true &&
globalGroup?.HasProperty ("SharedGUID") == true;
}
/// <summary>
/// HACK: Hide certain files that are currently being added to the Solution window.
/// </summary>
internal protected override void OnItemsAdded (IEnumerable<ProjectItem> objs)
{
if (Project.Loading) {
UpdateHiddenFiles (objs.OfType<ProjectFile> ());
}
base.OnItemsAdded (objs);
}
void UpdateHiddenFiles (IEnumerable<ProjectFile> files)
{
foreach (var file in files) {
if (FileShouldBeHidden (file.FilePath))
file.Flags = ProjectItemFlags.Hidden;
}
}
internal protected override async Task OnReevaluateProject (ProgressMonitor monitor)
{
await base.OnReevaluateProject (monitor);
isFSharpSdkProject = null;
UpdateHiddenFiles (Project.Files);
}
/// <summary>
/// ASP.NET Core projects have different build actions if the file is in the wwwroot folder.
/// It also uses Content build actions for *.json, *.config and *.cshtml files. To support
/// this the default file globs for the file are found and the MSBuild item name is returned.
/// </summary>
internal protected override string OnGetDefaultBuildAction (string fileName)
{
string include = MSBuildProjectService.ToMSBuildPath (Project.ItemDirectory, fileName);
var globItems = Project.MSBuildProject.FindGlobItemsIncludingFile (include).ToList ();
if (globItems.Count == 1)
return globItems [0].Name;
return base.OnGetDefaultBuildAction (fileName);
}
protected internal override IEnumerable<ExecutionTarget> OnGetExecutionTargets (
OperationContext ctx,
ConfigurationSelector configuration,
SolutionItemRunConfiguration runConfig)
{
if (Project.HasMultipleTargetFrameworks) {
return GetMultipleTargetFrameworkExecutionTargets (ctx, configuration, runConfig);
}
return base.OnGetExecutionTargets (ctx, configuration, runConfig);
}
/// <summary>
/// This is only called if the main project has a CompileTarget which is not Library. Does not handle
/// configurations which have different CompileTargets.
/// </summary>
IEnumerable<ExecutionTarget> GetMultipleTargetFrameworkExecutionTargets (
OperationContext ctx,
ConfigurationSelector configuration,
SolutionItemRunConfiguration runConfig)
{
var targets = new List<ExecutionTarget> (Project.TargetFrameworkMonikers.Length);
foreach (TargetFrameworkMoniker framework in Project.TargetFrameworkMonikers) {
var target = new TargetFrameworkExecutionTarget (framework);
targets.Add (target);
}
return targets;
}
protected internal override Task OnExecute (
ProgressMonitor monitor,
ExecutionContext context,
ConfigurationSelector configuration,
SolutionItemRunConfiguration runConfiguration)
{
TargetFrameworkMoniker framework = Project.TargetFramework.Id;
if (Project.HasMultipleTargetFrameworks) {
var frameworkContext = context?.ExecutionTarget as TargetFrameworkExecutionTarget;
if (frameworkContext != null) {
framework = frameworkContext.Framework;
if (!(configuration is DotNetProjectFrameworkConfigurationSelector))
configuration = new DotNetProjectFrameworkConfigurationSelector (configuration, frameworkContext.FrameworkShortName);
}
}
if (IsSupportedFramework (framework))
return OnExecute (monitor, context, configuration, framework, runConfiguration);
return base.OnExecute (monitor, context, configuration, runConfiguration);
}
/// <summary>
/// Used by SdkProjectExtensions to indicate they support the target framework. Called to check if
/// an action, such as OnExecute should be called for a particular target framework in a multi-target
/// framework project.
/// </summary>
protected virtual bool IsSupportedFramework (TargetFrameworkMoniker framework)
{
return true;
}
protected internal virtual Task OnExecute (
ProgressMonitor monitor,
ExecutionContext context,
ConfigurationSelector configuration,
TargetFrameworkMoniker framework,
SolutionItemRunConfiguration runConfiguration)
{
return base.OnExecute (monitor, context, configuration, runConfiguration);
}
protected internal override ExecutionCommand OnCreateExecutionCommand (
ConfigurationSelector configSel,
DotNetProjectConfiguration configuration,
ProjectRunConfiguration runConfiguration)
{
if (IsSupportedFramework (configuration.TargetFramework.Id))
return OnCreateExecutionCommand (configSel, configuration, configuration.TargetFramework.Id, runConfiguration);
return base.OnCreateExecutionCommand (configSel, configuration, runConfiguration);
}
protected internal virtual ExecutionCommand OnCreateExecutionCommand (
ConfigurationSelector configSel,
DotNetProjectConfiguration configuration,
TargetFrameworkMoniker framework,
ProjectRunConfiguration runConfiguration)
{
return base.OnCreateExecutionCommand (configSel, configuration, runConfiguration);
}
}
}
|