-
Notifications
You must be signed in to change notification settings - Fork 2
/
targets.py
231 lines (191 loc) · 7.78 KB
/
targets.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
import os, sublime, sublime_plugin, subprocess, json, re
FILE_REGEX = '^(..[^:\n]*):([0-9]+):?([0-9]+)?:? (.*)'
SYNTAX = 'Packages/Makefile/Make Output.sublime-syntax'
WORKING_DIR = '${folder:${project_path:${file_path}}}'
CANNED = {
'target': 'make_targets',
'file_regex': FILE_REGEX,
'working_dir': WORKING_DIR,
'selector': 'source.makefile',
'syntax': SYNTAX,
'keyfiles': ['Makefile', 'makefile'],
'cancel': {'kill': True},
'variants': [],
'makefile': None
}
TARGET_REGEX = '(.+)\s*:\s{1}'
def plugin_loaded():
build_file = 'MakeTargets.build-template'
dest_file = '{}/User/MakeTargets.sublime-build'.format(sublime.packages_path())
if not os.path.isfile(dest_file):
with open(dest_file, 'w') as f:
json.dump(CANNED, f, indent=2)
def plugin_unloaded():
settings = Settings()
settings.clear_on_change('show_last_cmd_status_bar')
settings.clear_on_change('ignored_target_prefixes')
settings.clear_on_change('target_regex')
settings.clear_on_change('regen_on_save')
settings.clear_on_change('hide_dup_targets')
settings.clear_on_change('phony_name')
settings.clear_on_change('sort_targets')
settings.clear_on_change('job_number')
def Window(window=None):
return window if window else sublime.active_window()
def Variables(window=None):
return Window(window).extract_variables()
def Expand(variable, window=None):
return sublime.expand_variables(variable, Variables(window))
def Settings(file='MakeTargets.sublime-settings'):
return sublime.load_settings(file)
def PanelArg(variant='', caption='MakeTargets'):
return dict(
args=dict(
build_system='Packages/User/MakeTargets.sublime-build',
choice_build_system=True,
choice_variant=True,
variant=variant
),
caption=caption,
command='build'
)
class MakeTargetsCommand(sublime_plugin.WindowCommand):
def __init__(self, edit):
sublime_plugin.WindowCommand.__init__(self, edit)
settings = Settings()
settings.add_on_change('show_last_cmd_status_bar', self.on_show_last_change)
settings.add_on_change('ignored_target_prefixes', self.on_ignore_prefixes_change)
settings.add_on_change('target_regex', self.on_target_regex_change)
settings.add_on_change('hide_dup_targets', self.on_hide_dup_targets_change)
settings.add_on_change('phony_name', self.on_phony_name_change)
settings.add_on_change('sort_targets', self.on_sort_targets_change)
settings.add_on_change('job_number', self.on_job_number_change)
self.build = Settings('MakeTargets.sublime-build')
self._targets = None
self.need_regen = True
self.target_regex = re.compile(settings.get('target_regex', TARGET_REGEX))
self.hide_dups = settings.get('hide_dup_targets', False)
self.phony = self.load_phony()
self.sort_targets = settings.get('sort_targets', False)
self.job_num = settings.get('job_number', None)
def load_phony(self):
phony = Settings().get('phony_name', None)
if phony and not phony.startswith('.'):
phony = '.' + phony
return phony
@property
def makefile(self):
return os.path.join(Expand('${project_path}', self.window), 'Makefile')
@property
def targets(self):
if not self._targets:
targets = []
if os.path.isfile(self.makefile):
with open(self.makefile, 'r') as f:
for line in f.readlines():
if self.phony:
if line.startswith(self.phony):
line = line.split(':', 1)[1].strip()
targets = line.split(' ')
break
elif self.target_regex.search(line):
line = line.strip()
if line and not any([line.startswith(ignore) for ignore in Settings().get('ignored_target_prefixes', [])]):
target = line.split(':')[0].strip()
if not (self.hide_dups and target in targets):
targets.append(target)
if self.sort_targets:
targets.sort()
self._targets = targets
return self._targets
def build_now(self, target, args={}):
self.window.run_command('exec', dict(
update_phantoms_only=True
))
cmd = 'make -j{} {}'.format(self.job_num if self.job_num else '', target).strip()
self.window.run_command('exec', dict(
cmd=cmd,
file_regex=args.get('file_regex', FILE_REGEX),
syntax=args.get('syntax', SYNTAX),
working_dir=args.get('working_dir', Expand(WORKING_DIR, self.window))
))
settings = Settings()
if settings.get('show_last_cmd_status_bar', False):
value = settings.get('status_bar_format', '{command}')
if '{command}' not in value:
value += '{command}'
self.window.active_view().set_status('mt_last_target', value.format(command=cmd))
def show_panel(self):
panel_args = {
'items': [
PanelArg(
variant=target.get('name', ''),
caption='MakeTargets - {}'.format(target.get('make_target', ''))
)
for target in self.build.get('variants')
]
}
panel_args['items'].insert(0, PanelArg())
self.window.run_command('quick_panel', panel_args)
def regen_targets(self, makefile=None):
self.need_regen = False
self._targets = None
self.build.set('makefile', makefile if makefile else self.makefile)
self.build.set('variants', [dict(name=target, make_target=target) for target in self.targets])
sublime.save_settings('MakeTargets.sublime-build')
def run(self, **args):
if args.get('kill'):
self.window.run_command('exec', dict(
kill=True
))
return
if args.get('regen', False):
self.regen_targets(args.get('makefile', None))
return
if self.need_regen or (self.targets and not self.build.get('variants', None) or (self.makefile != self.build.get('makefile', None))):
self.regen_targets()
self.show_panel()
return
if args.get('palette', False):
self.show_panel()
return
target = args.get('make_target', '')
if target == '<<no target>>':
target = ''
self.build_now(target, args)
# override
def on_show_last_change(self):
if not Settings().get('show_last_cmd_status_bar', False):
self.window.active_view().erase_status('mt_last_target')
# override
def on_ignore_prefixes_change(self):
self.need_regen = True
# override
def on_target_regex_change(self):
self.target_regex = re.compile(Settings().get('target_regex', TARGET_REGEX))
# override
def on_hide_dup_targets_change(self):
self.hide_dups = Settings().get('hide_dup_targets', False)
# override
def on_phony_name_change(self):
self.phony = self.load_phony()
self.need_regen = True
# override
def on_sort_targets_change(self):
self.sort_targets = Settings().get('sort_targets', False)
self.need_regen = True
# override
def on_job_number_change(self):
self.job_num = Settings().get('job_number', None)
class MakeTargetsEventListener(sublime_plugin.EventListener):
def __init__(self):
sublime_plugin.EventListener.__init__(self)
settings = Settings()
settings.add_on_change('regen_on_save', self.on_regen_on_save_change)
self.regen_on_save = settings.get('regen_on_save', False)
# override
def on_regen_on_save_change(self):
self.regen_on_save = Settings().get('regen_on_save', False)
def on_post_save_async(self, view):
if self.regen_on_save and view.file_name().endswith('Makefile'):
view.window().run_command('make_targets', {'regen': True, 'makefile': view.file_name()})