|
| 1 | +import * as os from "os"; |
| 2 | +import * as path from "path"; |
| 3 | + |
1 | 4 | import * as vscode from "vscode";
|
2 | 5 | import { Range } from "vscode-languageclient/node";
|
3 | 6 |
|
@@ -737,6 +740,74 @@ export class RubyLsp {
|
737 | 740 | );
|
738 | 741 | }
|
739 | 742 | }),
|
| 743 | + vscode.commands.registerCommand(Command.ProfileCurrentFile, async () => { |
| 744 | + const workspace = this.currentActiveWorkspace(); |
| 745 | + |
| 746 | + if (!workspace) { |
| 747 | + vscode.window.showInformationMessage("No workspace found"); |
| 748 | + return; |
| 749 | + } |
| 750 | + |
| 751 | + try { |
| 752 | + const { stdout } = await workspace.execute("vernier --version"); |
| 753 | + const version = stdout.trim(); |
| 754 | + const [major, minor, _] = version.split(".").map(Number); |
| 755 | + |
| 756 | + if (major < 1 || (major === 1 && minor < 8)) { |
| 757 | + const install = await vscode.window.showInformationMessage( |
| 758 | + "Vernier version 1.8.0 or higher is required for profiling. Would you like to install it?", |
| 759 | + "Install", |
| 760 | + ); |
| 761 | + |
| 762 | + if (install === "Install") { |
| 763 | + await workspace.execute("gem install vernier"); |
| 764 | + } else { |
| 765 | + return; |
| 766 | + } |
| 767 | + } |
| 768 | + } catch (error) { |
| 769 | + const install = await vscode.window.showInformationMessage( |
| 770 | + "Vernier is required for profiling. Would you like to install it?", |
| 771 | + "Install", |
| 772 | + ); |
| 773 | + |
| 774 | + if (install === "Install") { |
| 775 | + await workspace.execute("gem install vernier"); |
| 776 | + } else { |
| 777 | + return; |
| 778 | + } |
| 779 | + } |
| 780 | + |
| 781 | + const currentFile = vscode.window.activeTextEditor?.document.uri.fsPath; |
| 782 | + |
| 783 | + if (!currentFile) { |
| 784 | + vscode.window.showInformationMessage( |
| 785 | + "No file opened in the editor to profile", |
| 786 | + ); |
| 787 | + return; |
| 788 | + } |
| 789 | + |
| 790 | + await vscode.window.withProgress( |
| 791 | + { |
| 792 | + location: vscode.ProgressLocation.Notification, |
| 793 | + title: "Profiling in progress...", |
| 794 | + cancellable: false, |
| 795 | + }, |
| 796 | + async () => { |
| 797 | + const profileUri = vscode.Uri.file( |
| 798 | + path.join(os.tmpdir(), `profile-${Date.now()}.cpuprofile`), |
| 799 | + ); |
| 800 | + |
| 801 | + await workspace.execute( |
| 802 | + `vernier run --output ${profileUri.fsPath} --format cpuprofile -- ruby ${currentFile}`, |
| 803 | + ); |
| 804 | + |
| 805 | + await vscode.commands.executeCommand("vscode.open", profileUri, { |
| 806 | + viewColumn: vscode.ViewColumn.Beside, |
| 807 | + }); |
| 808 | + }, |
| 809 | + ); |
| 810 | + }), |
740 | 811 | ];
|
741 | 812 | }
|
742 | 813 |
|
|
0 commit comments