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,59 @@ 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 (
115+ string.format (" The user-provided server path (%s) does not exist." , state .settings .server .binary ),
116+ vim .log .levels .ERROR
117+ )
118+ end
119+ return
120+ end
121+
122+ -- If the user did not provide a path, we build it and place it in either the data path, or the
123+ -- first writable path we find in the runtime.
124+ local datapath = vim .fn .stdpath (" data" )
125+ local runtimepath = vim .api .nvim_list_runtime_paths ()
126+ table.insert (runtimepath , 1 , datapath )
127+
128+ local bin_folder
129+ for _ , path in ipairs (runtimepath ) do
130+ local ok , err = vim .loop .fs_access (path , " w" )
131+ if err == nil and ok ~= nil and ok then
132+ bin_folder = path .. u .path_separator .. " gitlab.nvim" .. u .path_separator .. " bin"
133+ if vim .fn .mkdir (bin_folder , " p" ) == 1 then
134+ state .settings .server .binary = bin_folder .. u .path_separator .. " server"
135+ break
136+ end
137+ end
138+ end
109139
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
140+ if state .settings .server .binary == nil then
141+ u .notify (" Could not find a writable folder in the runtime path to save the server to." , vim .log .levels .ERROR )
142+ return
143+ end
113144
114145 if not override then
115- local binary_exists = vim .loop .fs_stat (state .settings .bin )
146+ local binary_exists = vim .loop .fs_stat (state .settings .server . binary )
116147 if binary_exists ~= nil then
117148 return
118149 end
119150 end
120151
121- local version_output = vim .system ({ " git" , " describe" , " --tags" , " --always" }, { cwd = parent_dir }):wait ()
152+ local version_output = vim
153+ .system ({ " git" , " describe" , " --tags" , " --always" }, { cwd = state .settings .root_path })
154+ :wait ()
122155 local version = version_output .code == 0 and vim .trim (version_output .stdout ) or " unknown"
123156
124157 local ldflags = string.format (" -X main.Version=%s" , version )
125158 local res = vim
126159 .system (
127- { " go" , " build" , " -ldflags" , ldflags , " -o" , bin_name },
160+ { " go" , " build" , " -buildvcs=false " , " - ldflags" , ldflags , " -o" , state . settings . server . binary },
128161 { cwd = state .settings .root_path .. u .path_separator .. " cmd" }
129162 )
130163 :wait ()
@@ -133,6 +166,12 @@ M.build = function(override)
133166 u .notify (string.format (" Failed to install with status code %d:\n %s" , res .code , res .stderr ), vim .log .levels .ERROR )
134167 return false
135168 end
169+
170+ local Path = require (" plenary.path" )
171+ local src = Path :new (state .settings .root_path .. u .path_separator .. " cmd" .. u .path_separator .. " config" )
172+ local dest = Path :new (bin_folder .. u .path_separator .. " config" )
173+ src :copy ({ destination = dest , recursive = true , override = true })
174+
136175 u .notify (" Installed successfully!" , vim .log .levels .INFO )
137176 return true
138177end
@@ -185,7 +224,7 @@ M.get_version = function(callback)
185224 local version_output = vim .system ({ " git" , " describe" , " --tags" , " --always" }, { cwd = parent_dir }):wait ()
186225 local plugin_version = version_output .code == 0 and vim .trim (version_output .stdout ) or " unknown"
187226
188- local args = { " -s" , " -X" , " GET" , string.format (" localhost:%s/version" , state .settings .port ) }
227+ local args = { " -s" , " -X" , " GET" , string.format (" localhost:%s/version" , state .settings .server . port ) }
189228
190229 -- We call the "/version" endpoint here instead of through the regular jobs pattern because earlier versions of the plugin
191230 -- may not have it. We handle a 404 as an "unknown" version error.
0 commit comments