Skip to content

Commit

Permalink
Refresh Lightning Component
Browse files Browse the repository at this point in the history
  • Loading branch information
exiahuang committed Dec 10, 2018
1 parent 1e91f91 commit d407767
Show file tree
Hide file tree
Showing 10 changed files with 138 additions and 74 deletions.
2 changes: 2 additions & 0 deletions baseutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,7 @@ def get_file_attr(self, full_file_path):
"is_sfdc_file" : True,
"is_src" : file_extension in [".cls", ".component", ".page", ".trigger"] and os.path.isfile(full_file_path),
"is_lux" : False,
"is_lux_root" : False,
"lux_type" : "",
"lux_name" : ""
}
Expand All @@ -520,6 +521,7 @@ def get_file_attr(self, full_file_path):
attr["metadata_sub_folder"] = ""
attr["metadata_folder"] = attr["dir"]
attr["metadata_type"] = "AuraDefinitionBundle"
attr["is_lux_root"] = True
elif attr["p_dir"] in ["aura", "reports", "dashboards", "documents", "email"]:
attr["metadata_sub_folder"] = attr["dir"]
attr["metadata_folder"] = attr["p_dir"]
Expand Down
13 changes: 0 additions & 13 deletions config/Context.sublime-menu
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,6 @@
{
"caption": "Preview Lightning App",
"command": "preview_lightning_app"
},
{ "caption": "-" },
{
"caption": "Open Controller",
"command": "open_controller"
},
{
"caption": "Open VisualPage",
"command": "open_visualpage"
},
{
"caption": "Open TestClass",
"command": "open_testclass"
}
]
},
Expand Down
15 changes: 10 additions & 5 deletions config/Side bar.sublime-menu
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,26 @@
"caption": "Sfdx-Xy",
"children":[
{
"caption": "Copy Lightning Component",
"command": "copy_aura",
"caption": "Deploy Directory To SFDC",
"command": "deploy_dir",
"args": {"dirs": []}
},
{ "caption": "-" },
{
"caption": "Open Lightning Component",
"command": "open_aura",
"args": {"dirs": []}
},
{
"caption": "Deploy Directory To SFDC",
"command": "deploy_dir",
"caption": "Refresh Lightning Component",
"command": "refresh_aura",
"args": {"dirs": []}
},
{
"caption": "Copy Lightning Component",
"command": "copy_aura",
"args": {"dirs": []}
},
{ "caption": "-" },
{
"caption": "Delete Lightning Component(Danger)",
"command": "delete_aura",
Expand Down
2 changes: 1 addition & 1 deletion config/sfdc.version.sublime-settings
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "SalesforceXyTools",
"version": "2.0.9",
"version": "2.1.0",
"description": "SalesforceXyTools for Sublime Text is Rapid development tools for Salesforce Development.",
"author": "Exia.Huangxy",
"email": "exia.huang@outlook.com",
Expand Down
4 changes: 4 additions & 0 deletions help/bug-fix-history.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# SalesforceXyTools Update History

## 2.1.0
* Refresh Lightning Component
* cache bug fix

## 2.0.9
* Support Lightning Dev
- Save to server
Expand Down
50 changes: 47 additions & 3 deletions main_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
)
from .templates import Template
from .uiutil import SublConsole
from .const import AURA_DEFTYPE_EXT


##########################################################################################
Expand Down Expand Up @@ -649,9 +650,8 @@ def updateMeta(self, deep=0):

if self.meta_attr["is_lux"]:
self._updateLux()
return

self._update_meta_to_server()
else:
self._update_meta_to_server()

# Lightning update
def _updateLux(self):
Expand Down Expand Up @@ -766,6 +766,50 @@ def is_enabled(self):
return attr["is_src"] or attr["is_lux"]


# refresh_aura
class RefreshAuraCommand(sublime_plugin.WindowCommand):
def run(self, dirs):
self.sf_basic_config = SfBasicConfig()
self.settings = self.sf_basic_config.get_setting()
self.sublconsole = SublConsole(self.sf_basic_config)
self.metadata_util = util.MetadataUtil(self.sf_basic_config)

if len(dirs) == 0: return False
aura_name = os.path.basename(dirs[0])
dir_path = os.path.dirname(dirs[0])
dir_name = os.path.basename(dir_path)
if dir_name != "aura":
self.sublconsole.showlog("It seems not a lightinig component! ")
return

self.aura_dir = dirs[0]
self.sublconsole.thread_run(target=self._refresh_aura)

def _refresh_aura(self):
attr = baseutil.SysIo().get_file_attr(self.aura_dir)
aura_soql = "SELECT Id, CreatedDate, CreatedById, CreatedBy.Name, LastModifiedDate, LastModifiedById, LastModifiedBy.Name, AuraDefinitionBundle.DeveloperName, AuraDefinitionBundleId, DefType, Format, Source FROM AuraDefinition"
aura_soql = aura_soql + " Where AuraDefinitionBundle.DeveloperName = '%s'" % (attr["file_name"])
tooling_api = util.sf_login(self.sf_basic_config, Soap_Type=ToolingApi)
result = tooling_api.query(aura_soql)
if 'records' in result and len(result['records']) > 0:
for file in os.listdir(self.aura_dir):
if not "-meta.xml" in file:
os.remove( os.path.join(self.aura_dir, file) )
for AuraDefinition in result['records']:
if AuraDefinition["DefType"] in AURA_DEFTYPE_EXT:
fileName = attr["file_name"] + AURA_DEFTYPE_EXT[AuraDefinition["DefType"]]
self.sublconsole.save_file( os.path.join(self.aura_dir, fileName), AuraDefinition["Source"])
self.metadata_util.update_lux_metas(result['records'])
self.sublconsole.showlog("Refresh lightinig ok! ")
else:
self.sublconsole.showlog("Refresh lightinig error! ", type="error")

def is_enabled(self, dirs):
if len(dirs) == 0: return False
dir_path = os.path.dirname(dirs[0])
dir_name = os.path.basename(dir_path)
return dir_name == "aura"

##########################################################################################
# Apex Test
##########################################################################################
Expand Down
2 changes: 1 addition & 1 deletion messages.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"2.0.9": "help/bug-fix-history.md",
"2.1.0": "help/bug-fix-history.md",
"install": "help/install.txt"
}
4 changes: 2 additions & 2 deletions packages.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
"description": "SalesforceXyTools for Sublime Text is Rapid development tools for Salesforce Development.",
"author": "Exia.Huangxy",
"homepage": "https://github.com/exiahuang/SalesforceXyTools",
"last_modified": "2018-12-08 00:00:00",
"last_modified": "2018-12-09 00:00:00",
"platforms": {
"*": [
{
"version": "2.0.9",
"version": "2.1.0",
"release_notes": "",
"url": "https://nodeload.github.com/exiahuang/SalesforceXyTools/zipball/master"
}
Expand Down
2 changes: 0 additions & 2 deletions salesforce/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -551,8 +551,6 @@ def updateApexClass(self, id, body):
def updateLux(self, id, post_body):
url = self.base_url + 'tooling/sobjects/AuraDefinition/' + id
status_code, result = self._call_api(method="PATCH", url=url, data=post_body, return_type="text")
print(status_code)
print(result)
return status_code, result

def runTestAsynchronous(self, id_list):
Expand Down
118 changes: 71 additions & 47 deletions util.py
Original file line number Diff line number Diff line change
Expand Up @@ -345,51 +345,60 @@ def reload(self):
allMetadataResult = meta_api.getAllMetadataMap()
allMetadataResult["AuraDefinition"] = self._load_lux_cache()
self.save_dict(allMetadataResult)

def _covert_AuraDefinition_to_cache_dict(self, AuraDefinition_records):
AuraDefinition_MetadataMap = {}
for AuraDefinition in AuraDefinition_records:
deftype = AuraDefinition["DefType"]
if deftype in AURA_DEFTYPE_EXT:
aura_name = AuraDefinition["AuraDefinitionBundle"]["DeveloperName"]
aura_key = "aura/%s/%s%s" % (aura_name, aura_name, AURA_DEFTYPE_EXT[deftype])
AuraDefinition_MetadataMap[aura_key] = {
"id": AuraDefinition["Id"],
"fileName": aura_key,
"fullName": aura_name + AURA_DEFTYPE_EXT[deftype],
"createdById": AuraDefinition["CreatedById"],
"createdByName": AuraDefinition["CreatedBy"]["Name"],
"createdDate": AuraDefinition["CreatedDate"],
"lastModifiedById": AuraDefinition["LastModifiedById"],
"lastModifiedByName": AuraDefinition["LastModifiedBy"]["Name"] if AuraDefinition["LastModifiedBy"] else '',
"lastModifiedDate": AuraDefinition["LastModifiedDate"],
"manageableState": "",
"type": "AuraDefinition",
"AuraDefinitionSrc": True,
"DefType": AuraDefinition["DefType"],
"DefExt": AURA_DEFTYPE_EXT[deftype],
"Format": AuraDefinition["Format"],
"AuraDefinitionBundleId": AuraDefinition["AuraDefinitionBundleId"],
"AuraDefinitionBundleName": aura_name
}
return AuraDefinition_MetadataMap

def _load_lux_cache(self, attr=None):
aura_soql = 'SELECT Id, CreatedDate, CreatedById, CreatedBy.Name, LastModifiedDate, LastModifiedById, LastModifiedBy.Name, AuraDefinitionBundle.DeveloperName, AuraDefinitionBundleId, DefType, Format FROM AuraDefinition'
aura_soql = 'SELECT Id, CreatedDate, CreatedById, CreatedBy.Name, LastModifiedDate, LastModifiedById, LastModifiedBy.Name, AuraDefinitionBundle.DeveloperName, AuraDefinitionBundleId, DefType, Format FROM AuraDefinition'
if attr:
aura_soql = aura_soql + " Where AuraDefinitionBundle.DeveloperName = '%s' and DefType = '%s' limit 1" % (attr["lux_name"], attr["lux_type"])

meta_api = sf_login(self.sf_basic_config, Soap_Type=MetadataApi)
AuraDefinition = meta_api.query(aura_soql)
AuraDefinition_MetadataMap = {}
if AuraDefinition and 'records' in AuraDefinition:
for meta in AuraDefinition['records']:
deftype = meta["DefType"]
if deftype in AURA_DEFTYPE_EXT:
aura_name = meta["AuraDefinitionBundle"]["DeveloperName"]
aura_key = "aura/%s/%s%s" % (aura_name, aura_name, AURA_DEFTYPE_EXT[deftype])
AuraDefinition_MetadataMap[aura_key] = {
"id": meta["Id"],
"fileName": aura_key,
"fullName": aura_name + AURA_DEFTYPE_EXT[deftype],
"createdById": meta["CreatedById"],
"createdByName": meta["CreatedBy"]["Name"],
"createdDate": meta["CreatedDate"],
"lastModifiedById": meta["LastModifiedById"],
"lastModifiedByName": meta["LastModifiedBy"]["Name"] if meta["LastModifiedBy"] else '',
"lastModifiedDate": meta["LastModifiedDate"],
"manageableState": "",
"type": "AuraDefinition",
"AuraDefinitionSrc": True,
"DefType": meta["DefType"],
"DefExt": AURA_DEFTYPE_EXT[deftype],
"Format": meta["Format"],
"AuraDefinitionBundleId": meta["AuraDefinitionBundleId"],
"AuraDefinitionBundleName": aura_name
}
if attr:
return AuraDefinition_MetadataMap[aura_key]
AuraDefinition_MetadataMap = self._covert_AuraDefinition_to_cache_dict(AuraDefinition['records'])
if attr:
if len(AuraDefinition_MetadataMap) > 0 : return list(AuraDefinition_MetadataMap.values())[0]
else : return {}
return AuraDefinition_MetadataMap

def get_meta_attr(self, full_path):
sysio = SysIo()
attr = sysio.get_file_attr(full_path)
if attr and "file_key" in attr:
meta = self.get_meta(attr["metadata_type"], attr["file_key"])
if meta:
attr.update(meta)
if not meta:
print('load from server')
self.update_metadata_cache_by_filename(full_path)
meta = self.get_meta(attr["metadata_type"], attr["file_key"])
if meta: attr.update(meta)
return attr

def get_describe_metadata_cache(self, deep=1):
Expand Down Expand Up @@ -434,7 +443,7 @@ def get_meta_by_category(self, category):

def get_meta(self, category, fileName):
category_meta = self.get_meta_by_category(category)
if fileName in category_meta:
if category_meta and fileName in category_meta:
meta = category_meta[fileName]
meta["webUrl"] = self.get_web_url(meta)
return meta
Expand All @@ -459,17 +468,18 @@ def is_modified(self, full_path, id):
def get_web_url(self, sel_meta):
self.sublconsole.debug(sel_meta)
returl = ""
sel_category = sel_meta["type"]
if sel_category == "Workflow":
returl = '/01Q?setupid=WorkflowRules'
elif sel_category == "CustomLabels":
returl = '/101'
elif sel_category == "AuraDefinition" and all (k in sel_meta for k in ("id", "DefType", "Format", "AuraDefinitionBundleId")):
returl = '/_ui/common/apex/debug/ApexCSIPage?action=openFile&extent=AuraDefinition&Id=%s&AuraDefinitionBundleId=%s&DefType=%s&Format=%s' % (sel_meta["id"], sel_meta["AuraDefinitionBundleId"], sel_meta["DefType"],sel_meta["Format"])
elif sel_meta and "id" in sel_meta and sel_meta["id"]:
returl = '/' + sel_meta["id"]
elif sel_category == "CustomObject":
returl = '/p/setup/layout/LayoutFieldList?type=' + sel_meta["fullName"]
if "type" in sel_meta:
sel_category = sel_meta["type"]
if sel_category == "Workflow":
returl = '/01Q?setupid=WorkflowRules'
elif sel_category == "CustomLabels":
returl = '/101'
elif sel_category == "AuraDefinition" and all (k in sel_meta for k in ("id", "DefType", "Format", "AuraDefinitionBundleId")):
returl = '/_ui/common/apex/debug/ApexCSIPage?action=openFile&extent=AuraDefinition&Id=%s&AuraDefinitionBundleId=%s&DefType=%s&Format=%s' % (sel_meta["id"], sel_meta["AuraDefinitionBundleId"], sel_meta["DefType"],sel_meta["Format"])
elif sel_meta and "id" in sel_meta and sel_meta["id"]:
returl = '/' + sel_meta["id"]
elif sel_category == "CustomObject":
returl = '/p/setup/layout/LayoutFieldList?type=' + sel_meta["fullName"]

return returl

Expand Down Expand Up @@ -536,6 +546,8 @@ def _del_meta(self, meta):

def _save_meta(self, server_meta):
all_cache = self.get_cache()
if server_meta["type"] not in all_cache:
all_cache[server_meta["type"]] = {}
all_cache[server_meta["type"]][server_meta["fileName"]] = server_meta
self.sublconsole.debug("_save_meta")
self.sublconsole.debug(server_meta)
Expand All @@ -547,7 +559,7 @@ def _get_server_meta(self, full_path, id):
if attr["is_lux"]:
return self._load_lux_cache(attr)
else:
soql = "SELECT Id, Name, CreatedDate, CreatedById, CreatedBy.Name, LastModifiedDate, LastModifiedById, LastModifiedBy.Name FROM %s Where Id = '%s'" % (attr["metadata_type"], id)
soql = "SELECT Id, Name, CreatedDate, CreatedById, CreatedBy.Name, LastModifiedDate, LastModifiedById, LastModifiedBy.Name FROM %s Where Id = '%s'" % (attr["metadata_type"], id)
tooling_api = sf_login(self.sf_basic_config, Soap_Type=ToolingApi)
result = tooling_api.query(soql)
if 'records' in result and len(result['records']) > 0:
Expand All @@ -568,14 +580,18 @@ def _get_server_meta(self, full_path, id):
return meta_cache_bean
return

def update_metadata_cache_by_filename(self, full_path, is_lux=False):
attr = self.get_meta_attr(full_path)
if is_lux :
print('update lightning cache')
def update_metadata_cache_by_filename(self, full_path):
# attr = self.get_meta_attr(full_path)
sysio = SysIo()
attr = sysio.get_file_attr(full_path)
if attr["is_lux"] :
self._save_meta(self._load_lux_cache(attr))
else:
if "metadata_type" not in attr or "name" not in attr: return
soql = "SELECT Id, Name, CreatedDate, CreatedById, CreatedBy.Name, LastModifiedDate, LastModifiedById, LastModifiedBy.Name FROM %s Where Name = '%s' limit 1" % (attr["metadata_type"], attr["name"])
if attr["metadata_type"] == "AuraDefinitionBundle":
soql = "SELECT Id, CreatedDate, CreatedById, CreatedBy.Name, LastModifiedDate, LastModifiedById, LastModifiedBy.Name FROM %s Where DeveloperName = '%s' limit 1" % (attr["metadata_type"], attr["name"])
else:
soql = "SELECT Id, Name, CreatedDate, CreatedById, CreatedBy.Name, LastModifiedDate, LastModifiedById, LastModifiedBy.Name FROM %s Where Name = '%s' limit 1" % (attr["metadata_type"], attr["name"])
tooling_api = sf_login(self.sf_basic_config, Soap_Type=ToolingApi)
result = tooling_api.query(soql)
if 'records' in result and len(result['records']) > 0:
Expand All @@ -596,6 +612,14 @@ def update_metadata_cache_by_filename(self, full_path, is_lux=False):
self._save_meta(meta_cache_bean)
return

def update_lux_metas(self, AuraDefinition_records):
all_cache = self.get_cache()
if "AuraDefinition" not in all_cache:
all_cache["AuraDefinition"] = {}
all_cache["AuraDefinition"].update( self._covert_AuraDefinition_to_cache_dict(AuraDefinition_records) )
self.sublconsole.save_and_open_in_panel(json.dumps(all_cache, ensure_ascii=False, indent=4), self.save_dir, self.file_name , is_open=False)


class SfDataUtil():
def __init__(self, sf_basic_config = None):
if sf_basic_config:
Expand Down

0 comments on commit d407767

Please sign in to comment.