forked from manateelazycat/lsp-bridge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lsp-bridge-lsp-installer.el
317 lines (285 loc) · 12.6 KB
/
lsp-bridge-lsp-installer.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
;;; lsp-bridge-lsp-installer.el --- LSP server installer -*- lexical-binding: t -*-
;; Filename: lsp-bridge-lsp-installer.el
;; Description: LSP server installer
;; Author: Andy Stewart <[email protected]>
;; Maintainer: Andy Stewart <[email protected]>
;; Copyright (C) 2022, Andy Stewart, all rights reserved.
;; Created: 2022-09-27 09:28:09
;; Version: 0.1
;; Last-Updated: 2022-09-27 09:28:09
;; By: Andy Stewart
;; URL: https://www.github.org/manateelazycat/lsp-bridge-lsp-installer
;; Keywords:
;; Compatibility: GNU Emacs 28.1
;;
;; Features that might be required by this library:
;;
;;
;;
;;; This file is NOT part of GNU Emacs
;;; License
;;
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 3, or (at your option)
;; any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program; see the file COPYING. If not, write to
;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth
;; Floor, Boston, MA 02110-1301, USA.
;;; Commentary:
;;
;; LSP server installer
;;
;;; Installation:
;;
;; Put lsp-bridge-lsp-installer.el to your load-path.
;; The load-path is usually ~/elisp/.
;; It's set in your ~/.emacs like this:
;; (add-to-list 'load-path (expand-file-name "~/elisp"))
;;
;; And the following to your ~/.emacs startup file.
;;
;; (require 'lsp-bridge-lsp-installer)
;;
;; No need more.
;;; Customize:
;;
;;
;;
;; All of the above can customize by:
;; M-x customize-group RET lsp-bridge-lsp-installer RET
;;
;;; Change log:
;;
;; 2022/09/27
;; * First released.
;;
;;; Acknowledgements:
;;
;;
;;
;;; TODO
;;
;;
;;
;;; Require
(require 'cl-lib)
;;; Code:
(defun lsp-bridge-install-omnisharp ()
(interactive)
(let* ((to-append (if (string= lsp-bridge-csharp-lsp-server "omnisharp-dotnet")
(cond
((eq system-type 'windows-nt) "omnisharp-win-x64-net6.0.zip")
((eq system-type 'darwin)
(if (eq (car (split-string system-configuration "-")) 'aarch64)
"omnisharp-osx-arm64-net6.0.zip"
"omnisharp-osx-x64-net6.0.zip"))
(t "omnisharp-linux-x64-net6.0.zip"))
"omnisharp-mono.zip"))
(url (concat "https://github.com/OmniSharp/omnisharp-roslyn/releases/latest/download/" to-append))
(down-des (if (eq system-type 'windows-nt)
(substitute-in-file-name (concat "\\$USERPROFILE\\AppData\\Local\\Temp\\" to-append))
"/tmp/omnisharp-mono.zip"))
(install-des (if (eq system-type 'windows-nt)
(expand-file-name (format "%s.cache/omnisharp/" user-emacs-directory))
"~/.emacs.d/.cache/omnisharp/"
(format "%s.cache/omnisharp/" user-emacs-directory))))
(url-copy-file url down-des 1)
(unless (file-directory-p install-des)
(make-directory install-des t))
(call-process-shell-command (format "%s -xf %s -C %s" "tar" down-des install-des))
(call-process-shell-command (format "chmod +x %s" (format "%sOmniSharp" install-des)))
))
(defconst tabnine-bridge--version-tempfile "version")
(defcustom tabnine-bridge-binaries-folder (expand-file-name "~/.TabNine")
"Path to TabNine binaries folder.
`lsp-bridge-install-tabnine' will use this directory."
:type 'string)
(defcustom tabnine-bridge-install-static-binary (file-exists-p "/etc/nixos/hardware-configuration.nix")
"Whether to install the musl-linked static binary instead of
the standard glibc-linked dynamic binary.
Only useful on GNU/Linux. Automatically set if NixOS is detected."
:type 'boolean)
(defcustom codeium-download-url-prefix "https://github.com/Exafunction/codeium/releases/download/language-server-v"
"codeium download url prefix"
:type 'string)
(defun tabnine-bridge--get-target ()
"Return TabNine's system configuration. Used for finding the correct binary."
(let* ((system-architecture (car (split-string system-configuration "-")))
(tabnine-architecture
(cond
((or (string= system-architecture "aarch64")
(and (eq system-type 'darwin)
(string= system-architecture "x86_64")
;; Detect AArch64 running x86_64 Emacs
(string= (shell-command-to-string "arch -arm64 uname -m") "arm64\n")))
"aarch64")
((or (string= system-architecture "arm")
(and (eq system-type 'darwin)
(string= system-architecture "x86_64")
;; Detect AArch64 running x86_64 Emacs
(string= (shell-command-to-string "arch -arm64 uname -m") "arm64\n")))
"aarch64")
((string= system-architecture "x86_64")
"x86_64")
((string-match system-architecture "i.86")
"i686")
(t
(error "Unknown or unsupported architecture %s" system-architecture))))
(os
(cond
((or (eq system-type 'ms-dos)
(eq system-type 'windows-nt)
(eq system-type 'cygwin))
"pc-windows-gnu")
((or (eq system-type 'darwin))
"apple-darwin")
(tabnine-bridge-install-static-binary
"unknown-linux-musl")
(t
"unknown-linux-gnu"))))
(concat tabnine-architecture "-" os)))
(defun tabnine-bridge--get-exe ()
"Return TabNine's binary file name. Used for finding the correct binary."
(cond
((or (eq system-type 'ms-dos)
(eq system-type 'windows-nt)
(eq system-type 'cygwin))
"TabNine.exe")
(t
"TabNine")))
(defun lsp-bridge-install-tabnine ()
"Install TabNine binary into `tabnine-bridge-binaries-folder'."
(interactive)
(let ((version-tempfile (concat
(file-name-as-directory
tabnine-bridge-binaries-folder)
tabnine-bridge--version-tempfile))
(target (tabnine-bridge--get-target))
(exe (tabnine-bridge--get-exe))
(binaries-dir tabnine-bridge-binaries-folder))
(message version-tempfile)
(message "Getting current version...")
(make-directory (file-name-directory version-tempfile) t)
(url-copy-file "https://update.tabnine.com/bundles/version" version-tempfile t)
(let ((version (string-trim (with-temp-buffer (insert-file-contents version-tempfile) (buffer-string)))))
(when (= (length version) 0)
(error "TabNine installation failed. Please try again"))
(message "Current version is %s" version)
(let* ((url (concat "https://update.tabnine.com/bundles/" version "/" target "/TabNine.zip"))
(version-directory (file-name-as-directory
(concat
(file-name-as-directory
(concat (file-name-as-directory binaries-dir) version)))))
(target-directory (file-name-as-directory (concat version-directory target) ))
(bundle-path (concat version-directory (format "%s.zip" target)))
(target-path (concat target-directory exe)))
(message "Installing at %s. Downloading %s ..." target-path url)
(make-directory target-directory t)
(url-copy-file url bundle-path t)
(condition-case ex
(let ((default-directory target-directory))
(if (or (eq system-type 'ms-dos)
(eq system-type 'windows-nt)
(eq system-type 'cygwin))
(shell-command (format "tar -xf %s" (expand-file-name bundle-path)))
(shell-command (format "unzip -o %s -d %s"
(expand-file-name bundle-path)
(expand-file-name target-directory)))))
('error
(error "Unable to unzip automatically. Please go to [%s] and unzip the content of [%s] into [%s/]."
(expand-file-name version-directory)
(file-name-nondirectory bundle-path)
(file-name-sans-extension (file-name-nondirectory bundle-path)))))
(dolist (filename (directory-files target-directory))
(unless (member filename '("." ".."))
(set-file-modes (concat target-directory filename) (string-to-number "744" 8))))
(delete-file bundle-path)
(delete-file version-tempfile)
(message "TabNine installation complete.")))))
(defcustom codeium-bridge-folder (expand-file-name "~/.codeium")
"Path to Codeium binary folder.
`lsp-bridge-install-update-codeium' will use this directory."
:type 'string)
(defvar codeium-bridge-binary-version
(let ((version-file (concat (file-name-as-directory codeium-bridge-folder) "version")))
(string-trim (if (file-exists-p version-file)
(with-temp-buffer
(insert-file-contents version-file)
(buffer-string))
""))))
(defun lsp-bridge-codeium-auth ()
"Getting auth token for Codeium."
(interactive)
(lsp-bridge-call-async "codeium_auth"))
(defun lsp-bridge-codeium-input-auth-token (auth-token)
"Inputting auth token for Codeium."
(interactive "sAuth token: ")
(lsp-bridge-call-async "codeium_get_api_key" auth-token))
(defun lsp-bridge-install-update-codeium ()
"Install or update Codeium binary in `codeium-bridge-folder'."
(interactive)
(let* ((system-arch (car (split-string system-configuration "-")))
(arch (cond ((string= system-arch "x86_64")
"x64")
(t "arm")))
(platform (cond ((eq system-type 'gnu/linux)
"linux")
((or (eq system-type 'ms-dos)
(eq system-type 'windows-nt)
(eq system-type 'cygwin))
"windows")
((eq system-type 'darwin)
"macos")))
(extension (if (or (eq system-type 'ms-dos)
(eq system-type 'windows-nt)
(eq system-type 'cygwin))
"exe.gz"
"gz"))
(binary-dir (file-name-as-directory codeium-bridge-folder))
(version-file (concat binary-dir "version"))
(version (with-current-buffer (url-retrieve-synchronously "https://api.github.com/repos/Exafunction/codeium/releases/latest")
(re-search-forward "^{")
(goto-char (1- (point)))
(substring (car (last (split-string (gethash "name" (json-parse-buffer)) "-"))) 1)))
(file-name (format "language_server_%s_%s.%s"
platform
arch
extension))
(compress-file (concat binary-dir file-name))
(binary-file (string-trim-right compress-file "\\.gz"))
;; Binary file after rename
(last-binary-file (if (or (eq system-type 'ms-dos)
(eq system-type 'windows-nt)
(eq system-type 'cygwin))
(concat binary-dir "language_server.exe")
(concat binary-dir "language_server")))
(download-url (concat codeium-download-url-prefix version "/" file-name)))
(make-directory binary-dir t)
(if (string= version codeium-bridge-binary-version)
(message "Don't need update.")
(url-copy-file download-url compress-file t)
(shell-command (format "gzip -d %s" compress-file))
(rename-file binary-file last-binary-file t)
(shell-command (format "chmod +x %s" last-binary-file))
(with-temp-buffer
(insert version)
(write-region (point-min) (point-max) version-file nil nil nil t))
(setq codeium-bridge-binary-version version)
(message "Done."))))
(defun lsp-bridge-copilot-login ()
(interactive)
(lsp-bridge-call-async "copilot_login"))
(defun lsp-bridge-copilot-logout ()
(interactive)
(lsp-bridge-call-async "copilot_logout"))
(defun lsp-bridge-copilot-status ()
(interactive)
(lsp-bridge-call-async "copilot_status"))
(provide 'lsp-bridge-lsp-installer)
;;; lsp-bridge-lsp-installer.el ends here