Skip to content

Instantly share code, notes, and snippets.

@pioz
Last active September 11, 2024 09:33
Show Gist options
  • Save pioz/37d4ab739f213997400c95f10c23cccc to your computer and use it in GitHub Desktop.
Save pioz/37d4ab739f213997400c95f10c23cccc to your computer and use it in GitHub Desktop.
Sublime Text plugin to copy Rails migration version
import sublime
import sublime_plugin
import os
import json
class CopyMigrationVersionCommand(sublime_plugin.WindowCommand):
def run(self):
file_name = self.window.active_view().file_name()
if file_name:
version = os.path.basename(file_name).split('_')[0]
sublime.set_clipboard(version)
sublime.status_message(f"Copied: {version}")
else:
sublime.status_message("No file selected")
def is_visible(self):
file_name = self.window.active_view().file_name()
if file_name:
base_file_name = os.path.basename(file_name)
if file_name.endswith(f"/db/migrate/{base_file_name}"):
return True
return False
# Add item in the side bar context menu
def plugin_loaded():
context_menu_path = os.path.join(sublime.packages_path(), 'User', 'Side Bar.sublime-menu')
context_menu = []
if os.path.exists(context_menu_path):
with open(context_menu_path, 'r') as file:
context_menu = json.load(file)
new_item = {
"caption": "Copy Migration Version",
"command": "copy_migration_version"
}
if new_item not in context_menu:
context_menu.append(new_item)
with open(context_menu_path, 'w') as f:
f.write(sublime.encode_value(context_menu, pretty=True))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment