3030
3131-- Starts the Go server and call the callback provided
3232M .start = function (callback )
33- local port = tonumber (state .settings .port ) or 0
33+ local port = tonumber (state .settings .server . port ) or 0
3434 local parsed_port = nil
3535 local callback_called = false
3636
@@ -51,7 +51,7 @@ M.start = function(callback)
5151 settings = settings :gsub (' "' , ' \\ "' )
5252 end
5353
54- local command = string.format (' "%s" "%s"' , state .settings .bin , settings )
54+ local command = string.format (' "%s" "%s"' , state .settings .server . binary , settings )
5555
5656 local job_id = vim .fn .jobstart (command , {
5757 on_stdout = function (_ , data )
@@ -61,7 +61,7 @@ M.start = function(callback)
6161 port = line :match (" Server started on port:%s+(%d+)" )
6262 if port ~= nil then
6363 parsed_port = port
64- state .settings .port = port
64+ state .settings .server . port = port
6565 break
6666 end
6767 end
@@ -105,26 +105,54 @@ end
105105-- Builds the Go binary with the current Git tag.
106106M .build = function (override )
107107 local file_path = u .current_file_path ()
108- local parent_dir = vim .fn .fnamemodify (file_path , " :h:h:h:h" )
108+ state .settings .root_path = vim .fn .fnamemodify (file_path , " :h:h:h:h" )
109+
110+ -- If the user provided a path to the server, don't build it.
111+ if state .settings .server .binary ~= nil then
112+ local binary_exists = vim .loop .fs_stat (state .settings .server .binary )
113+ if binary_exists == nil then
114+ u .notify (string.format (" The user-provided server path (%s) does not exist." , state .settings .server .binary ), vim .log .levels .ERROR )
115+ end
116+ return
117+ end
118+
119+ -- If the user did not provide a path, we build it and place it in either the data path, or the
120+ -- first writable path we find in the runtime.
121+ local datapath = vim .fn .stdpath (' data' )
122+ local runtimepath = vim .api .nvim_list_runtime_paths ()
123+ table.insert (runtimepath , 1 , datapath )
124+
125+ local bin_folder
126+ for _ , path in ipairs (runtimepath ) do
127+ local ok , err = vim .loop .fs_access (path , ' w' )
128+ if err == nil and ok ~= nil and ok then
129+ bin_folder = path .. u .path_separator .. " gitlab.nvim" .. u .path_separator .. " bin"
130+ if vim .fn .mkdir (bin_folder , " p" ) == 1 then
131+ state .settings .server .binary = bin_folder .. u .path_separator .. " server"
132+ break
133+ end
134+ end
135+ end
109136
110- local bin_name = u .is_windows () and " bin.exe" or " bin"
111- state .settings .root_path = parent_dir
112- state .settings .bin = parent_dir .. u .path_separator .. " cmd" .. u .path_separator .. bin_name
137+ if state .settings .server .binary == nil then
138+ u .notify (" Could not find a writable folder in the runtime path to save the server to." , vim .log .levels .ERROR )
139+ return
140+ end
113141
114142 if not override then
115- local binary_exists = vim .loop .fs_stat (state .settings .bin )
143+ local binary_exists = vim .loop .fs_stat (state .settings .server . binary )
116144 if binary_exists ~= nil then
117145 return
118146 end
119147 end
120148
121- local version_output = vim .system ({ " git" , " describe" , " --tags" , " --always" }, { cwd = parent_dir }):wait ()
149+ local version_output = vim .system ({ " git" , " describe" , " --tags" , " --always" }, { cwd = state . settings . root_path }):wait ()
122150 local version = version_output .code == 0 and vim .trim (version_output .stdout ) or " unknown"
123151
124152 local ldflags = string.format (" -X main.Version=%s" , version )
125153 local res = vim
126154 .system (
127- { " go" , " build" , " -ldflags" , ldflags , " -o" , bin_name },
155+ { " go" , " build" , " -buildvcs=false " , " - ldflags" , ldflags , " -o" , state . settings . server . binary },
128156 { cwd = state .settings .root_path .. u .path_separator .. " cmd" }
129157 )
130158 :wait ()
@@ -133,6 +161,12 @@ M.build = function(override)
133161 u .notify (string.format (" Failed to install with status code %d:\n %s" , res .code , res .stderr ), vim .log .levels .ERROR )
134162 return false
135163 end
164+
165+ local Path = require (" plenary.path" )
166+ local src = Path :new (state .settings .root_path .. u .path_separator .. " cmd" .. u .path_separator .. " config" )
167+ local dest = Path :new (bin_folder .. u .path_separator .. " config" )
168+ src :copy ({ destination = dest , recursive = true , override = true })
169+
136170 u .notify (" Installed successfully!" , vim .log .levels .INFO )
137171 return true
138172end
@@ -185,7 +219,7 @@ M.get_version = function(callback)
185219 local version_output = vim .system ({ " git" , " describe" , " --tags" , " --always" }, { cwd = parent_dir }):wait ()
186220 local plugin_version = version_output .code == 0 and vim .trim (version_output .stdout ) or " unknown"
187221
188- local args = { " -s" , " -X" , " GET" , string.format (" localhost:%s/version" , state .settings .port ) }
222+ local args = { " -s" , " -X" , " GET" , string.format (" localhost:%s/version" , state .settings .server . port ) }
189223
190224 -- We call the "/version" endpoint here instead of through the regular jobs pattern because earlier versions of the plugin
191225 -- may not have it. We handle a 404 as an "unknown" version error.
0 commit comments