Skip to content

Commit

Permalink
Merge pull request #47 from DannyBen/add/compat
Browse files Browse the repository at this point in the history
Add compatibility layer
  • Loading branch information
DannyBen authored Sep 29, 2021
2 parents 2cbef03 + bb16a4d commit 57695ba
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 0 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,19 @@ Options:
- [User Guide](https://runfile.dannyb.co/)
- [Learn by Example](https://github.com/DannyBen/runfile/tree/master/examples#readme)

## Breaking changes in 0.12.0

Note that version 0.12.0 removes several commands from the DSL.
If you were using any of the Colsole commands (`say`, `resay`, etc.), or any of
the `run`, `run_bg` commands, you will have to adjust your Runfile since they are
no longer available.

For convenience, you can simply `require runfile/compatibility` at the top of
your Runfile, to still have this functionality included.

More info and alternative solutions in
[this pull request](https://github.com/DannyBen/runfile/pull/46).

## Contributing / Support

If you experience any issue, have a question or a suggestion, or if you wish
Expand Down
84 changes: 84 additions & 0 deletions lib/runfile/compatibility.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
# This file provides a compatibility layer for version 0.12.0 so that
# all the Colsole methods (say, resay...) and ExecHandler methods
# (run, run_bg...) are still available
# Simply require 'runfile/compatibility' to have the same functionality
# as older versions.
#
# Notes:
# - You need to have the colsole gem installed
# - Requiring this file also includes it (see the end of the file).
# - This functinality will be removed in future versions
# - More info: https://github.com/DannyBen/runfile/pull/46
require 'colsole'

module Runfile
module Compatibilty
include Colsole

# Run a command, wait until it is done and continue
def run(cmd)
cmd = @before_run_block.call(cmd) if @before_run_block
return false unless cmd
say "!txtgrn!> #{cmd}" unless Runfile.quiet
system cmd
@after_run_block.call(cmd) if @after_run_block
end

# Run a command, wait until it is done, then exit
def run!(cmd)
cmd = @before_run_block.call(cmd) if @before_run_block
return false unless cmd
say "!txtgrn!> #{cmd}" unless Runfile.quiet
exec cmd
end

# Run a command in the background, optionally log to a log file and save
# the process ID in a pid file
def run_bg(cmd, pid: nil, log: '/dev/null')
cmd = @before_run_block.call(cmd) if @before_run_block
return false unless cmd
full_cmd = "exec #{cmd} >#{log} 2>&1"
say "!txtgrn!> #{full_cmd}" unless Runfile.quiet
process = IO.popen "exec #{cmd} >#{log} 2>&1"
File.write pidfile(pid), process.pid if pid
@after_run_block.call(cmd) if @after_run_block
return process.pid
end

# Stop a command started with 'run_bg'. Provide the name of he pid file you
# used in 'run_bg'
def stop_bg(pid)
file = pidfile(pid)
if File.exist? file
pid = File.read file
File.delete file
run "kill -s TERM #{pid}"
else
say "!txtred!PID file not found." unless Runfile.quiet
end
end

# Set a block to be called before each run
def before_run(&block)
@before_run_block = block
end

# Set a block to be called after each run
def after_run(&block)
@after_run_block = block
end

private

def pid_dir
defined?(Runfile.pid_dir) ? Runfile.pid_dir : nil
end

def pidfile(pid)
pid_dir ? "#{pid_dir}/#{pid}.pid" : "#{pid}.pid"
end

end
end

include Runfile::Compatibilty

0 comments on commit 57695ba

Please sign in to comment.