Skip to content

Commit

Permalink
Merge pull request #4 from jonathanmorris180/feat/general-updates
Browse files Browse the repository at this point in the history
Feat/general updates
  • Loading branch information
jonathanmorris180 authored Jan 17, 2024
2 parents 0d6160e + 068901f commit 623cf53
Show file tree
Hide file tree
Showing 13 changed files with 226 additions and 57 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,10 @@ There are a number of options available to configure `salesforce.nvim`. See the
```lua
require("salesforce").setup({
-- Prints useful logs about what events are triggered, as well as outputs of the Salesforce CLI
debug = false,
debug = {
to_file = false,
to_console = false,
},
popup = {
-- The width of the popup window.
width = 100,
Expand Down
6 changes: 4 additions & 2 deletions doc/salesforce.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@ To use this plugin, you must first setup your project by running `require("sales
You can pass in a lua table of options to customize the plugin. The default options are:
>
{
-- Prints useful logs about what events are triggered, as well as outputs of the Salesforce CLI
debug = false,
debug = {
to_file = false, -- logs debug messages to a file at vim.fn.stdpath("cache") .. "/salesforce.log"
to_console = false,
},
popup = {
-- The width of the popup window.
width = 100,
Expand Down
5 changes: 4 additions & 1 deletion lua/salesforce/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ function Config:new()
self.__index = self
-- default config values
o.options = {
debug = false,
debug = {
to_file = false,
to_console = false,
},
popup = {
-- The width of the popup window.
width = 100,
Expand Down
10 changes: 7 additions & 3 deletions lua/salesforce/debug.lua
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,12 @@ function Debugger:log(scope, item, ...)
end

function Debugger:log_str(debug_str)
print(debug_str)
vim.fn.writefile({ debug_str }, self.log_file_path, "a")
if Config:get_options().debug.to_console then
print(debug_str)
end
if Config:get_options().debug.to_file then
vim.fn.writefile({ debug_str }, self.log_file_path, "a")
end
end

---prints the table if debug is true.
Expand All @@ -59,7 +63,7 @@ end
---@param indent number?: the default indent value, starts at 0.
---@private
function Debugger:tprint(table, indent)
if not Config:get_options().debug then
if not Config:get_options().debug.to_file and not Config:get_options().debug.to_console then
return
end

Expand Down
39 changes: 31 additions & 8 deletions lua/salesforce/diff.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
local Util = require("salesforce.util")
local Job = require("plenary.job")
local Debug = require("salesforce.debug")
local OrgManager = require("salesforce.org_manager")

function Job:is_running()
if self.handle and not vim.loop.is_closing(self.handle) and vim.loop.is_active(self.handle) then
return true
else
return false
end
end

local M = {}

Expand Down Expand Up @@ -61,7 +70,7 @@ end
local function execute_job(command)
local args = Util.split(command, " ")
table.remove(args, 1)
Job:new({
local new_job = Job:new({
command = "sf",
args = args,
on_exit = diff_callback,
Expand All @@ -70,31 +79,45 @@ local function execute_job(command)
Debug:log("diff.lua", "Command stderr is: %s", data)
end)
end,
}):start()
})
if not M.current_job or not M.current_job:is_running() then
M.current_job = new_job
M.current_job:start()
end
end

M.diff_with_org = function()
if M.current_job and M.current_job:is_running() then
-- needs to be here or temp dir will be overwritten
Util.notify_command_in_progress("diff with org")
return
end
local path = vim.fn.expand("%:p")
local file_name = vim.fn.expand("%:t")
local file_name_no_ext = Util.get_file_name_without_extension(file_name)
local metadataType = Util.get_metadata_type(path)
local default_username = OrgManager:get_default_username()

if metadataType == nil then
vim.notify("Not a supported metadata type.", vim.log.levels.ERROR)
return
end

Util.clear_and_notify("Diffing " .. file_name .. " with the org...")
if default_username == nil then
vim.notify("No default org found.", vim.log.levels.ERROR)
return
end

Util.clear_and_notify(string.format("Diffing %s with org %s...", file_name, default_username))
temp_dir = vim.fn.tempname()
local temp_dir_with_suffix = string.format("%s/main/default", temp_dir)
vim.fn.mkdir(temp_dir_with_suffix, "p")
Debug:log("diff.lua", "Created temp dir: " .. temp_dir_with_suffix)
Debug:log("diff.lua", "Created temp dir: " .. temp_dir)

local command = string.format(
"sf project retrieve start -m %s:%s -r %s --json",
"sf project retrieve start -m %s:%s -r %s -o %s --json",
metadataType,
file_name_no_ext,
temp_dir
temp_dir,
default_username
)
Debug:log("diff.lua", "Command: " .. command)
execute_job(command)
Expand Down
26 changes: 22 additions & 4 deletions lua/salesforce/execute_anon.lua
Original file line number Diff line number Diff line change
@@ -1,13 +1,24 @@
local Popup = require("salesforce.popup")
local Debug = require("salesforce.debug")
local OrgManager = require("salesforce.org_manager")
local Job = require("plenary.job")
local Util = require("salesforce.util")

function Job:is_running()
if self.handle and not vim.loop.is_closing(self.handle) and vim.loop.is_active(self.handle) then
return true
else
return false
end
end

local M = {}

M.execute_anon = function()
Debug:log("execute_anon.lua", "Executing anonymous Apex script...")
local path = vim.fn.expand("%:p")
local file_type = vim.fn.expand("%:e")
local default_username = OrgManager:get_default_username()

if file_type ~= "apex" then
vim.notify("Not an Apex script file.", vim.log.levels.ERROR)
Expand All @@ -16,11 +27,11 @@ M.execute_anon = function()

Popup:create_popup({})
Popup:write_to_popup("Executing anonymous Apex script...")
local command = "sf apex run -f " .. path
local command = string.format("sf apex run -f %s -o %s", path, default_username)
Debug:log("execute_anon.lua", "Running " .. command .. "...")
Job:new({
local new_job = Job:new({
command = "sf",
args = { "apex", "run", "-f", path },
args = { "apex", "run", "-f", path, "-o", default_username },
on_exit = function(j, code)
vim.schedule(function()
if code == 0 then
Expand All @@ -42,7 +53,14 @@ M.execute_anon = function()
end
end)
end,
}):start()
})

if not M.current_job or not M.current_job:is_running() then
M.current_job = new_job
M.current_job:start()
else
Util.notify_command_in_progress("script execution")
end
end

return M
39 changes: 33 additions & 6 deletions lua/salesforce/file_manager.lua
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
local Config = require("salesforce.config")
local OrgManager = require("salesforce.org_manager")
local Job = require("plenary.job")
local Debug = require("salesforce.debug")
local Util = require("salesforce.util")

function Job:is_running()
if self.handle and not vim.loop.is_closing(self.handle) and vim.loop.is_active(self.handle) then
return true
else
return false
end
end

local M = {}

local function push_to_org_callback(j)
Expand Down Expand Up @@ -114,7 +123,7 @@ end
local function push(command)
local args = Util.split(command, " ")
table.remove(args, 1)
Job:new({
local new_job = Job:new({
command = "sf",
args = args,
on_exit = push_to_org_callback,
Expand All @@ -123,13 +132,20 @@ local function push(command)
Debug:log("file_manager.lua", "Command stderr is: %s", data)
end)
end,
}):start()
})

if not M.current_job or not M.current_job:is_running() then
M.current_job = new_job
M.current_job:start()
else
Util.notify_command_in_progress("push/pull")
end
end

local function pull(command)
local args = Util.split(command, " ")
table.remove(args, 1)
Job:new({
local new_job = Job:new({
command = "sf",
args = args,
on_exit = pull_from_org_callback,
Expand All @@ -138,25 +154,36 @@ local function pull(command)
Debug:log("file_manager.lua", "Command stderr is: %s", data)
end)
end,
}):start()
})

if not M.current_job or not M.current_job:is_running() then
M.current_job = new_job
M.current_job:start()
else
Util.notify_command_in_progress("push/pull")
end
end

M.push_to_org = function()
local path = vim.fn.expand("%:p")
local file_name = vim.fn.expand("%:t")
local default_username = OrgManager:get_default_username()

Util.clear_and_notify("Pushing " .. file_name .. " to the org...")
local command = string.format("sf project deploy start -d %s --json", path)
local command =
string.format("sf project deploy start -d %s --json -o %s", path, default_username)
Debug:log("file_manager.lua", "Command: " .. command)
push(command)
end

M.pull_from_org = function()
local path = vim.fn.expand("%:p")
local file_name = vim.fn.expand("%:t")
local default_username = OrgManager:get_default_username()

Util.clear_and_notify("Pulling " .. file_name .. " from the org...")
local command = string.format("sf project retrieve start -d %s --json", path)
local command =
string.format("sf project retrieve start -d %s --json -o %s", path, default_username)
Debug:log("file_manager.lua", "Command: " .. command)
if Config:get_options().file_manager.ignore_conflicts then
Debug:log("file_manager.lua", "Ignoring conflicts becuase of config option")
Expand Down
6 changes: 4 additions & 2 deletions lua/salesforce/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@
--- You can pass in a lua table of options to customize the plugin. The default options are:
--- >
--- {
--- -- Prints useful logs about what events are triggered, as well as outputs of the Salesforce CLI
--- debug = false,
--- debug = {
--- to_file = false, -- logs debug messages to a file at vim.fn.stdpath("cache") .. "/salesforce.log"
--- to_console = false,
--- },
--- popup = {
--- -- The width of the popup window.
--- width = 100,
Expand Down
Loading

0 comments on commit 623cf53

Please sign in to comment.