Skip to content
This repository has been archived by the owner on May 30, 2024. It is now read-only.

Add code lens for running migrations #1015

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,12 @@
"category": "Ruby LSP",
"when": "editorActive && editorLangId == ruby"
},
{
"command": "rubyLsp.runMigrationInTerminal",
"title": "Run current migration in terminal",
"category": "Ruby LSP",
"when": "editorActive && editorLangId == ruby"
},
{
"command": "rubyLsp.showSyntaxTree",
"title": "Show syntax tree",
Expand Down
1 change: 1 addition & 0 deletions src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export enum Command {
RunTest = "rubyLsp.runTest",
RunTestInTerminal = "rubyLsp.runTestInTerminal",
DebugTest = "rubyLsp.debugTest",
RunMigrationInTerminal = "rubyLsp.runMigrationInTerminal",
OpenLink = "rubyLsp.openLink",
ShowSyntaxTree = "rubyLsp.showSyntaxTree",
}
Expand Down
64 changes: 64 additions & 0 deletions src/migrationController.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import * as vscode from "vscode";

import { Telemetry } from "./telemetry";
import { Workspace } from "./workspace";

export class MigrationController {
private terminal: vscode.Terminal | undefined;
private readonly telemetry: Telemetry;
private readonly currentWorkspace: () => Workspace | undefined;

constructor(
_context: vscode.ExtensionContext,
telemetry: Telemetry,
currentWorkspace: () => Workspace | undefined,
) {
this.telemetry = telemetry;
this.currentWorkspace = currentWorkspace;

vscode.window.onDidCloseTerminal((terminal: vscode.Terminal): void => {
if (terminal === this.terminal) this.terminal = undefined;
});
}

async runMigrationInTerminal(command?: string) {
// eslint-disable-next-line no-param-reassign
command ??= "bin/rails db:migrate";

if (this.terminal === undefined) {
this.terminal = this.getTerminal();
}

this.terminal.show();
this.terminal.sendText(command);

const workspace = this.currentWorkspace();

if (workspace?.lspClient?.serverVersion) {
await this.telemetry.sendCodeLensEvent(
"migrate_in_terminal",
workspace.lspClient.serverVersion,
);
}
}

// Get an existing terminal or create a new one. For multiple workspaces, it's important to create a new terminal for
// each workspace because they might be using different Ruby versions. If there's no workspace, we fallback to a
// generic name
private getTerminal() {
const workspace = this.currentWorkspace();
const name = workspace
? `${workspace.workspaceFolder.name}: migration`
: "Ruby LSP: migration";

const previousTerminal = vscode.window.terminals.find(
(terminal) => terminal.name === name,
);

return previousTerminal
? previousTerminal
: vscode.window.createTerminal({
name,
});
}
}
13 changes: 13 additions & 0 deletions src/rubyLsp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { Command, STATUS_EMITTER, pathExists } from "./common";
import { VersionManager } from "./ruby";
import { StatusItems } from "./status";
import { TestController } from "./testController";
import { MigrationController } from "./migrationController";
import { Debugger } from "./debugger";

// The RubyLsp class represents an instance of the entire extension. This should only be instantiated once at the
Expand All @@ -21,6 +22,7 @@ export class RubyLsp {
private readonly context: vscode.ExtensionContext;
private readonly statusItems: StatusItems;
private readonly testController: TestController;
private readonly migrationController: MigrationController;
private readonly debug: Debugger;

constructor(context: vscode.ExtensionContext) {
Expand All @@ -31,6 +33,11 @@ export class RubyLsp {
this.telemetry,
this.currentActiveWorkspace.bind(this),
);
this.migrationController = new MigrationController(
context,
this.telemetry,
this.currentActiveWorkspace.bind(this),
);
this.debug = new Debugger(context, this.workspaceResolver.bind(this));
this.registerCommands(context);

Expand Down Expand Up @@ -328,6 +335,12 @@ export class RubyLsp {
Command.DebugTest,
this.testController.debugTest.bind(this.testController),
),
vscode.commands.registerCommand(
Command.RunMigrationInTerminal,
this.migrationController.runMigrationInTerminal.bind(
this.migrationController,
),
),
);
}

Expand Down
2 changes: 1 addition & 1 deletion src/telemetry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export interface ConfigurationEvent {
}

export interface CodeLensEvent {
type: "test" | "debug" | "test_in_terminal" | "link";
type: "test" | "debug" | "test_in_terminal" | "migrate_in_terminal" | "link";
lspVersion: string;
}

Expand Down
Loading