forked from dcarroll/sublime-lightning
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lightningsave.py
726 lines (605 loc) · 22.4 KB
/
lightningsave.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
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
import sublime
import sublime_plugin
import os
import subprocess
import json
class Helper(sublime_plugin.WindowCommand):
def foo(self):
return
def bundle_op_is_visible(self, dirs):
if len(dirs) == 0:
return False
else:
return self.dir_is_aura(dirs[0])
def file_op_is_visible(self, dirs):
return self.parent_dir_is_aura(dirs[0])
def dir_is_aura(self, working_dir):
return os.path.basename(working_dir) == "aura"
def parent_dir_is_aura(self, working_dir):
return os.path.basename(os.path.dirname(working_dir)) == "aura"
def is_metadata(self, working_dir):
return os.path.basename(os.path.dirname(working_dir)) == "metadata"
def is_static_resource(self, file):
for root, dirs, files in Helper.walk_up(self, file):
if "staticresources" in dirs:
return True
return False
def get_resource_name(self, file):
for root, dirs, files in Helper.walk_up(self, os.path.dirname(file)):
if os.path.basename(os.path.dirname(root)) == "staticresources":
return os.path.basename(root)
def is_bundle_type(self, working_dir, comp_type):
files = os.listdir(working_dir[0])
for filename in files:
fname, fext = os.path.splitext(filename)
if (fext == "." + comp_type):
return True
return False
def has_this_file(self, working_dir, filename):
return os.path.exists(os.path.join(working_dir, filename))
def do_login(self, username, password):
self.window.run_command(
'exec',
{'cmd': ["force", "login", "-u", username, "-p", password]})
return
def do_aura_query(self):
return
def do_fetch(self, bundle, adir):
os.chdir(adir)
if (bundle == 'all'):
self.window.run_command(
'exec',
{'cmd': ["force", "fetch", "-t", "aura", "-d", adir],
'working_dir': adir})
else:
self.window.run_command(
'exec',
{'cmd': ["force", "fetch", "-t", "aura", "-n", bundle, "-d",
adir], 'working_dir': adir})
return
def get_instance_url(self):
p = subprocess.Popen(['force', 'active', '-j'],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
out = p.communicate()[0]
data = json.loads(out.decode("utf-8"))
return data["instanceUrl"]
def get_namespace(self):
p = subprocess.Popen(['force', 'active', '-j'],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
out = p.communicate()[0]
data = json.loads(out.decode("utf-8"))
return data["namespace"]
def get_app_name(self, adir):
os.chdir(adir)
return os.path.basename(adir)
def open_selected_bundle(self, index):
if (index == -1):
return
if (index == 0):
self.do_fetch("all", self.window.folders()[0])
else:
self.do_fetch(self.messages[index][2], self.window.folders()[0])
return
def open_selected_metadata(self, index):
if (index == -1):
return
self.show_metadata_instance_list(self.messages[index][0])
return
def fetch_selected_metadata(self, index):
item = self.messages[index][0]
print("Type: " + self.type)
print("Item: " + item)
self.window.run_command(
'exec',
{'cmd': ["force", "fetch", "-t", self.type, "-n", item],
'working_dir': self.window.folders()[0]})
return
def show_metadata_instance_list(self, metaname):
self.type = metaname
self.messages = []
p = subprocess.Popen(["force", "describe", "-t", "metadata",
"-n", metaname, "-j"],
stdout=subprocess.PIPE)
result = p.communicate()[0]
try:
m = json.loads(result.decode("utf-8"))
for mm in m:
x = [mm['FullName'], "Modified by: " +
mm['LastModifiedByName'],
"File name: " + mm['FileName'],
"Id: " + mm['Id']]
self.messages.append(x)
print(x)
self.window = sublime.active_window()
sublime.set_timeout(
lambda:
self.window.show_quick_panel(
self.messages,
self.fetch_selected_metadata,
sublime.MONOSPACE_FONT), 10)
except:
return
def open_url(self, url):
self.window.run_command(
'exec',
{'cmd': ["open", url]}
)
def show_metadata_type_list(self):
self.messages = []
p = subprocess.Popen(["force", "describe", "-t", "metadata",
"-j"], stdout=subprocess.PIPE)
result = p.communicate()[0]
try:
m = json.loads(result.decode("utf-8"))
for mm in m:
x = [mm['XmlName'], "In folder: " + mm['DirectoryName'],
"Suffix: " + mm['Suffix']]
self.messages.append(x)
self.window = sublime.active_window()
self.window.show_quick_panel(self.messages,
self.open_selected_metadata,
sublime.MONOSPACE_FONT)
except:
return
def show_package_list(self):
self.messages = []
p = subprocess.Popen(["force", "describe", "-t", "metadata",
"-j"], stdout=subprocess.PIPE)
result = p.communicate()[0]
try:
m = json.loads(result.decode("utf-8"))
for mm in m:
x = [mm['XmlName'], "In folder: " + mm['DirectoryName'],
"Suffix: " + mm['Suffix']]
self.messages.append(x)
self.window = sublime.active_window()
self.window.show_quick_panel(self.messages,
self.open_selected_metadata,
sublime.MONOSPACE_FONT)
except:
return
def show_bundle_list(self):
self.messages = []
p = subprocess.Popen(["force", "query", "Select Id, DeveloperName, "
"MasterLabel, Description From "
"AuraDefinitionBundle",
"--format:json"], stdout=subprocess.PIPE)
result = p.communicate()[0]
try:
m = json.loads(result.decode("utf-8"))
self.messages.append(["All Bundles", "*", "Every Bundle",
"All the lightning bundles in your org!"])
for mm in m:
x = [mm['MasterLabel'], mm['Id'], mm["DeveloperName"],
mm["Description"]]
self.messages.append(x)
self.window = sublime.active_window()
self.window.show_quick_panel(self.messages,
self.open_selected_bundle,
sublime.MONOSPACE_FONT)
except:
return
def make_bundle_file(self, file_name, extension, snippet, dirs):
working_dir = dirs[0]
os.chdir(working_dir)
if extension == "cmp" or extension == "app" or extension == "evt":
fn, ex = os.path.splitext(file_name)
os.mkdir(os.path.join(working_dir, file_name))
os.chdir(fn)
working_dir = os.getcwd()
app = open(file_name + "." + extension, "wb")
if int(sublime.version()) >= 3000:
app.write(bytes(snippet, 'UTF-8'))
else: # To support Sublime Text 2
app.write(bytes(snippet))
app.close()
filename = os.path.join(working_dir, file_name + "." + extension)
self.window.open_file(filename)
cmd = 'push -f=' + filename
self.window.run_command(
'exec',
{'cmd': ["force", "aura", cmd]})
return app
def walk_up(self, bottom):
"""
mimic os.walk, but walk 'up'
instead of down the directory tree
"""
bottom = os.path.realpath(bottom)
# get files in current dir
try:
names = os.listdir(bottom)
except Exception as e:
print(e)
return
dirs, nondirs = [], []
for name in names:
if os.path.isdir(os.path.join(bottom, name)):
dirs.append(name)
else:
nondirs.append(name)
yield bottom, dirs, nondirs
new_path = os.path.realpath(os.path.join(bottom, '..'))
# see if we are at the top
if new_path == bottom:
return
for x in Helper.walk_up(self, new_path):
yield x
class LoginCommand(sublime_plugin.WindowCommand):
def run(self):
self.window.show_input_panel(
"Username: ",
"",
self.get_password,
None,
None)
pass
def get_password(self, username):
self.username = username
self.window.show_input_panel(
"Password: ",
"",
self.do_login,
None,
None)
pass
def do_login(self, password):
Helper(self.window).do_login(self.username, password)
return
def is_visible(self):
return True
class FetchMetaCommand(sublime_plugin.WindowCommand):
def run(self):
print("Running FetchMetaCommand")
Helper(self.window).show_metadata_type_list()
def is_visible(self):
return True
class FetchPackageCommand(sublime_plugin.WindowCommand):
def run(self):
print("Running FetchPackageCommand")
Helper(self.window).show_package_list()
def is_visible(self):
return True
class FetchCommand(sublime_plugin.WindowCommand):
def run(self):
# self.window.show_input_panel(
# "Bundle name: ",
# "all",
# self.do_fetch,
# None,
# None)
# pass
Helper(self.window).show_bundle_list()
def do_fetch(self, bundle):
self.dirs = self.window.folders()
Helper(self.window).show_bundle_list()
return
def is_visible(self):
return True
class LightningNewAppCommand(sublime_plugin.WindowCommand):
def run(self, dirs):
self.dirs = dirs
self.window.show_input_panel("App Name:", "", self.on_done, None, None)
pass
def on_done(self, file_name):
Helper(self.window).make_bundle_file(
file_name,
"app",
"<aura:application>\n\n</aura:application>",
self.dirs)
return
def is_visible(self, dirs):
return Helper(self.window).bundle_op_is_visible(dirs)
class LightningNewComponentCommand(sublime_plugin.WindowCommand):
def run(self, dirs):
self.dirs = dirs
self.window.show_input_panel(
"Component Name:",
"",
self.on_done,
None,
None)
pass
def on_done(self, file_name):
Helper(self.window).make_bundle_file(
file_name,
"cmp",
"<aura:component>\n\n</aura:component>",
self.dirs)
return
def is_visible(self, dirs):
return Helper(self.window).bundle_op_is_visible(dirs)
class LightningNewEventCommand(sublime_plugin.WindowCommand):
def run(self, dirs):
self.dirs = dirs
self.window.show_input_panel(
"Event Name:",
"",
self.on_done,
None,
None)
pass
def on_done(self, file_name):
Helper(self.window).make_bundle_file(
file_name,
"evt",
'<aura:event type="APPLICATION">\n\n</aura:event>',
self.dirs)
return
def is_visible(self, dirs):
return Helper(self.window).bundle_op_is_visible(dirs)
class LightningPreviewCommand(sublime_plugin.WindowCommand):
def run(self, dirs):
self.dirs = dirs
appname = Helper.get_app_name(self, dirs[0])
url = Helper.get_instance_url(self)
namespace = Helper.get_namespace(self)
if len(namespace) == 0:
url = url + "/c/" + appname + ".app"
else:
url = url + "/" + namespace + "/" + appname + ".app"
Helper.open_url(self, url)
def is_visible(self, dirs):
helper = Helper(self.window)
if len(dirs) == 0:
return False
isvalidbundle = helper.is_bundle_type(dirs, "app")
return Helper(self.window).file_op_is_visible(dirs) and \
isvalidbundle
class LightningNewControllerCommand(sublime_plugin.WindowCommand):
def run(self, dirs):
self.dirs = dirs
name = os.path.basename(dirs[0]) + "Controller"
Helper(self.window).make_bundle_file(
name,
"js",
"({\n"
"\tmyAction: function(component, event, helper) {\n"
"\t}\n"
"})",
self.dirs)
def is_visible(self, dirs):
helper = Helper(self.window)
if len(dirs) == 0:
return False
hasfile = helper.has_this_file(
dirs[0],
os.path.basename(dirs[0]) + "Controller.js")
isvalidbundle = helper.is_bundle_type(dirs, "app") or \
helper.is_bundle_type(dirs, "cmp")
return Helper(self.window).file_op_is_visible(dirs) and \
isvalidbundle and not hasfile
class LightningNewSvgCommand(sublime_plugin.WindowCommand):
def run(self, dirs):
self.dirs = dirs
name = os.path.basename(dirs[0])
Helper(self.window).make_bundle_file(
name,
"svg",
'<?xml version="1.0" encoding="UTF-8" standalone="no"?>\n'
'<svg width="120px" height="120px" viewBox="0 0 120 120" '
'version="1.1" xmlns="http://www.w3.org/2000/svg" '
'xmlns:xlink="http://www.w3.org/1999/xlink">\n'
'\t<g stroke="none" stroke-width="1" fill="none" '
'fill-rule="evenodd">\n'
'\t\t<path d="M120,108 C120,114.6 114.6,120 108,120 L12,120 '
'C5.4,120 '
'0,114.6 0,108 L0,12 C0,5.4 5.4,0 12,0 L108,0 C114.6,0 120,5.4 '
'120,12 L120,108 L120,108 Z" id="Shape" fill="#2A739E"/>\n'
'\t\t<path d="M77.7383308,20 L61.1640113,20 '
'L44.7300055,63.2000173 '
'L56.0543288,63.2000173 L40,99.623291 L72.7458388,54.5871812 '
'L60.907727,54.5871812 L77.7383308,20 Z" id="Path-1" '
'fill="#FFFFFF"/>\n'
'\t</g>\n'
'</svg>\n',
self.dirs)
def is_visible(self, dirs):
helper = Helper(self.window)
if len(dirs) == 0:
return False
hasfile = helper.has_this_file(
dirs[0],
os.path.basename(dirs[0]) + ".svg")
isvalidbundle = helper.is_bundle_type(dirs, "app") or \
helper.is_bundle_type(dirs, "cmp")
return Helper(self.window).file_op_is_visible(dirs) and \
isvalidbundle and not hasfile
class LightningNewDesignCommand(sublime_plugin.WindowCommand):
def run(self, dirs):
self.dirs = dirs
name = os.path.basename(dirs[0])
Helper(self.window).make_bundle_file(
name,
"design",
'<design:component>\n'
'</design:component>',
self.dirs)
def is_visible(self, dirs):
helper = Helper(self.window)
if len(dirs) == 0:
return False
hasfile = helper.has_this_file(
dirs[0],
os.path.basename(dirs[0]) + ".design")
isvalidbundle = helper.is_bundle_type(dirs, "cmp")
return Helper(self.window).file_op_is_visible(dirs) and \
isvalidbundle and not hasfile
class LightningNewRendererCommand(sublime_plugin.WindowCommand):
def run(self, dirs):
self.dirs = dirs
name = os.path.basename(dirs[0]) + "Renderer"
Helper(self.window).make_bundle_file(
name,
"js",
"({\n"
"\trender: function(component, helper) {\n"
"\t\treturn this.superRender();\n"
"\t}\n"
"})",
self.dirs)
def is_visible(self, dirs):
helper = Helper(self.window)
if len(dirs) == 0:
return False
hasfile = helper.has_this_file(
dirs[0],
os.path.basename(dirs[0]) + "Renderer.js")
isvalidbundle = helper.is_bundle_type(dirs, "app") or \
helper.is_bundle_type(dirs, "cmp")
return Helper(self.window).file_op_is_visible(dirs) and \
isvalidbundle and not hasfile
class LightningNewHelperCommand(sublime_plugin.WindowCommand):
def run(self, dirs):
self.dirs = dirs
name = os.path.basename(dirs[0]) + "Helper"
Helper(self.window).make_bundle_file(
name,
"js",
"({\n"
"\thelperMethod: function() {\n"
"\n\t}\n"
"})",
self.dirs)
def is_visible(self, dirs):
helper = Helper(self.window)
if len(dirs) == 0:
return False
hasfile = helper.has_this_file(
dirs[0],
os.path.basename(dirs[0]) + "Helper.js")
isvalidbundle = helper.is_bundle_type(dirs, "app") or \
helper.is_bundle_type(dirs, "cmp")
return Helper(self.window).file_op_is_visible(dirs) and \
isvalidbundle and not hasfile
class LightningNewDocumentationCommand(sublime_plugin.WindowCommand):
def run(self, dirs):
self.dirs = dirs
name = os.path.basename(dirs[0]) + "Documentation"
Helper(self.window).make_bundle_file(
name,
"auradoc",
'<aura:documentation>\n'
'\t<aura:description>Documentation</aura:description>\n'
'\t<aura:example name="ExampleName" '
'ref="exampleComponentName" label="Label">\n'
'\t\tExample Description\n'
'\t</aura:example>\n'
'</aura:documentation>',
self.dirs)
def is_visible(self, dirs):
helper = Helper(self.window)
if len(dirs) == 0:
return False
hasfile = helper.has_this_file(
dirs[0],
os.path.basename(dirs[0]) + "Documentation.js")
isvalidbundle = helper.is_bundle_type(dirs, "app") or \
helper.is_bundle_type(dirs, "cmp")
return Helper(self.window).file_op_is_visible(dirs) and \
isvalidbundle and not hasfile
class LightningNewStyleCommand(sublime_plugin.WindowCommand):
def run(self, dirs):
self.dirs = dirs
name = os.path.basename(dirs[0]) + "Style"
Helper(self.window).make_bundle_file(
name,
"css",
'.THIS.container {'
'\twidth: 100%;\n'
'\tpadding: 10px 0px;\n'
'\tmargin: 0px;\n'
'}\n\n'
'.THIS .center {\n'
'\tmargin: 0px auto;\n'
'}\n\n'
'.THIS .content {\n'
'\tmax-width: 768px;\n'
'\tfont-size: 12px;\n'
'\tcolor: #3c3d3e;\n'
'}',
self.dirs)
def is_visible(self, dirs):
helper = Helper(self.window)
if len(dirs) == 0:
return False
hasfile = helper.has_this_file(
dirs[0],
os.path.basename(dirs[0]) + "Style.css")
isvalidbundle = helper.is_bundle_type(dirs, "app") or \
helper.is_bundle_type(dirs, "cmp")
return Helper(self.window).file_op_is_visible(dirs) and \
isvalidbundle and not hasfile
class LightningDeleteCommand(sublime_plugin.WindowCommand):
def run(self, files):
for f in files:
command = "delete -p=" + f
self.window.run_command(
'exec',
{'cmd': ["force", "aura", command]})
self.window.find_open_file(f).close()
return
def is_visible(self, files):
for f in files:
p = os.path.dirname(f)
if os.path.basename(p) == "aura":
return True
else:
p = os.path.dirname(p)
if os.path.basename(p) == "aura":
return True
else:
return False
return False
class LightningDeleteBundleCommand(sublime_plugin.WindowCommand):
def run(self, dirs):
# comment
for d in dirs:
command = 'delete -p=' + d
self.window.run_command(
'exec',
{'cmd': ["force", "aura", command]})
for root, dirs, files in os.walk(d):
for name in files:
v = self.window.find_open_file(os.path.join(root, name))
if not (v is None):
v.close()
return
def is_visible(self, dirs):
for d in dirs:
p = os.path.dirname(d)
if os.path.basename(p) == "aura":
return 1 == 1
else:
p = os.path.dirname(p)
if os.path.basename(p) == "aura":
return 1 == 1
else:
return 1 == 2
return False
class LightningSave(sublime_plugin.EventListener):
def on_post_save(self, view):
filename = view.file_name()
if Helper.parent_dir_is_aura(self, os.path.dirname(filename)):
command = 'push -f=' + filename
view.window().run_command(
'exec',
{'cmd': ["force", "aura", command]})
elif Helper.is_metadata(self, os.path.dirname(filename)):
if Helper.is_static_resource(self, filename):
print("is static resource")
else:
command = '-f=' + filename
view.window().run_command(
'exec',
{'cmd': ["force", "push", command]})
elif Helper.is_static_resource(self, os.path.dirname(filename)):
resource_name = Helper.get_resource_name(self, filename)
command = '-t=StaticResource'
command2 = '-n=' + resource_name
view.window().run_command(
'exec',
{'cmd': ["force", "push", command, command2]})
return