|
| 1 | +# This file provides a compatibility layer for version 0.12.0 so that |
| 2 | +# all the Colsole methods (say, resay...) and ExecHandler methods |
| 3 | +# (run, run_bg...) are still available |
| 4 | +# Simply require 'runfile/compatibility' to have the same functionality |
| 5 | +# as older versions. |
| 6 | +# |
| 7 | +# Notes: |
| 8 | +# - You need to have the colsole gem installed |
| 9 | +# - Requiring this file also includes it (see the end of the file). |
| 10 | +# - This functinality will be removed in future versions |
| 11 | +# - More info: https://github.com/DannyBen/runfile/pull/46 |
| 12 | +require 'colsole' |
| 13 | + |
| 14 | +module Runfile |
| 15 | + module Compatibilty |
| 16 | + include Colsole |
| 17 | + |
| 18 | + # Run a command, wait until it is done and continue |
| 19 | + def run(cmd) |
| 20 | + cmd = @before_run_block.call(cmd) if @before_run_block |
| 21 | + return false unless cmd |
| 22 | + say "!txtgrn!> #{cmd}" unless Runfile.quiet |
| 23 | + system cmd |
| 24 | + @after_run_block.call(cmd) if @after_run_block |
| 25 | + end |
| 26 | + |
| 27 | + # Run a command, wait until it is done, then exit |
| 28 | + def run!(cmd) |
| 29 | + cmd = @before_run_block.call(cmd) if @before_run_block |
| 30 | + return false unless cmd |
| 31 | + say "!txtgrn!> #{cmd}" unless Runfile.quiet |
| 32 | + exec cmd |
| 33 | + end |
| 34 | + |
| 35 | + # Run a command in the background, optionally log to a log file and save |
| 36 | + # the process ID in a pid file |
| 37 | + def run_bg(cmd, pid: nil, log: '/dev/null') |
| 38 | + cmd = @before_run_block.call(cmd) if @before_run_block |
| 39 | + return false unless cmd |
| 40 | + full_cmd = "exec #{cmd} >#{log} 2>&1" |
| 41 | + say "!txtgrn!> #{full_cmd}" unless Runfile.quiet |
| 42 | + process = IO.popen "exec #{cmd} >#{log} 2>&1" |
| 43 | + File.write pidfile(pid), process.pid if pid |
| 44 | + @after_run_block.call(cmd) if @after_run_block |
| 45 | + return process.pid |
| 46 | + end |
| 47 | + |
| 48 | + # Stop a command started with 'run_bg'. Provide the name of he pid file you |
| 49 | + # used in 'run_bg' |
| 50 | + def stop_bg(pid) |
| 51 | + file = pidfile(pid) |
| 52 | + if File.exist? file |
| 53 | + pid = File.read file |
| 54 | + File.delete file |
| 55 | + run "kill -s TERM #{pid}" |
| 56 | + else |
| 57 | + say "!txtred!PID file not found." unless Runfile.quiet |
| 58 | + end |
| 59 | + end |
| 60 | + |
| 61 | + # Set a block to be called before each run |
| 62 | + def before_run(&block) |
| 63 | + @before_run_block = block |
| 64 | + end |
| 65 | + |
| 66 | + # Set a block to be called after each run |
| 67 | + def after_run(&block) |
| 68 | + @after_run_block = block |
| 69 | + end |
| 70 | + |
| 71 | + private |
| 72 | + |
| 73 | + def pid_dir |
| 74 | + defined?(Runfile.pid_dir) ? Runfile.pid_dir : nil |
| 75 | + end |
| 76 | + |
| 77 | + def pidfile(pid) |
| 78 | + pid_dir ? "#{pid_dir}/#{pid}.pid" : "#{pid}.pid" |
| 79 | + end |
| 80 | + |
| 81 | + end |
| 82 | +end |
| 83 | + |
| 84 | +include Runfile::Compatibilty |
0 commit comments