Skip to content

Commit

Permalink
Generalize project system interactions to CPS
Browse files Browse the repository at this point in the history
We can share behavior across all CPS projects for handling files additions/removals.  CPS is much more efficient for bulk edits - they work from the file system automatically, so we don't have to do batched edits on the UI thread in VS. This is something we should take advantage of for as many project types as possible, not limited to DotNetCoreWeb.

Addresses aspnet#710
  • Loading branch information
jimmylewis committed Sep 19, 2023
1 parent 0234c50 commit dbf2a68
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 6 deletions.
7 changes: 7 additions & 0 deletions src/LibraryManager.Vsix/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ internal static class Constants
public const string ErrorCodeLink = "https://github.com/aspnet/LibraryManager/wiki/Error-codes#{0}";
public const string WAP = "{349C5851-65DF-11DA-9384-00065B846F21}";
public const string WebsiteProject = "{E24C65DC-7377-472B-9ABA-BC803B73C61A}";
/// <summary>
/// Project capability for .NET web projects. Used only for UI context rules.
/// </summary>
public const string DotNetCoreWebCapability = "DotNetCoreWeb";
/// <summary>
/// Project capability for CPS-based projects. Used to determine how to add/remove items to the project.
/// </summary>
public const string CpsCapability = "CPS";
}
}
4 changes: 2 additions & 2 deletions src/LibraryManager.Vsix/Contracts/HostInteraction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,9 @@ public async Task<bool> DeleteFilesAsync (IEnumerable<string> relativeFilePaths,

//Delete from project
Project project = VsHelpers.GetDTEProjectFromConfig(_configFilePath);
bool isCoreProject = await VsHelpers.IsDotNetCoreWebProjectAsync(project);
bool isCpsProject = await VsHelpers.IsCpsProject(project);

if (!isCoreProject)
if (!isCpsProject)
{
var logAction = new Action<string, LogLevel>((message, level) => { Logger.Log(message, level); });
bool deleteFromProject = await VsHelpers.DeleteFilesFromProjectAsync(project, absolutePaths, logAction, cancellationToken);
Expand Down
16 changes: 12 additions & 4 deletions src/LibraryManager.Vsix/Shared/VsHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public static async Task AddFileToProjectAsync(this Project project, string file
{
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();

if (IsCapabilityMatch(project, Constants.DotNetCoreWebCapability))
if (IsCapabilityMatch(project, Constants.CpsCapability))
{
return;
}
Expand Down Expand Up @@ -140,7 +140,7 @@ public static async Task AddFilesToProjectAsync(Project project, IEnumerable<str
{
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync(cancellationToken);

if (project == null || IsCapabilityMatch(project, Constants.DotNetCoreWebCapability))
if (project == null || IsCapabilityMatch(project, Constants.CpsCapability))
{
return;
}
Expand Down Expand Up @@ -244,11 +244,19 @@ public static bool IsKind(this Project project, params string[] kindGuids)
return false;
}

public static async Task<bool> IsDotNetCoreWebProjectAsync(Project project)
/// <summary>
/// Returns whether the DTE project is a CPS-based project
/// </summary>
/// <remarks>
/// This is used for taking advantage of some shortcuts. For example, CPS projects can automatically
/// reflect files on disk, so that rather than using legacy project system APIs on the UI thread,
/// we can interact with the filesystem on a background thread.
/// </remarks>
public static async Task<bool> IsCpsProject(Project project)
{
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();

if (project == null || IsCapabilityMatch(project, Constants.DotNetCoreWebCapability))
if (project == null || IsCapabilityMatch(project, Constants.CpsCapability))
{
return true;
}
Expand Down

0 comments on commit dbf2a68

Please sign in to comment.