Skip to content
This repository
Browse code

Rakefile: Split the modules and methods out of the Rakefile and into …

…a small library.
  • Loading branch information...
commit c1f920d57eef067ae261adef14878396b1f98b33 1 parent 68eb39e
Wael M. Nasreddine authored November 03, 2011
127  Rakefile
... ...
@@ -1,104 +1,8 @@
1  
-module Janus
2  
-  # Errors
3  
-  JanusError = Class.new Exception
4  
-  BlockNotGivenError = Class.new JanusError
5  
-  RubyGemsNotFoundError = Class.new JanusError
  1
+ROOT_PATH = File.expand_path(File.join(File.dirname(__FILE__)))
  2
+$: << File.join(ROOT_PATH, 'lib')
6 3
 
7  
-  module VIM
8  
-    extend self
9  
-
10  
-    # Folders
11  
-    def folders
12  
-      %w[ _backup _temp ]
13  
-    end
14  
-  end
15  
-end
16  
-
17  
-# Return the root path
18  
-#
19  
-# @return [String] The absolute path to Janus repository
20  
-def root_path
21  
-  @root_path ||= File.expand_path(File.dirname(__FILE__))
22  
-end
23  
-
24  
-# Expand the path of a given file
25  
-#
26  
-# @param [String] file
27  
-# @return [String] The expanded path to the given file.
28  
-def expand(file)
29  
-  File.expand_path(file)
30  
-end
31  
-
32  
-# Find an installed gem
33  
-#
34  
-# @param [String] The gem name to search for
35  
-# @param [Mixed] The gem requirements
36  
-# @return [Array] The found gems
37  
-def find_gem(gem_name, *requirements)
38  
-  begin
39  
-    require 'rubygems'
40  
-    if Gem.const_defined?(:Specification)
41  
-      Gem::Specification.find_all_by_name(gem_name, *requirements)
42  
-    elsif Gem.respond_to?(:source_index)
43  
-      Gem.source_index.find_name(gem_name, *requirements)
44  
-    else
45  
-      Gem.cache.find_name(gem_name, *requirements)
46  
-    end
47  
-  rescue LoadError
48  
-    raise Janus::RubyGemsNotFoundError
49  
-  end
50  
-end
51  
-
52  
-# Install a gem
53  
-#
54  
-# @param [String] The gem name
55  
-def install_gem(gem_name)
56  
-  require 'rubygems'
57  
-
58  
-  # Install the gem only if it can't be found
59  
-  if find_gem(gem_name).length == 0
60  
-    sh "gem install #{gem_name}"
61  
-  end
62  
-rescue Janus::RubyGemsNotFoundError
63  
-  puts "Could not install the gem #{gem_name}, please do so manually."
64  
-  puts "gem install #{gem_name}"
65  
-end
66  
-
67  
-# Install a plugin
68  
-#
69  
-# @param [String] The group the plugin belongs to
70  
-# @param [String] The plugin name
71  
-# @param [&block] The installation block
72  
-def install_vim_plugin(group, name, &block)
73  
-  raise Janus::BlockNotGivenError unless block_given?
74  
-
75  
-  # Create a namespace for the plugin
76  
-  namespace(name) do
77  
-    task :verify_plugin do
78  
-      unless Dir["#{root_path}/#{group}/#{name}/**"].any?
79  
-        abort "The submodule #{group}/#{name} is not ready, please run 'git submodule update --init'"
80  
-      end
81  
-    end
82  
-
83  
-    # Define the plugin installation task
84  
-    desc "Install #{name} plugin."
85  
-    task :install do
86  
-      puts
87  
-      puts "*" * 40
88  
-      puts "*#{"Installing #{name}".center(38)}*"
89  
-      puts "*" * 40
90  
-      puts
91  
-      yield
92  
-    end
93  
-    task :install => :verify_plugin
94  
-  end
95  
-
96  
-  # Hook the plugin's install task to the global install task
97  
-  task :install => "#{name}:install"
98  
-end
99  
-
100  
-# Load all plugin installation tasks
101  
-Dir["#{root_path}/janus-*/tasks/**.rake"].each { |f| import f }
  4
+require 'janus'
  5
+include Janus
102 6
 
103 7
 desc "link ViM configuration files."
104 8
 task :link_vim_conf_files do
@@ -110,29 +14,6 @@ task :link_vim_conf_files do
110 14
   end
111 15
 end
112 16
 
113  
-# Get a list of submodules
114  
-#
115  
-# @return [Array] of submodules
116  
-def submodules
117  
-  `git submodule`.split("\n").collect { |s| s.split(" ")[1] }
118  
-end
119  
-
120  
-# Update a submodule
121  
-#
122  
-# @param [String] The submodule to update (relative path)
123  
-# @param [String] The branch we're tracking
124  
-def update_submodule(submodule, branch = "master")
125  
-  Dir.chdir submodule do
126  
-    puts
127  
-    puts "*" * 40
128  
-    puts "*#{"Updating #{submodule}".center(38)}*"
129  
-    puts "*" * 40
130  
-    puts
131  
-    sh "git checkout #{branch}"
132  
-    sh "git pull"
133  
-  end
134  
-end
135  
-
136 17
 namespace :dev do
137 18
   desc "Update submodules"
138 19
   task :update_submodules do
22  lib/janus.rb
... ...
@@ -0,0 +1,22 @@
  1
+module Janus
  2
+  # Return the root path
  3
+  #
  4
+  # @return [String] The absolute path to Janus repository
  5
+  def root_path
  6
+    ROOT_PATH
  7
+  end
  8
+
  9
+  # Expand the path of a given file
  10
+  #
  11
+  # @param [String] file
  12
+  # @return [String] The expanded path to the given file.
  13
+  def expand(file)
  14
+    File.expand_path(file)
  15
+  end
  16
+end
  17
+
  18
+require 'janus/errors'
  19
+require 'janus/git'
  20
+require 'janus/vim'
  21
+require 'janus/gems'
  22
+require 'janus/plugins'
6  lib/janus/errors.rb
... ...
@@ -0,0 +1,6 @@
  1
+module Janus
  2
+  # Errors
  3
+  JanusError = Class.new Exception
  4
+  BlockNotGivenError = Class.new JanusError
  5
+  RubyGemsNotFoundError = Class.new JanusError
  6
+end
36  lib/janus/gems.rb
... ...
@@ -0,0 +1,36 @@
  1
+module Janus
  2
+  # Find an installed gem
  3
+  #
  4
+  # @param [String] The gem name to search for
  5
+  # @param [Mixed] The gem requirements
  6
+  # @return [Array] The found gems
  7
+  def find_gem(gem_name, *requirements)
  8
+    begin
  9
+      require 'rubygems'
  10
+      if Gem.const_defined?(:Specification)
  11
+        Gem::Specification.find_all_by_name(gem_name, *requirements)
  12
+      elsif Gem.respond_to?(:source_index)
  13
+        Gem.source_index.find_name(gem_name, *requirements)
  14
+      else
  15
+        Gem.cache.find_name(gem_name, *requirements)
  16
+      end
  17
+    rescue LoadError
  18
+      raise RubyGemsNotFoundError
  19
+    end
  20
+  end
  21
+
  22
+  # Install a gem
  23
+  #
  24
+  # @param [String] The gem name
  25
+  def install_gem(gem_name)
  26
+    require 'rubygems'
  27
+
  28
+    # Install the gem only if it can't be found
  29
+    if find_gem(gem_name).length == 0
  30
+      sh "gem install #{gem_name}"
  31
+    end
  32
+  rescue RubyGemsNotFoundError
  33
+    puts "Could not install the gem #{gem_name}, please do so manually."
  34
+    puts "gem install #{gem_name}"
  35
+  end
  36
+end
24  lib/janus/git.rb
... ...
@@ -0,0 +1,24 @@
  1
+module Janus
  2
+  # Get a list of submodules
  3
+  #
  4
+  # @return [Array] of submodules
  5
+  def submodules
  6
+    `git submodule`.split("\n").collect { |s| s.split(" ")[1] }
  7
+  end
  8
+
  9
+  # Update a submodule
  10
+  #
  11
+  # @param [String] The submodule to update (relative path)
  12
+  # @param [String] The branch we're tracking
  13
+  def update_submodule(submodule, branch = "master")
  14
+    Dir.chdir submodule do
  15
+      puts
  16
+      puts "*" * 40
  17
+      puts "*#{"Updating #{submodule}".center(38)}*"
  18
+      puts "*" * 40
  19
+      puts
  20
+      sh "git checkout #{branch}"
  21
+      sh "git pull"
  22
+    end
  23
+  end
  24
+end
44  lib/janus/plugins.rb
... ...
@@ -0,0 +1,44 @@
  1
+require 'rake'
  2
+module Janus
  3
+  include Rake::DSL
  4
+  
  5
+  def self.included(base)
  6
+    # Load all plugin installation tasks
  7
+    Dir["#{root_path}/janus-*/tasks/**.rake"].each do |f|
  8
+      base.send :import, f
  9
+    end
  10
+  end
  11
+
  12
+  # Install a plugin
  13
+  #
  14
+  # @param [String] The group the plugin belongs to
  15
+  # @param [String] The plugin name
  16
+  # @param [&block] The installation block
  17
+  def install_vim_plugin(group, name, &block)
  18
+    raise Janus::BlockNotGivenError unless block_given?
  19
+
  20
+    # Create a namespace for the plugin
  21
+    namespace(name) do
  22
+      task :verify_plugin do
  23
+        unless Dir["#{root_path}/#{group}/#{name}/**"].any?
  24
+          abort "The submodule #{group}/#{name} is not ready, please run 'git submodule update --init'"
  25
+        end
  26
+      end
  27
+
  28
+      # Define the plugin installation task
  29
+      desc "Install #{name} plugin."
  30
+      task :install do
  31
+        puts
  32
+        puts "*" * 40
  33
+        puts "*#{"Installing #{name}".center(38)}*"
  34
+        puts "*" * 40
  35
+        puts
  36
+        yield
  37
+      end
  38
+      task :install => :verify_plugin
  39
+    end
  40
+
  41
+    # Hook the plugin's install task to the global install task
  42
+    task :install => "#{name}:install"
  43
+  end
  44
+end
10  lib/janus/vim.rb
... ...
@@ -0,0 +1,10 @@
  1
+module Janus
  2
+  module VIM
  3
+    extend self
  4
+
  5
+    # Folders
  6
+    def folders
  7
+      %w[ _backup _temp ]
  8
+    end
  9
+  end
  10
+end

0 notes on commit c1f920d

Please sign in to comment.
Something went wrong with that request. Please try again.