Skip to content

Commit a0a18e9

Browse files
committed
Enabled diff with server for aura and lwc components
1 parent 0f55222 commit a0a18e9

File tree

9 files changed

+83
-35
lines changed

9 files changed

+83
-35
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,3 +101,9 @@ well welcome to any contribution, open an issue for discussion before draft you
101101
* Press ``alt`` + Click ``left mouse button``: Retrieve debug log detail by id
102102
* Press ``alt`` + DblClick ``left mouse button``: View code coverage by name
103103
* Press ``alt`` + Triple Click ``left mouse button``: Cancel deployment by Id
104+
105+
# Development Reference
106+
107+
- [Sublime Test Documentation](https://www.sublimetext.com/docs/index.html)
108+
- [API Reference](https://www.sublimetext.com/docs/api_reference.html)
109+
- [Sublime Text Community Documentation](https://docs.sublimetext.io/guide/extensibility/plugins/)

config/snippets/Apex/Class Header - class header.sublime-snippet

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
* Object: $2
66
* Purpose: $3
77
* Author: $4 ($5)
8-
* Create Date: 2020-${6:03-27}
8+
* Create Date: 2022-${6:03-30}
99
* Modify History:
10-
* 2021-${6:01-13} $4 ${7:Create this class}
10+
* 2022-${6:03-30} $4 ${7:Create this class}
1111
**************************************************************************************************/
1212
]]></content>
1313
<tabTrigger>ch</tabTrigger>

events.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,10 @@ def on_hover(self, view, pt, hover_zone):
4343

4444
def on_new(self, view):
4545
"""
46-
1. Eveytime when you open a new view, default syntax is Apex
46+
1. Everytime when you open a new view, default syntax is Apex
4747
2. Set Status with current default project
4848
"""
49+
4950
view.set_syntax_file("Packages/Java/Java.tmLanguage")
5051
util.display_active_project(view)
5152

main.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,8 @@ def is_enabled(self):
306306

307307
self.settings = context.get_settings()
308308
self.attributes = util.get_file_attributes(self.file_name)
309-
if self.attributes["metadata_folder"] not in ["classes", "triggers", "pages", "components"]:
309+
diffable_attrbs = ["classes", "triggers", "pages", "components", "aura", "lwc"]
310+
if self.attributes["metadata_folder"] not in diffable_attrbs:
310311
return False
311312

312313
return True

processor.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1737,7 +1737,7 @@ def handle_thread(thread, timeout):
17371737
handle_set_component_attribute(component_attribute)
17381738

17391739
# If not succeed, just go to the error line
1740-
# Because error line in page is always at the line 1, so just work in class or trigger
1740+
# Only works in class or trigger since error line in page is always at the line 1
17411741
elif "success" in result and not result["success"]:
17421742
print('error result', result)
17431743
# Maybe network issue
@@ -1759,8 +1759,8 @@ def handle_thread(thread, timeout):
17591759

17601760
# Check current view the saving code file
17611761
if not view or not view.file_name(): return
1762-
if not file_base_name in view.file_name(): return
1763-
if not extension in [".trigger", ".cls", ".page", ".cmp", '.js', '.css']: return
1762+
if file_base_name not in view.file_name(): return
1763+
if extension not in [".trigger", ".cls", ".page", ".cmp", '.js', '.css']: return
17641764

17651765
if "line" in result:
17661766
line = result["line"]
@@ -1941,6 +1941,7 @@ def handle_thread(thread, timeout):
19411941
return
19421942

19431943
result = api.result
1944+
19441945
if result["success"] and result["records"]:
19451946
lastModifiedDate = result["records"][0]["LastModifiedDate"]
19461947
util.set_component_attribute(attributes, lastModifiedDate)
@@ -1952,8 +1953,13 @@ def handle_thread(thread, timeout):
19521953

19531954
settings = context.get_settings()
19541955
api = ToolingApi(settings)
1956+
cmp_type = attributes["type"]
1957+
if cmp_type == "AuraDefinitionBundle":
1958+
cmp_type = 'AuraDefinition'
1959+
elif cmp_type == 'LightningComponentBundle':
1960+
cmp_type = "LightningComponentResource"
19551961
soql = "SELECT LastModifiedDate FROM %s WHERE Id = '%s'" % (
1956-
attributes["type"], attributes["id"]
1962+
cmp_type, attributes["id"]
19571963
)
19581964
thread = threading.Thread(target=api.query, args=(soql, True,))
19591965
thread.start()
@@ -2007,7 +2013,8 @@ def handle_thread(thread, timeout):
20072013
result = api.result
20082014

20092015
# If error, just skip, error is processed in ThreadProgress
2010-
if not result["success"]: return
2016+
if not result["success"]:
2017+
return
20112018

20122019
# Diff Change Compare
20132020
diff.diff_changes(file_name, result)

salesforce/api/tooling.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1161,14 +1161,15 @@ def save_to_server(self, component_attribute, body, is_check_only, check_save_co
11611161
# Result used in thread invoke
11621162
self.result = return_result
11631163

1164-
def save_lightning_to_server(self, component_attribute, body, check_save_conflict=True):
1164+
def save_lightning_to_server(self, component_attribute, body, is_check_only, check_save_conflict=True):
11651165
"""
11661166
Save Lightning AuraDefinition or LightningComponentResource such as component makeup, controller and helper or
11671167
Lightning Web component resource to Salesforce
11681168
11691169
Arguments:
11701170
@param component_attribute: attribute of component, e.g., component id, url
11711171
@param body: Code content
1172+
@param is_check_only: is check only
11721173
@param check_save_conflict: indicate whether to check saving conflict
11731174
@return: saving result
11741175
"""
@@ -1219,12 +1220,16 @@ def handle_error_message(result):
12191220

12201221
# Component Attribute
12211222
bundle_type = component_attribute["type"]
1222-
component_type = 'AuraDefinition' if bundle_type == "AuraDefinitionBundle" else 'LightningComponentResource'
1223+
component_type = 'AuraDefinition' \
1224+
if bundle_type == "AuraDefinitionBundle" \
1225+
else 'LightningComponentResource'
12231226
component_id = component_attribute["id"]
1224-
component_url = self.base_url + '/tooling/sobjects/' + component_type + '/' + component_id
1227+
component_url = self.base_url + \
1228+
'/tooling/sobjects/%s/%s' % (component_type, component_id)
12251229

12261230
# 2. Check conflict
1227-
if self.settings["check_save_conflict"] and check_save_conflict:
1231+
if self.settings["check_save_conflict"] and not is_check_only \
1232+
and check_save_conflict and "url" in component_attribute:
12281233
Printer.get('log').write("Start to check saving conflict")
12291234
query = "SELECT Id, LastModifiedById, LastModifiedBy.Id, " + \
12301235
"LastModifiedBy.Name, LastModifiedDate, SystemModstamp " + \
@@ -1274,7 +1279,7 @@ def handle_error_message(result):
12741279
data = {
12751280
"Source": body
12761281
}
1277-
result = self.patch(component_url, data)
1282+
result = self.patch(component_url, data) # {"str": "", "success": true}
12781283
return_result = {
12791284
"success": False,
12801285
"problem": '',

salesforce/lib/diff.py

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,31 +5,36 @@
55
import codecs
66
import os
77

8+
89
def diff_changes(file_name, result):
910
try:
1011
if "Body" in result:
1112
server = result["Body"].splitlines()
1213
elif "Markup" in result:
1314
server = result["Markup"].splitlines()
15+
elif "Source" in result:
16+
server = result["Source"].splitlines()
17+
else:
18+
show_diff_panel("No server text found to diff")
19+
return
1420

1521
local = codecs.open(file_name, "r", "utf-8").read().splitlines()
16-
except UnicodeDecodeError:
17-
show_diff_panel("Diff only works with UTF-8 files")
18-
return
1922

20-
time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time()))
21-
bdate_literal = result["LastModifiedDate"].split(".")[0]
22-
server_date = datetime.datetime.strptime(bdate_literal, "%Y-%m-%dT%H:%M:%S")
23-
local_date = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time()))
23+
time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time()))
24+
bdate_literal = result["LastModifiedDate"].split(".")[0]
25+
server_date = datetime.datetime.strptime(bdate_literal, "%Y-%m-%dT%H:%M:%S")
26+
local_date = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time()))
2427

25-
diff = difflib.unified_diff(server, local, "Server", "Local ", server_date, local_date, lineterm='')
26-
difftxt = u"\n".join(line for line in diff)
28+
diff = difflib.unified_diff(server, local, "Server", "Local ", server_date, local_date, lineterm='')
29+
difftxt = u"\n".join(line for line in diff)
2730

28-
if difftxt == "":
29-
show_diff_panel("There is no difference between %s and server" % os.path.basename(file_name))
30-
return
31+
if difftxt == "":
32+
show_diff_panel("There is no difference between %s and server" % os.path.basename(file_name))
33+
else:
34+
show_diff_panel(difftxt)
35+
except UnicodeDecodeError:
36+
show_diff_panel("Diff only works with UTF-8 files")
3137

32-
show_diff_panel(difftxt)
3338

3439
def diff_files(file_name, other_file_name):
3540
try:
@@ -51,10 +56,11 @@ def diff_files(file_name, other_file_name):
5156

5257
show_diff_panel(difftxt)
5358

54-
def show_diff_panel(difftxt):
59+
60+
def show_diff_panel(diff_txt):
5561
win = sublime.active_window()
5662
v = win.create_output_panel('diff_with_server')
5763
v.assign_syntax('Packages/Diff/Diff.tmLanguage')
5864

59-
v.run_command('append', {'characters': difftxt})
65+
v.run_command('append', {'characters': diff_txt})
6066
win.run_command("show_panel", {"panel": "output.diff_with_server"})

salesforce/soap.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def format_request_envelope(self, request_body):
4646
formatted_body = formatter.format_string(request_body)
4747
except Exception as ex:
4848
if self.settings["debug_mode"]:
49-
print ("[Format Request Body] " + str(ex))
49+
print("[Format Request Body] " + str(ex))
5050
formatted_body = request_body
5151

5252
return formatted_body
@@ -55,7 +55,7 @@ def format_request_envelope(self, request_body):
5555
def create_request(self, request_type, options={}):
5656
soap_body = getattr(self, "create_%s_request" % request_type)(options)
5757
if self.settings["debug_mode"]:
58-
print ("[Debug for {request_type}]: \n{seprate}\n{content}\n{seprate}".format(
58+
print("[Debug for {request_type}]: \n{seprate}\n{content}\n{seprate}".format(
5959
seprate="~" * 100,
6060
request_type=request_type,
6161
content=soap_body.decode("UTF-8")

util.py

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1821,7 +1821,12 @@ def reload_file_attributes(file_properties, settings=None, append=False):
18211821
"ApexComponent": "Markup"
18221822
}
18231823

1824-
# print(file_properties)
1824+
lightning_body = {
1825+
"AuraDefinitionBundle": "AuraDefinition",
1826+
"LightningComponentBundle": "LightningComponentResource"
1827+
}
1828+
1829+
# print(json.dumps(file_properties))
18251830

18261831
# If the package only contains `package.xml`
18271832
if isinstance(file_properties, dict):
@@ -1863,6 +1868,13 @@ def reload_file_attributes(file_properties, settings=None, append=False):
18631868
attrs["url"] = "/services/data/v%s.0/sobjects/%s/%s" % (
18641869
settings["api_version"], metdata_object, filep["id"]
18651870
)
1871+
elif metdata_object in lightning_body:
1872+
attrs["body"] = 'Source'
1873+
attrs["url"] = "/services/data/v%s.0/tooling/sobjects/%s/%s" % (
1874+
settings["api_version"],
1875+
lightning_body.get(metdata_object),
1876+
filep["id"]
1877+
)
18661878

18671879
# Check whether component is Test Class or not
18681880
if metdata_object == "ApexClass":
@@ -3199,13 +3211,23 @@ def get_component_attribute(file_name, switch=True, reload_cache=False):
31993211
xml_name = settings[metadata_folder]["xmlName"]
32003212
username = settings["username"]
32013213
components = load_metadata_cache(reload_cache=reload_cache, username=username)
3214+
lightning_cmps = {
3215+
"AuraDefinitionBundle": "AuraDefinition",
3216+
"LightningComponentBundle": "LightningComponentResource"
3217+
}
32023218
try:
3203-
component_attribute = components[xml_name][fullName.lower()]
3219+
cmp_attr = components[xml_name][fullName.lower()]
3220+
if 'url' not in cmp_attr and cmp_attr['type'] in lightning_cmps:
3221+
cmp_attr['url'] = "/services/data/v%s.0/tooling/sobjects/%s/%s" % (
3222+
settings["api_version"],
3223+
lightning_cmps.get(cmp_attr['type']),
3224+
cmp_attr["id"]
3225+
)
32043226
except:
3205-
component_attribute, name = None, None
3227+
cmp_attr, name = None, None
32063228

32073229
# Return tuple
3208-
return (component_attribute, name)
3230+
return cmp_attr, name
32093231

32103232

32113233
def delete_component_attribute(dirs_or_files, switch=True):

0 commit comments

Comments
 (0)