This repository has been archived by the owner on Nov 26, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathmain.py
159 lines (124 loc) · 5.58 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
151
152
153
154
155
156
157
158
159
import sublime
import sublime_plugin
import os
import subprocess
import json
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):
self.window = window
self.list_gruntfiles()
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):
# Load gruntfile paths from config
self.folders = get_grunt_file_paths()
self.grunt_files = []
for f in self.window.folders():
self.folders.append(f)
for f in self.folders:
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)
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):
self.wd = os.path.dirname(self.grunt_files[file])
self.chosen_gruntfile = self.grunt_files[file]
self.tasks = self.list_tasks()
if self.tasks is not None:
#fix quick panel unavailable
sublime.set_timeout(lambda: self.window.show_quick_panel(self.tasks, self.on_done), 1)
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_grunt_file_paths():
# Get the user settings
global_settings = sublime.load_settings('SublimeGrunt.sublime-settings')
# Check the settings for the current project
# If there is a setting for the paths in the project, it takes precidence
# No setting in the project, then use the global one
# If there is no global one, then use a default
return sublime.active_window().active_view().settings().get('SublimeGrunt', {}).get('gruntfile_paths', global_settings.get('gruntfile_paths', []))
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):
GruntRunner(self.window)
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"])