forked from guillermooo/dart-sublime-bundle
/
pub_support.py
116 lines (89 loc) · 3.78 KB
/
pub_support.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
# 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.)
import sublime
import sublime_plugin
from os.path import join
import os
import subprocess
import threading
from Dart.lib import ga
from Dart.lib.build.base import DartBuildCommandBase
from Dart.lib.event import EventSource
from Dart.lib.path import is_pubspec
from Dart.lib.path import is_view_dart_script
from Dart.lib.sdk import SDK
from Dart.sublime_plugin_lib import PluginLogger
from Dart.sublime_plugin_lib.panels import OutputPanel
from Dart.sublime_plugin_lib.plat import is_windows
from Dart.sublime_plugin_lib.plat import supress_window
from Dart.sublime_plugin_lib.sublime import after
_logger = PluginLogger(__name__)
class PubspecListener(sublime_plugin.EventListener):
def __init__(self, *args, **kwargs):
sublime_plugin.EventListener.__init__(self, *args, **kwargs)
def on_post_save(self, view):
if not is_pubspec(view):
return
_logger.debug("running pub with %s", view.file_name())
RunPub(view, view.file_name())
def RunPub(view, file_name):
# FIXME: Infefficient. We should store the path to the sdk away.
dartsdk_path = SDK().path
if not dartsdk_path:
_logger.debug("`dartsdk_path` missing; aborting pub")
return
PubThread(view.window(), dartsdk_path, file_name).start()
# TODO(guillermooo): Perhaps we can replace this with Default.exec.AsyncProc.
class PubThread(threading.Thread):
def __init__(self, window, dartsdk_path, file_name):
super(PubThread, self).__init__()
self.daemon = True
self.window = window
self.dartsdk_path = dartsdk_path
self.file_name = file_name
def run(self):
working_directory = os.path.dirname(self.file_name)
pub_path = join(self.dartsdk_path, 'bin', 'pub')
if is_windows():
pub_path += '.bat'
print('pub get %s' % self.file_name)
proc = subprocess.Popen([pub_path, 'get'],
cwd=working_directory,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
startupinfo=supress_window())
out, errs = proc.communicate()
data = out.decode('utf-8')
if proc.returncode != 0:
errs = errs.decode('utf-8')
_logger.error("error running pub: %s\n%s", self.file_name, errs)
data = 'error running pub: %s\n%s' % (self.file_name, errs)
after(50, lambda: self.callback(data))
def format_data(self, data):
return data.replace('\r', '')
def callback(self, data):
panel = OutputPanel('dart.out')
panel.write(self.format_data(data))
panel.show()
# We need a command because we want to use analytics. We can't simply use a
# .sublime-build file.
class DartPubBuildCommand(DartBuildCommandBase):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.add_event_handler(EventSource.ON_PUB_BUILD,
DartPubBuildCommand.on_pub_build)
def run(self, working_dir=None, **kwargs):
assert working_dir is not None, 'wrong call'
self.raise_event(self, EventSource.ON_PUB_BUILD)
self.execute(**{'working_dir': working_dir,
'shell_cmd': 'pub build',
'preamble': 'Running pub build...\n',
})
@classmethod
def on_pub_build(self, *args, **kwargs):
ga.Event(category='actions',
action='on_pub_build',
label='Running "pub build" command',
value=1,
).send()