-
Notifications
You must be signed in to change notification settings - Fork 12
/
Kulture.py
344 lines (289 loc) · 13 KB
/
Kulture.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
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
import sublime
import sublime_plugin
import urllib.request
import threading
import json
import os
import sys
import subprocess
import locale
import re
import shlex
if os.name == 'nt':
plat = 'win'
elif sys.platform == 'darwin':
plat = 'osx'
else:
plat = 'linux'
if plat == 'win':
try:
import _winreg
except (ImportError):
import winreg as _winreg
from ctypes import windll, create_unicode_buffer
class NotFoundError(Exception):
pass
if sys.version_info >= (3,):
installed_dir, _ = __name__.split('.')
else:
installed_dir = os.path.basename(os.getcwd())
class KTerminalSelector():
default = None
@staticmethod
def get():
package_dir = os.path.join(sublime.packages_path(), installed_dir)
default = None
if plat == 'win':
if os.path.exists(os.environ['SYSTEMROOT'] +
'\\System32\\WindowsPowerShell\\v1.0\\powershell.exe'):
# This mimics the default powershell colors since calling
# subprocess.POpen() ends up acting like launching powershell
# from cmd.exe. Normally the size and color are inherited
# from cmd.exe, but this creates a custom mapping, and then
# the LaunchPowerShell.bat file adjusts some other settings.
key_string = 'Console\\%SystemRoot%_system32_' + \
'WindowsPowerShell_v1.0_powershell.exe'
try:
key = _winreg.OpenKey(_winreg.HKEY_CURRENT_USER,
key_string)
except (WindowsError):
key = _winreg.CreateKey(_winreg.HKEY_CURRENT_USER,
key_string)
_winreg.SetValueEx(key, 'ColorTable05', 0,
_winreg.REG_DWORD, 5645313)
_winreg.SetValueEx(key, 'ColorTable06', 0,
_winreg.REG_DWORD, 15789550)
default = os.path.join(package_dir, 'PS.bat')
sublime_terminal_path = os.path.join(sublime.packages_path(), installed_dir)
# This should turn the path into an 8.3-style path, getting around unicode
# issues and spaces
buf = create_unicode_buffer(512)
if windll.kernel32.GetShortPathNameW(sublime_terminal_path, buf, len(buf)):
sublime_terminal_path = buf.value
os.putenv('sublime_terminal_path', sublime_terminal_path.replace(' ', '` '))
else :
default = os.environ['SYSTEMROOT'] + '\\System32\\cmd.exe'
elif plat == 'osx':
default = os.path.join(package_dir, 'Terminal.sh')
if not os.access(default, os.X_OK):
os.chmod(default, 0o755)
else:
ps = 'ps -eo comm | grep -E "gnome-session|ksmserver|' + \
'xfce4-session" | grep -v grep'
wm = [x.replace('\n', '') for x in os.popen(ps)]
if wm:
if wm[0] == 'gnome-session':
default = 'gnome-terminal'
elif wm[0] == 'xfce4-session':
default = 'xfce4-terminal'
elif wm[0] == 'ksmserver':
default = 'konsole'
if not default:
default = 'xterm'
KTerminalSelector.default = default
return default
class KTerminalCommand():
def get_path(self, paths):
if paths:
return paths[0]
elif self.window.active_view():
return self.window.active_view().file_name()
elif self.window.folders():
return self.window.folders()[0]
else:
sublime.error_message('Terminal: No place to open terminal to')
return False
def run_terminal(self, dir_, parameters):
try:
if not dir_:
raise NotFoundError('The file open in the selected view has ' +
'not yet been saved')
pathToProjJson = self.findProjectJsonFile()
print('path to project.json'+pathToProjJson)
dir_ = os.path.dirname(self.findProjectJsonFile())
for k, v in enumerate(parameters):
parameters[k] = v.replace('%CWD%', dir_)
args = [KTerminalSelector.get()]
args.extend(parameters)
if args[0] == 'gnome-terminal':
args = shlex.split(args[0] + ' -x bash -ic "' + args[1] + '"')
elif args[0] in ['terminal', 'konsole', 'xterm']:
args = shlex.split(args[0] + ' -e bash -ic "' + args[1] + '"')
elif args[0] == 'xfce4-terminal':
args = shlex.split(args[0] + ' -H --working-directory=' + dir_ + ' -x ' + args[1])
encoding = locale.getpreferredencoding(do_setlocale=True)
if sys.version_info >= (3,):
cwd = dir_
else:
cwd = dir_.encode(encoding)
print(args)
if plat == 'win':
startupinfo = subprocess.STARTUPINFO()
startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
subprocess.Popen(args, cwd=cwd, startupinfo=startupinfo)
elif plat == 'linux':
subprocess.Popen(args, cwd=cwd)
else:
subprocess.Popen(args, cwd=cwd)
except (OSError) as exception:
print(str(exception))
sublime.error_message('Terminal: The terminal ' +
KTerminalSelector.get() + ' was not found')
except (Exception) as exception:
sublime.error_message('Terminal: ' + str(exception))
def findProjectJsonFile(self):
currentFile = self.window.active_view().file_name()
if not currentFile:
msg = 'Kulture: Please save the file you are editing and try again'
sublime.error_message(msg)
raise NotFoundError(msg)
# check if there is a sublime project file
sublimeProjectFile = self.window.active_view().window().project_file_name()
if(sublimeProjectFile):
sublimeProjFileDir = os.path.dirname(sublimeProjectFile)
expectedPathToProjeJsonFromProj = os.path.join(sublimeProjFileDir,'project.json')
if(os.path.isfile(expectedPathToProjeJsonFromProj)):
msg = 'found project.json in project directory: ['+expectedPathToProjeJsonFromProj+']'
print(msg)
sublime.status_message(msg)
return expectedPathToProjeJsonFromProj
# project.json not found in directory as sublime project so search for it
msg = 'project.json not found with sublime project. looking based on file being edited.'
print(msg)
sublime.status_message(msg)
currentDir = os.path.dirname(currentFile)
pathToCheck = os.path.join(currentDir,'project.json')
previousPath = ''
counter = 0
while counter < 100:
counter += 1
if(previousPath == pathToCheck):
break
print('checking for project.json at: ['+pathToCheck+']')
if os.path.isfile(pathToCheck):
print('project.json found at: ['+pathToCheck+']')
return pathToCheck
previousPath = pathToCheck
parentDir = os.path.abspath(os.path.join(os.path.dirname(pathToCheck),os.pardir))
pathToCheck = os.path.join(parentDir,'project.json')
sublime.error_message('project.json not found')
print('path to project.json not found')
return
class KOpenTerminalCommand(sublime_plugin.WindowCommand, KTerminalCommand):
def run(self, paths=[], parameters=None):
path = self.get_path(paths)
if not path:
return
if parameters == None:
settings = sublime.load_settings('Terminal.sublime-settings')
parameters = settings.get('parameters')
if not parameters:
parameters = []
if os.path.isfile(path):
path = os.path.dirname(path)
self.run_terminal(path, parameters)
class KRunCommand(sublime_plugin.WindowCommand):
def run(self):
# find project.json by starting with the dir of the file being edited
file_name = self.findProjectJsonFile()
if file_name is None:
print('Unable to locate project.json from file ['+self.window.active_view().file_name()+']')
# todo: write an error to the status bar
return
try:
json_file = json.load(open(file_name))
except UnicodeDecodeError:
msg = 'Unable to open "'+file_name+'" Please save the file with UTF-8 encoding and try again'
print(msg)
sublime.error_message(msg)
retrun
except IOError:
print('project.json not found')
return
self.commands = []
try:
json_commands = json_file['commands']
for command in json_commands:
args = json_commands[command]
if command.lower() == 'web' or command.lower() == 'kestrel':
url_regex = r'(?:--server.urls |server.urls=)(http:\/\/[^\s]+)'
match = re.search(url_regex, args)
try:
url = match.group(1)
except AttributeError as e:
url = 'http://localhost:5000'
self.commands.append(['dnx ' + command, 'Run server at ' + url])
else:
self.commands.append(['dnx ' + command, args])
except LookupError:
pass
self.commands.append(['dnu restore', 'Restore packages'])
self.commands.append(['dnu pack', 'Bundle application for deployment'])
self.commands.append(['dnu build', 'Build NuGet packages for the project in given directory'])
self.window.show_quick_panel(self.commands, self.commandlist)
def commandlist(self, position):
if (position > -1):
self.window.run_command('k_open_terminal', {'parameters':[self.commands[position][0]]})
i = 0
else:
return
def findProjectJsonFile(self):
currentFile = self.window.active_view().file_name()
if not currentFile:
msg = 'Kulture: Please save the file you are editing and try again'
sublime.error_message(msg)
raise NotFoundError(msg)
# check if there is a sublime project file
sublimeProjectFile = self.window.active_view().window().project_file_name()
if(sublimeProjectFile):
sublimeProjFileDir = os.path.dirname(sublimeProjectFile)
expectedPathToProjeJsonFromProj = os.path.join(sublimeProjFileDir,'project.json')
if(os.path.isfile(expectedPathToProjeJsonFromProj)):
msg = 'found project.json in project directory: ['+expectedPathToProjeJsonFromProj+']'
print(msg)
sublime.status_message(msg)
return expectedPathToProjeJsonFromProj
# project.json not found in directory as sublime project so search for it
msg = 'project.json not found with sublime project. looking based on file being edited.'
print(msg)
sublime.status_message(msg)
currentDir = os.path.dirname(currentFile)
pathToCheck = os.path.join(currentDir,'project.json')
previousPath = ''
counter = 0
while counter < 100:
counter += 1
if(previousPath == pathToCheck):
break
print('checking for project.json at: ['+pathToCheck+']')
if os.path.isfile(pathToCheck):
print('project.json found at: ['+pathToCheck+']')
return pathToCheck
previousPath = pathToCheck
parentDir = os.path.abspath(os.path.join(os.path.dirname(pathToCheck),os.pardir))
pathToCheck = os.path.join(parentDir,'project.json')
sublime.error_message('project.json not found')
print('path to project.json not found')
return
class RetrievePackageNames(threading.Thread):
def __init__(self,timeout):
self.timeout = timeout
self.result = None
threading.Thread.__init__(self)
def run(self):
try:
request = urllib.request.Request('https://www.myget.org/F/aspnetvnext/api/v2/Packages()?$select=Id&$format=json&orderby=DownloadCount&$top=100',
headers={'User-Agent': 'Sublime'})
http_file = urllib.request.urlopen(request, timeout=self.timeout)
self.result = []
response = json.loads(http_file.read().decode('utf-8'))['d']
for package in response:
self.result.append(package['Id'])
print(self.result)
return
except (urllib.request.HTTPError) as e:
self.message = '%s: HTTP error %s contacting API' % (__name__, str(e.code))
except (urllib.request.URLError) as e:
self.message = '%s: URL error %s contacting API' % (__name__, str(e.reason))
self.result = False
return