sonic-pi-cli 0.0.4 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +5 -5
  2. data/bin/sonic_pi +6 -4
  3. data/lib/sonic_pi.rb +35 -12
  4. metadata +7 -8
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 04134a6ca8a18fa541d125769286b79471e73358
4
- data.tar.gz: a0755613302da38527122b885ae2796c92a6e7be
2
+ SHA256:
3
+ metadata.gz: 69fb6262128d0b144aeb03a3135b5fa19eeed8d52a3fefab4f5c6317356983cc
4
+ data.tar.gz: e3c6d4c4ccc9cb99675dd537d7f08ae5d9b00c0b31639ea4ffba1f2cf28cb17c
5
5
  SHA512:
6
- metadata.gz: f0470ad219ef6f84ba7021ad0872a1c3ad31be0f661c06c631ba754cbaf1e9cf60af4865ca22d63bc0fcee21fa4995a8255bea4eadf588081ef36d95b45682f1
7
- data.tar.gz: 48a73410ed6d6e86780b8a3e11d0dbf3da8bdf4baaadeae1b278f7696b92912f92c6bab578f96c65b2d6d53701da7cef81a63dacf2bfe7bcbb0748764f43a5ea
6
+ metadata.gz: 99d188d6b33023baf1cab49f51615b203f3dd0b0c6c436a4893027baf2e7a95aa5b602481e7b74c58e16e65c51b657172c38ae16bd8c76f31e232fbebef42a0b
7
+ data.tar.gz: 77010bf05ef77eec503f9eb0c31a4acbed999cfc25bce50e63968031a708c220e96e6c8ffc97acb77ecef75f3715ce7c69e8ead752361b8e7c95a7c8ecd0119d
@@ -1,6 +1,6 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- require 'sonic_pi'
3
+ require "sonic_pi"
4
4
 
5
5
  def stdin
6
6
  unless STDIN.tty?
@@ -45,14 +45,16 @@ end
45
45
  def run
46
46
  app = SonicPi.new
47
47
 
48
- app.test_connection!
49
-
50
48
  case args_and_stdin
51
- when '--help', ''
49
+ when '--help', '-h', ''
52
50
  print_help
53
51
  when 'stop'
52
+ app.test_connection!
53
+
54
54
  app.stop
55
55
  else
56
+ app.test_connection!
57
+
56
58
  app.run(args_and_stdin)
57
59
  end
58
60
  end
@@ -4,25 +4,30 @@ require 'osc-ruby'
4
4
  require 'securerandom'
5
5
 
6
6
  class SonicPi
7
+ PORT_LOG_REGEX = Regexp.compile(/Listen port:\s+(?<port>\d+)/)
8
+
9
+ def initialize(port=nil)
10
+ @port = port || find_port
11
+ end
12
+
7
13
  RUN_COMMAND = "/run-code"
8
14
  STOP_COMMAND = "/stop-all-jobs"
9
15
  SERVER = 'localhost'
10
- PORT = 4557
11
16
  GUI_ID = 'SONIC_PI_CLI'
12
17
 
13
18
  def run(command)
14
- client.send(run_command(command))
19
+ send_command(RUN_COMMAND, command)
15
20
  end
16
21
 
17
22
  def stop
18
- client.send(stop_command)
23
+ send_command(STOP_COMMAND)
19
24
  end
20
25
 
21
26
  def test_connection!
22
27
  begin
23
- OSC::Server.new(PORT)
24
- abort("ERROR: Sonic Pi is not listening on #{PORT} - is it running?")
25
-
28
+ socket = UDPSocket.new
29
+ socket.bind(nil, @port)
30
+ abort("ERROR: Sonic Pi is not listening on #{@port} - is it running?")
26
31
  rescue
27
32
  # everything is good
28
33
  end
@@ -31,15 +36,33 @@ class SonicPi
31
36
  private
32
37
 
33
38
  def client
34
- client ||= OSC::Client.new(SERVER, PORT)
39
+ @client ||= OSC::Client.new(SERVER, @port)
35
40
  end
36
41
 
37
- def run_command(command)
38
- OSC::Message.new(RUN_COMMAND, GUI_ID, "#{command}")
42
+ def send_command(call_type, command=nil)
43
+ prepared_command = OSC::Message.new(call_type, GUI_ID, command)
44
+ client.send(prepared_command)
39
45
  end
40
46
 
41
- def stop_command
42
- OSC::Message.new(STOP_COMMAND, GUI_ID)
47
+ def find_port
48
+ port = 4557
49
+
50
+ begin
51
+ log_path = File.join(Dir.home, ".sonic-pi", "log", "server-output.log")
52
+
53
+ File.open(log_path, 'r') do |f|
54
+ port_log_entry =
55
+ f.each_line
56
+ .lazy
57
+ .map { |line| line[PORT_LOG_REGEX, "port"] }
58
+ .find { |match| !!match }
59
+
60
+ port = port_log_entry.to_i if port_log_entry
61
+ end
62
+ rescue Errno::ENOENT
63
+ # not to worry if the file doesn't exist
64
+ end
65
+
66
+ port
43
67
  end
44
68
  end
45
-
metadata CHANGED
@@ -1,29 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sonic-pi-cli
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nick Johnstone
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-09-21 00:00:00.000000000 Z
11
+ date: 2019-08-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: osc-ruby
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ">="
17
+ - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '0'
19
+ version: '1'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - ">="
24
+ - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '0'
26
+ version: '1'
27
27
  description: A simple command line interface for Sonic Pi
28
28
  email: ncwjohnstone@gmail.com
29
29
  executables:
@@ -52,8 +52,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
52
52
  - !ruby/object:Gem::Version
53
53
  version: '0'
54
54
  requirements: []
55
- rubyforge_project:
56
- rubygems_version: 2.2.2
55
+ rubygems_version: 3.0.3
57
56
  signing_key:
58
57
  specification_version: 4
59
58
  summary: Sonic Pi CLI