forked from guillermooo/dart-sublime-bundle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
execute.py
174 lines (144 loc) · 5.98 KB
/
execute.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
# Copyright (c) 2014, Guillermo López-Anglada. Please see the AUTHORS file for details.
# All rights reserved. Use of this source code is governed by a BSD-style
# license that can be found in the LICENSE file.)
'''Our own ST `exec` command.
Mostly lifted from Default.exec.py
'''
import sublime
import sublime_plugin
import os
import functools
import time
from Default.exec import ProcessListener
from Default.exec import AsyncProcess
from Dart.sublime_plugin_lib.sublime import after
from Dart.sublime_plugin_lib.panels import OutputPanel
class DartExecCommand(sublime_plugin.WindowCommand, ProcessListener):
def run(self,
cmd=None,
shell_cmd=None,
file_regex="",
line_regex="",
working_dir="",
encoding="utf-8",
env={},
quiet=False,
kill=False,
word_wrap=True,
syntax='',
preamble='',
panel_name='dart.out',
# Catches "path" and "shell"
**kwargs):
if kill:
if hasattr(self, 'proc') and self.proc:
self.proc.kill()
self.proc = None
self.append_string(None, "[Cancelled]")
return
# TODO(guillermooo): We cannot have multiple processes running at the
# same time, or processes that use separate output panels.
if not hasattr(self, 'out_panel'):
# Try not to call get_output_panel until the regexes are assigned
self.out_panel = OutputPanel(panel_name)
# Default to the current files directory if no working directory was given
if (not working_dir and
self.window.active_view() and
self.window.active_view().file_name()):
working_dir = os.path.dirname(
self.window.active_view().file_name())
self.out_panel.set('result_file_regex', file_regex)
self.out_panel.set('result_line_regex', line_regex)
self.out_panel.set('result_base_dir', working_dir)
self.out_panel.set('word_wrap', word_wrap)
self.out_panel.set('line_numbers', False)
self.out_panel.set('gutter', False)
self.out_panel.set('rulers', [])
self.out_panel.set('scroll_past_end', False)
self.out_panel.view.assign_syntax(syntax)
self.out_panel.set('color_scheme', '')
self.encoding = encoding
self.quiet = quiet
self.proc = None
if not self.quiet:
if shell_cmd:
print("Running " + shell_cmd)
else:
print("Running " + " ".join(cmd))
sublime.status_message("Building")
if preamble:
self.append_string(self.proc, preamble)
show_panel_on_build = sublime.load_settings(
"Dart - Plugin Settings.sublime-settings").get("show_panel_on_build", True)
if show_panel_on_build:
self.out_panel.show()
merged_env = env.copy()
if self.window.active_view():
user_env = self.window.active_view().settings().get('build_env')
if user_env:
merged_env.update(user_env)
# Change to the working dir, rather than spawning the process with it,
# so that emitted working dir relative path names make sense
if working_dir:
os.chdir(working_dir)
self.debug_text = ""
if shell_cmd:
self.debug_text += "[shell_cmd: " + shell_cmd + "]\n"
else:
self.debug_text += "[cmd: " + str(cmd) + "]\n"
self.debug_text += "[dir: " + str(os.getcwd()) + "]\n"
if "PATH" in merged_env:
self.debug_text += "[path: " + str(merged_env["PATH"]) + "]"
else:
self.debug_text += "[path: " + str(os.environ["PATH"]) + "]"
try:
# Forward kwargs to AsyncProcess
self.proc = AsyncProcess(cmd, shell_cmd, merged_env, self,
**kwargs)
except Exception as e:
self.append_string(None, str(e) + "\n")
self.append_string(None, self.debug_text + "\n")
if not self.quiet:
self.append_string(None, "[Finished]")
def append_data(self, proc, data):
if proc != self.proc:
# a second call to exec has been made before the first one
# finished, ignore it instead of intermingling the output.
if proc:
proc.kill()
return
try:
str_ = data.decode(self.encoding)
except UnicodeEncodeError:
str_ = "[Decode error - output not " + self.encoding + "]\n"
proc = None
return
# Normalize newlines, Sublime Text always uses a single \n separator
# in memory.
str_ = str_.replace('\r\n', '\n').replace('\r', '\n')
self.out_panel.write(str_)
def append_string(self, proc, str):
self.append_data(proc, str.encode(self.encoding))
def finish(self, proc):
if not self.quiet:
elapsed = time.time() - proc.start_time
exit_code = proc.exit_code()
if (exit_code == 0) or (exit_code == None):
self.append_string(proc, "[Finished in %.1fs]" % (elapsed))
else:
self.append_string(proc,
"[Finished in %.1fs with exit code %d]\n" %
(elapsed, exit_code))
self.append_string(proc, self.debug_text)
if proc != self.proc:
return
# XXX: What's this for?
errs = self.out_panel.view.find_all_results()
if len(errs) == 0:
sublime.status_message("Build finished")
else:
sublime.status_message(("Build finished with %d errors") % len(errs))
def on_data(self, proc, data):
after(0, functools.partial(self.append_data, proc, data))
def on_finished(self, proc):
after(0, functools.partial(self.finish, proc))