Skip to content

Commit

Permalink
A draft for lwc support
Browse files Browse the repository at this point in the history
  • Loading branch information
lushang committed Mar 17, 2019
1 parent 32ca5e4 commit f6cb37d
Show file tree
Hide file tree
Showing 15 changed files with 210 additions and 20 deletions.
7 changes: 6 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,9 @@ nosetests.xml
# idea
.idea/*
sublime.py
sublime_plugin.py
sublime_plugin.py

# temp remove
apex_tag.py
aura_tag.py
sp.py
11 changes: 9 additions & 2 deletions aura.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import sublime, sublime_plugin
import sublime
import sublime_plugin
import os
import re

Expand Down Expand Up @@ -46,7 +47,7 @@ def run(self, dirs, switch_project=True, source_org=None, element=None, update_m
}
})

base64_package = util.build_aura_package(dirs)
base64_package = util.build_package(dirs)
processor.handle_deploy_thread(base64_package, source_org=source_org, element=element, update_meta=update_meta)

def is_visible(self, dirs, switch_project=True):
Expand All @@ -62,6 +63,7 @@ def is_visible(self, dirs, switch_project=True):

return True


class PreviewLightingAppInServer(sublime_plugin.WindowCommand):
def __init__(self, *args, **kwargs):
super(PreviewLightingAppInServer, self).__init__(*args, **kwargs)
Expand Down Expand Up @@ -109,6 +111,7 @@ def is_enabled(self):

return True


class PreviewThisAppInServer(sublime_plugin.TextCommand):
def run(self, edit):
self.view.window().run_command('preview_lighting_app_in_server', {
Expand All @@ -129,6 +132,7 @@ def is_enabled(self):
def is_visible(self):
return self.is_enabled()


class RetrieveLightingFromServer(sublime_plugin.WindowCommand):
def __init__(self, *args, **kwargs):
super(RetrieveLightingFromServer, self).__init__(*args, **kwargs)
Expand Down Expand Up @@ -168,6 +172,7 @@ def is_visible(self, dirs):

return True


class DestructLightingFromServer(sublime_plugin.WindowCommand):
def __init__(self, *args, **kwargs):
super(DestructLightingFromServer, self).__init__(*args, **kwargs)
Expand All @@ -188,6 +193,7 @@ def is_visible(self, dirs):

return True


class CreateLightingElement(sublime_plugin.WindowCommand):
def __init__(self, *args, **kwargs):
super(CreateLightingElement, self).__init__(*args, **kwargs)
Expand Down Expand Up @@ -264,6 +270,7 @@ def is_visible(self, dirs, element=""):

return False


class CreateLightingDefinition(sublime_plugin.WindowCommand):
def __init__(self, *args, **kwargs):
super(CreateLightingDefinition, self).__init__(*args, **kwargs)
Expand Down
7 changes: 7 additions & 0 deletions config/menus/Main.sublime-menu
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,13 @@
"args": {
"_type": "Event"
}
},

{"caption": "-"},

{
"caption": "New Lightning Web Component",
"command": "create_lightning_web_component"
}
]
},
Expand Down
9 changes: 9 additions & 0 deletions config/templates/LWC/Basic.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<template>

<lightning-card title="Hello" icon-name="custom:custom14">
<div class="slds-m-around_medium">
Hello, {greeting}!
</div>
</lightning-card>

</template>
5 changes: 5 additions & 0 deletions config/templates/LWC/Basic.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { LightningElement, track } from 'lwc';

export default class Hello extends LightningElement {
@track greeting = 'World';
}
10 changes: 10 additions & 0 deletions config/templates/LWC/Basic.js-meta.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>45.0</apiVersion>
<isExposed>true</isExposed>
<targets>
<target>lightning__AppPage</target>
<target>lightning__RecordPage</target>
<target>lightning__HomePage</target>
</targets>
</LightningComponentBundle>
7 changes: 5 additions & 2 deletions context.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
'svg': '.svg'
}


def get_settings():
""" Load all settings in toolingapi.sublime-settings
Expand Down Expand Up @@ -77,10 +78,11 @@ def get_settings():
else:
settings["security_token"] = ""

# Get the default api version
# Get the default api version, updated the very default version to v45 for
# the support of Lightning Web Component
api_version = default_project.get("api_version")
if not api_version:
api_version = s.get("api_version", 37)
api_version = s.get("api_version", 45)

# Set deploy options
deploy_options = default_project.get("deploy_options")
Expand Down Expand Up @@ -246,6 +248,7 @@ def get_default_project_name():

return default_project_name


def build_metadata_objects_settings(settings, metadata_objects):
settings["all_metadata_folders"] = [c["directoryName"] for c in metadata_objects]
settings["all_metadata_objects"] = [c["xmlName"] for c in metadata_objects]
Expand Down
119 changes: 119 additions & 0 deletions lwc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
import sublime
import sublime_plugin
import os
import re

from . import context
from . import util
from . import processor
from .salesforce.lib.panel import Printer


class CreateLightningWebComponent(sublime_plugin.WindowCommand):
def __init__(self, *args, **kwargs):
super(CreateLightningWebComponent, self).__init__(*args, **kwargs)
# Get settings
self._settings = context.get_settings()
self._workspace = self._settings["workspace"]

def run(self, *args):
if self._settings["api_version"] < 45:
message = "Lightning Web Component only available for API Version >= 45, current is %s"
sublime.error_message(message % self._settings["api_version"])
return
self.window.show_input_panel("Please Input Lightning Web Component Name: ",
"", self.on_input, None, None)

def on_input(self, lwc_name):
# Create component to local according to user input
if not re.match('^[a-zA-Z]+\\w+$', lwc_name):
message = 'Invalid format, do you want to try again?'
if not sublime.ok_cancel_dialog(message):
return
self.window.show_input_panel("Please Input Lightning Web Component Name: ",
"", self.on_input, None, None)
return

# Build dir for new lighting web component
component_dir = os.path.join(self._workspace, "src", "lwc", lwc_name)
if not os.path.exists(component_dir):
os.makedirs(component_dir)
else:
message = "%s is already exist, do you want to try again?" % lwc_name
if not sublime.ok_cancel_dialog(message, "Try Again?"):
return
self.window.show_input_panel("Input Lightning Web Component Name: ",
"", self.on_input, None, None)
return

# Get template attribute
templates = util.load_templates()
template_bundle = templates.get("LWC")
for tpl_name in template_bundle:
template = template_bundle.get(tpl_name)
with open(os.path.join(self._workspace, ".templates", template["directory"])) as fp:
body = fp.read()

lwc_file = os.path.join(component_dir, lwc_name + template["extension"])

# Create Aura lighting file
with open(lwc_file, "w") as fp:
fp.write(body)

# If created succeed, just open it and refresh project
window = sublime.active_window()
window.open_file(lwc_file)
window.run_command("refresh_folder_list")

# Deploy Lightning Web Component to server
self.window.run_command("deploy_lwc_to_server", {
"dirs": [component_dir],
"switch_project": False,
"update_meta": True
})

def is_enabled(self):
return util.check_action_enabled()

def is_visible(self):
if self._settings["api_version"] < 45:
return False
return True


class DeployLwcToServer(sublime_plugin.WindowCommand):
def __init__(self, *args, **kwargs):
super(DeployLwcToServer, self).__init__(*args, **kwargs)
self.settings = context.get_settings()

def run(self, dirs, switch_project=True, source_org=None, update_meta=False):
if switch_project:
return self.window.run_command("switch_project", {
"callback_options": {
"callback_command": "deploy_lwc_to_server",
"args": {
"switch_project": False,
"source_org": self.settings["default_project_name"],
"dirs": dirs,
"update_meta": update_meta
}
}
})

base64_package = util.build_package(dirs, 'lwc')
processor.handle_deploy_thread(base64_package, source_org=source_org, update_meta=update_meta)

def is_visible(self, dirs, switch_project=True):
visible = True
if not dirs or len(dirs) == 0:
visible = False

for _dir in dirs:
attributes = util.get_file_attributes(_dir)
metadata_folder = attributes["metadata_folder"]
if metadata_folder != "lwc":
visible = False
if self.settings["default_project_name"] not in _dir:
visible = False

return visible
11 changes: 10 additions & 1 deletion main.py
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,7 @@ def on_input_name(self, name):
if not name: return
processor.handle_export_query_to_csv(self.tooling, self.soql, name)


class ExportDataTemplateCommand(sublime_plugin.WindowCommand):
def __init__(self, *args, **kwargs):
super(ExportDataTemplateCommand, self).__init__(*args, **kwargs)
Expand Down Expand Up @@ -587,6 +588,7 @@ def on_choose_recordtype(self, index):
def is_enabled(self):
return util.check_action_enabled()


class ExecuteRestTest(sublime_plugin.TextCommand):
def run(self, edit):
self.items = ["Get", "Post", "Put", "Patch", "Delete", "Tooling Query",
Expand Down Expand Up @@ -618,6 +620,7 @@ def is_enabled(self):
if not self.sel: return False
return True


class GotoComponentCommand(sublime_plugin.TextCommand):
"""
Move the cursor to the class name, press shift key and double click left mouse,
Expand Down Expand Up @@ -647,16 +650,19 @@ def run(self, edit, is_background=False, allowed_folders=None):

if is_background: self.view.window().focus_view(self.view)


class SetCheckPointCommand(sublime_plugin.TextCommand):
def run(self, edit, mark):
sel = [s for s in self.view.sel()]
self.view.add_regions(mark, sel, "invalid", "dot",
sublime.DRAW_SOLID_UNDERLINE | sublime.DRAW_EMPTY_AS_OVERWRITE)


class RemoveCheckPointCommand(sublime_plugin.TextCommand):
def run(self, edit, mark):
self.view.erase_regions(mark)


class ViewCodeCoverageCommand(sublime_plugin.TextCommand):
def run(self, edit):
util.view_coverage(self.attributes["name"], self.file_name, self.body)
Expand All @@ -678,7 +684,6 @@ def is_enabled(self):
if self.attributes["metadata_folder"] not in ["classes", "triggers"]:
return False


# Can't be Test Class
with open(self.file_name, encoding="utf-8") as fp:
self.body = fp.read()
Expand All @@ -691,6 +696,7 @@ def is_enabled(self):
def is_visible(self):
return self.is_enabled()


class ViewSelectedCodeCoverageCommand(sublime_plugin.TextCommand):
def run(self, edit):
# Keep all open views
Expand Down Expand Up @@ -2340,6 +2346,7 @@ def on_input(self, password):
def is_enabled(self):
return util.check_action_enabled()


class UpdateProjectPatternsCommand(sublime_plugin.WindowCommand):
def __init__(self, *args, **kwargs):
super(UpdateProjectPatternsCommand, self).__init__(*args, **kwargs)
Expand All @@ -2351,6 +2358,7 @@ def run(self):
def is_enabled(self):
return util.check_action_enabled()


class UpdateProjectCommand(sublime_plugin.WindowCommand):
def __init__(self, *args, **kwargs):
super(UpdateProjectCommand, self).__init__(*args, **kwargs)
Expand All @@ -2363,6 +2371,7 @@ def run(self):
def is_enabled(self):
return util.check_action_enabled()


class CreateNewProject(sublime_plugin.WindowCommand):
def __init__(self, *args, **kwargs):
super(CreateNewProject, self).__init__(*args, **kwargs)
Expand Down
2 changes: 2 additions & 0 deletions processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -1329,6 +1329,7 @@ def handle_thread(thread, timeout):
ThreadProgress(api, thread, "Describe Global", "Describe Global Succeed")
handle_thread(thread, timeout)


def handle_new_project(is_update=False, timeout=120):
def handle_thread(thread, timeout):
if thread.is_alive():
Expand Down Expand Up @@ -1402,6 +1403,7 @@ def handle_thread(thread, timeout):
ThreadProgress(api, thread, wating_message, wating_message + " Finished")
handle_thread(thread, timeout)


def handle_describe_metadata(callback_options, timeout=120):
def handle_thread(thread, timeout):
if thread.is_alive():
Expand Down
2 changes: 1 addition & 1 deletion salesforce/api/apex.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from ... import requests, util


class ApexApi():
class ApexApi:
def __init__(self, settings, **kwargs):
self.settings = settings
self.soap = SOAP(settings)
Expand Down
Loading

0 comments on commit f6cb37d

Please sign in to comment.