Class: Sashimi::AbstractRepository

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

Direct Known Subclasses

GitRepository, SvnRepository

Constant Summary collapse

TEMP_SUFFIX =
'-tmp'
@@plugins_path =

It’s the path where the plugins are stored.

".rails/plugins".to_path
@@rails_plugins_path =

Conventional path where Rails applications use to place their plugins.

"vendor/plugins".to_path
@@cache_file =

Cache file used to store informations about installed plugins.

'.plugins'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(plugin) ⇒ AbstractRepository

Returns a new instance of AbstractRepository.



42
43
44
# File 'lib/sashimi/repositories/abstract_repository.rb', line 42

def initialize(plugin)
  @plugin = plugin
end

Instance Attribute Details

#pluginObject (readonly)

Returns the value of attribute plugin.



40
41
42
# File 'lib/sashimi/repositories/abstract_repository.rb', line 40

def plugin
  @plugin
end

Class Method Details

.absolute_rails_plugins_pathObject

It’s absolute path of plugins in your Rails app.

path/to/rails_app # => /Users/luca/path/to/rails_app/vendor/plugins


180
181
182
183
# File 'lib/sashimi/repositories/abstract_repository.rb', line 180

def absolute_rails_plugins_path
  @@absolute_rails_plugins_path = [ path_to_rails_app,
    rails_plugins_path ].to_path(true)
end

.cache_contentObject Also known as: list

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



156
157
158
159
160
# File 'lib/sashimi/repositories/abstract_repository.rb', line 156

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

.find_homeObject

Find the user home directory



196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/sashimi/repositories/abstract_repository.rb', line 196

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)


215
216
217
# File 'lib/sashimi/repositories/abstract_repository.rb', line 215

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

.guess_version_control_systemObject

Guess the version control system for the current working directory.



191
192
193
# File 'lib/sashimi/repositories/abstract_repository.rb', line 191

def guess_version_control_system
  File.exists?('.git') ? :git : :svn
end

.instantiate_repository(plugin) ⇒ Object

Instantiate a repository (Svn or Git) for the given plugin.

If the plugin was already installed it load from the cache, otherwise it try to guess information from its url.



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

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

Instantiate a repository for the given plugin, loading informations from the cache.

Raises:



171
172
173
174
175
# File 'lib/sashimi/repositories/abstract_repository.rb', line 171

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

Istantiate a Repository for the given plugin, guessing if its url refers to a Svn or Git repository.



165
166
167
# File 'lib/sashimi/repositories/abstract_repository.rb', line 165

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

.local_repository_pathObject

It’s the absolute path where the plugins are stored.

Typically, it’s under your home directory.



146
147
148
# File 'lib/sashimi/repositories/abstract_repository.rb', line 146

def local_repository_path #:nodoc:
  @local_repository_path ||= [ find_home, @@plugins_path ].to_path
end

.path_to_rails_appObject

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



151
152
153
# File 'lib/sashimi/repositories/abstract_repository.rb', line 151

def path_to_rails_app
  $rails_app
end

.plugins_namesObject

Return all installed plugins names.



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

def plugins_names
  cache_content.keys.sort
end

.scm_add(file) ⇒ Object

Schedules an add for the given file on the current SCM system used by the Rails app.



128
129
130
# File 'lib/sashimi/repositories/abstract_repository.rb', line 128

def scm_add(file)
  scm_command(:add, file)
end

.scm_command(command, file) ⇒ Object

Execute the given command for the current SCM system used by the Rails app.



138
139
140
141
# File 'lib/sashimi/repositories/abstract_repository.rb', line 138

def scm_command(command, file)
  scm = guess_version_control_system
  Kernel.system("#{scm} #{command} #{file}")
end

.scm_remove(file) ⇒ Object

Schedules a remove for the given file on the current SCM system used by the Rails app.



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

def scm_remove(file)
  scm_command(:rm, file)
end

.under_version_control?Boolean

Check if the current working directory is under version control

Returns:

  • (Boolean)


186
187
188
# File 'lib/sashimi/repositories/abstract_repository.rb', line 186

def under_version_control?
  !Dir.glob(".{git,svn}").empty?
end

.update_rails_plugins(plugins_names) ⇒ Object

Update the plugins installed in a rails app.

If the application is under version control, Sashimi cares about to schedule add/remove plugin’s files from/to your SCM of choice.



92
93
94
95
96
97
98
# File 'lib/sashimi/repositories/abstract_repository.rb', line 92

def update_rails_plugins(plugins_names)
  if under_version_control?
    update_versioned_rails_plugins(plugins_names)
  else
    update_unversioned_rails_plugins(plugins_names)
  end
end

.update_unversioned_rails_plugins(plugins_names) ⇒ Object

Update the plugins installed in a not versioned Rails app.



101
102
103
104
105
106
# File 'lib/sashimi/repositories/abstract_repository.rb', line 101

def update_unversioned_rails_plugins(plugins_names)
  plugins_names.each do |plugin_name|
    FileUtils.rm_rf [ absolute_rails_plugins_path, plugin_name ].to_path
    Plugin.new(plugin_name).add
  end
end

.update_versioned_rails_plugins(plugins_names) ⇒ Object

Update the plugins installed in a versioned Rails app.



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/sashimi/repositories/abstract_repository.rb', line 109

def update_versioned_rails_plugins(plugins_names)
  with_path absolute_rails_plugins_path do
    plugins_names.each do |plugin_name|
      raise PluginNotFound.new(plugin_name) unless File.exists?(plugin_name)
      repository = Plugin.new(plugin_name).repository
      repository.copy_plugin_and_remove_hidden_folders
      files_scheduled_for_remove = repository.files_scheduled_for_remove
      files_scheduled_for_add    = repository.files_scheduled_for_add
      FileUtils.cp_r(repository.temp_plugin_name+'/.', plugin_name)
      repository.remove_temp_folder
      with_path plugin_name do
        files_scheduled_for_remove.each {|file| scm_remove file}
        files_scheduled_for_add.each {|file| scm_add file}
      end
    end
  end
end

.with_path(path, &block) ⇒ Object

Yield the given block executing it in the specified path



220
221
222
223
224
225
226
227
228
# File 'lib/sashimi/repositories/abstract_repository.rb', line 220

def with_path(path, &block)
  begin
    old_path = Dir.pwd
    FileUtils.cd(path)
    yield
  ensure
    FileUtils.cd(old_path)
  end
end

Instance Method Details

#aboutObject

Read the content of the about.yml.

New feature of Rails 2.1.0 github.com/rails/rails/commit/f5b991d76dc5d21f1870da067fb3101440256fba



242
243
244
245
246
# File 'lib/sashimi/repositories/abstract_repository.rb', line 242

def about
  with_path plugin_path do
    (YAML::load_file('about.yml') rescue {}).to_hash
  end
end

#addObject

Add the current plugin to a Rails app.

Raises:



56
57
58
59
60
61
62
# File 'lib/sashimi/repositories/abstract_repository.rb', line 56

def add
  raise PluginNotFound.new(plugin.name) unless cache_content.keys.include?(plugin.name)
  puts plugin.name.titleize + "\n"
  copy_plugin_and_remove_hidden_folders
  rename_temp_folder
  run_install_hook
end

#copy_plugin_and_remove_hidden_foldersObject

Copy a plugin to a Rails app and remove SCM hidden folders.



65
66
67
68
# File 'lib/sashimi/repositories/abstract_repository.rb', line 65

def copy_plugin_and_remove_hidden_folders
  copy_plugin_to_rails_app
  remove_hidden_folders
end

#files_scheduled_for_addObject

Returns a list of files that should be scheduled for SCM add.



249
250
251
252
# File 'lib/sashimi/repositories/abstract_repository.rb', line 249

def files_scheduled_for_add
  Dir[temp_plugin_name+"/**/*"].collect {|fn| fn.gsub(temp_plugin_name, '.')} -
    Dir[plugin.name+"/**/*"].collect{|fn| fn.gsub(plugin.name, '.')}
end

#files_scheduled_for_removeObject

Returns a list of files that should be scheduled for SCM remove.



255
256
257
258
# File 'lib/sashimi/repositories/abstract_repository.rb', line 255

def files_scheduled_for_remove
  Dir[plugin.name+"/**/*"].collect {|fn| fn.gsub(plugin.name, '.')} -
    Dir[temp_plugin_name+"/**/*"].collect {|fn| fn.gsub(temp_plugin_name, '.')}
end

#remove_temp_folderObject

Remove the temp folder, used by update process.



261
262
263
# File 'lib/sashimi/repositories/abstract_repository.rb', line 261

def remove_temp_folder
  FileUtils.rm_rf [ absolute_rails_plugins_path, temp_plugin_name ].to_path
end

#scm_typeObject

Return the SCM type

Subversion   # => svn
Git          # => git


235
236
237
# File 'lib/sashimi/repositories/abstract_repository.rb', line 235

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

#temp_plugin_nameObject

Returns the name used for temporary plugin folder.



266
267
268
# File 'lib/sashimi/repositories/abstract_repository.rb', line 266

def temp_plugin_name
  plugin.name + TEMP_SUFFIX
end

#uninstallObject

Uninstall the current plugin.



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

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