Skip to content
This repository
Browse code

Enable HTTP installation of plugins when svn isn't avaialable. Closes #…

…2661. [Chad Fowler]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@2885 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
commit d3275cadaa410fc00365db41d0155e4fa463e128 1 parent 55fab64
Sam Stephenson sstephenson authored

Showing 2 changed files with 74 additions and 8 deletions. Show diff stats Hide diff stats

  1. +2 0  railties/CHANGELOG
  2. +72 8 railties/lib/commands/plugin.rb
2  railties/CHANGELOG
... ... @@ -1,5 +1,7 @@
1 1 *SVN*
2 2
  3 +* Enable HTTP installation of plugins when svn isn't avaialable. Closes #2661. [Chad Fowler]
  4 +
3 5 * Load Rails::Info after initialization [Sam Stephenson]
4 6
5 7 * Added script/about to display formatted Rails::Info output [Sam Stephenson]
80 railties/lib/commands/plugin.rb
@@ -44,13 +44,9 @@
44 44 # and is licensed MIT: (http://www.opensource.org/licenses/mit-license.php)
45 45
46 46 $verbose = false
47   -`svn --version`
48   -unless $?.success?
49   - $stderr.puts "ERROR: Must have subversion (svn) available in the PATH to use plugin manager"
50   - exit 1
51   -end
52 47
53 48
  49 +require 'open-uri'
54 50 require 'fileutils'
55 51 require 'tempfile'
56 52
@@ -95,7 +91,12 @@ def install(name_uri_or_plugin)
95 91 puts "plugin not found: #{name_uri_or_plugin}"
96 92 end
97 93 end
98   -
  94 +
  95 + def use_svn?
  96 + `svn --version`
  97 + $?.success?
  98 + end
  99 +
99 100 def use_externals?
100 101 File.directory?("#{root}/vendor/plugins/.svn")
101 102 end
@@ -108,6 +109,7 @@ def use_checkout?
108 109 end
109 110
110 111 def best_install_method
  112 + return :http unless use_svn?
111 113 case
112 114 when use_externals? then :externals
113 115 when use_checkout? then :checkout
@@ -181,7 +183,14 @@ def install_using_externals
181 183 rails_env.externals = externals
182 184 install_using_checkout
183 185 end
184   -
  186 +
  187 + def install_using_http
  188 + root = rails_env.root
  189 + mkdir_p "#{root}/vendor/plugins"
  190 + Dir.chdir "#{root}/vendor/plugins"
  191 + RecursiveHTTPFetcher.new(uri).fetch
  192 + end
  193 +
185 194 def guess_name(url)
186 195 @name = File.basename(url)
187 196 if @name == 'trunk' || @name.empty?
@@ -662,9 +671,14 @@ def determine_install_method
662 671 requested = case
663 672 when @export then method = :export
664 673 when @checkout then method = :checkout
665   - else method = :externals
  674 + when @http then method = :http
  675 + else method = @base_command.environment.best_install_method
666 676 end
667 677 best = @base_command.environment.best_install_method
  678 + if best == :http and requested != :http
  679 + puts "Cannot install using subversion because `svn' cannot be found in your PATH"
  680 + exit 1
  681 + end
668 682 if best == :export and requested != :export
669 683 puts "Cannot install using #{requested} because this project is not under subversion."
670 684 exit 1
@@ -761,5 +775,55 @@ def parse!(args)
761 775 end
762 776
763 777 end
  778 +
  779 +class RecursiveHTTPFetcher
  780 + def initialize(urls_to_fetch, cwd = ".")
  781 + @cwd = cwd
  782 + @urls_to_fetch = urls_to_fetch.to_a
  783 + end
  784 +
  785 + def push_d(dir)
  786 + @cwd = File.join(@cwd, dir)
  787 + FileUtils.mkdir_p(@cwd)
  788 + end
  789 +
  790 + def pop_d
  791 + @cwd = File.dirname(@cwd)
  792 + end
  793 +
  794 + def links(base_url, contents)
  795 + links = []
  796 + contents.scan(/href\s*=\s*\"*[^\">]*/i) do |link|
  797 + link = link.sub(/href="/i, "")
  798 + next if link =~ /^http/i || link =~ /^\./
  799 + links << File.join(base_url, link)
  800 + end
  801 + links
  802 + end
  803 +
  804 + def download(link)
  805 + puts "+ #{File.join(@cwd, File.basename(link))}"
  806 + open(link) do |stream|
  807 + File.open(File.join(@cwd, File.basename(link)), "wb") do |file|
  808 + file.write(stream.read)
  809 + end
  810 + end
  811 + end
  812 +
  813 + def fetch(links = @urls_to_fetch)
  814 + links.each do |l|
  815 + (l =~ /\/$/ || links == @urls_to_fetch) ? fetch_dir(l) : download(l)
  816 + end
  817 + end
  818 +
  819 + def fetch_dir(url)
  820 + push_d(File.basename(url))
  821 + open(url) do |stream|
  822 + contents = stream.read
  823 + fetch(links(url, contents))
  824 + end
  825 + pop_d
  826 + end
  827 +end
764 828
765 829 Commands::Plugin.parse!

0 comments on commit d3275ca

Please sign in to comment.
Something went wrong with that request. Please try again.