/
abstract_repository.rb
256 lines (217 loc) · 7.15 KB
/
abstract_repository.rb
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
module Sashimi
class PluginNotFound < StandardError #:nodoc:
def initialize(plugin_name, message = nil)
@plugin_name = plugin_name
@message = message
end
def to_s
@message || @plugin_name + " isn't in the local repository."
end
end
class AbstractRepository
@@local_repository_sub_path = File.join('.rails', 'plugins')
@@cache_file = '.plugins'
attr_accessor :plugin
def initialize(plugin)
self.plugin = plugin
end
# Remove the repository
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
# Add to a Rails app.
def add
puts plugin.name.titleize + "\n"
copy_plugin_to_rails_app
remove_hidden_folders
run_install_hook
end
class << self
def instantiate_repository(plugin)
unless plugin.name.nil?
instantiate_repository_by_cache(plugin)
else
instantiate_repository_by_url(plugin)
end.new(plugin)
end
# Return all installed plugin names and summary, formatted for stdout.
def list
cache_content.sort.collect do |plugin, contents|
"#{plugin}\t\t#{contents['summary']}"
end.join("\n")
end
# Return all installed plugins names.
def plugins_names
cache_content.keys.sort
end
# Update the plugins installed in a rails app.
def update_rails_plugins(plugins_names)
change_dir(path_to_rails_app)
update_unversioned_rails_plugins(plugins_names) unless under_version_control?
end
# Update the plugins installed in a non versioned rails app.
def update_unversioned_rails_plugins(plugins_names)
change_dir(plugins_dir)
plugins_names.each do |plugin_name|
FileUtils.rm_rf(plugin_name)
Plugin.new(plugin_name).add
end
end
def local_repository_path #:nodoc:
@local_repository_path ||= File.join(find_home, @@local_repository_sub_path)
end
def cache_file #:nodoc:
@@cache_file
end
# Return the path to the Rails app where the user launched sashimi command.
def path_to_rails_app
$rails_app
end
# Read the cache file and return the content as an <tt>Hash</tt>.
def cache_content
change_dir_to_local_repository
@@cache_content ||= (YAML::load_file(cache_file) || {}).to_hash
end
def instantiate_repository_by_url(plugin)
git_url?(plugin.url) ? GitRepository : SvnRepository
end
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
# Changes the current directory with the given one
def change_dir(dir)
FileUtils.cd(dir)
end
# Change the current directory with local_repository_path
def change_dir_to_local_repository
change_dir(local_repository_path)
end
# Rails app plugins dir
def plugins_dir
@@plugins_dir ||= File.join('vendor', 'plugins')
end
# Check if the current working directory is under version control
def under_version_control?
!Dir.glob("{.git, .svn}").empty?
end
# Find the user home directory
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
# Try to guess if it's a Git url.
def git_url?(url)
url =~ /^git:\/\// || url =~ /\.git$/
end
end
# Return the SCM type
#
# Subversion # => svn
# Git # => git
def scm_type
self.class.name.demodulize.gsub(/Repository$/, '').downcase
end
# Read the content of the <tt>about.yml</tt>.
# New feature of Rails 2.1.x http:://dev.rubyonrails.org/changeset/9098
def about
change_dir_to_plugin_path
return {} unless File.exists?('about.yml')
(YAML::load_file('about.yml') || {}).to_hash
end
private
# Proxy for <tt>AbstractRepository#change_dir</tt>
def change_dir(dir)
self.class.change_dir(dir)
end
# Proxy for <tt>AbstractRepository#change_dir_to_local_repository</tt>
def change_dir_to_local_repository
self.class.change_dir_to_local_repository
end
# Change the current directory with the plugin one
def change_dir_to_plugin_path
change_dir(File.join(local_repository_path, plugin.name || plugin.guess_name))
end
# Proxy for <tt>AbstractRepository#local_repository_path</tt>
def local_repository_path
self.class.local_repository_path
end
# Proxy for <tt>AbstractRepository#cache_file</tt>
def cache_file
self.class.cache_file
end
# Proxy for <tt>AbstractRepository#cache_content</tt>
def cache_content
self.class.cache_content
end
# Proxy for <tt>AbstractRepository#plugins_dir</tt>
def plugins_dir
self.class.plugins_dir
end
# Proxy for <tt>AbstractRepository#path_to_rails_app</tt>
def path_to_rails_app
self.class.path_to_rails_app
end
# Prepare the plugin installation
def prepare_installation
FileUtils.mkdir_p(local_repository_path)
change_dir_to_local_repository
FileUtils.touch(cache_file)
end
# Add a plugin into the cache
def add_to_cache(plugin)
write_to_cache cache_content.to_hash.merge!(plugin)
end
# Remove a plugin from the cache
def remove_from_cache
cache_content.delete(plugin.name)
write_to_cache cache_content
end
# Write all the plugins into the cache
def write_to_cache(plugins)
change_dir_to_local_repository
FileUtils.mv(cache_file, "#{cache_file}-backup")
File.open(cache_file, 'w'){|f| f.write(plugins.to_yaml)}
FileUtils.rm("#{cache_file}-backup")
end
# Copy a plugin to a Rails app.
def copy_plugin_to_rails_app
change_dir(path_to_rails_app)
FileUtils.mkdir_p(plugins_dir)
FileUtils.cp_r(File.join(local_repository_path, plugin.name), File.join(plugins_dir, plugin.name))
end
# Remove SCM hidden folders.
def remove_hidden_folders
require 'find'
change_dir(File.join(path_to_rails_app, plugins_dir, plugin.name))
Find.find('./') do |path|
if File.basename(path) == '.'+scm_type
FileUtils.remove_dir(path, true)
Find.prune
end
end
end
# Run the plugin install hook.
def run_install_hook
install_hook_file = File.join(plugins_dir, plugin.name, 'install.rb')
load install_hook_file if File.exist? install_hook_file
end
end
end