Skip to content

Commit

Permalink
feat: automatically download the server executable if it is not present
Browse files Browse the repository at this point in the history
  • Loading branch information
vyfor committed Jan 3, 2025
1 parent c6ff458 commit 355d63a
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 22 deletions.
11 changes: 4 additions & 7 deletions lua/cord/server/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,10 @@ function M:connect(path, retried)

::spawn::
logger.debug 'Pipe not found. Spawning server executable...'
local retry = require('cord.server.spawn')
.spawn(
self.config.editor.client,
path,
self.config.advanced.server.executable_path
)
:await()

local process = require('cord.server.spawn').spawn(self.config, path)
local should_continue, retry = process:await()
if not should_continue then return end

logger.debug 'Server executable spawned'
if retry then return M:connect(path):await() end
Expand Down
39 changes: 24 additions & 15 deletions lua/cord/server/spawn/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,28 @@ local async = require 'cord.core.async'
local Future = require 'cord.core.async.future'
local M = {}

M.spawn = async.wrap(function(client_id, pipe_path, exec_path)
if not exec_path then
exec_path = require('cord.server.fs').get_executable_path()
end
M.spawn = async.wrap(function(config, pipe_path)
return Future.new(function(resolve, reject)
local exec_path = config.advanced.server.executable_path
local update_strategy = config.advanced.server.update
local client_id = config.editor.client
if not exec_path then
exec_path = require('cord.server.fs').get_executable_path()
end

local fs = require 'cord.core.uv.fs'
if not fs.stat(exec_path):await() then
error('Could not find server executable. Please update it', 0)
return
end
local fs = require 'cord.core.uv.fs'
local stat = fs.stat(exec_path):get()
if not stat then
if update_strategy == 'fetch' then
require('cord.server.update').fetch():await()
elseif update_strategy == 'build' then
require('cord.server.update').build():await()
else
require('cord.plugin.log').error 'Could not find the server executable'
end
return resolve(false, false)
end

return Future.new(function(resolve, reject)
local process = require 'cord.core.uv.process'
process.spawn_daemon {
cmd = exec_path,
Expand All @@ -24,16 +34,15 @@ M.spawn = async.wrap(function(client_id, pipe_path, exec_path)
client_id,
'-t',
require('cord.plugin.config').opts.advanced.server.timeout,
-- todo: uncomment on new release
-- '-r',
-- require('cord.plugin.config').opts.advanced.discord.reconnect_interval,
'-r',
require('cord.plugin.config').opts.advanced.discord.reconnect_interval,
},
on_stdout = function(data)
if data:match 'Ready' then resolve(false) end
if data:match 'Ready' then resolve(true, false) end
end,
on_stderr = function(err)
if err:match 'another instance is running' then
resolve(true)
resolve(true, true)
return
end

Expand Down

0 comments on commit 355d63a

Please sign in to comment.