-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathruby-shim
executable file
·47 lines (34 loc) · 1.74 KB
/
ruby-shim
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#!/bin/sh
# Record student runs of the Ruby intepreter
# Original version by @mikelikesbikes: https://gist.github.com/mikelikesbikes/8f8756f558fd4f25d566
# Which was inspired by answers here: http://stackoverflow.com/questions/692000/how-do-i-write-stderr-to-a-file-while-using-tee-with-a-pipe
# Immediately exit if any command fails
set -e
# Discover absolute path from symlink used to run this script
# OSX doesn't have `readlink -f` so this is an abominable hack
# Apple need to include this: https://github.com/harto/realpath-osx
realpath="$(python -c 'import os,sys; print os.path.realpath(sys.argv[1])' "$0")"
dir=$(dirname "$realpath")
# Capture path to "real" ruby
unshimmed_ruby="$(which -a ruby | head -2 | tail -1)"
# A path to a emphemeral part of the disk used to store the queued data
# captured by this command.
tmpdir="/tmp/ruby-runs"
# Ensure dir exists
mkdir -p $tmpdir
# Create a uniquely named (by process id) pipe
stderr_pipe="$tmpdir/stderr_pipe.$$"
mkfifo "$stderr_pipe"
# File path to contents of stderr
stderr_file="$tmpdir/stderr.$$"
# File path to metadata
metadata_file="$tmpdir/metadata.$$"
# In a background child process: append whatever gets written to the `err` pipe to
# /tmp/ruby-runs/stderr.$$ as well as writing it to this process's stderr stream
< "$stderr_pipe" tee -a "$stderr_file" >&2 &
# Invoke all metadata scripts in the realtime subfolder
# passing in the Ruby script name and path to unshimmed_ruby
find $dir/metadata/realtime -type f -name "*.sh" -exec sh {} "$unshimmed_ruby" "$@" \; > $metadata_file
# Replace this process with one running the "real" ruby executable (passed the same args that
# were passed to this executable) - redirect its sterr stream to the `err` pipe
exec $unshimmed_ruby "$@" 2> "$stderr_pipe"