-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdrush.py
200 lines (164 loc) · 6.38 KB
/
drush.py
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
import platform
import sublime
import sublime_plugin
import re
import os
local_data_sess = ''
if int(sublime.version()) > 3000:
from io import FileIO as file
local_data_sess = 'Local'
else:
local_data_sess = 'Settings'
os.environ["TERM"] = "dumb"
PLATFORM_IS_WINDOWS = platform.system() is 'Windows'
class DrushCommand(sublime_plugin.TextCommand):
working_dirs = ''
path = ''
folders = []
DEFAULT_DRUSH_EXECUTABLE = 'drush.bat' if PLATFORM_IS_WINDOWS else 'drush'
SETTINGS = sublime.load_settings("Drush.sublime-settings")
view_panel = None
last_cmd = ''
def run(self, edit):
view = self.view
self.working_dirs = view.window().folders()
if len(self.working_dirs) > 0:
self.path = self.working_dirs[0]
self.view_panel = view.window().show_input_panel('drush', self.last_cmd, self.after_input, self.on_change, None)
self.view_panel.set_name('drush_command_bar')
else:
sublime.message_dialog('No opened project was found!\nPlease open a Drupal project!')
def after_input(self, text):
if text.strip() == "":
sublime.status_message("No command provided")
return
else:
self._runDrush(text)
def on_change(self, text):
if text.strip() == "":
return
if self.SETTINGS.get('remember_last_command') or True :
self.last_cmd = text.strip()
def _runDrush(self, text):
try:
root = ''
uri = ''
command_splitted = ''
drush = str(self._get_drush_executable())
drush_args = str(self._get_drush_sup_args()).replace('\\', '/')
drush_project_args = str(self._get_drush_sup_project_args()).replace('\\', '/')
if "--root=" in drush_args:
root = ''
elif "--root=" in drush_project_args:
root = ''
elif "--root=" in text:
root = ''
else:
root = str('--root=' + self.path).replace('\\', '/')
if "--uri=" in drush_args:
uri = ''
elif "--uri=" in drush_project_args:
uri = ''
elif "--uri=" in text:
uri = ''
import shlex
command_splitted = shlex.split(str(drush + ' ' + drush_project_args + ' ' + drush_args + ' ' + text.replace('\\', '/') + ' ') + root + uri)
sublime.active_window().run_command('exec', {'cmd': command_splitted, "working_dir": self.path})
except OSError as e:
error_message = 'Drush error: '
if e.errno is 2:
error_message += 'Could not find your "drush" executable.'
error_message += str(e)
sublime.status_message(error_message)
return ('', error_message)
def _get_drush_executable(self):
return self.SETTINGS.get('drush_executable') or self.DEFAULT_DRUSH_EXECUTABLE
def getDirectories(self):
return sublime.active_window().folders()
# Credit to titoBouzout - https://github.com/titoBouzout/SideBarEnhancements
if int(sublime.version()) < 3000:
def getProjectFile(self):
if not self.getDirectories():
return None
import json
data = file(os.path.normpath(os.path.join(sublime.packages_path(), '..', local_data_sess, 'Session.sublime_session')), 'r').read()
data = data.replace('\t', ' ')
data = json.loads(data, strict=False)
projects = data['workspaces']['recent_workspaces']
if os.path.lexists(os.path.join(sublime.packages_path(), '..', local_data_sess, 'Auto Save Session.sublime_session')):
data = file(os.path.normpath(os.path.join(sublime.packages_path(), '..', local_data_sess, 'Auto Save Session.sublime_session')), 'r').read()
data = data.replace('\t', ' ')
data = json.loads(data, strict=False)
if 'workspaces' in data and 'recent_workspaces' in data['workspaces'] and data['workspaces']['recent_workspaces']:
projects += data['workspaces']['recent_workspaces']
projects = list(set(projects))
for project_file in projects:
project_file = re.sub(r'^/([^/])/', '\\1:/', project_file);
project_json = json.loads(file(project_file, 'r').read(), strict=False)
if 'folders' in project_json:
folders = project_json['folders']
found_all = True
for directory in self.getDirectories():
found = False
for folder in folders:
folder_path = re.sub(r'^/([^/])/', '\\1:/', folder['path']);
if folder_path == directory.replace('\\', '/'):
found = True
break;
if found == False:
found_all = False
break;
if found_all:
return project_file
return None
else:
def getProjectFile(self):
return sublime.active_window().project_file_name()
def hasOpenedProject(self):
return self.getProjectFile() != None
def getProjectJson(self):
if not self.hasOpenedProject():
return None
if int(sublime.version()) < 3000:
import json
return json.loads(file(self.getProjectFile(), 'r').read(), strict=False)
else:
return sublime.active_window().project_data()
def _get_drush_sup_args(self):
drush_args = self.SETTINGS.get('drush_args')
if drush_args:
return drush_args
else:
return ''
def _get_drush_sup_project_args(self):
p_drush_args = self.getProjectJson()
if p_drush_args:
if 'settings' in p_drush_args:
if 'Drush' in p_drush_args['settings']:
if 'drush_args' in p_drush_args['settings']['Drush']:
return p_drush_args['settings']['Drush']['drush_args']
return ''
class DrushEvents(sublime_plugin.EventListener):
def on_post_save(self, view):
dc = DrushCommand(view)
save_cmd = dc.SETTINGS.get('exec_on_save_command')
if save_cmd != "":
drupal_dir = self._get_site_home_dir(view.file_name())
if drupal_dir != False:
shell_cmd = "%s --root='%s'" % (save_cmd, drupal_dir)
dc._runDrush(shell_cmd)
# show user the status message
sublime.status_message("Drush command executed: %s" % shell_cmd)
def _site_dir_info(self, file_path):
top_level_paths = ['sites', 'modules']
for tl in top_level_paths:
index = file_path.find(tl)
if index > -1:
return (os.path.isfile(file_path[0:index] + 'authorize.php'), file_path[0:index])
return (False, None)
def _get_site_home_dir(self, file_path):
info = self._site_dir_info(file_path)
# if the cwd is a drupal directory, return that directory
if info[0]:
return info[1]
return False