Skip to content

Commit

Permalink
cleanup and refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Irame committed Apr 18, 2023
1 parent 84937f9 commit 14a8628
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 31 deletions.
10 changes: 5 additions & 5 deletions SmartCmdArgs/SmartCmdArgs.Shared/CmdArgsPackage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,13 +125,13 @@ private void CPSCreateAndSetProfileIfUsed(Guid ProjectGuid) {
if (!CPSCustomProjectEnabled || ProjectGuid == Guid.Empty)
return;

if (! ToolWindowViewModel.TreeViewModel.AllItems.Any(a=>a is CmdArgument))
return;

var ivProject = vsHelper.HierarchyForProjectGuid(ProjectGuid);
if (!ivProject.IsCpsProject())
return;

if (!ToolWindowViewModel.TreeViewModel.AllArguments.Any())
return;

var project = ivProject.GetProject();
if (CpsProjectSupport.IsActiveLaunchProfileCustomProfile(project))
return;
Expand Down Expand Up @@ -551,8 +551,6 @@ private void UpdateCommandsForProject(IVsHierarchy project)
return;
}

CPSCreateAndSetProfileIfUsed(project.GetGuid());

var solutionData = toolWindowStateLoadedFromSolution ?? new SuoDataJson();

// joins data from solution and project
Expand Down Expand Up @@ -674,6 +672,8 @@ private void UpdateCommandsForProject(IVsHierarchy project)
// push projectData to the ViewModel
ToolWindowViewModel.PopulateFromProjectData(project, projectData);

CPSCreateAndSetProfileIfUsed(project.GetGuid());

Logger.Info($"Updated Commands for project '{project.GetName()}'.");
}

Expand Down
53 changes: 30 additions & 23 deletions SmartCmdArgs/SmartCmdArgs.Shared/Helper/CpsProjectSupport.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
using Microsoft.VisualStudio.ProjectSystem;
using Microsoft.VisualStudio.ProjectSystem.Debug;
using Microsoft.VisualStudio.ProjectSystem.Properties;
using Microsoft.VisualStudio.RpcContracts.Build;
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace SmartCmdArgs.Helper
{
public static class CpsProjectSupport
{
public const string CustomProfileName = "Smart CLI Args";

private static bool TryGetProjectServices(EnvDTE.Project project, out IUnconfiguredProjectServices unconfiguredProjectServices, out IProjectServices projectServices)
{
IVsBrowseObjectContext context = project as IVsBrowseObjectContext;
Expand Down Expand Up @@ -84,19 +88,24 @@ public static void SetCpsProjectArguments(EnvDTE.Project project, string argumen
if (activeLaunchProfile == null)
return;

WritableLaunchProfile writableLaunchProfile = new WritableLaunchProfile(activeLaunchProfile, cpsUseCustomProfile);
WritableLaunchProfile writableLaunchProfile = new WritableLaunchProfile(activeLaunchProfile);
writableLaunchProfile.CommandLineArgs = arguments;

if (cpsUseCustomProfile)
{
writableLaunchProfile.Name = CustomProfileName;
writableLaunchProfile.DoNotPersist = true;
}

// Does not work on VS2015, which should be okay ...
// We don't hold references for VS2015, where the interface is called IThreadHandling
IProjectThreadingService projectThreadingService = projectServices.ThreadingPolicy;
projectThreadingService.ExecuteSynchronously(() =>
projectThreadingService.ExecuteSynchronously(async () =>
{
var task = launchSettingsProvider.AddOrUpdateProfileAsync(writableLaunchProfile, addToFront: false);
if (setActive)
task = task.ContinueWith(_ => launchSettingsProvider.SetActiveProfileAsync(writableLaunchProfile.Name));
await launchSettingsProvider.AddOrUpdateProfileAsync(writableLaunchProfile, addToFront: false);
return task;
if (setActive)
await launchSettingsProvider.SetActiveProfileAsync(writableLaunchProfile.Name);
});
}
}
Expand Down Expand Up @@ -129,7 +138,7 @@ public static Dictionary<string, string> GetCpsProjectAllArguments(EnvDTE.Projec

class WritableLaunchProfile : ILaunchProfile
#if VS17
, Microsoft.VisualStudio.ProjectSystem.Debug.IPersistOption
, IPersistOption
#endif
{
public string Name { set; get; }
Expand All @@ -141,32 +150,30 @@ class WritableLaunchProfile : ILaunchProfile
public string LaunchUrl { set; get; }
public ImmutableDictionary<string, string> EnvironmentVariables { set; get; }
public ImmutableDictionary<string, object> OtherSettings { set; get; }

// IPersistOption
public bool DoNotPersist { get; set; }

public const string CustomProfileName = "Smart CLI Args";
public WritableLaunchProfile(ILaunchProfile launchProfile, bool cpsUseCustomProfile)
// copy constructor
public WritableLaunchProfile(ILaunchProfile launchProfile)
{
if (!cpsUseCustomProfile)
{
Name = launchProfile.Name;
CommandLineArgs = launchProfile.CommandLineArgs;
} else
{
Name = CustomProfileName;
DoNotPersist = true;
if (launchProfile.Name == CustomProfileName)
CommandLineArgs = launchProfile.CommandLineArgs;
else
CommandLineArgs = "";//ensure we dont get into an odd state where hidden args are being applied

}
Name = launchProfile.Name;
ExecutablePath = launchProfile.ExecutablePath;
CommandName = launchProfile.CommandName;
CommandLineArgs = launchProfile.CommandLineArgs;
WorkingDirectory = launchProfile.WorkingDirectory;
LaunchBrowser = launchProfile.LaunchBrowser;
LaunchUrl = launchProfile.LaunchUrl;
EnvironmentVariables = launchProfile.EnvironmentVariables;
OtherSettings = launchProfile.OtherSettings;

#if VS17
if (launchProfile is IPersistOption persistOptionLaunchProfile)
{
// IPersistOption
DoNotPersist = persistOptionLaunchProfile.DoNotPersist;
}
#endif
}
}
}
6 changes: 3 additions & 3 deletions SmartCmdArgs/SmartCmdArgs.Shared/Helper/ProjectArguments.cs
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ private static void GetCpsProjectAllArguments(EnvDTE.Project project, List<CmdAr
} },
// C# - Lagacy DotNetCore
{ProjectKinds.CSCore, new ProjectArgumentsHandlers() {
SetArguments = (project, arguments) => SetCpsProjectArguments(project, arguments, false),
SetArguments = (project, arguments) => SetCpsProjectArguments(project, arguments, cpsUseCustomProfile: false),
GetAllArguments = (project, allArgs) => GetCpsProjectAllArguments(project, allArgs)
} },
// F#
Expand Down Expand Up @@ -356,12 +356,12 @@ public static void AddAllArguments(IVsHierarchy project, List<CmdArgumentJson> a
}
}

public static void SetArguments(IVsHierarchy project, string arguments, bool CpsUseCustomProfile=false)
public static void SetArguments(IVsHierarchy project, string arguments, bool cpsUseCustomProfile)
{
if (project.IsCpsProject())
{
Logger.Info($"Setting arguments on CPS project of type '{project.GetKind()}'.");
SetCpsProjectArguments(project.GetProject(), arguments, CpsUseCustomProfile);
SetCpsProjectArguments(project.GetProject(), arguments, cpsUseCustomProfile);
}
else
{
Expand Down

0 comments on commit 14a8628

Please sign in to comment.