extension.js (5477B)
1 "use strict"; 2 Object.defineProperty(exports, "__esModule", { value: true }); 3 exports.activate = activate; 4 exports.deactivate = deactivate; 5 const vscode = require("vscode"); 6 const cp = require("child_process"); 7 // 1. Activation Event 8 function activate(context) { 9 console.log('Githrun extension is now active!'); 10 // Check if githrun CLI is installed 11 checkGithrunInstallation(); 12 // Register the CodeLens Provider 13 const docSelector = [ 14 { language: 'markdown', scheme: 'file' }, 15 { language: 'python', scheme: 'file' }, 16 { language: 'plaintext', scheme: 'file' } 17 ]; 18 const codeLensProvider = new GithrunCodeLensProvider(); 19 context.subscriptions.push(vscode.languages.registerCodeLensProvider(docSelector, codeLensProvider)); 20 // --- REGISTER COMMANDS --- 21 // Command 1: Run from CodeLens (Clicking the button) 22 context.subscriptions.push(vscode.commands.registerCommand('githrun.runUrl', (url) => { 23 runGithrun(url); 24 })); 25 // Command 2: Install CLI 26 context.subscriptions.push(vscode.commands.registerCommand('githrun.installCli', () => { 27 installGithrun(); 28 })); 29 // Command 3: Run from Input Box (Ctrl+Shift+P) 30 context.subscriptions.push(vscode.commands.registerCommand('githrun.runInput', () => { 31 runFromInput(); 32 })); 33 // Command 4: Run Current Selection 34 context.subscriptions.push(vscode.commands.registerCommand('githrun.runSelection', () => { 35 runSelection(); 36 })); 37 } 38 // 2. The CodeLens Provider (The "Scanner") 39 class GithrunCodeLensProvider { 40 constructor() { 41 // Regex to find GitHub/Gist URLs ending in .py 42 this.regex = /https:\/\/(gist\.)?github\.com\/[\w\-\.\/]+\.py/g; 43 } 44 provideCodeLenses(document) { 45 const text = document.getText(); 46 const lenses = []; 47 let match; 48 // Loop through all matches in the file 49 while ((match = this.regex.exec(text)) !== null) { 50 const url = match[0]; 51 const startPos = document.positionAt(match.index); 52 const endPos = document.positionAt(match.index + url.length); 53 const range = new vscode.Range(startPos, endPos); 54 // Create the "Run" button 55 const cmd = { 56 title: "$(play) Run with Githrun", // The text user sees 57 tooltip: "Execute this script remotely", 58 command: "githrun.runUrl", 59 arguments: [url] 60 }; 61 lenses.push(new vscode.CodeLens(range, cmd)); 62 } 63 return lenses; 64 } 65 } 66 // 3. The Runner Logic 67 function runGithrun(url) { 68 if (!url) { 69 return; 70 } 71 // Get config for auto-install flag 72 const config = vscode.workspace.getConfiguration('githrun'); 73 const autoInstall = config.get('autoInstallDeps') ? ' --auto-install' : ''; 74 // Create or show terminal 75 const terminalName = 'Githrun'; 76 // Explicitly type 't' as a vscode.Terminal 77 let terminal = vscode.window.terminals.find((t) => t.name === terminalName); 78 if (!terminal) { 79 terminal = vscode.window.createTerminal(terminalName); 80 } 81 terminal.show(); 82 // Use the CLI command you built in python! 83 terminal.sendText(`githrun run ${url}${autoInstall}`); 84 } 85 // Helper: Run from Input Box 86 async function runFromInput() { 87 const url = await vscode.window.showInputBox({ 88 placeHolder: "https://github.com/username/repo/blob/main/script.py", 89 prompt: "Enter a GitHub URL or Gist to run with Githrun" 90 }); 91 if (url) { 92 runGithrun(url.trim()); 93 } 94 } 95 // Helper: Run from Selection 96 function runSelection() { 97 const editor = vscode.window.activeTextEditor; 98 if (!editor) { 99 vscode.window.showErrorMessage("No active text editor found."); 100 return; 101 } 102 const selection = editor.document.getText(editor.selection); 103 if (!selection) { 104 vscode.window.showInformationMessage("Please highlight a GitHub URL first."); 105 return; 106 } 107 // Basic validation 108 const trimmed = selection.trim(); 109 if (trimmed.startsWith("http")) { 110 runGithrun(trimmed); 111 } 112 else { 113 vscode.window.showWarningMessage("The selected text does not look like a URL."); 114 } 115 } 116 // 4. Installation Helper 117 function checkGithrunInstallation() { 118 // Check version to verify installation 119 cp.exec('githrun --version', (err, stdout) => { 120 // Regex looks for standard version patterns like 1.0.0, 0.2, v1.0 etc. 121 // It allows for "githrun version 1.0.0" or just "1.0.0" 122 const versionMatch = stdout && stdout.match(/(\d+\.\d+(\.\d+)?)/); 123 if (err || !versionMatch) { 124 console.log('Githrun CLI check failed:', err ? err.message : 'No version number in stdout'); 125 vscode.window.showWarningMessage("Githrun CLI is not detected. Install it to run remote scripts?", "Install Now").then((selection) => { 126 if (selection === "Install Now") { 127 installGithrun(); 128 } 129 }); 130 } 131 else { 132 console.log(`Githrun CLI detected: ${versionMatch[0]}`); 133 } 134 }); 135 } 136 function installGithrun() { 137 const terminal = vscode.window.createTerminal("Install Githrun"); 138 terminal.show(); 139 terminal.sendText("pip install githrun"); 140 vscode.window.showInformationMessage("Installing Githrun... Try running the script once finished."); 141 } 142 function deactivate() { } 143 //# sourceMappingURL=extension.js.map