Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 545 lines (473 sloc) 15.684 kb
0a735ca @dhh Added script/plugin to manage plugins (install, remove, list, etc) [R…
dhh authored
1 # Rails Plugin Manager.
2 #
3 # Installing plugins:
4 #
f44a0b1 @romdi fix usage examples and more to use new invocations
romdi authored
5 # $ rails plugin install continuous_builder asset_timestamping
ff44cc2 @amatsuda whitespace
amatsuda authored
6 #
5c08607 @tpope Make script/plugin install <plugin> -r <revision> option work with gi…
tpope authored
7 # Specifying revisions:
8 #
9 # * Subversion revision is a single integer.
10 #
11 # * Git revision format:
12 # - full - 'refs/tags/1.8.0' or 'refs/heads/experimental'
13 # - short: 'experimental' (equivalent to 'refs/heads/experimental')
14 # 'tag 1.8.0' (equivalent to 'refs/tags/1.8.0')
15 #
16 #
ff44cc2 @amatsuda whitespace
amatsuda authored
17 # This is Free Software, copyright 2005 by Ryan Tomayko (rtomayko@gmail.com)
5500d13 @dhh Patches go to trac now
dhh authored
18 # and is licensed MIT: (http://www.opensource.org/licenses/mit-license.php)
0a735ca @dhh Added script/plugin to manage plugins (install, remove, list, etc) [R…
dhh authored
19
20 $verbose = false
08acb95 @jeremy Improve script/plugin on Windows. Closes #2646.
jeremy authored
21
d3275ca @sstephenson Enable HTTP installation of plugins when svn isn't avaialable. Closes…
sstephenson authored
22 require 'open-uri'
0a735ca @dhh Added script/plugin to manage plugins (install, remove, list, etc) [R…
dhh authored
23 require 'fileutils'
24 require 'tempfile'
25
26 include FileUtils
27
28 class RailsEnvironment
29 attr_reader :root
30
31 def initialize(dir)
32 @root = dir
33 end
21d50e4 @jeremy script/plugin: handle root paths and plugin names which contain space…
jeremy authored
34
0a735ca @dhh Added script/plugin to manage plugins (install, remove, list, etc) [R…
dhh authored
35 def self.find(dir=nil)
36 dir ||= pwd
37 while dir.length > 1
38 return new(dir) if File.exist?(File.join(dir, 'config', 'environment.rb'))
39 dir = File.dirname(dir)
40 end
41 end
ff44cc2 @amatsuda whitespace
amatsuda authored
42
0a735ca @dhh Added script/plugin to manage plugins (install, remove, list, etc) [R…
dhh authored
43 def self.default
44 @default ||= find
45 end
ff44cc2 @amatsuda whitespace
amatsuda authored
46
0a735ca @dhh Added script/plugin to manage plugins (install, remove, list, etc) [R…
dhh authored
47 def self.default=(rails_env)
48 @default = rails_env
49 end
ff44cc2 @amatsuda whitespace
amatsuda authored
50
0a735ca @dhh Added script/plugin to manage plugins (install, remove, list, etc) [R…
dhh authored
51 def install(name_uri_or_plugin)
52 if name_uri_or_plugin.is_a? String
ff44cc2 @amatsuda whitespace
amatsuda authored
53 if name_uri_or_plugin =~ /:\/\//
0a735ca @dhh Added script/plugin to manage plugins (install, remove, list, etc) [R…
dhh authored
54 plugin = Plugin.new(name_uri_or_plugin)
55 else
56 plugin = Plugins[name_uri_or_plugin]
57 end
58 else
59 plugin = name_uri_or_plugin
60 end
33733d5 @neerajdotname no need to check for nil
neerajdotname authored
61 if plugin
0a735ca @dhh Added script/plugin to manage plugins (install, remove, list, etc) [R…
dhh authored
62 plugin.install
63 else
00cecf8 @dhh Fixed that installing plugins from SVN repositories that use trunk/ w…
dhh authored
64 puts "Plugin not found: #{name_uri_or_plugin}"
0a735ca @dhh Added script/plugin to manage plugins (install, remove, list, etc) [R…
dhh authored
65 end
66 end
ff44cc2 @amatsuda whitespace
amatsuda authored
67
d3275ca @sstephenson Enable HTTP installation of plugins when svn isn't avaialable. Closes…
sstephenson authored
68 def use_svn?
9318131 @jeremy Eliminate Subversion dependencies in scripts/plugin. Correct install…
jeremy authored
69 require 'active_support/core_ext/kernel'
70 silence_stderr {`svn --version` rescue nil}
3c8d425 @dhh Fail gracefully from svn checks (closes #2700) [Rick Olson]
dhh authored
71 !$?.nil? && $?.success?
d3275ca @sstephenson Enable HTTP installation of plugins when svn isn't avaialable. Closes…
sstephenson authored
72 end
73
0a735ca @dhh Added script/plugin to manage plugins (install, remove, list, etc) [R…
dhh authored
74 def use_externals?
9318131 @jeremy Eliminate Subversion dependencies in scripts/plugin. Correct install…
jeremy authored
75 use_svn? && File.directory?("#{root}/vendor/plugins/.svn")
0a735ca @dhh Added script/plugin to manage plugins (install, remove, list, etc) [R…
dhh authored
76 end
21d50e4 @jeremy script/plugin: handle root paths and plugin names which contain space…
jeremy authored
77
0a735ca @dhh Added script/plugin to manage plugins (install, remove, list, etc) [R…
dhh authored
78 def use_checkout?
79 # this is a bit of a guess. we assume that if the rails environment
9935a35 @technoweenie Enhances plugin#discover allowing it to discover svn:// like URIs (cl…
technoweenie authored
80 # is under subversion then they probably want the plugin checked out
0a735ca @dhh Added script/plugin to manage plugins (install, remove, list, etc) [R…
dhh authored
81 # instead of exported. This can be overridden on the command line
82 File.directory?("#{root}/.svn")
83 end
21d50e4 @jeremy script/plugin: handle root paths and plugin names which contain space…
jeremy authored
84
0a735ca @dhh Added script/plugin to manage plugins (install, remove, list, etc) [R…
dhh authored
85 def best_install_method
d3275ca @sstephenson Enable HTTP installation of plugins when svn isn't avaialable. Closes…
sstephenson authored
86 return :http unless use_svn?
0a735ca @dhh Added script/plugin to manage plugins (install, remove, list, etc) [R…
dhh authored
87 case
88 when use_externals? then :externals
89 when use_checkout? then :checkout
90 else :export
91 end
92 end
93
94 def externals
95 return [] unless use_externals?
21d50e4 @jeremy script/plugin: handle root paths and plugin names which contain space…
jeremy authored
96 ext = `svn propget svn:externals "#{root}/vendor/plugins"`
e609d83 @jeffs Fixed an incompatibility with Ruby 1.9.
jeffs authored
97 lines = ext.respond_to?(:lines) ? ext.lines : ext
98 lines.reject{ |line| line.strip == '' }.map do |line|
ff44cc2 @amatsuda whitespace
amatsuda authored
99 line.strip.split(/\s+/, 2)
0a735ca @dhh Added script/plugin to manage plugins (install, remove, list, etc) [R…
dhh authored
100 end
101 end
21d50e4 @jeremy script/plugin: handle root paths and plugin names which contain space…
jeremy authored
102
0a735ca @dhh Added script/plugin to manage plugins (install, remove, list, etc) [R…
dhh authored
103 def externals=(items)
104 unless items.is_a? String
105 items = items.map{|name,uri| "#{name.ljust(29)} #{uri.chomp('/')}"}.join("\n")
106 end
107 Tempfile.open("svn-set-prop") do |file|
108 file.write(items)
109 file.flush
bdc1033 @technoweenie Fix plugin install bug at dir with space. (closes #5359) [Yoshimasa N…
technoweenie authored
110 system("svn propset -q svn:externals -F \"#{file.path}\" \"#{root}/vendor/plugins\"")
0a735ca @dhh Added script/plugin to manage plugins (install, remove, list, etc) [R…
dhh authored
111 end
112 end
113 end
114
115 class Plugin
116 attr_reader :name, :uri
ff44cc2 @amatsuda whitespace
amatsuda authored
117
11f53db @lifo Remove outdated script/plugin options
lifo authored
118 def initialize(uri, name = nil)
0a735ca @dhh Added script/plugin to manage plugins (install, remove, list, etc) [R…
dhh authored
119 @uri = uri
120 guess_name(uri)
121 end
ff44cc2 @amatsuda whitespace
amatsuda authored
122
9bfcfa8 @technoweenie preliminary support for plugin meta files
technoweenie authored
123 def self.find(name)
11f53db @lifo Remove outdated script/plugin options
lifo authored
124 new(name)
9bfcfa8 @technoweenie preliminary support for plugin meta files
technoweenie authored
125 end
ff44cc2 @amatsuda whitespace
amatsuda authored
126
0a735ca @dhh Added script/plugin to manage plugins (install, remove, list, etc) [R…
dhh authored
127 def to_s
128 "#{@name.ljust(30)}#{@uri}"
129 end
ff44cc2 @amatsuda whitespace
amatsuda authored
130
9bfcfa8 @technoweenie preliminary support for plugin meta files
technoweenie authored
131 def svn_url?
e7fe1c4 @sstephenson Make "script/plugin install" work with svn+ssh URLs
sstephenson authored
132 @uri =~ /svn(?:\+ssh)?:\/\/*/
9bfcfa8 @technoweenie preliminary support for plugin meta files
technoweenie authored
133 end
ff44cc2 @amatsuda whitespace
amatsuda authored
134
d07d6e9 @dhh Added support for installing plugins hosted at git repositories (clos…
dhh authored
135 def git_url?
1b0654e @tekin Ensure script/plugin installs ssh/git plugins. [#277 state:resolved]
tekin authored
136 @uri =~ /^git:\/\// || @uri =~ /\.git$/
d07d6e9 @dhh Added support for installing plugins hosted at git repositories (clos…
dhh authored
137 end
ff44cc2 @amatsuda whitespace
amatsuda authored
138
0a735ca @dhh Added script/plugin to manage plugins (install, remove, list, etc) [R…
dhh authored
139 def installed?
140 File.directory?("#{rails_env.root}/vendor/plugins/#{name}") \
141 or rails_env.externals.detect{ |name, repo| self.uri == repo }
142 end
ff44cc2 @amatsuda whitespace
amatsuda authored
143
9318131 @jeremy Eliminate Subversion dependencies in scripts/plugin. Correct install…
jeremy authored
144 def install(method=nil, options = {})
0a735ca @dhh Added script/plugin to manage plugins (install, remove, list, etc) [R…
dhh authored
145 method ||= rails_env.best_install_method?
d07d6e9 @dhh Added support for installing plugins hosted at git repositories (clos…
dhh authored
146 if :http == method
147 method = :export if svn_url?
5c08607 @tpope Make script/plugin install <plugin> -r <revision> option work with gi…
tpope authored
148 method = :git if git_url?
d07d6e9 @dhh Added support for installing plugins hosted at git repositories (clos…
dhh authored
149 end
9318131 @jeremy Eliminate Subversion dependencies in scripts/plugin. Correct install…
jeremy authored
150
151 uninstall if installed? and options[:force]
152
0a735ca @dhh Added script/plugin to manage plugins (install, remove, list, etc) [R…
dhh authored
153 unless installed?
9318131 @jeremy Eliminate Subversion dependencies in scripts/plugin. Correct install…
jeremy authored
154 send("install_using_#{method}", options)
4f1e37c Add install.rb file to plugin generation which is loaded, if it exist…
Marcel Molina authored
155 run_install_hook
0a735ca @dhh Added script/plugin to manage plugins (install, remove, list, etc) [R…
dhh authored
156 else
9318131 @jeremy Eliminate Subversion dependencies in scripts/plugin. Correct install…
jeremy authored
157 puts "already installed: #{name} (#{uri}). pass --force to reinstall"
0a735ca @dhh Added script/plugin to manage plugins (install, remove, list, etc) [R…
dhh authored
158 end
159 end
9318131 @jeremy Eliminate Subversion dependencies in scripts/plugin. Correct install…
jeremy authored
160
161 def uninstall
162 path = "#{rails_env.root}/vendor/plugins/#{name}"
163 if File.directory?(path)
ff7be74 @dhh Added more information to script/plugin's doings to ease debugging #3…
dhh authored
164 puts "Removing 'vendor/plugins/#{name}'" if $verbose
59432fe @dhh Added uninstall.rb hook to plugin handling, such that plugins have a …
dhh authored
165 run_uninstall_hook
9318131 @jeremy Eliminate Subversion dependencies in scripts/plugin. Correct install…
jeremy authored
166 rm_r path
167 else
168 puts "Plugin doesn't exist: #{path}"
169 end
93b98cb @lifo Ensure script/plugin doesn't show error message when svn externals ar…
lifo authored
170
171 if rails_env.use_externals?
172 # clean up svn:externals
173 externals = rails_env.externals
ff44cc2 @amatsuda whitespace
amatsuda authored
174 externals.reject!{|n, u| name == n or name == u}
93b98cb @lifo Ensure script/plugin doesn't show error message when svn externals ar…
lifo authored
175 rails_env.externals = externals
176 end
9318131 @jeremy Eliminate Subversion dependencies in scripts/plugin. Correct install…
jeremy authored
177 end
178
9bfcfa8 @technoweenie preliminary support for plugin meta files
technoweenie authored
179 def info
9ac7afc @technoweenie Fix script/plugin about so it uses about.yml and not meta.yml. [Jame…
technoweenie authored
180 tmp = "#{rails_env.root}/_tmp_about.yml"
9bfcfa8 @technoweenie preliminary support for plugin meta files
technoweenie authored
181 if svn_url?
182 cmd = "svn export #{@uri} \"#{rails_env.root}/#{tmp}\""
183 puts cmd if $verbose
184 system(cmd)
185 end
9ac7afc @technoweenie Fix script/plugin about so it uses about.yml and not meta.yml. [Jame…
technoweenie authored
186 open(svn_url? ? tmp : File.join(@uri, 'about.yml')) do |stream|
9bfcfa8 @technoweenie preliminary support for plugin meta files
technoweenie authored
187 stream.read
9ac7afc @technoweenie Fix script/plugin about so it uses about.yml and not meta.yml. [Jame…
technoweenie authored
188 end rescue "No about.yml found in #{uri}"
9bfcfa8 @technoweenie preliminary support for plugin meta files
technoweenie authored
189 ensure
190 FileUtils.rm_rf tmp if svn_url?
191 end
192
ff44cc2 @amatsuda whitespace
amatsuda authored
193 private
4f1e37c Add install.rb file to plugin generation which is loaded, if it exist…
Marcel Molina authored
194
195 def run_install_hook
196 install_hook_file = "#{rails_env.root}/vendor/plugins/#{name}/install.rb"
b1ce7e4 @jeremy Ruby 1.9 compat: File.exists\? -> File.exist\? en masse. References #…
jeremy authored
197 load install_hook_file if File.exist? install_hook_file
4f1e37c Add install.rb file to plugin generation which is loaded, if it exist…
Marcel Molina authored
198 end
199
59432fe @dhh Added uninstall.rb hook to plugin handling, such that plugins have a …
dhh authored
200 def run_uninstall_hook
201 uninstall_hook_file = "#{rails_env.root}/vendor/plugins/#{name}/uninstall.rb"
b1ce7e4 @jeremy Ruby 1.9 compat: File.exists\? -> File.exist\? en masse. References #…
jeremy authored
202 load uninstall_hook_file if File.exist? uninstall_hook_file
59432fe @dhh Added uninstall.rb hook to plugin handling, such that plugins have a …
dhh authored
203 end
204
9318131 @jeremy Eliminate Subversion dependencies in scripts/plugin. Correct install…
jeremy authored
205 def install_using_export(options = {})
206 svn_command :export, options
0a735ca @dhh Added script/plugin to manage plugins (install, remove, list, etc) [R…
dhh authored
207 end
ff44cc2 @amatsuda whitespace
amatsuda authored
208
9318131 @jeremy Eliminate Subversion dependencies in scripts/plugin. Correct install…
jeremy authored
209 def install_using_checkout(options = {})
210 svn_command :checkout, options
0a735ca @dhh Added script/plugin to manage plugins (install, remove, list, etc) [R…
dhh authored
211 end
ff44cc2 @amatsuda whitespace
amatsuda authored
212
9318131 @jeremy Eliminate Subversion dependencies in scripts/plugin. Correct install…
jeremy authored
213 def install_using_externals(options = {})
0a735ca @dhh Added script/plugin to manage plugins (install, remove, list, etc) [R…
dhh authored
214 externals = rails_env.externals
215 externals.push([@name, uri])
216 rails_env.externals = externals
9318131 @jeremy Eliminate Subversion dependencies in scripts/plugin. Correct install…
jeremy authored
217 install_using_checkout(options)
0a735ca @dhh Added script/plugin to manage plugins (install, remove, list, etc) [R…
dhh authored
218 end
d3275ca @sstephenson Enable HTTP installation of plugins when svn isn't avaialable. Closes…
sstephenson authored
219
9318131 @jeremy Eliminate Subversion dependencies in scripts/plugin. Correct install…
jeremy authored
220 def install_using_http(options = {})
d3275ca @sstephenson Enable HTTP installation of plugins when svn isn't avaialable. Closes…
sstephenson authored
221 root = rails_env.root
00cecf8 @dhh Fixed that installing plugins from SVN repositories that use trunk/ w…
dhh authored
222 mkdir_p "#{root}/vendor/plugins/#{@name}"
223 Dir.chdir "#{root}/vendor/plugins/#{@name}" do
8887616 @technoweenie boot.rb sets RAILS_ROOT, so remove that. Install http plugins inside…
technoweenie authored
224 puts "fetching from '#{uri}'" if $verbose
00cecf8 @dhh Fixed that installing plugins from SVN repositories that use trunk/ w…
dhh authored
225 fetcher = RecursiveHTTPFetcher.new(uri, -1)
8887616 @technoweenie boot.rb sets RAILS_ROOT, so remove that. Install http plugins inside…
technoweenie authored
226 fetcher.quiet = true if options[:quiet]
227 fetcher.fetch
228 end
9318131 @jeremy Eliminate Subversion dependencies in scripts/plugin. Correct install…
jeremy authored
229 end
ff44cc2 @amatsuda whitespace
amatsuda authored
230
5c08607 @tpope Make script/plugin install <plugin> -r <revision> option work with gi…
tpope authored
231 def install_using_git(options = {})
232 root = rails_env.root
3ce16b4 @amatsuda Ruby 1.9 compat: Avoid using the return value of FileUtils.mkdir_p, a…
amatsuda authored
233 mkdir_p(install_path = "#{root}/vendor/plugins/#{name}")
5c08607 @tpope Make script/plugin install <plugin> -r <revision> option work with gi…
tpope authored
234 Dir.chdir install_path do
235 init_cmd = "git init"
236 init_cmd += " -q" if options[:quiet] and not $verbose
237 puts init_cmd if $verbose
238 system(init_cmd)
239 base_cmd = "git pull --depth 1 #{uri}"
240 base_cmd += " -q" if options[:quiet] and not $verbose
241 base_cmd += " #{options[:revision]}" if options[:revision]
242 puts base_cmd if $verbose
243 if system(base_cmd)
8bc0f90 @lifo script/plugin install should remove .gitignore too. [#1306 state:reso…
lifo authored
244 puts "removing: .git .gitignore" if $verbose
245 rm_rf %w(.git .gitignore)
5c08607 @tpope Make script/plugin install <plugin> -r <revision> option work with gi…
tpope authored
246 else
247 rm_rf install_path
248 end
249 end
d07d6e9 @dhh Added support for installing plugins hosted at git repositories (clos…
dhh authored
250 end
9318131 @jeremy Eliminate Subversion dependencies in scripts/plugin. Correct install…
jeremy authored
251
252 def svn_command(cmd, options = {})
253 root = rails_env.root
254 mkdir_p "#{root}/vendor/plugins"
21d50e4 @jeremy script/plugin: handle root paths and plugin names which contain space…
jeremy authored
255 base_cmd = "svn #{cmd} #{uri} \"#{root}/vendor/plugins/#{name}\""
9318131 @jeremy Eliminate Subversion dependencies in scripts/plugin. Correct install…
jeremy authored
256 base_cmd += ' -q' if options[:quiet] and not $verbose
257 base_cmd += " -r #{options[:revision]}" if options[:revision]
ff7be74 @dhh Added more information to script/plugin's doings to ease debugging #3…
dhh authored
258 puts base_cmd if $verbose
9318131 @jeremy Eliminate Subversion dependencies in scripts/plugin. Correct install…
jeremy authored
259 system(base_cmd)
d3275ca @sstephenson Enable HTTP installation of plugins when svn isn't avaialable. Closes…
sstephenson authored
260 end
261
0a735ca @dhh Added script/plugin to manage plugins (install, remove, list, etc) [R…
dhh authored
262 def guess_name(url)
263 @name = File.basename(url)
264 if @name == 'trunk' || @name.empty?
265 @name = File.basename(File.dirname(url))
266 end
d07d6e9 @dhh Added support for installing plugins hosted at git repositories (clos…
dhh authored
267 @name.gsub!(/\.git$/, '') if @name =~ /\.git$/
0a735ca @dhh Added script/plugin to manage plugins (install, remove, list, etc) [R…
dhh authored
268 end
ff44cc2 @amatsuda whitespace
amatsuda authored
269
0a735ca @dhh Added script/plugin to manage plugins (install, remove, list, etc) [R…
dhh authored
270 def rails_env
271 @rails_env || RailsEnvironment.default
272 end
273 end
274
275 # load default environment and parse arguments
276 require 'optparse'
325abe9 @dyba Substituted RailsCommands for Rails::Commands
dyba authored
277 module Rails
278 module Commands
279 class Plugin
280 attr_reader :environment, :script_name
281 def initialize
282 @environment = RailsEnvironment.default
283 @rails_root = RailsEnvironment.default.root
284 @script_name = File.basename($0)
285 end
ff44cc2 @amatsuda whitespace
amatsuda authored
286
325abe9 @dyba Substituted RailsCommands for Rails::Commands
dyba authored
287 def environment=(value)
288 @environment = value
289 RailsEnvironment.default = value
290 end
ff44cc2 @amatsuda whitespace
amatsuda authored
291
325abe9 @dyba Substituted RailsCommands for Rails::Commands
dyba authored
292 def options
293 OptionParser.new do |o|
294 o.set_summary_indent(' ')
295 o.banner = "Usage: plugin [OPTIONS] command"
296 o.define_head "Rails plugin manager."
ff44cc2 @amatsuda whitespace
amatsuda authored
297
325abe9 @dyba Substituted RailsCommands for Rails::Commands
dyba authored
298 o.separator ""
299 o.separator "GENERAL OPTIONS"
ff44cc2 @amatsuda whitespace
amatsuda authored
300
325abe9 @dyba Substituted RailsCommands for Rails::Commands
dyba authored
301 o.on("-r", "--root=DIR", String,
302 "Set an explicit rails app directory.",
303 "Default: #{@rails_root}") { |rails_root| @rails_root = rails_root; self.environment = RailsEnvironment.new(@rails_root) }
ff44cc2 @amatsuda whitespace
amatsuda authored
304
325abe9 @dyba Substituted RailsCommands for Rails::Commands
dyba authored
305 o.on("-v", "--verbose", "Turn on verbose output.") { |verbose| $verbose = verbose }
306 o.on("-h", "--help", "Show this help message.") { puts o; exit }
ff44cc2 @amatsuda whitespace
amatsuda authored
307
325abe9 @dyba Substituted RailsCommands for Rails::Commands
dyba authored
308 o.separator ""
309 o.separator "COMMANDS"
ff44cc2 @amatsuda whitespace
amatsuda authored
310
325abe9 @dyba Substituted RailsCommands for Rails::Commands
dyba authored
311 o.separator " install Install plugin(s) from known repositories or URLs."
312 o.separator " remove Uninstall plugins."
ff44cc2 @amatsuda whitespace
amatsuda authored
313
325abe9 @dyba Substituted RailsCommands for Rails::Commands
dyba authored
314 o.separator ""
315 o.separator "EXAMPLES"
316 o.separator " Install a plugin from a subversion URL:"
317 o.separator " #{@script_name} plugin install http://example.com/my_svn_plugin\n"
318 o.separator " Install a plugin from a git URL:"
319 o.separator " #{@script_name} plugin install git://github.com/SomeGuy/my_awesome_plugin.git\n"
320 o.separator " Install a plugin and add a svn:externals entry to vendor/plugins"
321 o.separator " #{@script_name} plugin install -x my_svn_plugin\n"
322 end
0a735ca @dhh Added script/plugin to manage plugins (install, remove, list, etc) [R…
dhh authored
323 end
ff44cc2 @amatsuda whitespace
amatsuda authored
324
325abe9 @dyba Substituted RailsCommands for Rails::Commands
dyba authored
325 def parse!(args=ARGV)
326 general, sub = split_args(args)
327 options.parse!(general)
ff44cc2 @amatsuda whitespace
amatsuda authored
328
325abe9 @dyba Substituted RailsCommands for Rails::Commands
dyba authored
329 command = general.shift
330 if command =~ /^(install|remove)$/
331 command = Commands.const_get(command.capitalize).new(self)
332 command.parse!(sub)
333 else
334 puts "Unknown command: #{command}" unless command.blank?
335 puts options
336 exit 1
337 end
0a735ca @dhh Added script/plugin to manage plugins (install, remove, list, etc) [R…
dhh authored
338 end
ff44cc2 @amatsuda whitespace
amatsuda authored
339
325abe9 @dyba Substituted RailsCommands for Rails::Commands
dyba authored
340 def split_args(args)
341 left = []
342 left << args.shift while args[0] and args[0] =~ /^-/
343 left << args.shift if args[0]
344 [left, args]
345 end
ff44cc2 @amatsuda whitespace
amatsuda authored
346
325abe9 @dyba Substituted RailsCommands for Rails::Commands
dyba authored
347 def self.parse!(args=ARGV)
348 Plugin.new.parse!(args)
349 end
0a735ca @dhh Added script/plugin to manage plugins (install, remove, list, etc) [R…
dhh authored
350 end
ff44cc2 @amatsuda whitespace
amatsuda authored
351
325abe9 @dyba Substituted RailsCommands for Rails::Commands
dyba authored
352 class Install
353 def initialize(base_command)
354 @base_command = base_command
355 @method = :http
356 @options = { :quiet => false, :revision => nil, :force => false }
0a735ca @dhh Added script/plugin to manage plugins (install, remove, list, etc) [R…
dhh authored
357 end
ff44cc2 @amatsuda whitespace
amatsuda authored
358
325abe9 @dyba Substituted RailsCommands for Rails::Commands
dyba authored
359 def options
360 OptionParser.new do |o|
361 o.set_summary_indent(' ')
362 o.banner = "Usage: #{@base_command.script_name} install PLUGIN [PLUGIN [PLUGIN] ...]"
363 o.define_head "Install one or more plugins."
364 o.separator ""
365 o.separator "Options:"
366 o.on( "-x", "--externals",
367 "Use svn:externals to grab the plugin.",
368 "Enables plugin updates and plugin versioning.") { |v| @method = :externals }
369 o.on( "-o", "--checkout",
370 "Use svn checkout to grab the plugin.",
371 "Enables updating but does not add a svn:externals entry.") { |v| @method = :checkout }
372 o.on( "-e", "--export",
373 "Use svn export to grab the plugin.",
374 "Exports the plugin, allowing you to check it into your local repository. Does not enable updates or add an svn:externals entry.") { |v| @method = :export }
375 o.on( "-q", "--quiet",
376 "Suppresses the output from installation.",
377 "Ignored if -v is passed (rails plugin -v install ...)") { |v| @options[:quiet] = true }
378 o.on( "-r REVISION", "--revision REVISION",
379 "Checks out the given revision from subversion or git.",
380 "Ignored if subversion/git is not used.") { |v| @options[:revision] = v }
381 o.on( "-f", "--force",
382 "Reinstalls a plugin if it's already installed.") { |v| @options[:force] = true }
383 o.separator ""
384 o.separator "You can specify plugin names as given in 'plugin list' output or absolute URLs to "
385 o.separator "a plugin repository."
386 end
0a735ca @dhh Added script/plugin to manage plugins (install, remove, list, etc) [R…
dhh authored
387 end
325abe9 @dyba Substituted RailsCommands for Rails::Commands
dyba authored
388
389 def determine_install_method
390 best = @base_command.environment.best_install_method
391 @method = :http if best == :http and @method == :export
392 case
393 when (best == :http and @method != :http)
394 msg = "Cannot install using subversion because `svn' cannot be found in your PATH"
395 when (best == :export and (@method != :export and @method != :http))
396 msg = "Cannot install using #{@method} because this project is not under subversion."
397 when (best != :externals and @method == :externals)
398 msg = "Cannot install using externals because vendor/plugins is not under subversion."
399 end
400 if msg
401 puts msg
402 exit 1
403 end
404 @method
0a735ca @dhh Added script/plugin to manage plugins (install, remove, list, etc) [R…
dhh authored
405 end
ff44cc2 @amatsuda whitespace
amatsuda authored
406
325abe9 @dyba Substituted RailsCommands for Rails::Commands
dyba authored
407 def parse!(args)
408 options.parse!(args)
409 if args.blank?
410 puts options
411 exit 1
412 end
413 environment = @base_command.environment
414 install_method = determine_install_method
415 puts "Plugins will be installed using #{install_method}" if $verbose
416 args.each do |name|
417 ::Plugin.find(name).install(install_method, @options)
418 end
419 rescue StandardError => e
420 puts "Plugin not found: #{args.inspect}"
421 puts e.inspect if $verbose
bf5d154 @amatsuda Print proper "Usage:" messages for "rails plugin" command
amatsuda authored
422 exit 1
423 end
0a735ca @dhh Added script/plugin to manage plugins (install, remove, list, etc) [R…
dhh authored
424 end
ff44cc2 @amatsuda whitespace
amatsuda authored
425
325abe9 @dyba Substituted RailsCommands for Rails::Commands
dyba authored
426 class Remove
427 def initialize(base_command)
428 @base_command = base_command
0a735ca @dhh Added script/plugin to manage plugins (install, remove, list, etc) [R…
dhh authored
429 end
ff44cc2 @amatsuda whitespace
amatsuda authored
430
325abe9 @dyba Substituted RailsCommands for Rails::Commands
dyba authored
431 def options
432 OptionParser.new do |o|
433 o.set_summary_indent(' ')
434 o.banner = "Usage: #{@base_command.script_name} remove name [name]..."
435 o.define_head "Remove plugins."
436 end
bf5d154 @amatsuda Print proper "Usage:" messages for "rails plugin" command
amatsuda authored
437 end
325abe9 @dyba Substituted RailsCommands for Rails::Commands
dyba authored
438
439 def parse!(args)
440 options.parse!(args)
441 if args.blank?
442 puts options
443 exit 1
444 end
445 root = @base_command.environment.root
446 args.each do |name|
447 ::Plugin.new(name).uninstall
448 end
0a735ca @dhh Added script/plugin to manage plugins (install, remove, list, etc) [R…
dhh authored
449 end
450 end
451
325abe9 @dyba Substituted RailsCommands for Rails::Commands
dyba authored
452 class Info
453 def initialize(base_command)
454 @base_command = base_command
455 end
9bfcfa8 @technoweenie preliminary support for plugin meta files
technoweenie authored
456
325abe9 @dyba Substituted RailsCommands for Rails::Commands
dyba authored
457 def options
458 OptionParser.new do |o|
459 o.set_summary_indent(' ')
460 o.banner = "Usage: #{@base_command.script_name} info name [name]..."
461 o.define_head "Shows plugin info at {url}/about.yml."
462 end
9bfcfa8 @technoweenie preliminary support for plugin meta files
technoweenie authored
463 end
464
325abe9 @dyba Substituted RailsCommands for Rails::Commands
dyba authored
465 def parse!(args)
466 options.parse!(args)
467 args.each do |name|
468 puts ::Plugin.find(name).info
469 puts
470 end
9bfcfa8 @technoweenie preliminary support for plugin meta files
technoweenie authored
471 end
472 end
473 end
0a735ca @dhh Added script/plugin to manage plugins (install, remove, list, etc) [R…
dhh authored
474 end
ff44cc2 @amatsuda whitespace
amatsuda authored
475
d3275ca @sstephenson Enable HTTP installation of plugins when svn isn't avaialable. Closes…
sstephenson authored
476 class RecursiveHTTPFetcher
9318131 @jeremy Eliminate Subversion dependencies in scripts/plugin. Correct install…
jeremy authored
477 attr_accessor :quiet
00cecf8 @dhh Fixed that installing plugins from SVN repositories that use trunk/ w…
dhh authored
478 def initialize(urls_to_fetch, level = 1, cwd = ".")
479 @level = level
d3275ca @sstephenson Enable HTTP installation of plugins when svn isn't avaialable. Closes…
sstephenson authored
480 @cwd = cwd
8e68ec4 @amatsuda Ruby 1.9 compat: use String#lines instead of to_a in Ruby 1.9
amatsuda authored
481 @urls_to_fetch = RUBY_VERSION >= '1.9' ? urls_to_fetch.lines : urls_to_fetch.to_a
9318131 @jeremy Eliminate Subversion dependencies in scripts/plugin. Correct install…
jeremy authored
482 @quiet = false
483 end
484
485 def ls
486 @urls_to_fetch.collect do |url|
92f9448 @azimux Make script/plugin work with svn+ssh urls. [#662 state:resolved]
azimux authored
487 if url =~ /^svn(\+ssh)?:\/\/.*/
9935a35 @technoweenie Enhances plugin#discover allowing it to discover svn:// like URIs (cl…
technoweenie authored
488 `svn ls #{url}`.split("\n").map {|entry| "/#{entry}"} rescue nil
489 else
490 open(url) do |stream|
491 links("", stream.read)
492 end rescue nil
493 end
9318131 @jeremy Eliminate Subversion dependencies in scripts/plugin. Correct install…
jeremy authored
494 end.flatten
d3275ca @sstephenson Enable HTTP installation of plugins when svn isn't avaialable. Closes…
sstephenson authored
495 end
496
497 def push_d(dir)
498 @cwd = File.join(@cwd, dir)
499 FileUtils.mkdir_p(@cwd)
500 end
501
502 def pop_d
503 @cwd = File.dirname(@cwd)
504 end
505
506 def links(base_url, contents)
507 links = []
508 contents.scan(/href\s*=\s*\"*[^\">]*/i) do |link|
509 link = link.sub(/href="/i, "")
82a85e8 @dhh Skip svnindex.xsl files when installing plugins (closes #7070) [david…
dhh authored
510 next if link =~ /svnindex.xsl$/
6967b42 @NZKoz Make sure script/plugin doesn't skip files beginning with http (i.e. …
NZKoz authored
511 next if link =~ /^(\w*:|)\/\// || link =~ /^\./
d3275ca @sstephenson Enable HTTP installation of plugins when svn isn't avaialable. Closes…
sstephenson authored
512 links << File.join(base_url, link)
513 end
514 links
515 end
ff44cc2 @amatsuda whitespace
amatsuda authored
516
d3275ca @sstephenson Enable HTTP installation of plugins when svn isn't avaialable. Closes…
sstephenson authored
517 def download(link)
9318131 @jeremy Eliminate Subversion dependencies in scripts/plugin. Correct install…
jeremy authored
518 puts "+ #{File.join(@cwd, File.basename(link))}" unless @quiet
d3275ca @sstephenson Enable HTTP installation of plugins when svn isn't avaialable. Closes…
sstephenson authored
519 open(link) do |stream|
520 File.open(File.join(@cwd, File.basename(link)), "wb") do |file|
521 file.write(stream.read)
522 end
523 end
524 end
ff44cc2 @amatsuda whitespace
amatsuda authored
525
d3275ca @sstephenson Enable HTTP installation of plugins when svn isn't avaialable. Closes…
sstephenson authored
526 def fetch(links = @urls_to_fetch)
527 links.each do |l|
528 (l =~ /\/$/ || links == @urls_to_fetch) ? fetch_dir(l) : download(l)
529 end
530 end
ff44cc2 @amatsuda whitespace
amatsuda authored
531
d3275ca @sstephenson Enable HTTP installation of plugins when svn isn't avaialable. Closes…
sstephenson authored
532 def fetch_dir(url)
00cecf8 @dhh Fixed that installing plugins from SVN repositories that use trunk/ w…
dhh authored
533 @level += 1
534 push_d(File.basename(url)) if @level > 0
d3275ca @sstephenson Enable HTTP installation of plugins when svn isn't avaialable. Closes…
sstephenson authored
535 open(url) do |stream|
536 contents = stream.read
537 fetch(links(url, contents))
538 end
00cecf8 @dhh Fixed that installing plugins from SVN repositories that use trunk/ w…
dhh authored
539 pop_d if @level > 0
540 @level -= 1
d3275ca @sstephenson Enable HTTP installation of plugins when svn isn't avaialable. Closes…
sstephenson authored
541 end
542 end
0a735ca @dhh Added script/plugin to manage plugins (install, remove, list, etc) [R…
dhh authored
543
325abe9 @dyba Substituted RailsCommands for Rails::Commands
dyba authored
544 Rails::Commands::Plugin.parse!
Something went wrong with that request. Please try again.