Skip to content

Commit

Permalink
feat: environment variables management (XcodebuildEditEnvVars) (#251)
Browse files Browse the repository at this point in the history
  • Loading branch information
wojciech-kulik authored Nov 19, 2024
1 parent 77ef9b1 commit a2fa192
Show file tree
Hide file tree
Showing 10 changed files with 112 additions and 0 deletions.
16 changes: 16 additions & 0 deletions doc/xcodebuild.txt
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,7 @@ Configuration
| `XcodebuildPreviousDevice` | Selects previous device |
| `XcodebuildSelectTestPlan` | Show test plan picker |
| `XcodebuildShowConfig` | Print current project configuration |
| `XcodebuildEditEnvVars` | Edit environment variables |
| `XcodebuildBootSimulator` | Boot selected simulator |
| `XcodebuildInstallApp` | Install application |
| `XcodebuildUninstallApp` | Uninstall application |
Expand Down Expand Up @@ -701,6 +702,10 @@ M.clean_derived_data() *xcodebuild.actions.clean_derived_data*
Cleans the derived data.


M.edit_env_vars() *xcodebuild.actions.edit_env_vars*
Opens `env.txt` file in a new tab.


M.run({callback}) *xcodebuild.actions.run*
Launches the app.

Expand Down Expand Up @@ -1854,6 +1859,17 @@ M.create_app_dir()
Creates the `.nvim/xcodebuild` folder if it doesn't exist.


*xcodebuild.project.appdata.initialize_env_vars*
M.initialize_env_vars()


M.read_env_vars() *xcodebuild.project.appdata.read_env_vars*
Reads the environment variables from disk.

Returns: ~
(table<string,string>|nil)


*xcodebuild.project.appdata.read_original_logs*
M.read_original_logs()
Reads the original Xcode logs.
Expand Down
7 changes: 7 additions & 0 deletions lua/xcodebuild/actions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ local testRunner = require("xcodebuild.tests.runner")
local projectBuilder = require("xcodebuild.project.builder")
local projectConfig = require("xcodebuild.project.config")
local projectManager = require("xcodebuild.project.manager")
local appdata = require("xcodebuild.project.appdata")
local lsp = require("xcodebuild.integrations.lsp")

local M = {}
Expand Down Expand Up @@ -111,6 +112,12 @@ function M.clean_derived_data()
projectBuilder.clean_derived_data()
end

---Opens `env.txt` file in a new tab.
function M.edit_env_vars()
appdata.initialize_env_vars()
vim.cmd("tabedit " .. appdata.env_vars_filepath)
end

---Launches the app.
---@param callback function|nil
function M.run(callback)
Expand Down
15 changes: 15 additions & 0 deletions lua/xcodebuild/core/xcode.lua
Original file line number Diff line number Diff line change
Expand Up @@ -681,7 +681,15 @@ function M.launch_app_on_device(destination, bundleId, callback)
}
debug_print("launch_app_on_device", command)

local appdata = require("xcodebuild.project.appdata")
local env = nil
for key, value in pairs(appdata.read_env_vars() or {}) do
env = env or {}
env["DEVICECTL_CHILD_" .. key] = value
end

return vim.fn.jobstart(command, {
env = env,
stderr_buffered = true,
on_stderr = show_stderr_output,
on_exit = callback_or_error("launch", callback),
Expand Down Expand Up @@ -726,7 +734,14 @@ function M.launch_app_on_simulator(destination, bundleId, waitForDebugger, callb
util.shell("open -a Simulator")
end

local env = nil
for key, value in pairs(appdata.read_env_vars() or {}) do
env = env or {}
env["SIMCTL_CHILD_" .. key] = value
end

return vim.fn.jobstart(command, {
env = env,
stdout_buffered = false,
stderr_buffered = false,
detach = true,
Expand Down
1 change: 1 addition & 0 deletions lua/xcodebuild/docs/commands.lua
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
--- | `XcodebuildPreviousDevice` | Selects previous device |
--- | `XcodebuildSelectTestPlan` | Show test plan picker |
--- | `XcodebuildShowConfig` | Print current project configuration |
--- | `XcodebuildEditEnvVars` | Edit environment variables |
--- | `XcodebuildBootSimulator` | Boot selected simulator |
--- | `XcodebuildInstallApp` | Install application |
--- | `XcodebuildUninstallApp` | Uninstall application |
Expand Down
1 change: 1 addition & 0 deletions lua/xcodebuild/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,7 @@ function M.setup(options)
vim.api.nvim_create_user_command("XcodebuildShowCurrentFileTargets", call(actions.show_current_file_targets), { nargs = 0 })

-- Other
vim.api.nvim_create_user_command("XcodebuildEditEnvVars", call(actions.edit_env_vars), { nargs = 0 })
vim.api.nvim_create_user_command("XcodebuildShowConfig", call(actions.show_current_config), { nargs = 0 })
vim.api.nvim_create_user_command("XcodebuildBootSimulator", call(actions.boot_simulator), { nargs = 0 })
vim.api.nvim_create_user_command("XcodebuildCleanDerivedData", call(actions.clean_derived_data), { nargs = 0 })
Expand Down
3 changes: 3 additions & 0 deletions lua/xcodebuild/integrations/remote_debugger.lua
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,10 @@ local function start_dap()
notifications.send("Connecting to device...")
setup_connection_listeners()

local appdata = require("xcodebuild.project.appdata")

dap.run({
env = appdata.read_env_vars(),
name = "iOS Remote Debugger",
type = "codelldb",
request = "launch",
Expand Down
11 changes: 11 additions & 0 deletions lua/xcodebuild/platform/device_proxy.lua
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,17 @@ function M.launch_app(destination, bundleId, callback)
destination,
}

local appdata = require("xcodebuild.project.appdata")
local env = appdata.read_env_vars()
if env then
table.insert(command, "--env")

for key, value in pairs(env) do
table.insert(command, key)
table.insert(command, value)
end
end

return vim.fn.jobstart(command, {
on_exit = callback_or_error("launch", callback),
})
Expand Down
2 changes: 2 additions & 0 deletions lua/xcodebuild/platform/macos.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ local M = {}
---@return number # job id
function M.launch_app(appPath, callback)
return vim.fn.jobstart({ "open", appPath }, {
env = appdata.read_env_vars(),
on_exit = function(_, code)
if code == 0 then
util.call(callback)
Expand Down Expand Up @@ -47,6 +48,7 @@ function M.launch_and_debug(appPath, callback)
program = appPath,
stopOnEntry = false,
waitFor = true,
env = appdata.read_env_vars(),
})

util.call(callback)
Expand Down
54 changes: 54 additions & 0 deletions lua/xcodebuild/project/appdata.lua
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ M.snapshots_dir = M.appdir .. "/failing-snapshots"
M.coverage_report_filepath = M.appdir .. "/coverage.json"
M.test_explorer_filepath = M.appdir .. "/test-explorer.json"
M.breakpoints_filepath = M.appdir .. "/breakpoints.json"
M.env_vars_filepath = M.appdir .. "/env.txt"

M.GETSNAPSHOTS_TOOL = "getsnapshots"
M.PROJECT_HELPER_TOOL = "project_helper.rb"
Expand All @@ -77,6 +78,59 @@ function M.create_app_dir()
util.shell("mkdir -p .nvim/xcodebuild")
end

function M.initialize_env_vars()
local path = M.env_vars_filepath

if not util.file_exists(path) then
vim.fn.writefile({
"# Environment Variables",
"#",
"# Add your environment variables here.",
"# Each line should be in the format of `KEY=VALUE`.",
"#",
"# Example:",
"#",
"# OS_ACTIVITY_MODE=disable",
"",
"",
}, path)
end
end

---Reads the environment variables from disk.
---@return table<string,string>|nil
function M.read_env_vars()
local path = M.env_vars_filepath

if not util.file_exists(path) then
return nil
end

local success, lines = pcall(vim.fn.readfile, path)
if not success then
return nil
end

local filteredLines = vim.tbl_filter(function(line)
return not vim.startswith(line, "#") and vim.trim(line) ~= ""
end, lines)

local result = {}

for _, line in ipairs(filteredLines) do
local parts = vim.split(line, "=", { plain = true })
if #parts == 2 then
result[parts[1]] = parts[2]
end
end

if vim.tbl_isempty(result) then
return nil
end

return result
end

---Reads the original Xcode logs.
---@return string[]
function M.read_original_logs()
Expand Down
2 changes: 2 additions & 0 deletions lua/xcodebuild/ui/picker_actions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ function M.show_xcode_project_actions()
"Install Application",
"Uninstall Application",
"---------------------------------",
"Edit Environment Variables",
"Clean DerivedData",
"Open Project in Xcode",
}
Expand Down Expand Up @@ -226,6 +227,7 @@ function M.show_xcode_project_actions()

function() end,

actions.edit_env_vars,
actions.clean_derived_data,
actions.open_in_xcode,
}
Expand Down

0 comments on commit a2fa192

Please sign in to comment.