Class: Sashimi::AbstractRepository

Inherits:
Object
  • Object
show all
Defined in:
lib/sashimi/repositories/abstract_repository.rb

Direct Known Subclasses

GitRepository, SvnRepository

Constant Summary collapse

@@local_repository_sub_path =
File.join('.rails', 'plugins')
@@cache_file =
'.plugins'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(plugin) ⇒ AbstractRepository

Returns a new instance of AbstractRepository.



18
19
20
# File 'lib/sashimi/repositories/abstract_repository.rb', line 18

def initialize(plugin)
  self.plugin = plugin
end

Instance Attribute Details

#pluginObject

Returns the value of attribute plugin.



16
17
18
# File 'lib/sashimi/repositories/abstract_repository.rb', line 16

def plugin
  @plugin
end

Class Method Details

.cache_contentObject

Read the cache file and return the content as an Hash.



67
68
69
70
# File 'lib/sashimi/repositories/abstract_repository.rb', line 67

def cache_content
  change_dir_to_local_repository
  @@cache_content ||= (YAML::load_file(cache_file) || {}).to_hash
end

.cache_fileObject

:nodoc:



57
58
59
# File 'lib/sashimi/repositories/abstract_repository.rb', line 57

def cache_file #:nodoc:
  @@cache_file
end

.change_dir(dir) ⇒ Object

Changes the current directory with the given one



83
84
85
# File 'lib/sashimi/repositories/abstract_repository.rb', line 83

def change_dir(dir)
  FileUtils.cd(dir)
end

.change_dir_to_local_repositoryObject

Change the current directory with local_repository_path



88
89
90
# File 'lib/sashimi/repositories/abstract_repository.rb', line 88

def change_dir_to_local_repository
  change_dir(local_repository_path)
end

.find_homeObject

Find the user home directory



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/sashimi/repositories/abstract_repository.rb', line 98

def find_home
  ['HOME', 'USERPROFILE'].each do |homekey|
    return ENV[homekey] if ENV[homekey]
  end
  if ENV['HOMEDRIVE'] && ENV['HOMEPATH']
    return "#{ENV['HOMEDRIVE']}:#{ENV['HOMEPATH']}"
  end
  begin
    File.expand_path("~")
  rescue StandardError => ex
    if File::ALT_SEPARATOR
      "C:/"
    else
      "/"
    end
  end
end

.git_url?(url) ⇒ Boolean

Try to guess if it’s a Git url.

Returns:

  • (Boolean)


117
118
119
# File 'lib/sashimi/repositories/abstract_repository.rb', line 117

def git_url?(url)
  url =~ /^git:\/\// || url =~ /\.git$/
end

.instantiate_repository(plugin) ⇒ Object



38
39
40
41
42
43
44
# File 'lib/sashimi/repositories/abstract_repository.rb', line 38

def instantiate_repository(plugin)
  unless plugin.name.nil?
    instantiate_repository_by_cache(plugin)
  else
    instantiate_repository_by_url(plugin)
  end.new(plugin)
end

.instantiate_repository_by_cache(plugin) ⇒ Object

Raises:



76
77
78
79
80
# File 'lib/sashimi/repositories/abstract_repository.rb', line 76

def instantiate_repository_by_cache(plugin)
  cached_plugin = cache_content[plugin.name]
  raise PluginNotFound.new(plugin.name) if cached_plugin.nil?
  cached_plugin['type'] == 'git' ? GitRepository : SvnRepository
end

.instantiate_repository_by_url(plugin) ⇒ Object



72
73
74
# File 'lib/sashimi/repositories/abstract_repository.rb', line 72

def instantiate_repository_by_url(plugin)
  git_url?(plugin.url) ? GitRepository : SvnRepository
end

.listObject

Return all installed plugin names



47
48
49
50
51
# File 'lib/sashimi/repositories/abstract_repository.rb', line 47

def list
  cache_content.sort.collect do |plugin, contents|
    "#{plugin}\t\t#{contents['summary']}"
  end.join("\n")
end

.local_repository_pathObject

:nodoc:



53
54
55
# File 'lib/sashimi/repositories/abstract_repository.rb', line 53

def local_repository_path #:nodoc:
  @local_repository_path ||= File.join(find_home, @@local_repository_sub_path) 
end

.path_to_rails_appObject

Return the path to the Rails app where the user launched sashimi command.



62
63
64
# File 'lib/sashimi/repositories/abstract_repository.rb', line 62

def path_to_rails_app
  $rails_app
end

.plugins_dirObject

Rails app plugins dir



93
94
95
# File 'lib/sashimi/repositories/abstract_repository.rb', line 93

def plugins_dir
  @@plugins_dir ||= File.join('vendor', 'plugins')
end

Instance Method Details

#aboutObject

Read the content of the about.yml. New feature of Rails 2.1.x ://dev.rubyonrails.org/changeset/9098



132
133
134
135
136
# File 'lib/sashimi/repositories/abstract_repository.rb', line 132

def about
  change_dir_to_plugin_path
  return {} unless File.exists?('about.yml')
  (YAML::load_file('about.yml') || {}).to_hash
end

#addObject

Add to a Rails app.



31
32
33
34
35
# File 'lib/sashimi/repositories/abstract_repository.rb', line 31

def add
  copy_plugin_to_rails_app
  remove_hidden_folders
  run_install_hook
end

#scm_typeObject

Return the SCM type

Subversion   # => svn
Git          # => git


126
127
128
# File 'lib/sashimi/repositories/abstract_repository.rb', line 126

def scm_type
  self.class.name.demodulize.gsub(/Repository$/, '').downcase
end

#uninstallObject

Remove the repository

Raises:



23
24
25
26
27
28
# File 'lib/sashimi/repositories/abstract_repository.rb', line 23

def uninstall
  change_dir_to_local_repository
  raise PluginNotFound.new(plugin.name) unless File.exists?(plugin.name)
  FileUtils.rm_rf(plugin.name)
  remove_from_cache
end