Skip to content

Commit 881d8ce

Browse files
committed
First impl
1 parent e1760c2 commit 881d8ce

13 files changed

+337
-0
lines changed

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.exe binary

LICENSE

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
Copyright 2022 vim-denops
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy
4+
of this software and associated documentation files (the "Software"), to deal
5+
in the Software without restriction, including without limitation the rights
6+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
copies of the Software, and to permit persons to whom the Software is
8+
furnished to do so, subject to the following conditions:
9+
10+
The above copyright notice and this permission notice shall be included in all
11+
copies or substantial portions of the Software.
12+
13+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19+
SOFTWARE.
20+

README.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# 🎃 denops-shared-server
2+
3+
This is a helper Vim plugin to install/uninstall [denops.vim][]'s shared server.
4+
5+
[denops.vim]: https://github.com/vim-denops/denops.vim
6+
7+
## Usage
8+
9+
Call `denops_shared_server#install()` function to install denops-shared-server
10+
on the system like
11+
12+
```vim
13+
:call denops_shared_server#install()
14+
```
15+
16+
And uninstall the server with `denops_shared_server#uninstall()` like
17+
18+
```vim
19+
:call denops_shared_server#uninstall()
20+
```
21+
22+
This plugin uses the following methods to install the shared server on the system.
23+
24+
| OS | Method |
25+
| ------- | -------------------------------------------------- |
26+
| Windows | Bundled [WinSW v2](https://github.com/winsw/winsw) |
27+
| macOS | System's `launchctl` (launchd) |
28+
| Linux | System's `systemctl` (systemd) |
29+
30+
Note that Windows user requires .NET Framework 4.6.1 (preinstalled since Windows 10 November Update version 1511).
31+
32+
## License
33+
34+
The code follows MIT license written in [LICENSE](./LICENSE). Contributors need
35+
to agree that any modifications sent in this repository follow the license.

autoload/denops_shared_server.vim

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
function! denops_shared_server#install() abort
2+
if !exists('g:denops_server_addr')
3+
call denops#util#error('No denops shared server address (g:denops_server_addr) is given.')
4+
return
5+
endif
6+
let [hostname, port] = s:parse_server_addr(g:denops_server_addr)
7+
let options = {
8+
\ 'deno': exepath(g:denops#deno),
9+
\ 'script': denops#util#script_path('@denops-private/cli.ts'),
10+
\ 'hostname': hostname,
11+
\ 'port': port,
12+
\}
13+
if executable('launchctl')
14+
call denops_shared_server#launchctl#install(options)
15+
elseif executable('systemctl')
16+
call denops_shared_server#systemctl#install(options)
17+
elseif has('win32')
18+
call denops_shared_server#winsw#install(options)
19+
else
20+
call denops#util#error('This platform is not supported. Please configure denops-shared-server manually.')
21+
return
22+
endif
23+
call denops#util#info('wait 1 second for the shared server startup...')
24+
sleep 1
25+
call denops#util#info('connect to the shared server')
26+
call denops#server#connect()
27+
call denops#util#info('stop the local server')
28+
call denops#server#stop()
29+
endfunction
30+
31+
function! denops_shared_server#uninstall() abort
32+
if executable('launchctl')
33+
call denops_shared_server#launchctl#uninstall()
34+
elseif executable('systemctl')
35+
call denops_shared_server#systemctl#uninstall()
36+
elseif has('win32')
37+
call denops_shared_server#winsw#uninstall()
38+
else
39+
call denops#util#error('This platform is not supported. Please configure denops-shared-server manually.')
40+
return
41+
endif
42+
endfunction
43+
44+
function! denops_shared_server#_render(template, context) abort
45+
let content = join(a:template, "\n")
46+
for [key, value] in items(a:context)
47+
let content = substitute(content, printf('{{%s}}', key), escape(value, '\'), 'g')
48+
endfor
49+
return split(content, "\n", v:true)
50+
endfunction
51+
52+
function! s:parse_server_addr(addr) abort
53+
let parts = split(a:addr, ':', v:true)
54+
if len(parts) isnot# 2
55+
throw printf('[denops-shared-server] Server address must follow `{hostname}:{port}` format but `%s` is given', a:addr)
56+
endif
57+
return [parts[0], parts[1]]
58+
endfunction
641 KB
Binary file not shown.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>Label</key>
6+
<string>{{name}}</string>
7+
8+
<key>ProgramArguments</key>
9+
<array>
10+
<string>{{deno}}</string>
11+
<string>run</string>
12+
<string>-A</string>
13+
<string>--no-check</string>
14+
<string>{{script}}</string>
15+
<string>--hostname</string>
16+
<string>{{hostname}}</string>
17+
<string>--port</string>
18+
<string>{{port}}</string>
19+
</array>
20+
21+
<key>RunAtLoad</key>
22+
<true/>
23+
<key>KeepAlive</key>
24+
<true/>
25+
</dict>
26+
</plist>
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
let s:file = expand('<sfile>:p')
2+
let s:name = "io.github.vim-denops.LaunchAtLogin"
3+
let s:plist_file = expand(printf('~/Library/LaunchAgents/%s.plist', s:name))
4+
let s:template_file = printf('%s/launchctl.template', fnamemodify(s:file, ':h'))
5+
6+
function! denops_shared_server#launchctl#install(options) abort
7+
let content = denops_shared_server#_render(readfile(s:template_file, 'b'), {
8+
\ 'name': s:name,
9+
\ 'home': expand('~'),
10+
\ 'deno': a:options.deno,
11+
\ 'script': a:options.script,
12+
\ 'hostname': a:options.hostname,
13+
\ 'port': a:options.port,
14+
\})
15+
call denops#util#info(printf('create the plist `%s`', s:plist_file))
16+
call mkdir(fnamemodify(s:plist_file, ':h'), 'p')
17+
call writefile(content, s:plist_file, 'b')
18+
19+
call denops#util#info(printf('unload the plist `%s`', s:plist_file))
20+
call system(printf('launchctl unload %s', s:plist_file))
21+
22+
call denops#util#info(printf('load the plist `%s`', s:plist_file))
23+
echo system(printf('launchctl load -w %s', s:plist_file))
24+
endfunction
25+
26+
function! denops_shared_server#launchctl#uninstall() abort
27+
call denops#util#info(printf('unload the plist `%s`', s:plist_file))
28+
echo system(printf('launchctl unload %s', s:plist_file))
29+
30+
call denops#util#info(printf('delete the plist `%s`', s:plist_file))
31+
call delete(s:plist_file)
32+
endfunction
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[Unit]
2+
Description = Denops shared server
3+
4+
[Service]
5+
Type=simple
6+
Restart = always
7+
ExecStart={{deno}} run -A --no-check {{script}} --hostname {{hostname}} --port {{port}}
8+
9+
[Install]
10+
WantedBy=default.target
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
let s:file = expand('<sfile>:p')
2+
let s:name = "denops-shared-server"
3+
let s:unit_file = expand(printf('~/.config/systemd/user/%s.service', s:name))
4+
let s:template_file = printf('%s/systemctl.template', fnamemodify(s:file, ':h'))
5+
6+
function! denops_shared_server#systemctl#install(options) abort
7+
let content = denops_shared_server#_render(readfile(s:template_file, 'b'), {
8+
\ 'deno': a:options.deno,
9+
\ 'script': a:options.script,
10+
\ 'hostname': a:options.hostname,
11+
\ 'port': a:options.port,
12+
\})
13+
call denops#util#info(printf('create the unit file `%s`', s:unit_file))
14+
call mkdir(fnamemodify(s:unit_file, ':h'), 'p')
15+
call writefile(content, s:unit_file, 'b')
16+
17+
call denops#util#info(printf('enable the unit `%s`', s:name))
18+
echo system(printf('systemctl --user enable %s.service', s:name))
19+
20+
call denops#util#info(printf('start the unit `%s`', s:name))
21+
echo system(printf('systemctl --user start %s.service', s:name))
22+
endfunction
23+
24+
function! denops_shared_server#systemctl#uninstall() abort
25+
call denops#util#info(printf('stop the unit `%s`', s:name))
26+
echo system(printf('systemctl --user stop %s.service', s:name))
27+
28+
call denops#util#info(printf('disable the unit `%s`', s:name))
29+
echo system(printf('systemctl --user disable %s.service', s:name))
30+
31+
call denops#util#info(printf('delete the unit file `%s`', s:unit_file))
32+
call delete(s:unit_file)
33+
34+
call denops#util#info('daemon reload')
35+
echo system('systemctl --user daemon-reload')
36+
37+
call denops#util#info('reset failed')
38+
echo system('systemctl --user reset-failed')
39+
endfunction
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<service>
2+
<id>denopssharedserver</id>
3+
<name>denops-shared-server</name>
4+
<description>denops.vim shared server</description>
5+
<executable>{{deno}}</executable>
6+
<arguments>run -A --no-check {{script}} --hostname {{hostname}} --port {{port}}</arguments>
7+
<env name="PATH" value="{{path}}" />
8+
<env name="HOME" value="{{home}}" />
9+
<onfailure action="restart" />
10+
</service>

0 commit comments

Comments
 (0)