-
Notifications
You must be signed in to change notification settings - Fork 46
/
package.py
762 lines (632 loc) · 27 KB
/
package.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
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
import sublime, sublime_plugin
import os
import time
import json
import xml
import datetime
from . import util
from . import context
from . import processor
from .salesforce.lib.panel import Printer
class CombinePackageFiles(sublime_plugin.WindowCommand):
def __init__(self, *args, **kwargs):
super(CombinePackageFiles, self).__init__(*args, **kwargs)
def run(self, files):
package_xmls = []
for _file in files:
if _file.endswith("-meta.xml"):
continue
if _file.endswith(".xml"):
package_xmls.append(_file)
target_dir = os.path.split(files[0])[0]
self.window.run_command('combine_package_folder', {
"dirs": [],
"package_xmls": package_xmls,
"target_dir": target_dir
})
def is_visible(self, files):
if not files: return False
return True
class CombinePackageFolder(sublime_plugin.WindowCommand):
def __init__(self, *args, **kwargs):
super(CombinePackageFolder, self).__init__(*args, **kwargs)
def run(self, dirs, package_xmls=None, target_dir=None):
self.settings = context.get_settings()
# If combine package by folder
if not package_xmls:
package_xmls = []
for _dir in dirs:
for dirpath, dirnames, filenames in os.walk(_dir):
for filename in filenames:
if filename.endswith("-meta.xml"): continue
if not filename.endswith(".xml"): continue
# Package file name
package_xmls.append(os.path.join(dirpath, filename))
all_types = {}
for package_xml in package_xmls:
# Read package.xml content
with open(package_xml, "rb") as fp:
content = fp.read()
""" Combine types sample: [
{"ApexClass": ["test"]},
{"ApexTrigger": ["test"]}
]
"""
try:
_types = util.build_package_types(content)
except xml.parsers.expat.ExpatError as ee:
message = "%s parse error: %s" % (package_xml, str(ee))
Printer.get("error").write(message)
if not sublime.ok_cancel_dialog(message, "Skip?"): return
continue
except KeyError as ex:
if self.settings["debug_mode"]:
print ("%s is not valid package.xml" % package_xml)
continue
for _type in _types:
members = _types[_type]
if _type in all_types:
members.extend(all_types[_type])
members = list(set(members))
all_types[_type] = sorted(members)
if not all_types:
Printer.get("error").write_start().write("No available package.xml to combine")
return
# print (json.dumps(all_types, indent=4))
metadata_objects = []
for _type in all_types:
metadata_objects.append(
"<types>%s<name>%s</name></types>" % (
"".join(["<members>%s</members>" % m for m in all_types[_type]]),
_type
)
)
self.package_xml_content = """<?xml version="1.0" encoding="UTF-8"?>
<Package xmlns="http://soap.sforce.com/2006/04/metadata">
{metadata_objects}
<version>{api_version}.0</version>
</Package>
""".format(
metadata_objects="".join(metadata_objects),
api_version=self.settings["api_version"]
)
# Define target dir to contain the combined xml
if len(dirs) > 0: target_dir = dirs[0]
package_path = os.path.join(target_dir, "All.xml")
sublime.active_window().show_input_panel("Input Package.xml Path",
package_path, self.on_input_package_path, None, None)
def on_input_package_path(self, package_path):
# Check input
if not package_path:
message = 'Invalid path, do you want to try again?'
if not sublime.ok_cancel_dialog(message, "Try Again?"): return
self.window.show_input_panel("Please Input Name: ", "",
self.on_input_extractto, None, None)
return
base = os.path.split(package_path)[0]
if not os.path.exists(base):
os.makedirs(base)
with open(package_path, "wb") as fp:
fp.write(util.format_xml(self.package_xml_content))
view = sublime.active_window().open_file(package_path)
def is_visible(self, dirs):
if not dirs: return False
return True
class ReloadProjectCache(sublime_plugin.WindowCommand):
def __init__(self, *args, **kwargs):
super(ReloadProjectCache, self).__init__(*args, **kwargs)
def run(self, callback_command=None):
self.callback_command = callback_command
self.settings = context.get_settings()
self.metadata_objects = []
for m in self.settings["all_metadata_objects"]:
# Add parent metadata object
self.metadata_objects.append(m)
# Add child metadata object
child_types = util.get_child_types(m)
self.metadata_objects.extend(child_types)
self.selected_index = 0
self.chosen_types = []
self.build_items();
def build_items(self):
self.items = []
if self.chosen_types:
self.items.append("[√]All")
else:
self.items.append("[x]All")
for m in sorted(self.settings["all_metadata_objects"]):
# Add parent to items
if m in self.chosen_types:
self.items.append(" [%s]%s" % ("√", m))
else:
self.items.append(" [%s]%s" % ("x", m))
# Add children to items
child_types = util.get_child_types(m)
for c in child_types:
if c in self.chosen_types:
self.items.append(" [%s]%s" % ("√", c))
else:
self.items.append(" [%s]%s" % ("x", c))
sublime.set_timeout(lambda:self.window.show_quick_panel(self.items,
self.on_choose, sublime.MONOSPACE_FONT, self.selected_index), 10)
def on_choose(self, index):
if index == -1:
# Just when you select one metadata_object at least,
# start to reload cache, otherwise, do nothing
if self.chosen_types:
chosen_types = {}
for c in self.chosen_types:
chosen_types[c] = ["*"]
processor.handle_reload_project_cache(chosen_types,
self.callback_command)
return
# Store the index and chosen metadata
self.selected_index = index
selected_item = self.items[index]
if selected_item == "[x]All":
self.chosen_types = self.metadata_objects[:]
elif selected_item == "[√]All":
if len(self.chosen_types) == len(self.metadata_objects):
self.chosen_types = []
else:
self.chosen_types = self.metadata_objects[:]
else:
chosen_type = selected_item.strip()[3:]
# Get all children for chosen_type
# If chosen_type is child, child can't have children
attr = self.settings[chosen_type]
parent_type = attr["xmlName"]
child_types = util.get_child_types(parent_type)
if "[x]" in selected_item:
if chosen_type == parent_type:
if chosen_type not in self.chosen_types:
self.chosen_types.append(chosen_type)
for c in child_types:
self.chosen_types.append(c)
else:
# Add chosen selected child metadata object
if chosen_type not in self.chosen_types:
self.chosen_types.append(chosen_type)
# Add parent metadata object of selected child
if parent_type not in self.chosen_types:
self.chosen_types.append(parent_type)
else:
# Get all chosen child xml names
parent_type = parent_type
child_types = util.get_child_types(parent_type)
chosen_child_types = []
for c in child_types:
if c in self.chosen_types:
chosen_child_types.append(c)
if parent_type == chosen_type:
if len(child_types) == len(chosen_child_types):
self.chosen_types.remove(chosen_type)
for c in child_types:
if c in self.chosen_types:
self.chosen_types.remove(c)
else:
for c in child_types:
if c not in self.chosen_types:
self.chosen_types.append(c)
else:
# Remove child
self.chosen_types.remove(chosen_type)
# If all siblings are also not exist in selected list,
# parent should also be removed from selected list
if len(chosen_child_types) == 1:
self.chosen_types.remove(parent_type)
self.build_items()
def is_enabled(self):
self.settings = context.get_settings()
described_metadata = util.get_described_metadata(self.settings)
return described_metadata is not None
class BuildPackageXml(sublime_plugin.WindowCommand):
def __init__(self, *args, **kwargs):
super(BuildPackageXml, self).__init__(*args, **kwargs)
def run(self):
sublime.active_window().show_input_panel(
"Modified Last N Hours: ",
"1", self.on_input_last_n_hours, None, None
)
def on_input_last_n_hours(self, last_n_hours):
# Default value of last_n_hours is 7
self.last_n_hours = last_n_hours
if not self.last_n_hours:
self.last_n_hours = 1
if not hasattr(self, "filters"):
sublime.active_window().show_input_panel(
"Input filters for members separated with comma: ",
"", self.on_input_filters, None, None
)
else:
self.on_input_filters(",".join(self.filters))
def on_input_filters(self, filters):
self.filters = filters.split(",") if filters else []
package_cache = os.path.join(self.settings["workspace"], ".config", "package.json")
if not os.path.exists(package_cache):
return self.window.run_command("reload_project_cache", {
"callback_command": "build_package_xml"
})
self.cache = json.loads(open(package_cache).read())
# Get types from settings of current view,
# if current view doesn't have the `types` setting,
# try to build the types from content of current file
""" types format: {
"ApexClass": [
"AClass",
"BClass",
...
],
"ApexTrigger": [
"ATrigger",
"BTrigger",
...
]
}
"""
view = self.window.active_view()
types = view.settings().get("types") if view else {}
if view and not types:
try:
with open(view.file_name(), "rb") as fp:
content = fp.read()
types = util.build_package_types(content)
except Exception as ex:
types = {}
self.members = []
self.matched_package = {}
for metadata_object, members in self.cache.items():
if not members:
continue
if metadata_object in types:
display = "[√]" + metadata_object
else:
display = "[x]" + metadata_object
self.members.append(display)
matched_members = []
for mem in members:
fullName = mem.get("fullName")
# Check filters
if self.filters and not self.is_filter_match(fullName):
continue
# Check lastModifiedDate
lastModifiedDate = datetime.datetime.strptime(
mem.get("lastModifiedDate")[:-5],
"%Y-%m-%dT%H:%M:%S"
)
intervalHours = datetime.timedelta(
hours=int(self.last_n_hours)
)
if lastModifiedDate < datetime.datetime.now() - intervalHours:
continue
matched_members.append(fullName)
if fullName in types.get(metadata_object, []):
fullName = "[√]" + metadata_object + " => " + fullName
else:
fullName = "[x]" + metadata_object + " => " + fullName
self.members.append(" %s" % fullName)
# If no matched member, just skip
if not matched_members:
self.members.remove(display)
continue
self.matched_package[metadata_object] = matched_members
if not self.members:
message = "No matched member found by filters('%s'), do you want to retry" % ",".join(self.filters)
if sublime.ok_cancel_dialog(message, "Retry"):
return sublime.active_window().show_input_panel(
"Input filters for members separated with comma: ",
"", self.on_input_filters, None, None
)
return
# Get the last subscribe index
selected_index = view.settings().get("selected_index") if view else 0
if not selected_index: selected_index = 0
self.window.show_quick_panel(self.members, self.on_done,
sublime.MONOSPACE_FONT, selected_index)
def is_filter_match(self, member):
isFilterMatch = False
for _filter in self.filters:
_filter = _filter.strip()
if _filter.lower() in member.lower():
isFilterMatch = True
break
return isFilterMatch
def on_done(self, index):
if index == -1:
del self.filters
return
chosen_element = self.members[index]
if " => " in chosen_element:
chosen_type, chosen_member = chosen_element.split(" => ")
is_chosen = "[√]" in chosen_type
chosen_type = chosen_type[7:]
else:
chosen_type, chosen_member = chosen_element, None
is_chosen = "[√]" in chosen_type
chosen_type = chosen_type[3:]
view = self.window.active_view()
if not view or not view.settings().has("types"):
view = self.window.new_file()
view.set_syntax_file("Packages/XML/xml.sublime-syntax")
view.run_command("new_view", {
"name": "package.xml",
"input": ""
})
view.settings().set("selected_index", index)
self.window.focus_view(view)
types = view.settings().get("types", {})
if not chosen_member:
if not is_chosen or len(types[chosen_type]) != len(self.matched_package[chosen_type]):
types[chosen_type] = self.matched_package[chosen_type]
else:
del types[chosen_type]
elif chosen_type in types:
if not is_chosen:
if chosen_member not in types[chosen_type]:
types[chosen_type].append(chosen_member)
else:
if len(types[chosen_type]) > 1:
# If there has more than one member, just remove the chosen member
types[chosen_type].remove(chosen_member)
else:
# If there is only one member, just remove the type
del types[chosen_type]
else:
types[chosen_type] = [chosen_member]
view.settings().set("types", types)
# Build package.xml content
metadata_objects = []
for _type in types:
metadata_objects.append(
"<types>%s<name>%s</name></types>" % (
"".join(["<members>%s</members>" % m for m in types[_type]]),
_type
)
)
self.package_xml_content = """<?xml version="1.0" encoding="UTF-8"?>
<Package xmlns="http://soap.sforce.com/2006/04/metadata">
{metadata_objects}
<version>{api_version}.0</version>
</Package>
""".format(
metadata_objects="".join(metadata_objects),
api_version=self.settings["api_version"]
)
view.run_command("new_dynamic_view", {
"view_name": "package.xml",
"erase_all": True,
"input": util.format_xml(self.package_xml_content).decode("UTF-8")
})
sublime.set_timeout(lambda:self.on_input_filters(",".join(self.filters)), 10)
def is_enabled(self):
self.settings = context.get_settings()
described_metadata = util.get_described_metadata(self.settings)
return described_metadata is not None
class BuildOrganizationPackageXml(sublime_plugin.WindowCommand):
def __init__(self, *args, **kwargs):
super(BuildOrganizationPackageXml, self).__init__(*args, **kwargs)
def run(self):
settings = context.get_settings()
package_cache = os.path.join(settings["workspace"], ".config", "package.json")
if not os.path.exists(package_cache):
return self.window.run_command("reload_project_cache", {
"callback_command": "build_organization_package_xml"
})
# Get package cache in JSON format
package = json.loads(open(package_cache).read())
# Build package.xml content
metadata_objects = []
for metadata_object, members in package.items():
# If there is no members, just skip
if not members: continue
metadata_objects.append(
"<types>%s<name>%s</name></types>" % (
"".join(["<members>%s</members>" % m for m in members]),
metadata_object
)
)
package_xml_content = """<?xml version="1.0" encoding="UTF-8"?>
<Package xmlns="http://soap.sforce.com/2006/04/metadata">
{metadata_objects}
<version>{api_version}.0</version>
</Package>
""".format(
metadata_objects="".join(metadata_objects),
api_version=settings["api_version"]
)
import threading
thread = threading.Thread(target=self.write_package_xml,
args=(package_xml_content, ))
thread.start()
def write_package_xml(self, content):
settings = context.get_settings()
package_xml_dir = os.path.join(settings["workspace"], ".config", "package.xml")
Printer.get("log").write("Start to prepare package.xml file, which will be opened as a new view")
with open(package_xml_dir, "wb") as fp:
fp.write(util.format_xml(content))
self.window.open_file(package_xml_dir)
class CreatePackageXml(sublime_plugin.WindowCommand):
def __init__(self, *args, **kwargs):
super(CreatePackageXml, self).__init__(*args, **kwargs)
def run(self, dirs):
_dir = dirs[0]
settings = context.get_settings()
package_xml_content = """<?xml version="1.0" encoding="UTF-8"?>
<Package xmlns="http://soap.sforce.com/2006/04/metadata">
<types>
<members>*</members>
<name>ApexClass</name>
</types>
<version>{0}.0</version>
</Package>
""".format(settings["api_version"])
file_name = os.path.join(_dir, "package-%s.xml" % \
time.strftime("%Y%m%d", time.localtime(time.time())))
if os.path.isfile(file_name):
message = "Package.xml is already exist, override?"
if not sublime.ok_cancel_dialog(message, "Override?"):
return
with open(file_name, "wb") as fp:
fp.write(util.format_xml(package_xml_content))
# If created succeed, just open it
sublime.active_window().open_file(file_name)
def is_visible(self, dirs):
if not dirs or len(dirs) > 1: return False
return True
class DestructPackageXmlFromServer(sublime_plugin.WindowCommand):
def __init__(self, *args, **kwargs):
super(DestructPackageXmlFromServer, self).__init__(*args, **kwargs)
def run(self, files=None):
message = "Confirm to destruct elements in this package.xml from server?"
if not sublime.ok_cancel_dialog(message, "Confirm?"): return
try:
with open(self._file, "rb") as fp:
content = fp.read()
self.types = util.build_package_types(content)
except Exception as ex:
Printer.get('error').write(str(ex))
return
processor.handle_destructive_package_xml(self.types)
def is_visible(self, files=None):
self._file = None
if files and len(files) > 1:
return False
elif files and len(files) == 1:
# Invoked from sidebar menu
self._file = files[0]
else:
# Invoked from context menu
view = sublime.active_window().active_view()
self._file = view.file_name()
if not self._file or not self._file.endswith(".xml"):
return False
return True
class DeployPackage(sublime_plugin.WindowCommand):
def __init__(self, *args, **kwargs):
super(DeployPackage, self).__init__(*args, **kwargs)
def run(self, dirs, switch=True, source_org=None, chosen_classes=[]):
settings = context.get_settings()
source_org = settings["default_project_name"]
if switch:
return self.window.run_command("switch_project", {
"callback_options": {
"callback_command": "deploy_package",
"args": {
"dirs": dirs,
"switch": False,
"source_org": source_org
}
}
})
deploy_options = settings["deploy_options"]
testLevel = deploy_options.get("testLevel", "NoTestRun")
if testLevel == "RunSpecifiedTests" and not chosen_classes:
return self.window.run_command("choose_test_classes", {
"callback_options": {
"callback_command": "deploy_package",
"args": {
"dirs": dirs,
"switch": False,
"source_org": source_org
}
}
})
processor.handle_deploy_thread(
util.compress_package(self.package_dir),
source_org=source_org,
chosen_classes=chosen_classes
);
def is_visible(self, dirs):
if not dirs: return False
if len(dirs) > 1: return False
if os.path.exists(dirs[0]+"/package.xml"):
self.package_dir = dirs[0]
elif os.path.exists(dirs[0]+"/src/package.xml"):
self.package_dir = dirs[0] + "/src"
else:
return False
return True
class RefreshPackage(sublime_plugin.WindowCommand):
def __init__(self, *args, **kwargs):
super(RefreshPackage, self).__init__(*args, **kwargs)
def run(self, dirs):
try:
with open(self.package_xml, "rb") as fp:
content = fp.read()
types = util.build_package_types(content)
except Exception as ex:
Printer.get('error').write(str(ex))
return
processor.handle_retrieve_package(types, self.extract_to)
def is_enabled(self, dirs):
if not dirs or len(dirs) > 1:
return False
settings = context.get_settings()
_dir = dirs[0]
pname = settings["default_project_name"]
if pname.lower() not in _dir.lower():
return False
if os.path.exists(_dir + "/src/package.xml"):
self.package_xml = _dir + "/src/package.xml"
self.extract_to = _dir
elif os.path.exists(_dir + "/package.xml"):
self.package_xml = _dir + "/package.xml"
# <base_path>/<project_name>/src/package.xml
# <base_path>/<project_name>/package.xml
self.extract_to = _dir
if _dir.endswith("src"):
self.extract_to = os.path.split(_dir)[0]
else:
return False
return True
def is_visible(self, dirs):
return self.is_enabled(dirs)
class RetrievePackageXmlFromServer(sublime_plugin.WindowCommand):
def __init__(self, *args, **kwargs):
super(RetrievePackageXmlFromServer, self).__init__(*args, **kwargs)
def run(self, files=None):
# Build types
try:
with open(self._file, "rb") as fp:
content = fp.read()
self.types = util.build_package_types(content)
except Exception as ex:
Printer.get('error').write(str(ex))
return
# Initiate extract_to
path, name = os.path.split(self._file)
name = name[:name.rfind(".")]
time_stamp = time.strftime("%Y%m%d%H%M", time.localtime(time.time()))
settings = context.get_settings()
project_name = settings["default_project_name"]
self.extract_to = os.path.join(path, "%s-%s-%s" % (
project_name, name, time_stamp
))
sublime.active_window().show_input_panel("Input ExtractedTo Path",
self.extract_to, self.on_input_extractto, None, None)
def on_input_extractto(self, extract_to):
# Check input
if not extract_to or not os.path.isabs(extract_to):
message = 'Invalid path, do you want to try again?'
if not sublime.ok_cancel_dialog(message, "Try Again?"): return
self.window.show_input_panel("Please Input Name: ", "",
self.on_input_extractto, None, None)
return
# Start retrieve
processor.handle_retrieve_package(self.types, extract_to)
def is_enabled(self, files=None):
self._file = None
if files and len(files) > 1:
return False
elif files and len(files) == 1:
# Invoked from sidebar menu
self._file = files[0]
else:
# Invoked from context menu
view = sublime.active_window().active_view()
self._file = view.file_name()
if not self._file or not self._file.endswith(".xml"):
return False
return True
def is_visible(self, files=None):
return self.is_enabled(files)