public
Description: Merb More: The Full Stack. Take what you need; leave what you don't.
Homepage: http://www.merbivore.com
Clone URL: git://github.com/wycats/merb-more.git
commit  966c97b4e7ea3d616ca6b128f49f7f2053511144
tree    57b03035e6a2abb296e8e3b76a91e72854d783e4
parent  6a7eaef09335ec991957fb6320f8be55a5c0b932
merb-more / merb-freezer / lib / merb-freezer / freezer_mode.rb
100644 87 lines (74 sloc) 2.66 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
require 'find'
#require '../../../merb_rake_helper'
module FreezerMode
  
  def sudo
    windows = (PLATFORM =~ /win32|cygwin/) rescue nil
    sudo = windows ? "" : "sudo"
  end
 
  def gitmodules
    File.join(Dir.pwd, ".gitmodules")
  end
 
  # Uses the Git submodules to freeze a component
  #
  def submodules_freeze
    # Ensure that required git commands are available
    %w(git-pull git-submodule).each do |bin|
      next if in_path?(bin)
      $stderr.puts "ERROR: #{bin} must be avaible in PATH - you might want to freeze using MODE=rubygems"
      exit 1
    end
 
    # Create directory to receive the frozen components
    create_freezer_dir(freezer_dir)
 
    if managed?(@component)
      puts "#{@component} seems to be already managed by git-submodule."
      if @update
        puts "Trying to update #{@component} ..."
        `cd #{freezer_dir}/#{@component} && git-pull`
      else
        puts "you might want to call this rake task using UPDATE=true if you wish to update the frozen gems using this task"
      end
    else
      puts "Creating submodule for #{@component} ..."
      if framework_component?
        `cd #{Dir.pwd} & git-submodule --quiet add #{Freezer.components[@component.gsub("merb-", '')]} #{File.basename(freezer_dir)}/#{@component}`
      else
        `cd #{Dir.pwd} & git-submodule --quiet add #{@component} gems/submodules/#{@component.match(/.*\/(.*)\..{3}$/)[1]}`
      end
      if $?.success?
        `git-submodule init`
      else
        # Should this instead be a raise?
        $stderr.puts("ERROR: unable to create submodule for #{@component} - you might want to freeze using MODE=rubygems")
      end
    end
  end
 
  # Uses rubygems to freeze the components locally
  #
  def rubygems_freeze
    create_freezer_dir(freezer_dir)
    action = update ? 'update' : 'install'
    puts "#{action} #{@component} and dependencies from rubygems"
    `#{sudo} gem #{action} #{@component} --no-rdoc --no-ri -i #{freezer_dir}`
  end
 
  def create_freezer_dir(path)
    unless File.directory?(path)
      puts "Creating freezer directory ..."
      FileUtils.mkdir_p(path)
    end
  end
 
  protected
 
  # returns true if submodules are used
  def in_submodule?(component)
    return false unless File.exists?(gitmodules)
    # File.read(gitmodules) =~ %r![submodule "#{freezer_dir}/#{component}"]!
    File.read(gitmodules).match(%r!#{freezer_dir}/#{component}!)
  end
 
  # returns true if the component is in a submodule
  def managed?(component)
    File.directory?(File.join(freezer_dir, component)) || in_submodule?(component)
  end
 
  def in_path?(bin)
    `which #{bin}`
    !$?.nil? && $?.success?
  end
 
end