Skip to content

Commit

Permalink
feat: run args management (XcodebuildEditRunArgs) (#252)
Browse files Browse the repository at this point in the history
  • Loading branch information
wojciech-kulik authored Nov 19, 2024
1 parent a2fa192 commit 026c447
Show file tree
Hide file tree
Showing 12 changed files with 122 additions and 11 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ It provides all essential actions for development including building, debugging,
- [x] Test Explorer to visually present a tree with all tests and results.
- [x] Built using official command line tools like `xcodebuild` and `xcrun simctl`.
- [x] Actions to build, run, debug, and test apps on simulators and physical devices.
- [x] Environment variables and run arguments management.
- [x] Buffer integration with test results (code coverage, success & failure marks, duration,
extra diagnostics).
- [x] Code coverage report with customizable levels.
Expand Down
19 changes: 19 additions & 0 deletions doc/xcodebuild.txt
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ Features *xcodebuild.features*
- Built using official command line tools like `xcodebuild` and `xcrun simctl`.
- Actions to build, run, debug, and test apps on simulators and
physical devices.
- Environment variables and run arguments management.
- Buffer integration with test results
(code coverage, success & failure marks, duration, extra diagnostics).
- Code coverage report with customizable levels.
Expand Down Expand Up @@ -496,6 +497,7 @@ Configuration
| `XcodebuildSelectTestPlan` | Show test plan picker |
| `XcodebuildShowConfig` | Print current project configuration |
| `XcodebuildEditEnvVars` | Edit environment variables |
| `XcodebuildEditRunArgs` | Edit run arguments |
| `XcodebuildBootSimulator` | Boot selected simulator |
| `XcodebuildInstallApp` | Install application |
| `XcodebuildUninstallApp` | Uninstall application |
Expand Down Expand Up @@ -706,6 +708,10 @@ M.edit_env_vars() *xcodebuild.actions.edit_env_vars*
Opens `env.txt` file in a new tab.


M.edit_run_args() *xcodebuild.actions.edit_run_args*
Opens `run_args.txt` file in a new tab.


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

Expand Down Expand Up @@ -1861,6 +1867,19 @@ M.create_app_dir()

*xcodebuild.project.appdata.initialize_env_vars*
M.initialize_env_vars()
Initializes the `.nvim/xcodebuild/env.txt` file.


*xcodebuild.project.appdata.initialize_run_args*
M.initialize_run_args()
Initializes the `.nvim/xcodebuild/run_args.txt` file.


M.read_run_args() *xcodebuild.project.appdata.read_run_args*
Reads the run arguments from disk.

Returns: ~
(string[]|nil)


M.read_env_vars() *xcodebuild.project.appdata.read_env_vars*
Expand Down
6 changes: 6 additions & 0 deletions lua/xcodebuild/actions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,12 @@ function M.edit_env_vars()
vim.cmd("tabedit " .. appdata.env_vars_filepath)
end

---Opens `run_args.txt` file in a new tab.
function M.edit_run_args()
appdata.initialize_run_args()
vim.cmd("tabedit " .. appdata.run_args_filepath)
end

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

local appdata = require("xcodebuild.project.appdata")
local runArgs = appdata.read_run_args()
if runArgs then
table.insert(command, "--")
for _, value in ipairs(runArgs) do
table.insert(command, value)
end
end

debug_print("launch_app_on_device", command)

local env = nil
for key, value in pairs(appdata.read_env_vars() or {}) do
env = env or {}
Expand Down Expand Up @@ -715,9 +724,23 @@ function M.launch_app_on_simulator(destination, bundleId, waitForDebugger, callb
bundleId,
}
command = util.skip_nil(command)
debug_print("launch_app_on_simulator", command)

local appdata = require("xcodebuild.project.appdata")
local runArgs = appdata.read_run_args()
if runArgs then
table.insert(command, "--")
for _, value in ipairs(runArgs) do
table.insert(command, value)
end
end

debug_print("launch_app_on_simulator", command)

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

local write_logs = function(_, output)
if output[#output] == "" then
Expand All @@ -734,12 +757,6 @@ 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,
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 @@ -83,6 +83,7 @@
--- | `XcodebuildSelectTestPlan` | Show test plan picker |
--- | `XcodebuildShowConfig` | Print current project configuration |
--- | `XcodebuildEditEnvVars` | Edit environment variables |
--- | `XcodebuildEditRunArgs` | Edit run arguments |
--- | `XcodebuildBootSimulator` | Boot selected simulator |
--- | `XcodebuildInstallApp` | Install application |
--- | `XcodebuildUninstallApp` | Uninstall application |
Expand Down
1 change: 1 addition & 0 deletions lua/xcodebuild/docs/features.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
--- - Built using official command line tools like `xcodebuild` and `xcrun simctl`.
--- - Actions to build, run, debug, and test apps on simulators and
--- physical devices.
--- - Environment variables and run arguments management.
--- - Buffer integration with test results
--- (code coverage, success & failure marks, duration, extra diagnostics).
--- - Code coverage report with customizable levels.
Expand Down
1 change: 1 addition & 0 deletions lua/xcodebuild/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,7 @@ function M.setup(options)

-- Other
vim.api.nvim_create_user_command("XcodebuildEditEnvVars", call(actions.edit_env_vars), { nargs = 0 })
vim.api.nvim_create_user_command("XcodebuildEditRunArgs", call(actions.edit_run_args), { 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
1 change: 1 addition & 0 deletions lua/xcodebuild/integrations/remote_debugger.lua
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ local function start_dap()

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

for _, value in ipairs(appdata.read_run_args() or {}) do
table.insert(command, value)
end

return vim.fn.jobstart(command, {
on_exit = callback_or_error("launch", callback),
})
Expand Down
13 changes: 12 additions & 1 deletion lua/xcodebuild/platform/macos.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,17 @@ local M = {}
---@param callback function|nil
---@return number # job id
function M.launch_app(appPath, callback)
return vim.fn.jobstart({ "open", appPath }, {
local command = { "open", appPath }

local runArgs = appdata.read_run_args()
if runArgs then
table.insert(command, "--args")
for _, value in ipairs(runArgs) do
table.insert(command, value)
end
end

return vim.fn.jobstart(command, {
env = appdata.read_env_vars(),
on_exit = function(_, code)
if code == 0 then
Expand Down Expand Up @@ -46,6 +56,7 @@ function M.launch_and_debug(appPath, callback)
request = "launch",
cwd = "${workspaceFolder}",
program = appPath,
args = appdata.read_run_args(),
stopOnEntry = false,
waitFor = true,
env = appdata.read_env_vars(),
Expand Down
47 changes: 47 additions & 0 deletions lua/xcodebuild/project/appdata.lua
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ 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.run_args_filepath = M.appdir .. "/run_args.txt"

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

---Initializes the `.nvim/xcodebuild/env.txt` file.
function M.initialize_env_vars()
local path = M.env_vars_filepath

Expand All @@ -97,6 +99,51 @@ function M.initialize_env_vars()
end
end

---Initializes the `.nvim/xcodebuild/run_args.txt` file.
function M.initialize_run_args()
local path = M.run_args_filepath

if not util.file_exists(path) then
vim.fn.writefile({
"# Run Arguments",
"#",
"# Add your run arguments here.",
"# Each line should be a separate argument.",
"#",
"# Example:",
"#",
"# -FIRDebugEnabled",
"",
"",
}, path)
end
end

---Reads the run arguments from disk.
---@return string[]|nil
function M.read_run_args()
local path = M.run_args_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)

if vim.tbl_isempty(filteredLines) then
return nil
end

return filteredLines
end

---Reads the environment variables from disk.
---@return table<string,string>|nil
function M.read_env_vars()
Expand Down
6 changes: 4 additions & 2 deletions lua/xcodebuild/ui/picker_actions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -177,12 +177,13 @@ function M.show_xcode_project_actions()
"Show Project Manager",
"Show Current Configuration",
"Show Configuration Wizard",
"Edit Environment Variables",
"Edit Run Arguments",
"---------------------------------",
"Boot Selected Simulator",
"Install Application",
"Uninstall Application",
"---------------------------------",
"Edit Environment Variables",
"Clean DerivedData",
"Open Project in Xcode",
}
Expand Down Expand Up @@ -218,6 +219,8 @@ function M.show_xcode_project_actions()
actions.show_project_manager_actions,
actions.show_current_config,
actions.configure_project,
actions.edit_env_vars,
actions.edit_run_args,

function() end,

Expand All @@ -227,7 +230,6 @@ 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 026c447

Please sign in to comment.