forked from DisposaBoy/GoSublime
/
gsdepends.py
184 lines (157 loc) · 4.15 KB
/
gsdepends.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
import gscommon as gs, margo, gsq
import threading, traceback, os, re
import sublime, sublime_plugin
DOMAIN = 'GsDepends'
CHANGES_SPLIT_PAT = re.compile(r'^##', re.MULTILINE)
CHANGES_MATCH_PAT = re.compile(r'^\s*(r[\d.]+[-]\d+)\s+(.+?)\s*$', re.DOTALL)
GOCODE_REPO = 'github.com/nsf/gocode'
MARGO_REPO = 'github.com/DisposaBoy/MarGo'
dep_check_done = False
class GsDependsOnLoad(sublime_plugin.EventListener):
def on_load(self, view):
global dep_check_done
sublime.set_timeout(gs.sync_settings, 0)
if not dep_check_done and gs.is_go_source_view(view):
dep_check_done = True
margo.dispatch(check_depends, 'checking dependencies')
class GsUpdateDeps(sublime_plugin.TextCommand):
def run(self, edit):
run_go_get()
def split_changes(s):
changes = []
for m in CHANGES_SPLIT_PAT.split(s):
m = CHANGES_MATCH_PAT.match(m)
if m:
changes.append((m.group(1), m.group(2)))
changes.sort()
return changes
def call_cmd(cmd):
_, _, exc = gs.runcmd(cmd)
return not exc
def do_hello():
global hello_sarting
if hello_sarting:
return
hello_sarting = True
tid = gs.begin(DOMAIN, 'Starting Gocode', False)
call_cmd(['gocode'])
gs.end(tid)
margo_cmd = list(gs.setting('margo_cmd', []))
if margo_cmd:
margo_cmd.extend([
"-d",
"-call", "replace",
"-addr", gs.setting('margo_addr', '')
])
tid = gs.begin(DOMAIN, 'Starting MarGo', False)
out, err, _ = gs.runcmd(margo_cmd)
gs.end(tid)
out = out.strip()
err = err.strip()
if err:
gs.notice(DOMAIN, err)
elif out:
gs.notice(DOMAIN, 'MarGo started %s' % out)
hello_sarting = False
else:
err = 'Missing `margo_cmd`'
gs.notice("MarGo", err)
hello_sarting = False
hello_sarting = False
def hello():
_, err = margo.post('/', 'hello', {}, True, False)
if err:
dispatch(do_hello, 'Starting MarGo and gocode...')
else:
call_cmd(['gocode'])
def run_go_get():
msg = 'Installing/updating gocode and MarGo...'
def f():
out, err, _ = gs.runcmd(['go', 'get', '-u', '-v', GOCODE_REPO, MARGO_REPO])
margo.bye_ni()
call_cmd(['gocode', 'close'])
gs.notice(DOMAIN, '%s done' % msg)
gs.println(DOMAIN, '%s done\n%s%s' % (msg, out, err))
do_hello()
dispatch(f, msg)
def check_depends():
gr = gs.go_env_goroot()
if not gr:
gs.notice(DOMAIN, 'The `go` command cannot be found')
return
e = gs.env()
if not e.get('GOROOT'):
os.environ['GOROOT'] = gr
elif not e.get('GOPATH'):
gs.notice(DOMAIN, "GOPATH and/or GOROOT appear to be unset")
gs.println(
'GoSublime: checking dependencies',
('\tGOROOT is: %s' % e.get('GOROOT', gr)),
('\tGOPATH is: %s' % e.get('GOPATH', ''))
)
missing = []
for cmd in ('gocode', 'MarGo'):
if not call_cmd([cmd, '--help']):
missing.append(cmd)
if missing:
def cb(i, _):
if i == 0:
run_go_get()
items = [[
'GoSublime depends on gocode and MarGo',
'Install %s (using `go get`)' % ', '.join(missing),
'gocode repo: %s' % GOCODE_REPO,
'MarGo repo: %s' % MARGO_REPO,
]]
gs.show_quick_panel(items, cb)
gs.println(DOMAIN, '\n'.join(items[0]))
return
changelog_fn = os.path.join(sublime.packages_path(), 'GoSublime', "CHANGELOG.md")
try:
with open(changelog_fn) as f:
s = f.read()
except IOError:
gs.notice(DOMAIN, traceback.format_exc())
return
changes = split_changes(s)
if changes:
def cb():
settings_fn = 'GoSublime-GsDepends.sublime-settings'
settings = sublime.load_settings(settings_fn)
new_rev = changes[-1][0]
old_rev = settings.get('tracking_rev', '')
def on_panel_close(i, win):
if i > 0:
settings.set('tracking_rev', new_rev)
sublime.save_settings(settings_fn)
win.open_file(changelog_fn)
if i == 1:
run_go_get()
if new_rev > old_rev:
items = [
[
" ",
"GoSublime updated to %s" % new_rev,
" ",
],
[
"Install/Update dependencies: Gocode, MarGo",
"go get -u %s" % GOCODE_REPO,
"go get -u %s" % MARGO_REPO,
],
[
"View changelog",
"Packages/GoSublime/CHANGELOG.md"
" ",
]
]
gs.show_quick_panel(items, on_panel_close)
sublime.set_timeout(cb, 0)
else:
margo.call(
path='/',
args='hello',
message='hello MarGo'
)
def dispatch(f, msg=''):
gsq.dispatch(DOMAIN, f, msg)