Skip to content

Commit

Permalink
Initial Recorder and Player
Browse files Browse the repository at this point in the history
  • Loading branch information
antono committed Dec 6, 2010
1 parent 6a4a600 commit c3b3732
Show file tree
Hide file tree
Showing 11 changed files with 205 additions and 7 deletions.
6 changes: 3 additions & 3 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
source "http://rubygems.org"
# Add dependencies required to use your gem here.
# Example:
# gem "activesupport", ">= 2.3.5"

gem 'xdg'
gem 'ohai'

# Add dependencies to develop your gem here.
# Include everything needed to run rake, tests, features, etc.
Expand Down
44 changes: 44 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
GEM
remote: http://rubygems.org/
specs:
diff-lcs (1.1.2)
extlib (0.9.15)
git (1.2.5)
jeweler (1.5.1)
bundler (~> 1.0.0)
git (>= 1.2.5)
rake
json (1.4.6)
mixlib-cli (1.2.0)
mixlib-config (1.1.2)
mixlib-log (1.2.0)
ohai (0.5.8)
extlib
json (>= 1.4.4, <= 1.4.6)
mixlib-cli
mixlib-config
mixlib-log
systemu
rake (0.8.7)
rcov (0.9.9)
rspec (2.1.0)
rspec-core (~> 2.1.0)
rspec-expectations (~> 2.1.0)
rspec-mocks (~> 2.1.0)
rspec-core (2.1.0)
rspec-expectations (2.1.0)
diff-lcs (~> 1.1.2)
rspec-mocks (2.1.0)
systemu (1.2.0)
xdg (1.0.0)

PLATFORMS
ruby

DEPENDENCIES
bundler (~> 1.0.0)
jeweler (~> 1.5.1)
ohai
rcov
rspec (~> 2.1.0)
xdg
25 changes: 25 additions & 0 deletions bin/shellcast
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/usr/bin/env ruby

$LOAD_PATH.unshift(File.expand_path('../../lib', __FILE__))

require 'rubygems'
require 'shellcast'

case ARGV[0]
when 'record'
ShellCast::Recorder.record!
when 'list'
ShellCast::Player.list
when 'play'
if ARGV[1]
ShellCast::Player.play(ARGV[1])
else
puts "Missing id for shellcast"
ShellCast::Player.list
puts "Select one..."
exit
end
else
puts "Usage: #{__FILE__} command [shellcast_id]"
puts "Commands: record, list, play, publish, get"
end
20 changes: 20 additions & 0 deletions lib/shellcast.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
require 'rubygems'
require 'fileutils'
require 'xdg'
require 'yaml'

module ShellCast

APP_NAME = 'shellcast'
DATA_DIR = File.join(XDG::Data.home, APP_NAME)

def shellcast_dir(shellcast_id)
File.join(ShellCast::DATA_DIR, shellcast_id)
end
module_function :shellcast_dir

autoload :Recorder, 'shellcast/recorder.rb'
autoload :Player, 'shellcast/player.rb'
autoload :Publisher, 'shellcast/publisher.rb'

end
38 changes: 38 additions & 0 deletions lib/shellcast/player.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
module ShellCast
class Player
def self.play(id)
new(id).play
end

def self.list
Dir[File.join(ShellCast::DATA_DIR, "**", 'meta')].each do |path|
metadata = YAML.load_file(path)
puts "#{metadata["id"]}: #{metadata["title"]}"
end
end

def initialize(id)
@shellcast_id = id
end

def play
puts ""
puts "Playback started"
puts "-" * 80
system(scriptreplay_cmd)
puts
puts "-" * 80
puts "Playback finished"
end

private

def shellcast_file(name)
File.join(ShellCast.shellcast_dir(@shellcast_id), name)
end

def scriptreplay_cmd
"scriptreplay #{shellcast_file('timing')} #{shellcast_file('data')}"
end
end
end
4 changes: 4 additions & 0 deletions lib/shellcast/publisher.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module ShellCast
class Publisher
end
end
53 changes: 53 additions & 0 deletions lib/shellcast/recorder.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
module ShellCast
class Recorder

def self.record!
new.record!
end

def initialize
@meta = {}
end

def record!
FileUtils.mkdir_p(shellcast_dir) unless File.exists?(shellcast_dir)
request_metadata
puts "Your session started"
puts "Type Ctrl+D or exit to finish recording"
puts "-" * 80
system(script_cmd)
puts "-" * 80
puts "Session finihed!"
puts "Shellcast ID:\t #{shellcast_id}"
puts "Shellcast path:\t #{shellcast_dir}"
end

def request_metadata
puts "Provide name for Your shellcast: "
@meta["title"] = STDIN.gets.strip
@meta["id"] = shellcast_id
puts shellcast_file('meta')
File.open(shellcast_file('meta'), 'w+') do |meta|
meta.puts @meta.to_yaml
end
end

private

def shellcast_dir
@shellcast_dir ||= ShellCast.shellcast_dir(shellcast_id)
end

def shellcast_id
@shellcast_id ||= Time.now.to_i.to_s
end

def shellcast_file(name)
File.join(ShellCast.shellcast_dir(shellcast_id), name)
end

def script_cmd
"script -c 'bash' #{shellcast_file('data')} -t 2> #{shellcast_file('timing')}"
end
end
end
Empty file added spec/shellcast/player_spec.rb
Empty file.
Empty file.
10 changes: 10 additions & 0 deletions spec/shellcast/recorder_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
require 'spec_helper'

describe ShellCast::Recorder do
describe "#record!" do
it "should start script session" do
subject.should_receive("system").with(anything)
subject.record!
end
end
end
12 changes: 8 additions & 4 deletions spec/shellcast_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
require 'spec_helper'

describe "Shellcast" do
it "fails" do
fail "hey buddy, you should probably rename this file and start specing for real"
describe ShellCast do
it "should have ::APP_NAME defined" do
ShellCast::APP_NAME.should == 'shellcast'
end

it "should provide XDG and APP_NAME based ::DATA_DIR" do
ShellCast::DATA_DIR.should == File.join(XDG::Data.home, ShellCast::APP_NAME)
end
end

0 comments on commit c3b3732

Please sign in to comment.