forked from joeferraro/MavensMate-SublimeText
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.py
More file actions
executable file
·1085 lines (966 loc) · 42.1 KB
/
util.py
File metadata and controls
executable file
·1085 lines (966 loc) · 42.1 KB
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import sublime
import sublime_plugin
import sys
import os
import subprocess
import json
import threading
import re
import time
import pipes
import shutil
import codecs
# import string
# import random
# from datetime import datetime, date, time
try:
import urllib, urllib2
except ImportError:
import urllib.request as urllib
try:
import apex_extensions
except:
import MavensMate.apex_extensions as apex_extensions
import traceback
from operator import itemgetter
from datetime import datetime
if os.name != 'nt':
import unicodedata
#PLUGIN_DIRECTORY = os.getcwd().replace(os.path.normpath(os.path.join(os.getcwd(), '..', '..')) + os.path.sep, '').replace(os.path.sep, '/')
#for future reference (windows/linux support)
#sublime.packages_path()
try:
mm_dir = os.getcwdu()
except:
mm_dir = os.path.dirname(__file__)
settings = sublime.load_settings('mavensmate.sublime-settings')
hide_panel = settings.get('mm_hide_panel_on_success', 1)
hide_time = settings.get('mm_hide_panel_time', 1)
packages_path = sublime.packages_path()
sublime_version = int(float(sublime.version()))
def package_check():
#ensure user settings are installed
try:
if not os.path.exists(packages_path+"/User/mavensmate.sublime-settings"):
shutil.copyfile(mm_dir+"/mavensmate.sublime-settings", packages_path+"/User/mavensmate.sublime-settings")
except:
pass
def mm_call(operation, mm_debug_panel=True, **kwargs):
settings = sublime.load_settings('mavensmate.sublime-settings')
if operation != 'new_project' and operation != 'new_project_from_existing_directory' and is_project_legacy() == True:
operation = 'upgrade_project'
if not os.path.exists( os.path.expanduser ( settings.get('mm_location') ) ) :
active_window_id = sublime.active_window().id()
printer = PanelPrinter.get(active_window_id)
printer.show()
message = '[OPERATION FAILED]: Could not find MavensMate.app. Download MavensMate.app from http://www.joe-ferraro.com/mavensmate/MavensMate.app and place in /Applications. Also, please ensure mm_app_location and mm_location are set properly in Sublime Text (MavensMate --> Settings --> User)'
printer.write('\n'+message+'\n')
return
printer = None
context = kwargs.get('context', None)
params = kwargs.get('params', None)
if mm_debug_panel:
try:
if isinstance(context, sublime.View):
active_window_id = sublime.active_window().id()
else:
active_window_id = context.window.id()
except:
active_window_id = sublime.active_window().id()
printer = PanelPrinter.get(active_window_id)
printer.show()
message = 'Handling requested operation...'
if operation == 'new_metadata':
message = 'Creating New '+params['metadata_type']+' => ' + params['metadata_name']
elif operation == 'synchronize':
if 'files' in params and len(params['files'])>0:
kind = params['files'][0]
elif 'directories' in params and len(params['directories'])>0:
kind = params['directories'][0]
else:
kind = '???'
message = 'Synchronizing to Server => ' + kind
elif operation == 'compile':
if 'files' in params and len(params['files']) == 1:
message = 'Compiling => ' + params['files'][0]
else:
message = 'Compiling Selected Metadata'
elif operation == 'compile_project':
message = 'Compiling Project'
elif operation == 'edit_project':
message = 'Opening Edit Project dialog'
elif operation == 'unit_test':
if 'selected' in params and len(params['selected']) == 1:
message = "Running Apex Test for " + params['selected'][0]
else:
message = 'Opening Apex Test Runner'
elif operation == 'clean_project':
message = 'Cleaning Project'
elif operation == 'deploy':
message = 'Opening Deploy dialog'
elif operation == 'execute_apex':
message = 'Opening Execute Apex dialog'
elif operation == 'upgrade_project':
message = 'Your MavensMate project needs to be upgraded. Opening the upgrade UI.'
elif operation == 'index_apex_overlays':
message = 'Indexing Apex Overlays'
elif operation == 'index_metadata':
message = 'Indexing Metadata'
elif operation == 'delete':
if 'files' in params and len(params['files']) == 1:
message = 'Deleting => ' + get_active_file()
else:
message = 'Deleting Selected Metadata'
elif operation == 'refresh':
if 'files' in params and len(params['files']) == 1:
message = 'Refreshing => ' + get_active_file()
else:
message = 'Refreshing Selected Metadata'
elif operation == 'open_sfdc_url':
message = 'Opening Selected Metadata'
elif operation == 'new_apex_overlay':
message = 'Creating Apex Overlay'
elif operation == 'delete_apex_overlay':
message = 'Deleting Apex Overlay'
elif operation == 'fetch_logs':
message = 'Fetching Apex Logs'
elif operation == 'project_from_existing_directory':
message = 'Opening New Project Dialog'
elif operation == 'index_apex':
message = 'Indexing Project Apex Metadata.'
if mm_debug_panel:
printer.write('\n'+message+'\n')
threads = []
thread = MavensMateTerminalCall(
operation,
project_name=get_project_name(),
active_file=get_active_file(),
mm_location= os.path.expanduser ( settings.get('mm_location') ),
params=params
)
threads.append(thread)
thread.start()
if mm_debug_panel == False:
ThreadProgress(thread, message, 'Operation complete')
thread_progress_handler(operation, threads, printer, 0)
def is_project_legacy():
if os.path.exists(mm_project_directory()+"/config/settings.yaml"):
return True
else:
return False
#monitors thread for activity, passes to the result handler when thread is complete
def thread_progress_handler(operation, threads, printer, i=0):
result = None
this_thread = None
next_threads = []
for thread in threads:
if printer != None:
printer.write('.')
if thread.is_alive():
next_threads.append(thread)
continue
if thread.result == None:
continue
this_thread = thread
result = thread.result
threads = next_threads
if len(threads):
sublime.set_timeout(lambda: thread_progress_handler(operation, threads, printer, i), 200)
return
handle_result(operation, printer, result, this_thread)
#handles the result of the mm script
def handle_result(operation, printer, result, thread):
try:
result = json.loads(result)
print_result_message(operation, result, printer, thread)
if operation == 'new_metadata' and 'success' in result and to_bool(result['success']) == True:
if 'messages' in result:
if type(result['messages']) is not list:
result['messages'] = [result['messages']]
for m in result['messages']:
if 'package.xml' not in m['fileName']:
file_name = m['fileName']
location = mm_project_directory() + "/" + file_name.replace('unpackaged/', 'src/')
sublime.active_window().open_file(location)
break
if 'success' in result and to_bool(result['success']) == True:
if printer != None:
printer.hide()
elif 'State' in result and result['State'] == 'Completed':
#tooling api
if printer != None:
printer.hide()
if operation == 'refresh':
sublime.set_timeout(lambda: sublime.active_window().active_view().run_command('revert'), 200)
clear_marked_line_numbers()
except AttributeError:
if printer != None:
printer.write('\n[OPERATION FAILED]: Whoops, unable to parse the response. Please report this issue at https://github.com/joeferraro/MavensMate-SublimeText')
printer.write('\n[RESPONSE FROM MAVENSMATE]: '+result+'\n')
except Exception:
if printer != None:
printer.write('\n[OPERATION FAILED]: Whoops, you found a bug. Please report this issue at https://github.com/joeferraro/MavensMate-SublimeText')
printer.write('\n[RESPONSE FROM MAVENSMATE]: '+result+'\n')
#prints the result of the mm operation, can be a string or a dict
def print_result_message(operation, res, printer, thread):
if 'State' in res and res['State'] == 'Failed' and 'CompilerErrors' in res:
#here we're parsing a response from the tooling endpoint
errors = json.loads(res['CompilerErrors'])
if type(errors) is not list:
errors = [errors]
for e in errors:
line_col = ""
line, col = 1, 1
if 'line' in e:
line = int(e['line'])
line_col = ' (Line: '+str(line)
mark_line_numbers([line], "bookmark")
if 'column' in e:
col = int(e['column'])
line_col += ', Column: '+str(col)
if len(line_col):
line_col += ')'
printer.write('\n[COMPILE FAILED]: ' + e['problem'] + line_col + '\n')
elif 'success' in res and to_bool(res['success']) == False and 'messages' in res:
#here we're parsing a response from the metadata endpoint
line_col = ""
msg = None
failures = None
if type( res['messages'] ) == list:
for m in res['messages']:
if 'problem' in m:
msg = m
break
if msg == None: #must not have been a compile error, must be a test run error
if 'run_test_result' in res and 'failures' in res['run_test_result'] and type( res['run_test_result']['failures'] ) == list:
failures = res['run_test_result']['failures']
elif 'failures' in res['run_test_result']:
failures = [res['run_test_result']['failures']]
#print(failures)
else:
msg = res['messages']
if msg != None:
if 'lineNumber' in msg:
line_col = ' (Line: '+msg['lineNumber']
mark_line_numbers([int(float(msg['lineNumber']))], "bookmark")
if 'columnNumber' in msg:
line_col += ', Column: '+msg['columnNumber']
if len(line_col) > 0:
line_col += ')'
printer.write('\n[DEPLOYMENT FAILED]: ' + msg['fileName'] + ': ' + msg['problem'] + line_col + '\n')
elif failures != None:
for f in failures:
printer.write('\n[DEPLOYMENT FAILED]: ' + f['name'] + ', ' + f['methodName'] + ': ' + f['message'] + '\n')
elif 'success' in res and res["success"] == False and 'line' in res:
#this is a response from the apex compile api
line_col = ""
line, col = 1, 1
if 'line' in res:
line = int(res['line'])
line_col = ' (Line: '+str(line)
mark_line_numbers([line], "bookmark")
if 'column' in res:
col = int(res['column'])
line_col += ', Column: '+str(col)
if len(line_col):
line_col += ')'
#scroll to the line and column of the exception
if settings.get('mm_compile_scroll_to_error', True) and not thread == None and os.path.exists(thread.active_file):
#open file, if already open it will bring it to focus
view = sublime.active_window().open_file(thread.active_file)
pt = view.text_point(line-1, col-1)
view.sel().clear()
view.sel().add(sublime.Region(pt))
view.show(pt)
printer.write('\n[COMPILE FAILED]: ' + res['problem'] + line_col + '\n')
elif 'success' in res and to_bool(res['success']) == True and 'Messages' in res and len(res['Messages']) > 0:
printer.write('\n[Operation completed Successfully - With Compile Errors]' + '\n')
printer.write('\n[COMPILE ERRORS] - Count:' )
for m in res['Messages']:
printer.write('\n' + 'FileName: ' + m['fileName'] + ': ' + m['problem'] + 'Line: ' + m['lineNumber'] + '\n')
elif 'success' in res and to_bool(res['success']) == True:
printer.write('\n[Operation completed Successfully]' + '\n')
elif 'success' in res and to_bool(res['success']) == False and 'body' in res:
printer.write('\n[OPERATION FAILED]:' + res['body'] + '\n')
elif 'success' in res and to_bool(res['success']) == False:
printer.write('\n[OPERATION FAILED]' + '\n')
else:
printer.write('\n[Operation Completed Successfully]' + '\n')
def parse_json_from_file(location):
try:
json_data = open(location)
data = json.load(json_data)
json_data.close()
return data
except:
return {}
def get_number_of_lines_in_file(file_path):
f = open(file_path)
lines = f.readlines()
f.close()
return len(lines) + 1
def get_execution_overlays(file_path):
try:
response = []
fileName, ext = os.path.splitext(file_path)
if ext == ".cls" or ext == ".trigger":
api_name = fileName.split("/")[-1]
overlays = parse_json_from_file(mm_project_directory()+"/config/.overlays")
for o in overlays:
if o['API_Name'] == api_name:
response.append(o)
return response
except:
return []
#creates resource-bundles for the static resource(s) selected
def create_resource_bundle(self, files):
for file in files:
fileName, fileExtension = os.path.splitext(file)
if fileExtension != '.resource':
sublime.message_dialog("You can only create resource bundles for static resources")
return
printer = PanelPrinter.get(self.window.id())
printer.show()
printer.write('\nCreating Resource Bundle(s)\n')
if not os.path.exists(mm_project_directory()+'/resource-bundles'):
os.makedirs(mm_project_directory()+'/resource-bundles')
for file in files:
fileName, fileExtension = os.path.splitext(file)
baseFileName = fileName.split("/")[-1]
if os.path.exists(mm_project_directory()+'/resource-bundles/'+baseFileName+fileExtension):
printer.write('[OPERATION FAILED]: The resource bundle already exists\n')
return
cmd = 'unzip \''+file+'\' -d \''+mm_project_directory()+'/resource-bundles/'+baseFileName+fileExtension+'\''
res = os.system(cmd)
printer.write('[Resource bundle creation complete]\n')
printer.hide()
send_usage_statistics('Create Resource Bundle')
def get_active_file():
try:
return sublime.active_window().active_view().file_name()
except Exception as e:
return ''
def get_project_name():
try:
return os.path.basename(sublime.active_window().folders()[0])
except:
return None
def check_for_workspace():
workspace = mm_workspace()
if workspace == None or workspace == "":
#os.makedirs(settings.get('mm_workspace')) we're not creating the directory here bc there's some sort of weird race condition going on
msg = 'Your [mm_workspace] property is not set. Open \'MavensMate > Settings > User\' or press \'Cmd+Shift+,\' and set this property to the full path of your workspace. Thx!'
sublime.error_message(msg)
raise BaseException
if not os.path.exists(workspace):
#os.makedirs(settings.get('mm_workspace')) we're not creating the directory here bc there's some sort of weird race condition going on
msg = 'Your [mm_workspace] directory \''+workspace+'\' does not exist. Please create the directory then try your operation again. Thx!'
sublime.error_message(msg)
raise BaseException
def sublime_project_file_path():
project_directory = sublime.active_window().folders()[0]
if os.path.isfile(project_directory+"/.sublime-project"):
return project_directory+"/.sublime-project"
elif os.path.isfile(project_directory+"/"+get_project_name()+".sublime-project"):
return project_directory+"/"+get_project_name()+".sublime-project"
else:
return None
# check for mavensmate .settings file
def is_mm_project():
workspace = mm_workspace();
if workspace == "" or workspace == None or not os.path.exists(workspace):
return False
try:
if os.path.isfile(sublime.active_window().folders()[0]+"/config/.settings"):
return True
elif os.path.isfile(sublime.active_window().folders()[0]+"/config/settings.yaml"):
return True
else:
return False
except:
return False
def get_file_extension(filename=None):
try :
if not filename: filename = get_active_file()
fn, ext = os.path.splitext(filename)
return ext
except:
pass
return None
def get_apex_file_properties():
return parse_json_from_file(mm_project_directory()+"/config/.apex_file_properties")
def is_mm_file(filename=None):
try :
if is_mm_project():
if not filename:
filename = get_active_file()
if os.path.exists(filename):
settings = sublime.load_settings('mavensmate.sublime-settings')
valid_file_extensions = settings.get("mm_apex_file_extensions", [])
if get_file_extension(filename) in valid_file_extensions:
return True
elif "-meta.xml" in filename:
return True
except:
pass
return False
def is_mm_dir(directory):
if is_mm_project():
if os.path.isdir(directory):
if os.path.basename(directory) == "src" or os.path.basename(directory) == get_project_name() or os.path.basename(os.path.abspath(os.path.join(directory, os.pardir))) == "src":
return True
return False
def is_browsable_file(filename=None):
try :
if is_mm_project():
if not filename:
filename = get_active_file()
if is_mm_file(filename):
basename = os.path.basename(filename)
data = get_apex_file_properties()
if basename in data:
return True
return os.path.isfile(filename+"-meta.xml")
except:
pass
return False
def is_apex_class_file(filename=None):
if not filename: filename = get_active_file()
if is_mm_file(filename):
f, ext = os.path.splitext(filename)
if ext == ".cls":
return True
return False
def is_apex_test_file(filename=None):
if not filename: filename = get_active_file()
if not is_apex_class_file(filename): return False
with codecs.open(filename, "r", "utf-8") as content_file:
content = content_file.read()
p = re.compile("@isTest\s", re.I + re.M)
if p.search(content):
p = re.compile("\stestMethod\s", re.I + re.M)
if p.search(content): return True
return False
def is_apex_webservice_file(filename=None):
if not filename: filename = get_active_file()
if not is_apex_class_file(filename): return False
with codecs.open(filename, "r", "utf-8") as content_file:
content = content_file.read()
p = re.compile("global\s+class\s", re.I + re.M)
if p.search(content):
p = re.compile("\swebservice\s", re.I + re.M)
if p.search(content): return True
return False
def mm_project_directory():
#return sublime.active_window().active_view().settings().get('mm_project_directory') #<= bug
folders = sublime.active_window().folders()
if len(folders) > 0:
return sublime.active_window().folders()[0]
else:
return mm_workspace()
def mm_workspace():
settings = sublime.load_settings('mavensmate.sublime-settings')
if settings.get('mm_workspace') != None:
workspace = settings.get('mm_workspace')
else:
workspace = sublime.active_window().active_view().settings().get('mm_workspace')
return workspace
def mark_overlays(lines):
mark_line_numbers(lines, "dot", "overlay")
def write_overlays(overlay_result):
result = json.loads(overlay_result)
if result["totalSize"] > 0:
for r in result["records"]:
sublime.set_timeout(lambda: mark_line_numbers([int(r["Line"])], "dot", "overlay"), 100)
def mark_line_numbers(lines, icon="dot", mark_type="compile_issue"):
points = [sublime.active_window().active_view().text_point(l - 1, 0) for l in lines]
regions = [sublime.Region(p, p) for p in points]
sublime.active_window().active_view().add_regions(mark_type, regions, "operation.fail",
icon, sublime.HIDDEN | sublime.DRAW_EMPTY)
def clear_marked_line_numbers(mark_type="compile_issue"):
try:
sublime.set_timeout(lambda: sublime.active_window().active_view().erase_regions(mark_type), 100)
except Exception as e:
print(e.message)
print('no regions to clean up')
def compile_callback(result):
try:
result = json.loads(result)
if 'success' in result and result['success'] == True:
clear_marked_line_numbers()
elif 'State' in result and result['State'] == 'Completed':
clear_marked_line_numbers()
except:
print('[MAVENSMATE] Issue handling compile result')
def print_debug_panel_message(message):
printer = PanelPrinter.get(sublime.active_window().id())
printer.show()
printer.write(message)
def get_apex_completions(search_name):
completions = []
if not os.path.exists(os.path.join(mm_project_directory(), 'config', '.apex_file_properties')):
return []
apex_props = parse_json_from_file(os.path.join(mm_project_directory(), "config", ".apex_file_properties"))
for p in apex_props.keys():
if p == search_name+".cls" and 'symbolTable' in apex_props[p]:
symbol_table = apex_props[p]['symbolTable']
if 'constructors' in symbol_table:
for c in symbol_table['constructors']:
completions.append((c["visibility"] + " " + c["name"], c["name"]))
if 'properties' in symbol_table:
for c in symbol_table['properties']:
completions.append((c["visibility"] + " " + c["name"], c["name"]))
if 'methods' in symbol_table:
for c in symbol_table['methods']:
params = ''
if 'parameters' in c and type(c['parameters']) is list and len(c['parameters']) > 0:
for p in c['parameters']:
params += p['name'] + " (" + p["type"] + ")"
completions.append((c["visibility"] + " " + c["name"]+"("+params+") "+c['returnType'], c["name"]))
return sorted(completions)
#parses the input from sublime text
def parse_new_metadata_input(input):
input = input.replace(" ", "")
if "," in input:
params = input.split(",")
api_name = params[0]
class_type_or_sobject_name = params[1]
return api_name, class_type_or_sobject_name
else:
return input
def to_bool(value):
"""
Converts 'something' to boolean. Raises exception for invalid formats
Possible True values: 1, True, "1", "TRue", "yes", "y", "t"
Possible False values: 0, False, None, [], {}, "", "0", "faLse", "no", "n", "f", 0.0, ...
"""
if str(value).lower() in ("yes", "y", "true", "t", "1"): return True
if str(value).lower() in ("no", "n", "false", "f", "0", "0.0", "", "none", "[]", "{}"): return False
raise Exception('Invalid value for boolean conversion: ' + str(value))
def get_tab_file_names():
tabs = []
win = sublime.active_window()
for vw in win.views():
if vw.file_name() is not None:
try:
extension = os.path.splitext(vw.file_name())[1]
extension = extension.replace(".","")
if extension in apex_extensions.valid_extensions:
tabs.append(vw.file_name())
except:
pass
else:
pass # leave new/untitled files (for the moment)
return tabs
def send_usage_statistics(action):
settings = sublime.load_settings('mavensmate.sublime-settings')
if settings.get('mm_send_usage_statistics') == True:
sublime.set_timeout(lambda: UsageReporter(action).start(), 3000)
def refresh_active_view():
sublime.set_timeout(sublime.active_window().active_view().run_command('revert'), 100)
def check_for_updates():
settings = sublime.load_settings('mavensmate.sublime-settings')
if settings.get('mm_check_for_updates') == True:
sublime.set_timeout(lambda: AutomaticUpgrader().start(), 5000)
def index_overlays():
mm_call('index_apex_overlays', False)
send_usage_statistics('Index Apex Overlays')
#preps code completion object for search in doxygen documentation
def prep_for_search(name):
#s1 = re.sub('(.)([A-Z]+)', r'\1_\2', name).strip()
#return re.sub('([a-z0-9])([A-Z])', r'\1_\2', s1).lower()
#return re.sub('([A-Z])', r'\1_', name)
return name.replace('_', '')
def start_mavensmate_app():
p = subprocess.Popen("pgrep -fc mmserver", stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=True)
processCount = p.communicate()[0]
msg = None
if p.stderr is not None:
msg = p.stdout.read()
if msg == None and int(processCount) <= 1:
mmLocation = os.path.expanduser ( settings.get('mm_app_location') )
mmServerLocation = os.path.expanduser ( settings.get('mm_location') )
command = mmLocation + " -m " + mmServerLocation
subprocess.Popen( command.split() )
else:
sublime.error_message("MavensMate is not running, please start it from your Applications folder.")
class UsageReporter(threading.Thread):
def __init__(self, action):
self.action = action
threading.Thread.__init__(self)
def run(self):
try:
ip_address = ''
try:
#get ip address
ip_address = urllib2.urlopen('http://ip.42.pl/raw').read()
except:
ip_address = 'unknown'
#get current version of mavensmate
json_data = open(mm_dir+"/packages.json")
data = json.load(json_data)
json_data.close()
current_version = data["packages"][0]["platforms"]["osx"][0]["version"]
#post to usage servlet
url = "https://mavensmate.appspot.com/usage"
headers = { "Content-Type":"application/x-www-form-urlencoded" }
handler = urllib2.HTTPSHandler(debuglevel=0)
opener = urllib2.build_opener(handler)
req = urllib2.Request("https://mavensmate.appspot.com/usage", data='version='+current_version+'&ip_address='+ip_address+'&action='+self.action+'', headers=headers)
response = opener.open(req).read()
#print response
except:
#traceback.print_exc(file=sys.stdout)
print('[MAVENSMATE] failed to send usage statistic')
class ThreadProgress():
"""
Animates an indicator, [= ], in the status area while a thread runs
:param thread:
The thread to track for activity
:param message:
The message to display next to the activity indicator
:param success_message:
The message to display once the thread is complete
"""
def __init__(self, thread, message, success_message, callback=None):
self.thread = thread
self.message = message
self.success_message = success_message
self.addend = 1
self.size = 8
self.callback = None
sublime.set_timeout(lambda: self.run(0), 100)
def run(self, i):
if not self.thread.is_alive():
if hasattr(self.thread, 'result') and not self.thread.result:
sublime.status_message('')
return
sublime.status_message(self.success_message)
if self.callback != None:
self.callback()
return
before = i % self.size
after = (self.size - 1) - before
sublime.status_message('%s [%s=%s]' % \
(self.message, ' ' * before, ' ' * after))
if not after:
self.addend = -1
if not before:
self.addend = 1
i += self.addend
sublime.set_timeout(lambda: self.run(i), 100)
def finish_update():
sublime.message_dialog("MavensMate has been updated successfully!")
printer = PanelPrinter.get(sublime.active_window().id())
printer.hide()
def get_version_number():
try:
json_data = open(mm_dir+"/packages.json")
data = json.load(json_data)
json_data.close()
version = data["packages"][0]["platforms"]["osx"][0]["version"]
return version
except:
return ''
class AutomaticUpgrader(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
def run(self):
try:
json_data = open(mm_dir+"/packages.json")
data = json.load(json_data)
json_data.close()
current_version = data["packages"][0]["platforms"]["osx"][0]["version"]
#j = json.load(urllib.urlopen("https://raw.github.com/joeferraro/MavensMate-SublimeText/master/packages.json"))
j = json.load(urllib.urlopen("https://raw.github.com/joeferraro/MavensMate-SublimeText/2.0/packages.json"))
latest_version = j["packages"][0]["platforms"]["osx"][0]["version"]
release_notes = "\n\nRelease Notes: "
try:
release_notes += j["packages"][0]["platforms"]["osx"][0]["release_notes"] + "\n\n"
except:
release_notes = ""
installed_version_int = int(float(current_version.replace(".", "")))
server_version_int = int(float(latest_version.replace(".", "")))
needs_update = False
if server_version_int > installed_version_int:
needs_update = True
if needs_update == True:
#if sublime.ok_cancel_dialog("A new version of MavensMate ("+latest_version+") is available. "+release_notes+"Would you like to update?", "Update"):
#sublime.set_timeout(lambda: sublime.run_command("update_me"), 1)
sublime.message_dialog("A new version of MavensMate for Sublime Text ("+latest_version+") is available. To update, select 'Plugins' from the MavensMate.app status bar menu.")
except:
print('[MAVENSMATE] skipping update check')
#calls out to the ruby scripts that interact with the metadata api
#pushes them to background threads and reads the piped response
class MavensMateTerminalCall(threading.Thread):
def __init__(self, operation, **kwargs):
self.operation = operation
self.project_name = kwargs.get('project_name', None)
self.active_file = kwargs.get('active_file', None)
self.mm_location = kwargs.get('mm_location', None)
self.params = kwargs.get('params', None)
self.process = None
self.result = None
self.callback = None
if self.params != None:
self.callback = self.params.get('callback', None)
threading.Thread.__init__(self)
def get_arguments(self, ui=False, html=False):
args = {
'-o' : self.operation,
'--html' : html
}
if sublime_version >= 3000:
args['-c'] = 'SUBLIME_TEXT_3'
else:
args['-c'] = 'SUBLIME_TEXT_2'
ui_operations = ['edit_project', 'new_project', 'unit_test', 'deploy', 'execute_apex', 'upgrade_project', 'new_project_from_existing_directory']
if self.operation in ui_operations:
args['--ui'] = True
arg_string = []
for x in args.keys():
if args[x] != None and args[x] != True and args[x] != False:
arg_string.append(x + ' ' + args[x] + ' ')
elif args[x] == True or args[x] == None:
arg_string.append(x + ' ')
stripped_string = ''.join(arg_string).strip()
return stripped_string
def submit_payload(self):
o = self.operation
if o == 'new_metadata':
# unique payload parameters
payload = {
'project_name' : self.project_name,
'api_name' : self.params.get('metadata_name', None),
'metadata_type' : self.params.get('metadata_type', None),
'apex_trigger_object_api_name' : self.params.get('object_api_name', None),
'apex_class_type' : self.params.get('apex_class_type', None)
}
elif o == 'new_project_from_existing_directory':
# no project name
payload = self.params
else:
params = {
'selected': [
'unit_test',
'deploy'
],
'files': [
'compile',
'synchronize',
'refresh',
'refresh_properties',
'open_sfdc_url',
'delete'
],
'directories': [
'refresh',
'synchronize',
'refresh_properties'
],
'type': [
'open_sfdc_url'
]
}
# common parameters
if o == 'new_apex_overlay' or o == 'delete_apex_overlay':
payload = self.params
else:
payload = {}
payload['project_name'] = self.project_name
#selected files
if o in params['files']:
payload['files'] = self.params.get('files', [])
#directories
if o in params['directories']:
payload['directories'] = self.params.get('directories', [])
#selected metadata
if o in params['selected']:
if self.params != None:
payload['selected'] = self.params.get('selected', [])
#open type
if o in params['type']:
payload['type'] = self.params.get('type', 'edit')
if type(payload) is dict:
payload = json.dumps(payload)
print(payload)
try:
self.process.stdin.write(payload)
except:
self.process.stdin.write(payload.encode('utf-8'))
self.process.stdin.close()
def run(self):
print('[MAVENSMATE] executing mm terminal call')
print("{0} {1}".format(pipes.quote(self.mm_location), self.get_arguments()))
self.process = subprocess.Popen("{0} {1}".format(pipes.quote(self.mm_location), self.get_arguments()), stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=True)
self.submit_payload()
if self.process.stdout is not None:
mm_response = self.process.stdout.readlines()
elif self.process.stderr is not None:
mm_response = self.process.stderr.readlines()
try:
response_body = '\n'.join(mm_response)
except:
strs = []
for line in mm_response:
strs.append(line.decode('utf-8'))
response_body = '\n'.join(strs)
print('[MAVENSMATE] response from mm: ' + response_body)
self.result = response_body
if self.operation == 'compile':
compile_callback(response_body)
if self.operation == 'new_apex_overlay' or self.operation == 'delete_apex_overlay':
sublime.set_timeout(lambda : index_overlays(), 100)
if self.callback != None:
print(self.callback)
self.callback(response_body)
#class representing the MavensMate activity/debug panel in Sublime Text
class PanelPrinter(object):
printers = {}
def __init__(self):
self.name = 'MavensMate-OutputPanel'
self.visible = False
self.hide_time = hide_time
self.queue = []
self.strings = {}
self.just_error = False
self.capture = False
self.input = None
self.input_start = None
self.on_input_complete = None
self.original_view = None
@classmethod
def get(cls, window_id):
printer = cls.printers.get(window_id)
if not printer:
printer = PanelPrinter()
printer.window_id = window_id
printer.init()
cls.printers[window_id] = printer
printer.write('==============================================\n')
printer.write('<---- MavensMate for Sublime Text v'+get_version_number()+' ---->\n')
printer.write('==============================================\n')
return printer
def error(self, string):
callback = lambda : self.error_callback(string)
sublime.set_timeout(callback, 1)
def error_callback(self, string):
string = str(string)
self.reset_hide()
self.just_error = True
sublime.error_message('MavensMate: ' + string)
def hide(self, thread = None):
settings = sublime.load_settings('mavensmate.sublime-settings')
hide = settings.get('mm_hide_panel_on_success', True)
if hide == True:
hide_time = time.time() + float(hide)
self.hide_time = hide_time
sublime.set_timeout(lambda : self.hide_callback(hide_time, thread), int(hide * 300))
def hide_callback(self, hide_time, thread):
if thread:
last_added = ThreadTracker.get_last_added(self.window_id)
if thread != last_added:
return
if self.visible and self.hide_time and hide_time == self.hide_time:
if not self.just_error:
self.window.run_command('hide_panel')
self.just_error = False
def init(self):
if not hasattr(self, 'panel'):
self.window = sublime.active_window()
self.panel = self.window.get_output_panel(self.name)
self.panel.set_read_only(True)
self.panel.settings().set('syntax', 'Packages/MavensMate/themes/MavensMate.hidden-tmLanguage')
self.panel.settings().set('color_scheme', 'Packages/MavensMate/themes/MavensMate.hidden-tmTheme')
self.panel.settings().set('word_wrap', True)
self.panel.settings().set('gutter', True)
self.panel.settings().set('line_numbers', True)
def reset_hide(self):
self.hide_time = None
def show(self, force = False):
self.init()
settings = sublime.load_settings('mavensmate.sublime-settings')
hide = settings.get('hide_output_panel', 1)
if force or hide != True or not isinstance(hide, bool):
self.visible = True
self.window.run_command('show_panel', {'panel': 'output.' + self.name})
def write(self, string, key = 'sublime_mm', finish = False):