Skip to content

Commit

Permalink
fix: update process handling
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanmorris180 committed Jan 17, 2024
1 parent 1e28db2 commit e1d6d08
Show file tree
Hide file tree
Showing 5 changed files with 210 additions and 111 deletions.
26 changes: 20 additions & 6 deletions lua/salesforce/diff.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@ 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 = {}

local temp_dir
Expand Down Expand Up @@ -71,7 +79,7 @@ local function execute_job(command)
M.is_processing = true
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 @@ -80,7 +88,13 @@ 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()
else
Util.notify_command_in_progress()
end
end

M.diff_with_org = function()
Expand All @@ -92,21 +106,21 @@ M.diff_with_org = function()
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_alias = OrgManager:get_default_alias()
local default_username = OrgManager:get_default_username()

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

if default_alias == nil then
if default_username == nil then
vim.notify("No default org found.", vim.log.levels.ERROR)
M.is_processing = false
return
end

Util.clear_and_notify(string.format("Diffing %s with org %s...", file_name, default_alias))
Util.clear_and_notify(string.format("Diffing %s with org %s...", file_name, default_username))
temp_dir = vim.fn.tempname()
Debug:log("diff.lua", "Created temp dir: " .. temp_dir)

Expand All @@ -115,7 +129,7 @@ M.diff_with_org = function()
metadataType,
file_name_no_ext,
temp_dir,
default_alias
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()
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()
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()
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
Loading

0 comments on commit e1d6d08

Please sign in to comment.