Skip to content

Commit 57695ba

Browse files
authored
Merge pull request #47 from DannyBen/add/compat
Add compatibility layer
2 parents 2cbef03 + bb16a4d commit 57695ba

File tree

2 files changed

+97
-0
lines changed

2 files changed

+97
-0
lines changed

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,19 @@ Options:
8484
- [User Guide](https://runfile.dannyb.co/)
8585
- [Learn by Example](https://github.com/DannyBen/runfile/tree/master/examples#readme)
8686

87+
## Breaking changes in 0.12.0
88+
89+
Note that version 0.12.0 removes several commands from the DSL.
90+
If you were using any of the Colsole commands (`say`, `resay`, etc.), or any of
91+
the `run`, `run_bg` commands, you will have to adjust your Runfile since they are
92+
no longer available.
93+
94+
For convenience, you can simply `require runfile/compatibility` at the top of
95+
your Runfile, to still have this functionality included.
96+
97+
More info and alternative solutions in
98+
[this pull request](https://github.com/DannyBen/runfile/pull/46).
99+
87100
## Contributing / Support
88101

89102
If you experience any issue, have a question or a suggestion, or if you wish

lib/runfile/compatibility.rb

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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

Comments
 (0)