Skip to content

Instantly share code, notes, and snippets.

@kalecser
Created June 21, 2016 16:18
Show Gist options
  • Save kalecser/9e8a11d50ac48e5ea77a88fc931897b3 to your computer and use it in GitHub Desktop.
Save kalecser/9e8a11d50ac48e5ea77a88fc931897b3 to your computer and use it in GitHub Desktop.
MagicBox driver
def required
method = caller_locations(1,1)[0].label
raise ArgumentError,
"A required keyword argument was not specified when calling '#{method}'"
end
require('childprocess')
require('fileutils')
class NamedProcess
def initialize(name: required, targetDirPath: required, command: required)
@name = name
@process = ChildProcess.build(*command)
@process.cwd = targetDirPath
@process.duplex = true # sets up pipe so process.io.stdin will be available after .start
end
def run_process
start_process
wait_until_exit
end
def start_process
@reader, writer = IO.pipe
@process.io.stdout = writer
@process.io.stderr = writer
@process.start
writer.close #close before using according to https://github.com/jarib/childprocess
end
def wait_until_exit
@reader.each { |line| log_line line}
@process.wait
exit_code = @process.exit_code;
if (exit_code != 0)
raise ">[#{@name}] Process exited with code #{exit_code}"
end
end
def wait_for(pattern)
while not @reader.eof do
line = @reader.readline
log_line line
if line.include? pattern
return
end
end
raise "[#{@name}] Pattern #{pattern} not found on process output"
end
def enter_text(text)
@process.io.stdin.puts(text)
end
private
def log_line(line)
puts "[#{@name}] #{line}"
end
end
class MagicBoxDriver
def initialize(targetDirPath)
@targetDirPath = targetDirPath
rename_vagrant_vm()
#setup_projects()
end
def vagrant_up
NamedProcess.new(
name: "MagicBox",
targetDirPath: @targetDirPath,
command: "vagrant up".split
).run_process
end
def vagrant_halt
end
private
def rename_vagrant_vm()
NamedProcess.new(
name: "Rename Vagrant VM",
targetDirPath: @targetDirPath,
command: "sed -i -e s/magicbox/magicbox-aperture/g Vagrantfile".split
).run_process
end
def setup_projects()
setup = NamedProcess.new(
name: "Setup projects",
targetDirPath: @targetDirPath,
command: "bin/setup.sh ".split
)
setup.start_process
setup.wait_for("What do you want?")
all_projects_option = "1\n"
setup.enter_text(all_projects_option)
setup.wait_until_exit
end
end
targetDirPath = 'test-target'
#FileUtils.rm_rf(targetDirPath)
#Dir.mkdir(targetDirPath)
git = NamedProcess.new(
name: 'Git',
targetDirPath: targetDirPath,
command: "git clone [email protected]:ebanx/Projects.git .".split)
#git.start_process
#git.wait_until_exit
magic_box = MagicBoxDriver.new(targetDirPath)
magic_box.vagrant_up();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment