class Plugin

Attributes

name[R]
uri[R]
versions[R]

Public Class Methods

find(name) click to toggle source
# File lib/commands/plugin/plugin.rb, line 12
def self.find(name)
  name =~ /\// ? new(name) : Repositories.instance.find_plugin(name)
end
new(uri, name=nil) click to toggle source
# File lib/commands/plugin/plugin.rb, line 6
def initialize(uri, name=nil)
  @uri = normalise_uri(uri)
  name.nil? ? guess_name(uri) : @name = name
  check_for_versions
end

Public Instance Methods

about(force_refresh=false) click to toggle source

Returns a hash representing this plugin's metadata

# File lib/commands/plugin/plugin.rb, line 61
def about(force_refresh=false)
  # Look in cache first
  unless force_refresh
    if Repositories.instance.source_cache['plugins'].has_key?(name)
      return Repositories.instance.source_cache['plugins'][name]
    end
  end
  # Get from original source
  tmp = "#{rails_env.root}/_tmp_about.yml"
  if svn_url?
    cmd = %Q(svn export #{@uri}/about.yml "#{tmp}")
    puts cmd if $verbose
    system(cmd)
  end
  about_uri = svn_url? ? tmp : File.join(@uri, 'about.yml')
  open(about_uri) do |stream|
    about_hash = YAML.load(stream.read)
    unless about_hash.is_a?(Hash) && !about_hash['plugin'].nil?
      raise("#{name}'s about.yml wasn't valid YAML")
    end
    return about_hash
  end 
rescue
  # Make yaml on the fly for this plugin. 
  # The 'plugin' field (uri to the resource) is the only required field.
  {
    'plugin' => uri
  }
ensure
  FileUtils.rm_rf tmp if svn_url?
end
install(method=nil, options = {}) click to toggle source
# File lib/commands/plugin/plugin.rb, line 29
def install(method=nil, options = {})
  options = {:version => "bleeding_edge"}.merge(options)

  method ||= rails_env.best_install_method?
  method   = :export if method == :http and svn_url?

  uninstall if installed? and options[:force]

  unless installed?
    send("install_using_#{method}", options)
    run_install_hook
  else
    puts "already installed: #{name} (#{uri}).  pass --force to reinstall"
  end
end
installed?() click to toggle source
# File lib/commands/plugin/plugin.rb, line 24
def installed?
  File.directory?("#{rails_env.root}/vendor/plugins/#{name}")        or rails_env.externals.detect{ |name, repo| self.uri == repo }
end
svn_url?() click to toggle source
# File lib/commands/plugin/plugin.rb, line 20
def svn_url?
  @uri =~ /svn(?:\+ssh)?:\/\/*/
end
to_s() click to toggle source
# File lib/commands/plugin/plugin.rb, line 16
def to_s
  "#{@name.ljust(30)}#{@uri}"
end
uninstall() click to toggle source
# File lib/commands/plugin/plugin.rb, line 45
def uninstall
  path = "#{rails_env.root}/vendor/plugins/#{name}"
  if File.directory?(path)
    puts "Removing 'vendor/plugins/#{name}'" if $verbose
    run_uninstall_hook
    rm_r path
  else
    puts "Plugin doesn't exist: #{path}"
  end
  # clean up svn:externals
  externals = rails_env.externals
  externals.reject!{|n,u| name == n or name == u}
  rails_env.externals = externals
end

Private Instance Methods

check_for_versions() click to toggle source
# File lib/commands/plugin/plugin.rb, line 157
def check_for_versions
  if svn_url?
    check_svn_versions
  else
    check_http_versions
  end      
end
check_http_versions() click to toggle source
# File lib/commands/plugin/plugin.rb, line 170
def check_http_versions
  fetcher = get_http_fetcher(@uri)
  folders = fetcher.ls
  if has_trunk?(folders)
    @versions = {"bleeding_edge" => "trunk/"}
    if has_tags?(folders)
      tag_fetcher = get_http_fetcher("#{@uri}tags/")
      available_tags = tag_fetcher.ls.collect { |tag| tag.gsub("/", "") }
      available_tags.each { |tag| @versions[tag] = "tags/#{tag}/" }
    end
  else
    @versions = {"bleeding_edge" => ""}
  end
end
check_svn_versions() click to toggle source

TODO

# File lib/commands/plugin/plugin.rb, line 166
def check_svn_versions
  @versions = {"bleeding_edge" => ""}
end
get_http_fetcher(uri, quiet=true) click to toggle source
# File lib/commands/plugin/plugin.rb, line 151
def get_http_fetcher(uri, quiet=true)
  fetcher = RecursiveHTTPFetcher.new(uri)
  fetcher.quiet = quiet
  return fetcher
end
guess_name(url) click to toggle source
# File lib/commands/plugin/plugin.rb, line 140
def guess_name(url)
  @name = File.basename(url)
  if @name == 'trunk' || @name.empty?
    @name = File.basename(File.dirname(url))
  end
end
has_tags?(plugin_root_list) click to toggle source
# File lib/commands/plugin/plugin.rb, line 193
def has_tags?(plugin_root_list)
  plugin_root_list.grep(/tags/).size == 1
end
has_trunk?(plugin_root_list) click to toggle source
# File lib/commands/plugin/plugin.rb, line 189
def has_trunk?(plugin_root_list)
  plugin_root_list.grep(/trunk/).size == 1
end
install_using_checkout(options = {}) click to toggle source
# File lib/commands/plugin/plugin.rb, line 109
def install_using_checkout(options = {})
  svn_command :checkout, options
end
install_using_export(options = {}) click to toggle source
# File lib/commands/plugin/plugin.rb, line 105
def install_using_export(options = {})
  svn_command :export, options
end
install_using_externals(options = {}) click to toggle source
# File lib/commands/plugin/plugin.rb, line 113
def install_using_externals(options = {})
  externals = rails_env.externals
  externals.push([@name, version_uri(options[:version])])
  rails_env.externals = externals
  install_using_checkout(options)
end
install_using_http(options = {}) click to toggle source
# File lib/commands/plugin/plugin.rb, line 120
def install_using_http(options = {})
  root = rails_env.root
  mkdir_p "#{root}/vendor/plugins"
  Dir.chdir "#{root}/vendor/plugins"
  puts "fetching from '#{version_uri(options[:version])}'" if $verbose
  fetcher = get_http_fetcher(version_uri(options[:version]), options[:quiet])
  fetcher.fetch(version_uri(options[:version]))
  rename_version_to_name(options[:version], options[:quiet])
end
normalise_uri(uri) click to toggle source

make sure uris always have a trailing slash

# File lib/commands/plugin/plugin.rb, line 198
def normalise_uri(uri)
  return (uri[-1, 1] == "/" ? uri : uri + '/')
end
rails_env() click to toggle source
# File lib/commands/plugin/plugin.rb, line 147
def rails_env
  @rails_env || RailsEnvironment.default
end
rename_version_to_name(version, quiet=false) click to toggle source
# File lib/commands/plugin/plugin.rb, line 202
def rename_version_to_name(version, quiet=false)
  return if @versions[version].empty?
  installed_folder = (version == "bleeding_edge") ? "trunk" : version
  puts "+ Renaming #{installed_folder} to #{name}" unless quiet and not $verbose
  FileUtils.mv("#{rails_env.root}/vendor/plugins/#{installed_folder}",
               "#{rails_env.root}/vendor/plugins/#{name}")
end
run_install_hook() click to toggle source
# File lib/commands/plugin/plugin.rb, line 95
def run_install_hook
  install_hook_file = "#{rails_env.root}/vendor/plugins/#{name}/install.rb"
  load install_hook_file if File.exists? install_hook_file
end
run_uninstall_hook() click to toggle source
# File lib/commands/plugin/plugin.rb, line 100
def run_uninstall_hook
  uninstall_hook_file = "#{rails_env.root}/vendor/plugins/#{name}/uninstall.rb"
  load uninstall_hook_file if File.exists? uninstall_hook_file
end
svn_command(cmd, options = {}) click to toggle source
# File lib/commands/plugin/plugin.rb, line 130
def svn_command(cmd, options = {})
  root = rails_env.root
  mkdir_p "#{root}/vendor/plugins"
  base_cmd = "svn #{cmd} #{version_uri(options[:version])} \"#{root}/vendor/plugins/#{name}\""
  base_cmd += ' -q' if options[:quiet] and not $verbose
  base_cmd += " -r #{options[:revision]}" if options[:revision] and (options[:version] == "bleeding_edge")
  puts base_cmd if $verbose
  system(base_cmd)
end
version_uri(version) click to toggle source
# File lib/commands/plugin/plugin.rb, line 185
def version_uri(version)
  @uri + @versions[version]
end