forked from lyp-packages/lys
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathext.rb
More file actions
89 lines (74 loc) · 2.08 KB
/
ext.rb
File metadata and controls
89 lines (74 loc) · 2.08 KB
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
Lyp.install_extension(__FILE__)
require 'fileutils'
require 'net/telnet'
SERVER_CODE = <<EOF
\\version "2.19.37"
\\require "lys"
#(lys:start-server)
EOF
module Lyp::Lilypond
LYS_SERVER_PATH = File.join(Lyp::TMP_ROOT, "lys-server.ly")
class << self
alias_method :orig_parse_lilypond_arg, :parse_lilypond_arg
def parse_lilypond_arg(arg, argv, argv_clean, options)
case arg
when '-s', '--server'
options[:server] = true
else
orig_parse_lilypond_arg(arg, argv, argv_clean, options)
end
end
alias_method :orig_invoke, :invoke
def invoke(argv, opts = {})
if opts[:server]
invoke_with_server(argv, opts)
else
orig_invoke(argv, opts)
end
end
def invoke_with_server(argv, opts)
session = setup_server_session
cmd = "(lys:compile-file #{FileUtils.pwd.inspect} #{argv.map {|a| a.inspect}.join(" ")})"
session.cmd(cmd) {|c| puts c.strip}
session.close
end
def setup_server_session
port = allocate_server_port(current_lilypond)
server = start_server_session(port)
msg = server.waitfor(/>/)
puts msg.lines[0].chomp
server
end
def allocate_server_port(lilypond_path)
1225
end
def start_server_session(port)
Net::Telnet::new("Host" => "localhost", "Port" => port)
rescue Errno::ECONNREFUSED
start_server(port)
Net::Telnet::new("Host" => "localhost", "Port" => port)
end
def start_server(port)
compile([LYS_SERVER_PATH], mode: :spawn, spawn_opts: {
[:out, :err] => "/tmp/lilypond-server-#{port}.log",
[:in] => "/dev/null"
})
sleep 0.1
wait_for_server(port)
end
def wait_for_server(port)
loop do
begin
session = Net::Telnet::new("Host" => "localhost", "Port" => port)
session.close
break
rescue Errno::ECONNREFUSED
sleep 0.1
end
end
end
end
end
unless File.exists?(Lyp::Lilypond::LYS_SERVER_PATH)
File.open(Lyp::Lilypond::LYS_SERVER_PATH, "w+") {|f| f << SERVER_CODE}
end