forked from tvooo/sublime-grunt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
150 lines (119 loc) · 5.17 KB
/
main.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
import sublime
import sublime_plugin
import os
import subprocess
import json
import pprint
from hashlib import sha1
package_name = "Grunt"
package_url = "https://github.com/tvooo/sublime-grunt"
cache_file_name = ".sublime-grunt.cache"
class GruntRunner(object):
def __init__(self, window, command):
self.window = window
self.list_gruntfiles(command)
def list_tasks(self):
try:
self.callcount = 0
json_result = self.fetch_json()
except TypeError as e:
self.window.new_file().run_command("grunt_error", {"message": "SublimeGrunt: JSON is malformed\n\n%s\n\n" % e})
sublime.error_message("Could not read available tasks\n")
else:
tasks = [[name, task['info'], task['meta']['info']] for name, task in json_result.items()]
return sorted(tasks, key=lambda task: task)
def run_expose(self):
package_path = os.path.join(sublime.packages_path(), package_name)
args = r'grunt --no-color --tasks "%s" expose:%s' % (package_path, os.path.basename(self.chosen_gruntfile))
expose = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=get_env_with_exec_args_path(), cwd=self.wd, shell=True)
(stdout, stderr) = expose.communicate()
if 127 == expose.returncode:
sublime.error_message("\"grunt\" command not found.\nPlease add Grunt's location to your PATH.")
return
return self.fetch_json()
def fetch_json(self):
jsonfilename = os.path.join(self.wd, cache_file_name)
data = None
if os.path.exists(jsonfilename):
filesha1 = hashfile(self.chosen_gruntfile)
json_data=open(jsonfilename)
try:
data = json.load(json_data)
if data[self.chosen_gruntfile]["sha1"] == filesha1:
return data[self.chosen_gruntfile]["tasks"]
finally:
json_data.close()
self.callcount += 1
if self.callcount == 1:
return self.run_expose()
if data is None:
raise TypeError("Could not expose gruntfile")
raise TypeError("Sha1 from grunt expose ({0}) is not equal to calculated ({1})".format(data[self.chosen_gruntfile]["sha1"], filesha1))
def list_gruntfiles(self, command):
self.grunt_files = []
self.folders = []
for f in self.window.folders():
self.folders.append(f)
if os.path.exists(os.path.join(f, "Gruntfile.js")):
self.grunt_files.append(os.path.join(f, "Gruntfile.js"))
elif os.path.exists(os.path.join(f, "Gruntfile.coffee")):
self.grunt_files.append(os.path.join(f, "Gruntfile.coffee"))
if len(self.grunt_files) > 0:
if len(self.grunt_files) == 1:
self.choose_file(0, command)
else:
self.window.show_quick_panel(self.grunt_files, self.choose_file)
else:
sublime.error_message("Gruntfile.js or Gruntfile.coffee not found!")
def choose_file(self, file, command):
self.wd = os.path.dirname(self.grunt_files[file])
self.chosen_gruntfile = self.grunt_files[file]
self.tasks = self.list_tasks()
if command != "" :
for task_id, task in enumerate(self.tasks):
if (task[0] == command):
self.on_done(task_id)
else :
if self.tasks is not None:
self.window.show_quick_panel(self.tasks, self.on_done)
def on_done(self, task):
if task > -1:
path = get_env_path()
exec_args = {'cmd': "grunt --no-color " + self.tasks[task][0], 'shell': True, 'working_dir': self.wd, 'path': path}
self.window.run_command("exec", exec_args)
def hashfile(filename):
with open(filename, mode='rb') as f:
filehash = sha1()
content = f.read();
filehash.update(str("blob " + str(len(content)) + "\0").encode('UTF-8'))
filehash.update(content)
return filehash.hexdigest()
def get_env_path():
path = os.environ['PATH']
settings = sublime.load_settings('SublimeGrunt.sublime-settings')
if settings:
exec_args = settings.get('exec_args')
if exec_args:
path = exec_args.get('path', os.environ['PATH'])
return str(path)
def get_env_with_exec_args_path():
env = os.environ.copy()
settings = sublime.load_settings('SublimeGrunt.sublime-settings')
if settings:
exec_args = settings.get('exec_args')
if exec_args:
path = str(exec_args.get('path', ''))
if path:
env['PATH'] = path
return env
class GruntCommand(sublime_plugin.WindowCommand):
def run(self, command):
GruntRunner(self.window, command)
class GruntKillCommand(sublime_plugin.WindowCommand):
def run(self):
self.window.run_command("exec", {"kill": True})
class GruntErrorCommand(sublime_plugin.TextCommand):
def run(self, edit, **args):
view = self.view
prefix = "Please file an issue on " + package_url + "/issues and attach this output.\n\n"
view.insert(edit, 0, prefix + args["message"])