Skip to content

Commit b386aea

Browse files
Merge pull request #55 from jonathanmorris180/fix/issue-54
fix: issue 54 - Diff command does not work for static resources
2 parents f2aa6bd + 56ae63c commit b386aea

File tree

3 files changed

+124
-51
lines changed

3 files changed

+124
-51
lines changed

doc/salesforce.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ You can pass in a lua table of options to customize the plugin. The default opti
5757
Setup function
5858

5959
Parameters ~
60-
{opts} `(table)` | nil Module config table. See |Config.options|.
60+
{opts} `(table | nil)` Module config table. See |Config.options|.
6161

6262
Usage ~
6363
`require("salesforce").setup({})`

lua/salesforce/diff.lua

Lines changed: 102 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,97 @@ local M = {}
1515

1616
local temp_dir
1717
local executable = Util.get_sf_executable()
18+
local file_name
19+
local current_buf
1820

19-
local function diff_callback(j)
21+
local function execute_job(args, callback)
22+
local all_args = Util.flatten(args)
23+
Debug:log("diff.lua", "Command: ")
24+
Debug:log("diff.lua", all_args)
25+
local new_job = Job:new({
26+
command = executable,
27+
args = all_args,
28+
on_exit = callback,
29+
env = Util.get_env(),
30+
on_stderr = function(_, data)
31+
vim.schedule(function()
32+
Debug:log("diff.lua", "Command stderr is: %s", data)
33+
end)
34+
end,
35+
})
36+
if not M.current_job or not M.current_job:is_running() then
37+
M.current_job = new_job
38+
M.current_job:start()
39+
end
40+
end
41+
42+
local function parse_sfdx_response(sfdx_output)
43+
local json_ok, sfdx_response = pcall(vim.json.decode, sfdx_output)
44+
if not json_ok or not sfdx_response then
45+
vim.notify("Failed to parse the SFDX command output", vim.log.levels.ERROR)
46+
vim.fn.delete(temp_dir, "rf")
47+
return
48+
end
49+
return sfdx_response
50+
end
51+
52+
local function finish(j)
2053
vim.schedule(function()
2154
local sfdx_output = j:result()
22-
local file_name = vim.fn.expand("%:t")
2355
sfdx_output = table.concat(sfdx_output)
2456
Debug:log("diff.lua", "Result from command:")
2557
Debug:log("diff.lua", sfdx_output)
2658

27-
local json_ok, sfdx_response = pcall(vim.json.decode, sfdx_output)
28-
if not json_ok or not sfdx_response then
29-
vim.notify("Failed to parse the 'diff' SFDX command output", vim.log.levels.ERROR)
59+
local sfdx_response = parse_sfdx_response(sfdx_output)
60+
61+
if not sfdx_response then
62+
return -- for the compiler, will never happen
63+
end
64+
65+
if sfdx_response.status ~= 0 or not sfdx_response.result or #sfdx_response.result == 0 then
66+
if sfdx_response.cause then
67+
vim.notify(sfdx_response.cause, vim.log.levels.ERROR)
68+
vim.fn.delete(temp_dir, "rf")
69+
return
70+
else
71+
vim.notify(
72+
"Unknown error converting from metadata to source format",
73+
vim.log.levels.ERROR
74+
)
75+
vim.fn.delete(temp_dir, "rf")
76+
return
77+
end
78+
end
79+
80+
local retrieved_file_path = Util.find_file(temp_dir .. "/converted", file_name)
81+
Debug:log("diff.lua", "Temp file path: " .. (retrieved_file_path or "Not found"))
82+
83+
if not retrieved_file_path or not vim.fn.filereadable(retrieved_file_path) then
84+
vim.notify("Failed to retrieve the file from the org", vim.log.levels.ERROR)
3085
vim.fn.delete(temp_dir, "rf")
3186
return
3287
end
3388

89+
Util.clear_and_notify("Diffing " .. file_name)
90+
vim.api.nvim_set_current_buf(current_buf) -- In case the user moves to a different buffer while waiting
91+
vim.cmd("vert diffsplit " .. retrieved_file_path)
92+
vim.fn.delete(temp_dir, "rf")
93+
end)
94+
end
95+
96+
local function convert_to_source(j)
97+
vim.schedule(function()
98+
local sfdx_output = j:result()
99+
sfdx_output = table.concat(sfdx_output)
100+
Debug:log("diff.lua", "Result from command:")
101+
Debug:log("diff.lua", sfdx_output)
102+
103+
local sfdx_response = parse_sfdx_response(sfdx_output)
104+
105+
if not sfdx_response then
106+
return -- for the compiler, will never happen
107+
end
108+
34109
if
35110
sfdx_response.result
36111
and sfdx_response.result.files
@@ -57,60 +132,32 @@ local function diff_callback(j)
57132
end
58133
end
59134

60-
local retrieved_file_path = Util.find_file(temp_dir, file_name)
61-
Debug:log("diff.lua", "Temp file path: " .. (retrieved_file_path or "Not found"))
62-
63-
if not retrieved_file_path or not vim.fn.filereadable(retrieved_file_path) then
64-
vim.notify("Failed to retrieve the file from the org", vim.log.levels.ERROR)
65-
vim.fn.delete(temp_dir, "rf")
66-
return
67-
end
68-
69-
Util.clear_and_notify("Diffing " .. file_name)
70-
vim.cmd("vert diffsplit " .. retrieved_file_path)
71-
vim.fn.delete(temp_dir, "rf")
135+
-- Now, convert to source
136+
-- Note: Org flag does not exist for this command
137+
local unpackaged_dir = temp_dir .. "/unpackaged"
138+
local output_dir = temp_dir .. "/converted"
139+
local args = {
140+
"project",
141+
"convert",
142+
"mdapi",
143+
"--json",
144+
["--root-dir"] = unpackaged_dir,
145+
["--output-dir"] = output_dir,
146+
}
147+
148+
execute_job(args, finish)
72149
end)
73150
end
74151

75-
local function expand(t)
76-
local res = {}
77-
for k, v in pairs(t) do
78-
res[#res + 1] = k
79-
res[#res + 1] = v
80-
end
81-
return res
82-
end
83-
84-
local function execute_job(args)
85-
local all_args = { "project", "retrieve", "start", "--unzip", unpack(expand(args)) } -- always ignore when retrieving temp files
86-
table.insert(all_args, "--json")
87-
Debug:log("diff.lua", "Command: ")
88-
Debug:log("diff.lua", all_args)
89-
local new_job = Job:new({
90-
command = executable,
91-
args = all_args,
92-
on_exit = diff_callback,
93-
env = Util.get_env(),
94-
on_stderr = function(_, data)
95-
vim.schedule(function()
96-
Debug:log("diff.lua", "Command stderr is: %s", data)
97-
end)
98-
end,
99-
})
100-
if not M.current_job or not M.current_job:is_running() then
101-
M.current_job = new_job
102-
M.current_job:start()
103-
end
104-
end
105-
106152
M.diff_with_org = function()
107153
if M.current_job and M.current_job:is_running() then
108154
-- needs to be here or temp dir will be overwritten
109155
Util.notify_command_in_progress("diff with org")
110156
return
111157
end
112158
local path = vim.fn.expand("%:p")
113-
local file_name = vim.fn.expand("%:t")
159+
file_name = vim.fn.expand("%:t")
160+
current_buf = vim.api.nvim_get_current_buf()
114161
local file_name_no_ext = Util.get_file_name_without_extension(file_name)
115162
local metadataType = Util.get_metadata_type(path)
116163
local default_username = OrgManager:get_default_username()
@@ -129,12 +176,17 @@ M.diff_with_org = function()
129176
temp_dir = vim.fn.tempname()
130177
Debug:log("diff.lua", "Created temp dir: " .. temp_dir)
131178
local args = {
179+
"project",
180+
"retrieve",
181+
"start",
182+
"--unzip",
183+
"--json",
132184
["-m"] = string.format("%s:%s", metadataType, file_name_no_ext),
133185
["--target-metadata-dir"] = temp_dir, -- See https://github.com/forcedotcom/cli/issues/3009
134186
["-o"] = default_username,
135187
}
136188

137-
execute_job(args)
189+
execute_job(args, convert_to_source)
138190
end
139191

140192
return M

lua/salesforce/util.lua

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,27 @@ function M.clear_and_notify(msg, log_level)
4848
vim.notify(msg, log_level)
4949
end
5050

51+
function M.flatten(args)
52+
local flattened = {}
53+
54+
-- First, insert all array values (with integer keys)
55+
for _, v in ipairs(args) do
56+
-- key here would be 1, 2, 3, etc., so we don't need it
57+
-- Need to use ipairs here because it preserves order
58+
table.insert(flattened, v)
59+
end
60+
61+
-- Then, insert all key-value pairs
62+
for k, v in pairs(args) do
63+
-- order isn't guaranteed here but the key-value pairs will be together
64+
if type(k) ~= "number" then
65+
table.insert(flattened, k)
66+
table.insert(flattened, v)
67+
end
68+
end
69+
return flattened
70+
end
71+
5172
-- recursively search for the file
5273
function M.find_file(path, target)
5374
local scanner = vim.loop.fs_scandir(path)

0 commit comments

Comments
 (0)