Skip to content

Commit

Permalink
Merge pull request #47 from Ztrizzo/component-generator
Browse files Browse the repository at this point in the history
Component generator
  • Loading branch information
jonathanmorris180 authored Sep 8, 2024
2 parents 4c688ad + 75d0c1d commit ce0f006
Show file tree
Hide file tree
Showing 5 changed files with 190 additions and 0 deletions.
2 changes: 2 additions & 0 deletions doc/salesforce.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ extension for VS Code. Out of the box commands include:
- `SalesforceRetrieveFromOrg`: Pull the current file from the org
- `SalesforceDiffFile`: Diff the current file against the file in the org
- `SalesforceSetDefaultOrg`: Set the default org for the current project
- `SalesforceCreateLightningComponent`: Create an Aura Component or LWC
- `SalesforceCreateApex`: Create Apex trigger or class

# Setup ~
To use this plugin, you must first setup your project by running `require("salesforce").setup({})`.
Expand Down
148 changes: 148 additions & 0 deletions lua/salesforce/component_generator.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
local Debug = require("salesforce.debug")
local Util = require("salesforce.util")

local M = {}

local category = "Component generation"

local prompt_for_name_and_dir = function(name_prompt, directory_prompt)
local lwc_name = vim.fn.input(name_prompt)
local directory = vim.fn.input(directory_prompt, vim.fn.expand("%:p:h"), "dir")
if vim.fn.isdirectory(directory) == 1 then
Debug:log("component_generator.lua", string.format("Selected directory: %s", directory))
else
Util.clear_and_notify(
string.format("Directory does not exist: %s", directory),
vim.log.levels.ERROR
)
directory = nil
end
return lwc_name, directory
end

local component_creation_callback = function(job)
vim.schedule(function()
local sfdx_output = job:result()
sfdx_output = table.concat(sfdx_output)
Debug:log("component_generator.lua", "Result from command:")
Debug:log("component_generator.lua", sfdx_output)
local json_ok, sfdx_response = pcall(vim.json.decode, sfdx_output)
if not json_ok or not sfdx_response then
vim.notify(
"Failed to parse the 'lightning generate component' SFDX command output",
vim.log.levels.ERROR
)
return
end

if sfdx_response.status == 0 then
Util.clear_and_notify(
string.format(
"Successfully created component in directory: %s",
sfdx_response.result.outputDir
)
)
else
Util.clear_and_notify(
string.format("Failed to create component: %s", vim.inspect(sfdx_response)),
vim.log.levels.ERROR
)
end
end)
end

M.create_lightning_component = function()
local name, dir =
prompt_for_name_and_dir("Lightning Component name: ", "Please select a directory: ")

if not dir then
return
end

Util.clear_prompt()

vim.ui.select(
{ "Aura", "Lightning Web Component" },
{
prompt = "Lightning Component Type: ",
},
vim.schedule_wrap(function(choice)
if not choice then
return
end
local type
if choice == "Aura" then
type = "aura"
else
type = "lwc"
end
local args = {
"lightning",
"generate",
"component",
"--name",
name,
"--type",
type,
"--output-dir",
dir,
"--json",
}
Util.clear_and_notify("Creating component...")
Util.send_cli_command(
args,
component_creation_callback,
category,
"component_generator.lua"
)
end)
)
end

M.create_apex = function()
local name, dir =
prompt_for_name_and_dir("Trigger or Class name: ", "Please select a directory: ")

if not dir then
return
end

Util.clear_prompt()

vim.ui.select(
{ "Class", "Trigger" },
{
prompt = "Apex file type: ",
},
vim.schedule_wrap(function(choice)
if not choice then
return
end
local type
if choice == "Class" then
type = "class"
else
type = "trigger"
end
local args = {
"apex",
"generate",
type,
"--name",
name,
"--output-dir",
dir,
"--json",
}
Util.clear_and_notify("Creating component...")
Util.send_cli_command(
args,
component_creation_callback,
category,
"component_generator.lua"
)
end)
)
end

return M
2 changes: 2 additions & 0 deletions lua/salesforce/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
--- - `SalesforceRetrieveFromOrg`: Pull the current file from the org
--- - `SalesforceDiffFile`: Diff the current file against the file in the org
--- - `SalesforceSetDefaultOrg`: Set the default org for the current project
--- - `SalesforceCreateLightningComponent`: Create an Aura Component or LWC
--- - `SalesforceCreateApex`: Create Apex trigger or class
---
--- # Setup ~
--- To use this plugin, you must first setup your project by running `require("salesforce").setup({})`.
Expand Down
29 changes: 29 additions & 0 deletions lua/salesforce/util.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
local Debug = require("salesforce.debug")

local Job = require("plenary.job")
local M = {}
local namespace = vim.api.nvim_create_namespace("salesforce.util")

Expand Down Expand Up @@ -38,6 +39,10 @@ function M.notify_default_org_not_set()
vim.notify(message, vim.log.levels.ERROR)
end

function M.clear_prompt()
vim.fn.feedkeys(":", "nx")
end

function M.clear_and_notify(msg, log_level)
vim.fn.feedkeys(":", "nx")
vim.notify(msg, log_level)
Expand Down Expand Up @@ -122,6 +127,30 @@ function M.salesforce_cli_available()
return false
end

M.send_cli_command = function(args, on_exit_callback, category, caller_name)
local command = table.concat(args, " ")
Debug:log(caller_name, "Executing command %s", command)
local new_job = Job:new({
command = M.get_sf_executable(),
env = M.get_env(),
args = args,
on_exit = on_exit_callback,
on_stderr = function(_, data)
vim.schedule(function()
Debug:log(caller_name, "Command stderr is: %s", data)
end)
end,
})
if not M[category] or not M[category].current_job:is_running() then
M[category] = {}
M[category].current_job = new_job
M[category].current_bufnr = vim.api.nvim_get_current_buf()
M[category].current_job:start()
else
M.notify_command_in_progress(category)
end
end

local function get_apex_ls_namespace()
local diagnostic_namespaces = vim.diagnostic.get_namespaces()
for id, ns in pairs(diagnostic_namespaces) do
Expand Down
9 changes: 9 additions & 0 deletions plugin/salesforce.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ local Diff = require("salesforce.diff")
local Config = require("salesforce.config")
local OrgManager = require("salesforce.org_manager")
local Debug = require("salesforce.debug")
local ComponentGenerator = require("salesforce.component_generator")

Debug:log("salesforce.lua", "Initializing Salesforce plugin commands...")

Expand Down Expand Up @@ -68,3 +69,11 @@ end, {})
vim.api.nvim_create_user_command("SalesforceSetDefaultOrg", function()
OrgManager:set_default_org()
end, {})

vim.api.nvim_create_user_command("SalesforceCreateLightningComponent", function()
ComponentGenerator:create_lightning_component()
end, {})

vim.api.nvim_create_user_command("SalesforceCreateApex", function()
ComponentGenerator:create_apex()
end, {})

0 comments on commit ce0f006

Please sign in to comment.