Skip to content

Commit

Permalink
separate commands
Browse files Browse the repository at this point in the history
  • Loading branch information
andyduong1920 committed Oct 26, 2023
1 parent fb8aded commit e836bcd
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 136 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@ Run the test from the `Status bar` or `Menu`
### IV/ Add new Item
1. Define the bar item in `src/barItems/definitions.ts`
2. Make it as default show by adding it to `src/barItems/defaultShow.ts`
3. Define the command in `src/barItems/commands.ts`
3. Define the command in `src/commands.ts`
4. Setup the Item behavior in `src/barItems/behaviors.ts` link to the command in step 3
122 changes: 122 additions & 0 deletions src/commands.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
import { commands, window, workspace } from "vscode";
import * as FileCheckHelpers from "./fileCheckHelpers";
import * as Utils from "./utils";

export const runTestFileCommand = commands.registerCommand(
"barHelper.runTestFile",
() => {
const editor = window.activeTextEditor;

if (editor === undefined) {
return;
} else {
const filePath = editor.document.fileName;
const relativePath = workspace.asRelativePath(filePath, false);

if (FileCheckHelpers.isRubyTestFile(filePath)) {
Utils.sendToTerminal(`bundle exec rspec ${relativePath}`);
} else if (FileCheckHelpers.isElixirTestFile(filePath)) {
Utils.sendToTerminal(`mix test ${relativePath}`);
}
}
}
);

export const runTestLineCommand = commands.registerCommand(
"barHelper.runTestLine",
() => {
const editor = window.activeTextEditor;

if (editor === undefined) {
return;
} else {
const filePath = editor.document.fileName;
const relativePath = workspace.asRelativePath(filePath, false);
const lineNumber = editor.selection.active.line + 1;

if (FileCheckHelpers.isRubyTestFile(filePath)) {
Utils.sendToTerminal(`bundle exec rspec ${relativePath}:${lineNumber}`);
} else if (FileCheckHelpers.isElixirTestFile(filePath)) {
Utils.sendToTerminal(`mix test ${relativePath}:${lineNumber}`);
}
}
}
);

export const runDBRemigrateCommand = commands.registerCommand(
"barHelper.runDBRemigrate",
() => {
// TODO: Support Elixir
Utils.sendToTerminal(
"bin/rails db:environment:set RAILS_ENV=development && bin/rails db:drop db:setup"
);
}
);

export const runDBMigrateCommand = commands.registerCommand(
"barHelper.runDBMigrate",
() => {
// TODO: Support Elixir
Utils.sendToTerminal("bundle exec rails db:migrate");
}
);

export const startInteractiveConsoleCommand = commands.registerCommand(
"barHelper.startInteractiveConsole",
() => {
// TODO: Support Elixir
Utils.sendToTerminal("bundle exec rails console");
}
);

export const startWebServerCommand = commands.registerCommand(
"barHelper.startWebServer",
() => {
// TODO: Support Elixir
Utils.sendToTerminal("foreman start -f Procfile.dev");
}
);

export const formatCodeFileCommand = commands.registerCommand(
"barHelper.formatCodeFile",
() => {
const editor = window.activeTextEditor;

if (editor === undefined) {
return;
} else {
const filePath = editor.document.fileName;
const relativePath = workspace.asRelativePath(filePath, false);

if (FileCheckHelpers.isRubyFile(filePath)) {
Utils.sendToTerminal(`bundle exec rubocop -a ${relativePath}`);
} else if (FileCheckHelpers.isElixirFile(filePath)) {
Utils.sendToTerminal(`mix format ${relativePath}`);
} else if (FileCheckHelpers.isJSFile(filePath)) {
// TODO: Support npm
Utils.sendToTerminal(`yarn eslint --color --fix ${relativePath}`);
}
}
}
);

export const runGitPushCommand = commands.registerCommand(
"barHelper.runGitPush",
() => {
Utils.sendToTerminal("ggpush -f");
}
);

export const runGitRebaseContinueCommand = commands.registerCommand(
"barHelper.runGitRebaseContinue",
() => {
Utils.sendToTerminal("ga . && g rebase --continue");
}
);

export const runGitRebaseSkipCommand = commands.registerCommand(
"barHelper.runGitRebaseSkip",
() => {
Utils.sendToTerminal("g rebase --skip");
}
);
139 changes: 4 additions & 135 deletions src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
"use strict";

import { commands, window, ExtensionContext, workspace } from "vscode";
import { window, ExtensionContext } from "vscode";

import * as FileCheckHelpers from "./fileCheckHelpers";
import * as BarItems from "./barItems/definition";
import * as Utils from "./utils";
import * as Commands from "./commands";
import { setupItemBehaviors } from "./barItems/behaviors";
import { defaultShowItems } from "./barItems/defaultShow";
import * as Utils from "./utils";

const onUpdatePath = () => {
const editor = window.activeTextEditor;
Expand All @@ -31,145 +32,13 @@ const onUpdatePath = () => {
export function activate(context: ExtensionContext) {
setupItemBehaviors();
defaultShowItems();

const runTestFileCommand = commands.registerCommand(
"barHelper.runTestFile",
() => {
const editor = window.activeTextEditor;

if (editor === undefined) {
return;
} else {
const filePath = editor.document.fileName;
const relativePath = workspace.asRelativePath(filePath, false);

if (FileCheckHelpers.isRubyTestFile(filePath)) {
Utils.sendToTerminal(`bundle exec rspec ${relativePath}`);
} else if (FileCheckHelpers.isElixirTestFile(filePath)) {
Utils.sendToTerminal(`mix test ${relativePath}`);
}
}
}
);

const runTestLineCommand = commands.registerCommand(
"barHelper.runTestLine",
() => {
const editor = window.activeTextEditor;

if (editor === undefined) {
return;
} else {
const filePath = editor.document.fileName;
const relativePath = workspace.asRelativePath(filePath, false);
const lineNumber = editor.selection.active.line + 1;

if (FileCheckHelpers.isRubyTestFile(filePath)) {
Utils.sendToTerminal(
`bundle exec rspec ${relativePath}:${lineNumber}`
);
} else if (FileCheckHelpers.isElixirTestFile(filePath)) {
Utils.sendToTerminal(`mix test ${relativePath}:${lineNumber}`);
}
}
}
);

const runDBRemigrateCommand = commands.registerCommand(
"barHelper.runDBRemigrate",
() => {
// TODO: Support Elixir
Utils.sendToTerminal(
"bin/rails db:environment:set RAILS_ENV=development && bin/rails db:drop db:setup"
);
}
);

const runDBMigrateCommand = commands.registerCommand(
"barHelper.runDBMigrate",
() => {
// TODO: Support Elixir
Utils.sendToTerminal("bundle exec rails db:migrate");
}
);

const startInteractiveConsoleCommand = commands.registerCommand(
"barHelper.startInteractiveConsole",
() => {
// TODO: Support Elixir
Utils.sendToTerminal("bundle exec rails console");
}
);

const startWebServerCommand = commands.registerCommand(
"barHelper.startWebServer",
() => {
// TODO: Support Elixir
Utils.sendToTerminal("foreman start -f Procfile.dev");
}
);

const formatCodeFileCommand = commands.registerCommand(
"barHelper.formatCodeFile",
() => {
const editor = window.activeTextEditor;

if (editor === undefined) {
return;
} else {
const filePath = editor.document.fileName;
const relativePath = workspace.asRelativePath(filePath, false);

if (FileCheckHelpers.isRubyFile(filePath)) {
Utils.sendToTerminal(`bundle exec rubocop -a ${relativePath}`);
} else if (FileCheckHelpers.isElixirFile(filePath)) {
Utils.sendToTerminal(`mix format ${relativePath}`);
} else if (FileCheckHelpers.isJSFile(filePath)) {
// TODO: Support npm
Utils.sendToTerminal(`yarn eslint --color --fix ${relativePath}`);
}
}
}
);

const runGitPushCommand = commands.registerCommand(
"barHelper.runGitPush",
() => {
Utils.sendToTerminal("ggpush -f");
}
);

const runGitRebaseContinueCommand = commands.registerCommand(
"barHelper.runGitRebaseContinue",
() => {
Utils.sendToTerminal("ga . && g rebase --continue");
}
);

const runGitRebaseSkipCommand = commands.registerCommand(
"barHelper.runGitRebaseSkip",
() => {
Utils.sendToTerminal("g rebase --skip");
}
);

onUpdatePath();

const textEditorDisposable = window.onDidChangeActiveTextEditor(onUpdatePath);

// Adjust here to add more items
context.subscriptions.concat([
textEditorDisposable,
runTestFileCommand,
runTestLineCommand,
runDBRemigrateCommand,
runDBMigrateCommand,
startInteractiveConsoleCommand,
startWebServerCommand,
formatCodeFileCommand,
runGitPushCommand,
runGitRebaseContinueCommand,
runGitRebaseSkipCommand,
...Object.values(Commands),
]);
}

Expand Down

0 comments on commit e836bcd

Please sign in to comment.