From 234a6734b3fcb3dd006f5756f342886f44f7da5a Mon Sep 17 00:00:00 2001 From: John Bintz Date: Thu, 9 Dec 2010 16:20:21 -0500 Subject: [PATCH] stuff --- .screenie | 8 ++++ Gemfile.lock | 54 ++++++++++++++++++++++ autotest/discover.rb | 1 + bin/screenie | 33 ++++++++++++++ lib/screenie/cli.rb | 0 lib/screenie/dsl/builder.rb | 35 ++++++++++++++ lib/screenie/dsl/screen.rb | 47 +++++++++++++++++++ lib/screenie/dsl/session.rb | 54 ++++++++++++++++++++++ screenie.gemspec | 3 ++ spec/lib/screenie/dsl/builder_spec.rb | 24 ++++++++++ spec/lib/screenie/dsl/screen_spec.rb | 48 +++++++++++++++++++ spec/lib/screenie/dsl/session_spec.rb | 66 +++++++++++++++++++++++++++ spec/lib/screenie_spec.rb | 5 ++ spec/spec_helper.rb | 3 ++ test/screenie.config | 7 +++ 15 files changed, 388 insertions(+) create mode 100644 .screenie create mode 100644 Gemfile.lock create mode 100755 bin/screenie create mode 100644 lib/screenie/cli.rb create mode 100644 lib/screenie/dsl/builder.rb create mode 100644 lib/screenie/dsl/screen.rb create mode 100644 lib/screenie/dsl/session.rb create mode 100644 spec/lib/screenie/dsl/builder_spec.rb create mode 100644 spec/lib/screenie/dsl/screen_spec.rb create mode 100644 spec/lib/screenie/dsl/session_spec.rb create mode 100644 test/screenie.config diff --git a/.screenie b/.screenie new file mode 100644 index 0000000..84bda9d --- /dev/null +++ b/.screenie @@ -0,0 +1,8 @@ +session :first do + screen "bundle exec autotest", :title => 'autotest' + screen! +end + +session :second do + screen %{echo "cats"} +end diff --git a/Gemfile.lock b/Gemfile.lock new file mode 100644 index 0000000..8ad69f2 --- /dev/null +++ b/Gemfile.lock @@ -0,0 +1,54 @@ +PATH + remote: . + specs: + screenie (0.0.1) + thor + +GEM + remote: http://rubygems.org/ + specs: + ZenTest (4.4.1) + aruba (0.2.7) + background_process + cucumber (~> 0.10.0) + autotest (4.4.6) + ZenTest (>= 4.4.1) + background_process (1.2) + builder (3.0.0) + cucumber (0.10.0) + builder (>= 2.1.2) + diff-lcs (~> 1.1.2) + gherkin (~> 2.3.2) + json (~> 1.4.6) + term-ansicolor (~> 1.0.5) + diff-lcs (1.1.2) + gherkin (2.3.2) + json (~> 1.4.6) + term-ansicolor (~> 1.0.5) + json (1.4.6) + mocha (0.9.10) + rake + rake (0.8.7) + rspec (2.2.0) + rspec-core (~> 2.2) + rspec-expectations (~> 2.2) + rspec-mocks (~> 2.2) + rspec-core (2.2.1) + rspec-expectations (2.2.0) + diff-lcs (~> 1.1.2) + rspec-mocks (2.2.0) + term-ansicolor (1.0.5) + thor (0.14.6) + +PLATFORMS + ruby + +DEPENDENCIES + aruba + autotest + bundler (>= 1.0.0) + cucumber + mocha + rspec + screenie! + thor diff --git a/autotest/discover.rb b/autotest/discover.rb index e69de29..cd6892c 100644 --- a/autotest/discover.rb +++ b/autotest/discover.rb @@ -0,0 +1 @@ +Autotest.add_discovery { "rspec2" } diff --git a/bin/screenie b/bin/screenie new file mode 100755 index 0000000..9c77c75 --- /dev/null +++ b/bin/screenie @@ -0,0 +1,33 @@ +#!which ruby + +require 'getoptlong' +require 'screenie/dsl/builder' + +opt = GetoptLong.new( + [ '--input', '-i', GetoptLong::REQUIRED_ARGUMENT ] +) + +files = %w{.screenie ~/.screenie} + +opt.each do |key, value| + case key + when '--input' + files = [ value ] + end +end + +session_name = ARGV.shift + +files.each do |path| + if File.exist?(target = File.expand_path(path)) + sessions = Screenie::DSL::Builder.new(File.read(target)) + + if !session_name || session_name.empty? + session = sessions.first + else + session = sessions[session_name.to_sym] + end + + session.to_cmd.each { |cmd| system cmd } + end +end diff --git a/lib/screenie/cli.rb b/lib/screenie/cli.rb new file mode 100644 index 0000000..e69de29 diff --git a/lib/screenie/dsl/builder.rb b/lib/screenie/dsl/builder.rb new file mode 100644 index 0000000..a0e865c --- /dev/null +++ b/lib/screenie/dsl/builder.rb @@ -0,0 +1,35 @@ +require 'screenie/dsl/session' + +module Screenie + module DSL + class Builder + attr_reader :sessions + + def initialize(code = '', &block) + @sessions = [] + + if !code.empty? + instance_eval(code) + else + instance_eval(&block) + end + end + + def session(name = nil, &block) + name ||= "screenie-#{Time.now.to_i}" + + session = Session.new(name.to_sym, &block) + + @sessions << session + end + + def first + @sessions.first + end + + def [](key) + @sessions.find { |session| session.name == key } + end + end + end +end diff --git a/lib/screenie/dsl/screen.rb b/lib/screenie/dsl/screen.rb new file mode 100644 index 0000000..6ad8ff4 --- /dev/null +++ b/lib/screenie/dsl/screen.rb @@ -0,0 +1,47 @@ +module Screenie + module DSL + class Screen + attr_accessor :screen_id + + def initialize(*options) + @options = options + @screen_id = nil + + if @options.first.kind_of?(::Fixnum) + @screen_id = @options.shift + end + end + + def stuff + output = [ %{-p #{@screen_id || 0} stuff} ] + + if @options.first.kind_of?(::String) + output << %{"#{@options.first}\n"} + end + + output * ' ' + end + + def screen + title = nil + if @options.last.kind_of?(::Hash) + opts = @options.last + title = opts[:title] + end + + if @screen_id == 0 + output = [] + output << %{-p 0 title "#{title}"} if title + else + output = %w{screen} + + output << %{-t "#{title}"} if title + + output << (@screen_id || 0) + end + + output * ' ' + end + end + end +end diff --git a/lib/screenie/dsl/session.rb b/lib/screenie/dsl/session.rb new file mode 100644 index 0000000..b63fb08 --- /dev/null +++ b/lib/screenie/dsl/session.rb @@ -0,0 +1,54 @@ +require 'screenie/dsl/screen' + +module Screenie + module DSL + class Session + attr_accessor :name + + def initialize(name = '', code = '', &block) + @name = name + @screens = [] + @start_screen = 0 + + @start_screen_to_use = nil + + if !code.empty? + instance_eval(code) + else + instance_eval(&block) + end + end + + def screen(*opts) + @screens << Screen.new(*opts) + end + + def screen!(*opts) + @start_screen_to_use = Screen.new(*opts) + @screens << @start_screen_to_use + end + + def to_cmd + output = [ %{screen -dmS "#{@name}"} ] + + available_screen_ids = (0..9).to_a + + output += @screens.collect { |screen| + if !screen.screen_id + screen.screen_id = available_screen_ids.shift + end + + commands = [] + commands << %{screen -X -S "#{@name}" #{screen.screen}} if !screen.screen.empty? + commands << %{screen -X -S "#{@name}" #{screen.stuff}} + }.flatten + + @start_screen = @start_screen_to_use.screen_id if @start_screen_to_use + + output << %{screen -x "#{@name}" -p #{@start_screen}} + + output + end + end + end +end \ No newline at end of file diff --git a/screenie.gemspec b/screenie.gemspec index 08d67d7..dbc34c4 100644 --- a/screenie.gemspec +++ b/screenie.gemspec @@ -17,6 +17,9 @@ Gem::Specification.new do |s| s.add_development_dependency "rspec" s.add_development_dependency "mocha" s.add_development_dependency "autotest" + s.add_development_dependency "cucumber" + s.add_development_dependency "aruba" + s.add_dependency 'thor' s.files = `git ls-files`.split("\n") s.executables = `git ls-files`.split("\n").map{|f| f =~ /^bin\/(.*)/ ? $1 : nil}.compact diff --git a/spec/lib/screenie/dsl/builder_spec.rb b/spec/lib/screenie/dsl/builder_spec.rb new file mode 100644 index 0000000..7c02e05 --- /dev/null +++ b/spec/lib/screenie/dsl/builder_spec.rb @@ -0,0 +1,24 @@ +require 'spec_helper' +require 'screenie/dsl/builder' + +describe Screenie::DSL::Builder do + describe '#sessions' do + context 'from block' do + subject { Screenie::DSL::Builder.new { session("cats") { } }} + + specify { subject[:cats].should_not be_nil } + end + + context 'from code' do + subject { Screenie::DSL::Builder.new(%{session("cats") { } })} + + specify { subject[:cats].should_not be_nil } + end + end + + describe '#first' do + subject { Screenie::DSL::Builder.new { session do; end }} + + its(:first) { should be_a_kind_of(Screenie::DSL::Session) } + end +end diff --git a/spec/lib/screenie/dsl/screen_spec.rb b/spec/lib/screenie/dsl/screen_spec.rb new file mode 100644 index 0000000..d9c6f2f --- /dev/null +++ b/spec/lib/screenie/dsl/screen_spec.rb @@ -0,0 +1,48 @@ +require 'spec_helper' +require 'screenie/dsl/screen' + +describe Screenie::DSL::Screen do + describe '#to_cmd' do + let(:screen) { Screenie::DSL::Screen.new(*params) } + + subject { screen } + + context 'blank' do + let(:params) { [] } + + its(:screen) { should == 'screen 0' } + end + + context 'with command' do + let(:params) { ["command"] } + + its(:screen) { should == 'screen 0' } + its(:stuff) { should == %{-p 0 stuff "command +"} } + end + + context 'with number id' do + let(:params) { [ 1 ] } + + its(:screen) { should == "screen 1" } + + specify { screen.screen_id.should == 1 } + end + + context 'with number id and command' do + let(:params) { [ 1, "command" ] } + + its(:screen) { should == "screen 1" } + its(:stuff) { should == %{-p 1 stuff "command +"} } + specify { screen.screen_id.should == 1 } + end + + context 'with title' do + let(:params) { [ { :title => 'My title' } ] } + + its(:screen) { should == %{screen -t "My title" 0} } + specify { screen.screen_id.should == nil } + end + end +end diff --git a/spec/lib/screenie/dsl/session_spec.rb b/spec/lib/screenie/dsl/session_spec.rb new file mode 100644 index 0000000..7c16d26 --- /dev/null +++ b/spec/lib/screenie/dsl/session_spec.rb @@ -0,0 +1,66 @@ +require 'spec_helper' +require 'screenie/dsl/session' + +describe Screenie::DSL::Session do + describe '#to_cmd' do + context 'one screen' do + subject { + Screenie::DSL::Session.new("my-session") { screen "test" }.to_cmd + } + + it { should == [ + %{screen -dmS "my-session"}, + %{screen -X -S "my-session" -p 0 stuff "test +"}, + %{screen -x "my-session" -p 0} + ] } + end + + context 'two screens' do + subject { + Screenie::DSL::Session.new("my-session") { screen "test"; screen "test2" }.to_cmd + } + + it { should == [ + %{screen -dmS "my-session"}, + %{screen -X -S "my-session" -p 0 stuff "test +"}, + %{screen -X -S "my-session" screen 1}, + %{screen -X -S "my-session" -p 1 stuff "test2 +"}, + %{screen -x "my-session" -p 0} + ] } + end + end + + describe 'from string' do + subject { + Screenie::DSL::Session.new("my-session", <<-EOR).to_cmd +screen "test" + EOR + } + + it { should == [ + %{screen -dmS "my-session"}, + %{screen -X -S "my-session" -p 0 stuff "test +"}, + %{screen -x "my-session" -p 0} + ] } + end + + describe 'screen!' do + subject { + Screenie::DSL::Session.new("my-session") { screen "test"; screen! "test2" }.to_cmd + } + + it { should == [ + %{screen -dmS "my-session"}, + %{screen -X -S "my-session" -p 0 stuff "test +"}, + %{screen -X -S "my-session" screen 1}, + %{screen -X -S "my-session" -p 1 stuff "test2 +"}, + %{screen -x "my-session" -p 1} + ] } + end +end diff --git a/spec/lib/screenie_spec.rb b/spec/lib/screenie_spec.rb index e69de29..9450438 100644 --- a/spec/lib/screenie_spec.rb +++ b/spec/lib/screenie_spec.rb @@ -0,0 +1,5 @@ +require 'spec_helper' + +describe Screenie do + +end \ No newline at end of file diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index e69de29..cd65f49 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -0,0 +1,3 @@ +RSpec.configure do |config| + config.mock_with :mocha +end \ No newline at end of file diff --git a/test/screenie.config b/test/screenie.config new file mode 100644 index 0000000..5eb59e3 --- /dev/null +++ b/test/screenie.config @@ -0,0 +1,7 @@ +session :testing do + screen %{bundle exec autotest}, :name => 'autotest' + screen %{rails s}, :name => 'rails' + screen :name => 'command prompt' + + select :first +end