Enable "run_file" for Sonic-Pi 2.10

Hello Sonic-Pi community :slight_smile:

Today, I hit the character limit for a buffer on Sonic-Pi and it is a real showstopper for me as I need way more complex code for my instrumentation etc.
So I looked up the “run_file” command as has been said from Robin Newman and others here in the forum.
The problem is, it is only available for Sonic-Pi 2.11 and upwards. As I’m using Debian, Sonic-Pi is still on version 2.10 and building newer versions just for this small extension is a nuisance.
So, I looked up this really small function in Sonic-Pi 2.11 and backported it to Sonic-Pi 2.10. Hope, I did nothing wrong here, at least it worked well for me :slight_smile:
Maybe others will need this, too, and don’t want the hassle of building a newer version just because of more complex songs etc.

So here is the patch for Debian 9 and maybe other Sonic-Pi distributions:
— /usr/lib/sonic-pi/server/sonicpi/lib/sonicpi/lang/core.rb.orig 2016-04-15 15:17:08.000000000 +0200
+++ /usr/lib/sonic-pi/server/sonicpi/lib/sonicpi/lang/core.rb 2019-03-30 23:44:08.425964723 +0100
@@ -3115,6 +3115,24 @@
examples: ["
load_example :rerezzed # will replace content of current buffer with the rerezzed example"]

  •  def run_file(path)
    
  •    path = File.expand_path(path.to_s)
    
  •    raise "Error - unable to run file - no file found with path: #{path}" unless File.exist?(path)
    
  •    __spider_eval(File.read(path))
    
  •  end
    
  •  doc name:           :run_file,
    
  •      introduced:     Version.new(2,11,0),
    
  •      summary:        "Evaluate the contents of the file as a new Run",
    
  •      args:           [[:filename, :path]],
    
  •      returns:        nil,
    
  •      opts:           nil,
    
  •      accepts_block:  false,
    
  •      doc:            "Reads the full contents of the file with `path` and executes it in a new Run. This works as if the code in the file was in a buffer and Run button was pressed.",
    
  •      examples: ["
    

+run_file “~/path/to/sonic-pi-code.rb” #=> will run the contents of this file"]
+
+
def __on_thread_death(&block)
gc_jobs = Thread.current.thread_variable_get(:sonic_pi__not_inherited__spider_in_thread_gc_jobs) || []
gc_jobs << block

Argh, forgot formatting, here in a better variant:

--- /usr/lib/sonic-pi/server/sonicpi/lib/sonicpi/lang/core.rb.orig	2016-04-15 15:17:08.000000000 +0200
+++ /usr/lib/sonic-pi/server/sonicpi/lib/sonicpi/lang/core.rb	2019-03-30 23:44:08.425964723 +0100
@@ -3115,6 +3115,24 @@
           examples:       ["
 load_example :rerezzed # will replace content of current buffer with the rerezzed example"]
 
+
+      def run_file(path)
+        path = File.expand_path(path.to_s)
+        raise "Error - unable to run file - no file found with path: #{path}" unless File.exist?(path)
+        __spider_eval(File.read(path))
+      end
+      doc name:           :run_file,
+          introduced:     Version.new(2,11,0),
+          summary:        "Evaluate the contents of the file as a new Run",
+          args:           [[:filename, :path]],
+          returns:        nil,
+          opts:           nil,
+          accepts_block:  false,
+          doc:            "Reads the full contents of the file with `path` and executes it in a new Run. This works as if the code in the file was in a buffer and Run button was pressed.",
+          examples: ["
+run_file \"~/path/to/sonic-pi-code.rb\" #=> will run the contents of this file"]
+
+
       def __on_thread_death(&block)
         gc_jobs = Thread.current.thread_variable_get(:sonic_pi__not_inherited__spider_in_thread_gc_jobs) || []
         gc_jobs << block
1 Like