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
|
//
// DotNetProjectExtensions.cs
//
// Author:
// Matt Ward <ward.matt@gmail.com>
//
// Copyright (C) 2012-2013 Matthew Ward
//
// 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.Linq;
using System.IO;
using MonoDevelop.Core;
using MonoDevelop.Projects;
using MonoDevelop.Projects.MSBuild;
using NuGet.Common;
using NuGet.Packaging.Core;
using NuGet.ProjectManagement;
using NuGet.ProjectModel;
namespace MonoDevelop.PackageManagement
{
internal static class DotNetProjectExtensions
{
public static readonly Guid WebApplication = Guid.Parse("{349C5851-65DF-11DA-9384-00065B846F21}");
public static readonly Guid WebSite = Guid.Parse("{E24C65DC-7377-472B-9ABA-BC803B73C61A}");
public static Func<string, bool> FileExists = File.Exists;
public static bool IsWebProject(this IDotNetProject project)
{
return project.HasProjectType(WebApplication) || project.HasProjectType(WebSite);
}
public static bool HasProjectType(this IDotNetProject project, Guid projectTypeGuid)
{
foreach (string guid in project.FlavorGuids) {
if (IsMatch(projectTypeGuid, guid)) {
return true;
}
}
return false;
}
static bool IsMatch(Guid guid, string guidStringToMatch)
{
Guid result;
if (Guid.TryParse(guidStringToMatch, out result)) {
return guid == result;
}
return false;
}
public static bool HasPackages (this DotNetProject project)
{
var nugetAwareProject = project as INuGetAwareProject;
if (nugetAwareProject != null)
return nugetAwareProject.HasPackages ();
return HasPackages (project.BaseDirectory, project.Name) || project.HasPackageReferences ();
}
public static string GetPackagesConfigFilePath (this DotNetProject project)
{
return GetPackagesConfigFilePath (project.BaseDirectory, project.Name);
}
public static bool HasPackages (this IDotNetProject project)
{
return AnyFileExists (GetPossiblePackagesConfigOrProjectJsonFilePaths (project.BaseDirectory, project.Name));
}
public static bool HasPackagesConfig (this IDotNetProject project)
{
return FileExists (GetPackagesConfigFilePath (project));
}
static bool HasPackages (string projectDirectory, string projectName)
{
return AnyFileExists (GetPossiblePackagesConfigOrProjectJsonFilePaths (projectDirectory, projectName));
}
static bool AnyFileExists (IEnumerable<string> files)
{
return files.Any (FileExists);
}
static IEnumerable<string> GetPossiblePackagesConfigOrProjectJsonFilePaths (string projectDirectory, string projectName)
{
yield return GetNonDefaultProjectPackagesConfigFilePath (projectDirectory, projectName);
yield return GetDefaultPackagesConfigFilePath (projectDirectory);
yield return ProjectJsonPathUtilities.GetProjectConfigPath (projectDirectory, projectName);
}
static string GetNonDefaultProjectPackagesConfigFilePath (string projectDirectory, string projectName)
{
return Path.Combine (projectDirectory, GetNonDefaultProjectPackagesConfigFileName (projectName));
}
static string GetNonDefaultProjectPackagesConfigFileName (string projectName)
{
return "packages." + projectName.Replace (' ', '_') + ".config";
}
static string GetDefaultPackagesConfigFilePath (string projectDirectory)
{
return Path.Combine (projectDirectory, NuGet.Configuration.NuGetConstants.PackageReferenceFile);
}
public static string GetPackagesConfigFilePath (this IDotNetProject project)
{
return GetPackagesConfigFilePath (project.BaseDirectory, project.Name);
}
static string GetPackagesConfigFilePath (string projectDirectory, string projectName)
{
string nonDefaultPackagesConfigFilePath = GetNonDefaultProjectPackagesConfigFilePath (projectDirectory, projectName);
if (FileExists (nonDefaultPackagesConfigFilePath)) {
return nonDefaultPackagesConfigFilePath;
}
return GetDefaultPackagesConfigFilePath (projectDirectory);
}
public static FilePath GetPackagesFolderPath (this DotNetProject project)
{
var solutionManager = PackageManagementServices.Workspace.GetSolutionManager (project.ParentSolution);
if (solutionManager == null)
return FilePath.Null;
NuGetProject nugetProject = solutionManager.GetNuGetProject (new DotNetProjectProxy (project));
if (nugetProject == null)
return FilePath.Null;
return nugetProject.GetPackagesFolderPath (solutionManager);
}
public static IEnumerable<string> GetDotNetCoreTargetFrameworks (this Project project)
{
foreach (MSBuildPropertyGroup propertyGroup in project.MSBuildProject.PropertyGroups) {
string framework = propertyGroup.GetValue ("TargetFramework", null);
if (framework != null)
return new [] { framework };
string frameworks = propertyGroup.GetValue ("TargetFrameworks", null);
if (frameworks != null)
return frameworks.Split (';');
}
return Enumerable.Empty<string> ();
}
public static bool IsDotNetCoreProject (this Project project)
{
return project.MSBuildProject.GetReferencedSDKs ().Any ();
}
public static bool HasPackageReferences (this DotNetProject project)
{
return project.Items.OfType<ProjectPackageReference> ().Any () ||
project.MSBuildProject.HasEvaluatedPackageReferences ();
}
public static ProjectPackageReference GetPackageReference (
this DotNetProject project,
PackageIdentity packageIdentity,
bool matchVersion = true)
{
return project.Items.OfType<ProjectPackageReference> ()
.FirstOrDefault (projectItem => projectItem.Equals (packageIdentity, matchVersion));
}
public static bool HasPackageReference (this DotNetProject project, string packageId)
{
return project.Items.OfType<ProjectPackageReference> ()
.Any (projectItem => StringComparer.OrdinalIgnoreCase.Equals (projectItem.Include, packageId));
}
public static bool HasPackageReferenceRestoreProjectStyle (this DotNetProject project)
{
string restoreStyle = project.ProjectProperties.GetValue ("RestoreProjectStyle");
return StringComparer.OrdinalIgnoreCase.Equals (restoreStyle, "PackageReference");
}
public static FilePath GetNuGetAssetsFilePath (this DotNetProject project)
{
return project.BaseIntermediateOutputPath.Combine (LockFileFormat.AssetsFileName);
}
public static bool NuGetAssetsFileExists (this DotNetProject project)
{
string assetsFile = project.GetNuGetAssetsFilePath ();
return File.Exists (assetsFile);
}
/// <summary>
/// If a NuGet package is installed into a .NET Core project then all .NET Core projects that
/// reference this project need to have their reference information updated. This allows the
/// assemblies from the NuGet package to be made available to the other projects since .NET
/// Core projects support transitive references. This method calls NotifyModified for each
/// project that references it, as well as for the project itself, passing the hint 'References'
/// which will cause the type system to refresh its reference information, which will be taken
/// from MSBuild.
///
/// All projects that reference .NET Core projects will have their references refreshed. If a
/// .NET Framework project (non SDK), has PackageReferences and references a .NET Standard project
/// (SDK project) then NuGet dependencies from the .NET Standard project are available to the
/// .NET Framework project transitively without needing the NuGet package to be installed into
/// the .NET Framework project. So refreshing the references of any project that references a
/// .NET Core project will ensure assemblies from NuGet packages are available after installing
/// a new NuGet package into the referenced project.
/// </summary>
/// <param name="project">.NET Core project</param>
/// <param name="transitiveOnly">If false then the project passed will also have its
/// references refreshed. Otherwise only the projects that reference the project will
/// have their references refreshed.</param>
public static void DotNetCoreNotifyReferencesChanged (this DotNetProject project, bool transitiveOnly = false)
{
if (!transitiveOnly)
project.NotifyModified ("References");
foreach (var referencingProject in project.GetReferencingProjects ()) {
referencingProject.NotifyModified ("References");
}
}
/// <summary>
/// Returns all projects that directly or indirectly referencing the specified project.
/// </summary>
public static IEnumerable<DotNetProject> GetReferencingProjects (this DotNetProject project)
{
var projects = new List<DotNetProject> ();
var traversedProjects = new Dictionary<string, bool> (StringComparer.OrdinalIgnoreCase);
traversedProjects.Add (project.ItemId, true);
foreach (var currentProject in project.ParentSolution.GetAllDotNetProjects ()) {
if (!traversedProjects.ContainsKey (currentProject.ItemId))
GetReferencingProjects (project, currentProject, traversedProjects, projects);
}
return projects;
}
static bool GetReferencingProjects (
DotNetProject mainProject,
DotNetProject project,
Dictionary<string, bool> traversedProjects,
List<DotNetProject> referencingProjects)
{
foreach (var projectReference in project.References.Where (IncludeProjectReference)) {
var resolvedProject = projectReference.ResolveProject (mainProject.ParentSolution) as DotNetProject;
if (resolvedProject == null)
continue;
if (resolvedProject == mainProject) {
traversedProjects [project.ItemId] = true;
referencingProjects.Add (project);
return true;
}
if (traversedProjects.TryGetValue (resolvedProject.ItemId, out bool referencesProject)) {
if (referencesProject) {
traversedProjects [project.ItemId] = referencesProject;
referencingProjects.Add (project);
return true;
}
continue;
}
if (GetReferencingProjects (mainProject, resolvedProject, traversedProjects, referencingProjects)) {
traversedProjects [project.ItemId] = true;
referencingProjects.Add (project);
return true;
}
}
traversedProjects [project.ItemId] = false;
return false;
}
static bool IncludeProjectReference (ProjectReference projectReference)
{
return projectReference.ReferenceType == ReferenceType.Project &&
projectReference.ReferenceOutputAssembly;
}
public static bool CanUpdatePackages (this DotNetProject project)
{
var nugetAwareProject = project as INuGetAwareProject;
if (nugetAwareProject != null)
return nugetAwareProject.HasPackages ();
return HasPackages (project.BaseDirectory, project.Name) || project.Items.OfType<ProjectPackageReference> ().Any ();
}
public static bool CanRestorePackages (this DotNetProject project)
{
var nugetAwareProject = project as INuGetAwareProject;
if (nugetAwareProject != null)
return nugetAwareProject.HasPackages ();
return HasPackages (project.BaseDirectory, project.Name) ||
DotNetCoreNuGetProject.CanCreate (project) ||
PackageReferenceNuGetProject.CanCreate (project);
}
}
}
|