Skip to content

Commit

Permalink
Address feedback from Diagnostics Hub team (#8170)
Browse files Browse the repository at this point in the history
* Add a MefComponent asset to the profiling vsix

* load package from shell (#8169)

---------

Co-authored-by: Adam Yoblick <[email protected]>
  • Loading branch information
StellaHuang95 and AdamYoblick authored Feb 13, 2025
1 parent 2b88856 commit 3e0feb5
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 14 deletions.
21 changes: 14 additions & 7 deletions Python/Product/Profiling/Profiling/CommandArgumentBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,12 @@ internal class CommandArgumentBuilder {
/// <summary>
/// Constructs a <see cref="PythonProfilingCommandArgs"/> based on the provided profiling target.
/// </summary>
public PythonProfilingCommandArgs BuildCommandArgsFromTarget(ProfilingTarget target) {
public PythonProfilingCommandArgs BuildCommandArgsFromTarget(ProfilingTarget target, PythonProfilingPackage pythonProfilingPackage) {
if (target == null) {
return null;
}

try {
var pythonProfilingPackage = PythonProfilingPackage.Instance;
var joinableTaskFactory = pythonProfilingPackage.JoinableTaskFactory;

PythonProfilingCommandArgs command = null;
Expand All @@ -48,7 +47,7 @@ public PythonProfilingCommandArgs BuildCommandArgsFromTarget(ProfilingTarget tar
var explorer = await pythonProfilingPackage.ShowPerformanceExplorerAsync();
var session = explorer.Sessions.AddTarget(target, name, save);

command = SelectBuilder(target, session);
command = SelectBuilder(target, session, pythonProfilingPackage);

});

Expand All @@ -62,20 +61,28 @@ public PythonProfilingCommandArgs BuildCommandArgsFromTarget(ProfilingTarget tar
/// <summary>
/// Select the appropriate builder based on the provided profiling target.
/// </summary>
private PythonProfilingCommandArgs SelectBuilder(ProfilingTarget target, SessionNode session) {
private PythonProfilingCommandArgs SelectBuilder(ProfilingTarget target, SessionNode session, PythonProfilingPackage pythonProfilingPackage) {
var projectTarget = target.ProjectTarget;
var standaloneTarget = target.StandaloneTarget;

if (projectTarget != null) {
return BuildProjectCommandArgs(projectTarget, session);
return BuildProjectCommandArgs(projectTarget, session, pythonProfilingPackage);
} else if (standaloneTarget != null) {
return BuildStandaloneCommandArgs(standaloneTarget, session);
}
return null;
}

private PythonProfilingCommandArgs BuildProjectCommandArgs(ProjectTarget projectTarget, SessionNode session) {
var solution = PythonProfilingPackage.Instance.Solution;
private PythonProfilingCommandArgs BuildProjectCommandArgs(ProjectTarget projectTarget, SessionNode session, PythonProfilingPackage pythonProfilingPackage) {
if (pythonProfilingPackage == null) {
return null;
}

var solution = pythonProfilingPackage.Solution;
if (solution == null) {
return null;
}

var project = solution.EnumerateLoadedPythonProjects()
.SingleOrDefault(p => p.GetProjectIDGuidProperty() == projectTarget.TargetProject);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
// See the Apache Version 2.0 License for specific language governing
// permissions and limitations under the License.

using System.Threading.Tasks;

namespace Microsoft.PythonTools.Profiling {

/// <summary>
Expand All @@ -23,6 +25,6 @@ public interface IPythonProfilerCommandService {
/// <summary>
/// Collects user input via a dialog and converts it into a <see cref="IPythonProfilingCommandArgs"/>.
/// </summary>
IPythonProfilingCommandArgs GetCommandArgsFromUserInput();
Task<IPythonProfilingCommandArgs> GetCommandArgsFromUserInput();
}
}
33 changes: 29 additions & 4 deletions Python/Product/Profiling/Profiling/PythonProfilerCommandService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ namespace Microsoft.PythonTools.Profiling {
using System;
using System.ComponentModel.Composition;
using System.Diagnostics;
using System.Threading.Tasks;
using System.Windows;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.Interop;
using Microsoft.VisualStudio.Threading;

/// <summary>
/// Implements a service to collect user input for profiling and convert to a <see cref="PythonProfilingCommandArgs"/>.
Expand All @@ -39,14 +43,18 @@ public PythonProfilerCommandService() {
/// <returns>
/// A <see cref="PythonProfilingCommandArgs"/> object based on user input, or <c>null</c> if canceled.
/// </returns>
public IPythonProfilingCommandArgs GetCommandArgsFromUserInput() {
public async Task<IPythonProfilingCommandArgs> GetCommandArgsFromUserInput() {
try {
var pythonProfilingPackage = PythonProfilingPackage.Instance;
var pythonProfilingPackage = await GetPythonProfilingPackageAsync();
if (pythonProfilingPackage == null) {
return null;

}
var targetView = new ProfilingTargetView(pythonProfilingPackage);

if (_userInputDialog.ShowDialog(targetView)) {
if (_userInputDialog.ShowDialog(targetView, pythonProfilingPackage)) {
var target = targetView.GetTarget();
return _commandArgumentBuilder.BuildCommandArgsFromTarget(target);
return _commandArgumentBuilder.BuildCommandArgsFromTarget(target, pythonProfilingPackage);
}
} catch (Exception ex) {
Debug.Fail($"Error displaying user input dialog: {ex.Message}");
Expand All @@ -55,5 +63,22 @@ public IPythonProfilingCommandArgs GetCommandArgsFromUserInput() {

return null;
}

private async Task<PythonProfilingPackage> GetPythonProfilingPackageAsync() {
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();

var shell = await ServiceProvider.GetGlobalServiceAsync(typeof(SVsShell)) as IVsShell;
if (shell != null) {
var packageGuid = typeof(PythonProfilingPackage).GUID;
int hr = shell.LoadPackage(ref packageGuid, out var packageObj);

Debug.WriteLine($"LoadPackage result: {hr}"); // Log HRESULT result

if (packageObj != null) {
return packageObj as PythonProfilingPackage;
}
}
return null;
}
}
}
3 changes: 1 addition & 2 deletions Python/Product/Profiling/Profiling/UserInputDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@

namespace Microsoft.PythonTools.Profiling {
internal class UserInputDialog {
public bool ShowDialog(ProfilingTargetView targetView) {
var pythonProfilingPackage = PythonProfilingPackage.Instance;
public bool ShowDialog(ProfilingTargetView targetView, PythonProfilingPackage pythonProfilingPackage) {
var dialog = new LaunchProfiling(pythonProfilingPackage, targetView);
return dialog.ShowModal() ?? false;
}
Expand Down
1 change: 1 addition & 0 deletions Python/Product/Profiling/PythonProfilingPackage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ namespace Microsoft.PythonTools.Profiling {
NameResourceID = 105,
DefaultName = "PythonPerfSession")]
[ProvideAutomationObject("PythonProfiling")]
[ProvideService(typeof(PythonProfilingPackage), IsAsyncQueryable = true)]
internal sealed class PythonProfilingPackage : AsyncPackage {
internal static PythonProfilingPackage Instance;
private static ProfiledProcess _profilingProcess; // process currently being profiled
Expand Down
1 change: 1 addition & 0 deletions Python/Product/Profiling/source.extension.vsixmanifest
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,6 @@
<Assets>
<Asset Type="Microsoft.VisualStudio.Package" Path="|%CurrentProject%;PkgdefProjectOutputGroup|" />
<Asset Type="Microsoft.VisualStudio.Assembly" Path="|%CurrentProject%;_GetTargetPath|" AssemblyName="|%CurrentProject%;_GetAssemblyName|" />
<Asset Type="Microsoft.VisualStudio.MefComponent" Path="|%CurrentProject%;_GetTargetPath|" />
</Assets>
</PackageManifest>

0 comments on commit 3e0feb5

Please sign in to comment.