Extracted Logic for vim in seperate class
authorAkshay Mankar <akshaym@thoughtworks.com>
Tue, 8 Jan 2013 18:43:45 +0000 (00:13 +0530)
committerAkshay Mankar <akshaym@thoughtworks.com>
Tue, 8 Jan 2013 19:00:23 +0000 (00:30 +0530)
copy_my_conf.gemspec
example/Vagrantfile
lib/boot.rb
lib/copy_my_conf.rb
lib/copy_my_conf/vim.rb [new file with mode: 0644]
spec/copy_my_conf/vim_spec.rb [new file with mode: 0644]
spec/copy_my_conf_spec.rb

index f65bb03..90e3572 100644 (file)
@@ -6,6 +6,6 @@ Gem::Specification.new do |s|
   s.description = "Copy your configurations easily into vagrant box"
   s.authors     = ["Akshay Mankar"]
   s.email       = 'itsakshaymankar@gmail.com'
-  s.files       = ["lib/copy_my_conf.rb"]
+  s.files       = Dir["lib/**/*.rb"]
   s.homepage    = 'http://github.com/akshaymankar/copy_my_conf'
 end
index ed3cdc1..a766e2b 100644 (file)
@@ -7,10 +7,10 @@ Vagrant::Config.run do |config|
   config.vm.box = "precise64"
   config.ssh.forward_agent = true
   
-  config.vm.provision CopyMyConf do |copy_conf|
-    copy_conf.ssh = true
+  config.vm.provision Vagrant::Provisioners::CopyMyConf do |copy_conf|
+    copy_conf.ssh = false
     copy_conf.vim = true
-    copy_conf.git = true
+    copy_conf.git = false
     copy_conf.user_home = "/home/vagrant"
   end
 
index 4a3b432..b12274b 100644 (file)
@@ -1 +1,2 @@
-require "copy_my_conf/config"
\ No newline at end of file
+require "copy_my_conf/config"
+require "copy_my_conf/vim"
index c10b814..60d14d9 100644 (file)
@@ -4,13 +4,19 @@ module Vagrant
     class CopyMyConf < Base
 
       def prepare
-        prepare_vim if config.vim
-        prepare_git if config.git
-        prepare_ssh if config.ssh
+        @to_be_copied = []
+        config.all_true.each do |c|
+          conf = self.class.const_get(c.capitalize).new
+          @to_be_copied << conf
+          conf.prepare env[:vm].config.vm, tmp_root
+        end
       end
 
       def provision!
         channel = env[:vm].channel
+        @to_be_copied.each do |conf|
+          conf.provision channel, user_home, tmp_root
+        end
         provision_ssh(channel) if config.ssh
         provision_vim(channel) if config.vim
         provision_git(channel) if config.git
@@ -36,25 +42,11 @@ module Vagrant
         env[:vm].config.vm.share_folder("git", "#{tmp_root}/git/", "#{tmp_root}/git")
       end
 
-      def prepare_vim
-        `mkdir -p #{tmp_root}/vim`
-        ["~/.vimrc", "~/.vim"].each do |file|
-          `cp -r #{file} #{tmp_root}/vim`
-        end
-        env[:vm].config.vm.share_folder("vim", "#{tmp_root}/vim/", "#{tmp_root}/vim")
-      end
-
       def provision_git(channel)
         puts "Copying your gitconfig"
         channel.execute("cp #{tmp_root}/git/.gitconfig ~/")
       end
 
-      def provision_vim(channel)
-        puts "Copying your vim configuratios"
-        channel.execute("rm -rf #{user_home}/.vim*")
-        channel.execute("cp -r #{tmp_root}/vim/.??* ~/")
-      end
-
       def provision_ssh(channel)
         puts "Copying your ssh keys and config"
         channel.sudo("mkdir -p #{tmp_root}/cached && chown -R vagrant #{tmp_root}/cached")
diff --git a/lib/copy_my_conf/vim.rb b/lib/copy_my_conf/vim.rb
new file mode 100644 (file)
index 0000000..f8935ed
--- /dev/null
@@ -0,0 +1,21 @@
+module Vagrant
+  module Provisioners
+    class CopyMyConf < Base
+      class Vim
+        def prepare vm, tmp_root
+          `mkdir -p #{tmp_root}/vim`
+          ["~/.vimrc", "~/.vim"].each do |file|
+            `cp -r #{file} #{tmp_root}/vim`
+          end
+          vm.share_folder("vim", "#{tmp_root}/vim", "#{tmp_root}/vim")
+        end
+
+        def provision channel, user_home, tmp_root
+          puts "Copying your vim configuratios"
+          channel.execute("rm -rf #{user_home}/.vim*")
+          channel.execute("cp -r #{tmp_root}/vim/.??* ~/")
+        end
+      end
+    end
+  end
+end
diff --git a/spec/copy_my_conf/vim_spec.rb b/spec/copy_my_conf/vim_spec.rb
new file mode 100644 (file)
index 0000000..fb0a56e
--- /dev/null
@@ -0,0 +1,20 @@
+require "spec_helper"
+
+module Vagrant
+  module Provisioners
+    class CopyMyConf < Base
+      describe Vim do
+        it "should copy dotfiles to temporary location in vagrant box" do
+          Vim.any_instance.stub(:`).and_return(nil)
+          vm = Object.new
+          tmp_root = "tmp_root"
+
+          vm.should_receive(:share_folder).with(anything, "#{tmp_root}/vim", "#{tmp_root}/vim")
+          vim = Vim.new
+
+          vim.prepare vm, tmp_root
+        end
+      end
+    end
+  end
+end
\ No newline at end of file
index fb2dcb9..b471ff3 100644 (file)
@@ -3,15 +3,36 @@ require "spec_helper"
 module Vagrant
   module Provisioners
     describe CopyMyConf do
-      xit "should prepare provisioning process" do
-        copy_my_conf = CopyMyConf.new
+      before(:each) do
+        env_vm = Object.new
+        env_vm_config = Object.new
+        @mock_vm = Object.new
+        @config = CopyMyConf.config_class.new
+        @env_channel = Object.new
+
+        CopyMyConf.any_instance.stub(:env).and_return({:vm => env_vm})
+        env_vm.stub(:config).and_return(env_vm_config)
+        env_vm.stub(:channel).and_return(@env_channel)
+        env_vm_config.stub(:vm).and_return(@mock_vm)
+        CopyMyConf.any_instance.stub(:config).and_return(@config)
+      end
+
+      it "should prepare provisioning process" do
+        @config.should_receive(:all_true).and_return([:vim])
+        CopyMyConf::Vim.any_instance.should_receive(:prepare).with(@mock_vm, anything)
 
-        config = CopyMyConf.config_class.new
-        config.vim = true
+        CopyMyConf.new.prepare
+      end
+
+      it "should provision the vm" do
+        @config.stub(:all_true).and_return([:vim])
+        copy_my_conf = CopyMyConf.new
 
-        copy_my_conf.stub(:config).and_return(config)
+        CopyMyConf::Vim.any_instance.stub(:prepare)
+        copy_my_conf.prepare
 
-        CopyMyConf::Vim.any_instance.should_receive(:prepare)
+        CopyMyConf::Vim.any_instance.should_receive(:provision).with(@env_channel, anything, anything)
+        copy_my_conf.provision!
       end
     end
   end