-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
306 lines (276 loc) · 11.9 KB
/
main.py
File metadata and controls
306 lines (276 loc) · 11.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
import sys, os
# Add lib directory to path
parent_folder_path = os.path.dirname(__file__)
sys.path.append(os.path.join(parent_folder_path, 'lib'))
import subprocess
import json
from flowlauncher import FlowLauncher
class WSLScriptRunner(FlowLauncher):
def __init__(self):
self.settings = {} # Initialize empty settings
self.setting_scripts_dir = False # Flag for settings mode
self._initialize_settings()
super().__init__()
def _initialize_settings(self):
"""Initialize settings, creating settings.json if it doesn't exist"""
self.settings = {} # Start with empty settings
try:
settings_path = os.path.join(os.path.dirname(__file__), "settings.json")
if os.path.exists(settings_path):
with open(settings_path, "r") as f:
loaded_settings = json.load(f)
if isinstance(loaded_settings, dict):
self.settings.update(loaded_settings)
except Exception as e:
print(f"Error loading settings: {str(e)}")
def query(self, param: str = "") -> list:
"""
Query is the entry point for your plugin,
the param is what user typed after the plugin action keyword.
"""
query = param.strip()
# Handle settings command with path
if query.lower().startswith('settings '):
path = query[9:].strip() # Get everything after "settings "
if path:
self.settings["scripts_dir"] = path
try:
settings_path = os.path.join(os.path.dirname(__file__), "settings.json")
with open(settings_path, "w") as f:
json.dump(self.settings, f, indent=2)
return [{
"Title": "Settings saved!",
"SubTitle": f"Scripts directory set to: {path}",
"IcoPath": "Images/app.png"
}]
except Exception as e:
return [{
"Title": "Error saving settings",
"SubTitle": str(e),
"IcoPath": "Images/error.png"
}]
# Handle bare settings command
if query.lower() == 'settings':
return [{
"Title": "Enter scripts directory path",
"SubTitle": "Type 'wsr settings YOUR_PATH' (e.g., wsr settings ~/scripts)",
"IcoPath": "Images/app.png"
}]
if "scripts_dir" not in self.settings:
return [{
"Title": "Scripts directory not configured",
"SubTitle": "Type 'wsr settings YOUR_PATH' to configure (e.g., wsr settings ~/scripts)",
"IcoPath": "Images/error.png"
}]
scripts_dir = self.get_scripts_dir()
if not scripts_dir:
return [{
"Title": "Error retrieving scripts directory",
"SubTitle": "Check settings or ensure WSL is running.",
"IcoPath": "Images/error.png"
}]
try:
# List scripts in WSL
list_cmd = f"find '{scripts_dir}' -maxdepth 1 -name '*.sh' -type f -exec basename {{}} \\;"
scripts = subprocess.check_output(["wsl", "bash", "-c", list_cmd],
text=True,
creationflags=subprocess.CREATE_NO_WINDOW).splitlines()
except subprocess.CalledProcessError:
return [{
"Title": "Error reading scripts directory",
"SubTitle": "Check if WSL is running and the directory exists",
"IcoPath": "Images/error.png"
}]
results = []
for script in scripts:
if not query or query.lower() in script.lower():
results.append({
"Title": script,
"SubTitle": f"Run {script}",
"IcoPath": "Images/app.png",
"JsonRPCAction": {
"method": "run_script",
"parameters": [script],
"dontHideAfterAction": False
},
"ContextData": {"script": script, "path": os.path.join(scripts_dir, script)}
})
if not query:
results.insert(0, {
"Title": "Configure Scripts Directory",
"SubTitle": f"Current: {self.settings.get('scripts_dir', 'Not configured')}",
"IcoPath": "Images/app.png"
})
return results if results else [{
"Title": "No matching scripts found",
"SubTitle": "Try a different search term or type 'settings' to configure",
"IcoPath": "Images/app.png"
}]
def context_menu(self, data) -> list:
"""
Optional context menu implementation
"""
if not data:
current_dir = self.settings.get("scripts_dir", "Not configured")
return [{
"Title": "Configure Scripts Directory",
"SubTitle": f"Current: {current_dir}",
"IcoPath": "Images/app.png",
"JsonRPCAction": {
"method": "start_settings",
"parameters": [],
"dontHideAfterAction": True
}
}]
script_data = data
script_path = script_data.get("path", "")
script_name = script_data.get("script", "")
return [
{
"Title": f"Open script in editor",
"SubTitle": f"Edit {script_name}",
"IcoPath": "Images/app.png",
"JsonRPCAction": {
"method": "edit_script",
"parameters": [script_path],
"dontHideAfterAction": False
}
},
{
"Title": "Open scripts directory",
"SubTitle": self.settings.get("scripts_dir", "Not configured"),
"IcoPath": "Images/app.png",
"JsonRPCAction": {
"method": "open_scripts_dir",
"parameters": [],
"dontHideAfterAction": False
}
}
]
def edit_script(self, script_path):
try:
subprocess.Popen(["wsl", "bash", "-c", f"code '{script_path}'"],
creationflags=subprocess.CREATE_NO_WINDOW)
except Exception:
return [{
"Title": "Error opening editor",
"SubTitle": "Make sure VS Code is installed in WSL",
"IcoPath": "Images/error.png"
}]
return []
def open_scripts_dir(self):
scripts_dir = self.get_scripts_dir()
if not scripts_dir:
return [{
"Title": "Error opening directory",
"SubTitle": "Scripts directory is not accessible",
"IcoPath": "Images/error.png"
}]
try:
subprocess.Popen(["wsl", "bash", "-c", f"code '{scripts_dir}'"],
creationflags=subprocess.CREATE_NO_WINDOW)
except Exception:
return [{
"Title": "Error opening directory",
"SubTitle": "Make sure VS Code is installed in WSL",
"IcoPath": "Images/error.png"
}]
return []
def get_scripts_dir(self):
if "scripts_dir" not in self.settings:
return None
scripts_dir = self.settings["scripts_dir"]
try:
resolved_path = subprocess.check_output(["wsl", "bash", "-c", f"echo $(eval echo {scripts_dir})"],
text=True,
creationflags=subprocess.CREATE_NO_WINDOW).strip()
if not resolved_path:
return None
# Ensure the directory exists
subprocess.check_output(["wsl", "bash", "-c", f"mkdir -p '{resolved_path}'"],
text=True,
creationflags=subprocess.CREATE_NO_WINDOW)
return resolved_path
except subprocess.CalledProcessError:
return None
def run_script(self, script_name):
scripts_dir = self.get_scripts_dir()
if not scripts_dir:
return [{
"Title": "Error executing script",
"SubTitle": "Scripts directory is not accessible.",
"IcoPath": "Images/error.png"
}]
# Convert Windows path separators to Unix style
script_path = os.path.join(scripts_dir, script_name).replace('\\', '/')
try:
# First ensure the script is executable
subprocess.check_output(["wsl", "bash", "-c", f"chmod +x '{script_path}'"],
creationflags=subprocess.CREATE_NO_WINDOW)
# Execute the script in WSL terminal
subprocess.Popen([
"wsl.exe",
"--cd", os.path.dirname(script_path),
"bash", "-ic", f"'{script_path}' ; echo '\\nPress Enter to close...' ; read"
])
except Exception as e:
return [{
"Title": "Error executing script",
"SubTitle": f"Error: {str(e)}",
"IcoPath": "Images/error.png"
}]
return []
def load_settings(self):
self._initialize_settings()
def save_settings(self):
"""Save current settings to settings.json"""
try:
settings_path = os.path.join(os.path.dirname(__file__), "settings.json")
os.makedirs(os.path.dirname(settings_path), exist_ok=True) # Ensure directory exists
with open(settings_path, "w") as f:
json.dump(self.settings, f, indent=2)
except Exception as e:
print(f"Error saving settings: {str(e)}")
def open_settings(self):
try:
# Update settings with the new scripts directory
self.settings["scripts_dir"] = self.get_query()
# Save to file
settings_path = os.path.join(os.path.dirname(__file__), "settings.json")
with open(settings_path, "w") as f:
json.dump(self.settings, f, indent=2)
return [{
"Title": "Settings saved",
"SubTitle": f"Scripts directory set to: {self.settings['scripts_dir']}",
"IcoPath": "Images/app.png"
}]
except Exception as e:
print(f"Error saving settings: {str(e)}")
return [{
"Title": "Error saving settings",
"SubTitle": str(e),
"IcoPath": "Images/error.png"
}]
def get_plugin_metadata(self):
return {
"ID": "FlowLauncher.WSLScriptRunner",
"ActionKeyword": "wsr",
"Name": "WSL Script Runner",
"Description": "Run your shell scripts in WSL",
"Author": "renzo",
"Version": "1.0.0",
"Language": "python",
"Website": "https://github.com/renzocastillo/WSLScriptRunner/",
"ExecuteFileName": "main.py",
"IcoPath": "Images/app.png"
}
def start_settings(self):
"""Start the settings input mode"""
self.setting_scripts_dir = True
return [{
"Title": "Enter scripts directory path",
"SubTitle": "Example: ~/scripts or /home/user/scripts",
"IcoPath": "Images/app.png"
}]
if __name__ == "__main__":
WSLScriptRunner()