Class: RailsEnvironment

Inherits:
Object show all
Defined in:
lib/commands/plugin.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dir) ⇒ RailsEnvironment

Returns a new instance of RailsEnvironment.



61
62
63
# File 'lib/commands/plugin.rb', line 61

def initialize(dir)
  @root = dir
end

Instance Attribute Details

#rootObject (readonly)

Returns the value of attribute root.



59
60
61
# File 'lib/commands/plugin.rb', line 59

def root
  @root
end

Class Method Details

.defaultObject



73
74
75
# File 'lib/commands/plugin.rb', line 73

def self.default
  @default ||= find
end

.default=(rails_env) ⇒ Object



77
78
79
# File 'lib/commands/plugin.rb', line 77

def self.default=(rails_env)
  @default = rails_env
end

.find(dir = nil) ⇒ Object



65
66
67
68
69
70
71
# File 'lib/commands/plugin.rb', line 65

def self.find(dir=nil)
  dir ||= pwd
  while dir.length > 1
    return new(dir) if File.exist?(File.join(dir, 'config', 'environment.rb'))
    dir = File.dirname(dir)
  end
end

Instance Method Details

#best_install_methodObject



115
116
117
118
119
120
121
122
# File 'lib/commands/plugin.rb', line 115

def best_install_method
  return :http unless use_svn?
  case
    when use_externals? then :externals
    when use_checkout? then :checkout
    else :export
  end
end

#externalsObject



124
125
126
127
128
129
130
# File 'lib/commands/plugin.rb', line 124

def externals
  return [] unless use_externals?
  ext = `svn propget svn:externals "#{root}/vendor/plugins"`
  ext.reject{ |line| line.strip == '' }.map do |line| 
    line.strip.split(/\s+/, 2) 
  end
end

#externals=(items) ⇒ Object



132
133
134
135
136
137
138
139
140
141
# File 'lib/commands/plugin.rb', line 132

def externals=(items)
  unless items.is_a? String
    items = items.map{|name,uri| "#{name.ljust(29)} #{uri.chomp('/')}"}.join("\n")
  end
  Tempfile.open("svn-set-prop") do |file|
    file.write(items)
    file.flush
    system("svn propset -q svn:externals -F \"#{file.path}\" \"#{root}/vendor/plugins\"")
  end
end

#install(name_uri_or_plugin) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/commands/plugin.rb', line 81

def install(name_uri_or_plugin)
  if name_uri_or_plugin.is_a? String
    if name_uri_or_plugin =~ /:\/\// 
      plugin = Plugin.new(name_uri_or_plugin)
    else
      plugin = Plugins[name_uri_or_plugin]
    end
  else
    plugin = name_uri_or_plugin
  end
  unless plugin.nil?
    plugin.install
  else
    puts "Plugin not found: #{name_uri_or_plugin}"
  end
end

#use_checkout?Boolean

Returns:

  • (Boolean)


108
109
110
111
112
113
# File 'lib/commands/plugin.rb', line 108

def use_checkout?
  # this is a bit of a guess. we assume that if the rails environment
  # is under subversion then they probably want the plugin checked out
  # instead of exported. This can be overridden on the command line
  File.directory?("#{root}/.svn")
end

#use_externals?Boolean

Returns:

  • (Boolean)


104
105
106
# File 'lib/commands/plugin.rb', line 104

def use_externals?
  use_svn? && File.directory?("#{root}/vendor/plugins/.svn")
end

#use_svn?Boolean

Returns:

  • (Boolean)


98
99
100
101
102
# File 'lib/commands/plugin.rb', line 98

def use_svn?
  require 'active_support/core_ext/kernel'
  silence_stderr {`svn --version` rescue nil}
  !$?.nil? && $?.success?
end