Skip to content

Commit

Permalink
Can install scripts from vim.org
Browse files Browse the repository at this point in the history
Evolved a new pattern for instantiating installers. Need to go back and update the Github installer later. Didn't want to enter into that nightmare just yet.
  • Loading branch information
Joe Fiorini committed Dec 19, 2010
1 parent 854ed2f commit 3f490c9
Show file tree
Hide file tree
Showing 12 changed files with 87 additions and 19 deletions.
6 changes: 6 additions & 0 deletions Gemfile.lock
Expand Up @@ -7,6 +7,7 @@ PATH
GEM
remote: http://rubygems.org/
specs:
addressable (2.2.2)
aruba (0.2.7)
background_process
cucumber (~> 0.10.0)
Expand All @@ -18,6 +19,7 @@ GEM
bourne (1.0)
mocha (= 0.9.8)
builder (3.0.0)
crack (0.1.8)
cucumber (0.10.0)
builder (>= 2.1.2)
diff-lcs (~> 1.1.2)
Expand Down Expand Up @@ -50,6 +52,9 @@ GEM
sys-uname (0.8.4)
term-ansicolor (1.0.5)
thor (0.14.6)
webmock (1.6.1)
addressable (>= 2.2.2)
crack (>= 0.1.7)

PLATFORMS
ruby
Expand All @@ -68,3 +73,4 @@ DEPENDENCIES
rspec (~> 2.3.0)
thor
vimmer!
webmock
1 change: 1 addition & 0 deletions features/fixtures/script_ids.json
@@ -0,0 +1 @@
{"2975":"fugitive.vim"}
2 changes: 1 addition & 1 deletion features/step_definitions/plugin_assertion_steps.rb
@@ -1,6 +1,6 @@
Then /^a plugin named "([^"]*)" should be installed$/ do |name|
@vimmer.installed_plugins.should include(name)
@vimmer.plugin_store[name].should =~ %r{https://github.com/tpope/vim-awesomemofo.git}
@vimmer.plugin_store[name].should =~ %r{https://github.com/.*/#{name}\.git}
end

Then /^I should still not have any plugins installed$/ do
Expand Down
4 changes: 3 additions & 1 deletion features/support/setup.rb
@@ -1,5 +1,7 @@
require 'aruba/cucumber'
require 'webmock'
require File.dirname(__FILE__) + '/stubbed_http_requests'

ENV['RUBYOPT'] = "-r#{File.expand_path(File.join(File.dirname(__FILE__), 'stub-commands.rb'))} #{ENV['RUBYOPT']}"
ENV['RUBYOPT'] = "-r#{File.expand_path(File.join(File.dirname(__FILE__), 'stub-commands.rb'))} -rwebmock -r#{File.expand_path(File.join(File.dirname(__FILE__), 'stubbed_http_requests'))} #{ENV['RUBYOPT']}"
ENV['VIMMER_HOME'] = File.expand_path(File.join(File.dirname(__FILE__),
%w(.. .. tmp aruba .vimmer)))
6 changes: 6 additions & 0 deletions features/support/stubbed_http_requests.rb
@@ -0,0 +1,6 @@
include WebMock::API

stub_request(:get, "http://www.vim-scripts.org/api/script_ids.json").
with(:headers => {'Accept'=>'*/*', 'User-Agent'=>'Ruby'}).
to_return(:status => 200, :body => File.read(File.dirname(__FILE__) + "/../fixtures/script_ids.json"))

6 changes: 5 additions & 1 deletion lib/vimmer/cli.rb
Expand Up @@ -8,7 +8,11 @@ class CLI < Thor
def install(path)
setup
begin
installer = Vimmer::Installers.for_url(path).new(:path => path)
installer = Vimmer::Installers.for_url(path)
# TODO: Make this consistent with VimDotOrg installer
if installer == Vimmer::Installers::Github
installer = installer.new(:path => path)
end
installer.install
rescue Vimmer::InstallerNotFoundError => e
$stderr.puts "The URL #{e.path} is invalid."
Expand Down
2 changes: 1 addition & 1 deletion lib/vimmer/installers.rb
Expand Up @@ -10,7 +10,7 @@ def for_url(url)
if Github.match?(url)
Github
elsif VimDotOrg.match?(url)
VimDotOrg
VimDotOrg.for_url(url)
else
raise Vimmer::InstallerNotFoundError.new(url)
end
Expand Down
34 changes: 27 additions & 7 deletions lib/vimmer/installers/vim_dot_org.rb
@@ -1,21 +1,41 @@
require 'json'

module Vimmer
module Installers

VIM_DOT_ORG_URL_PATTERN = %r{https?://(?:www\.)?vim\.org/scripts/script.php\?script_id=(\d{1,5})}

class VimDotOrg
attr_reader :path, :plugin_name
attr_reader :path, :plugin_name, :script_id


def initialize(args={})
@path = args[:path]
raise Vimmer::PluginNotFoundError if @path =~ /0000/
def self.match?(url)
!(url =~ VIM_DOT_ORG_URL_PATTERN).nil?
end

def self.for_url(path)

m = VIM_DOT_ORG_URL_PATTERN.match(path)
script_id = m[1]

raise Vimmer::PluginNotFoundError unless repository.key?(script_id)

def install
script_name = repository[script_id]
github_path_template = "https://github.com/vim-scripts/%s.git"

github_path = github_path_template % [script_name]

Github.new(:path => github_path)
end

private

def self.match?(url)
!(url =~ %r{https?://(?:www\.)?vim\.org/scripts/script.php\?script_id=\d{1,5}}).nil?
def self.repository
@repository ||= JSON.parse(Net::HTTP.get(repository_uri))
end

def self.repository_uri
URI.parse("http://www.vim-scripts.org/api/script_ids.json")
end


Expand Down
4 changes: 4 additions & 0 deletions spec/install/load_installer_spec.rb
Expand Up @@ -14,6 +14,10 @@

context "for a Vim.org URL" do

before do
Installers::VimDotOrg.stubs(:for_url).returns(Installers::VimDotOrg)
end

subject { Installers.for_url("http://vim.org/scripts/script.php?script_id=1234") }

it { should == Installers::VimDotOrg }
Expand Down
35 changes: 27 additions & 8 deletions spec/install/vim_dot_org_spec.rb
Expand Up @@ -15,12 +15,14 @@

context "with a non-existent URL" do

let(:installer) { VimDotOrg.new(:path => VDO_NOT_FOUND_URL) }
before do
VimDotOrg.stubs(:repository).returns({})
end

it { should be_a_valid_url(VDO_NOT_FOUND_URL) }

specify "the installer should raise an exception" do
lambda { installer.install }.should raise_error(Vimmer::PluginNotFoundError)
lambda { VimDotOrg.for_url(VDO_NOT_FOUND_URL) }.should raise_error(Vimmer::PluginNotFoundError)
end

end
Expand All @@ -34,21 +36,38 @@

context "with a good URL" do

let(:installer) { VimDotOrg.new(:path => "http://www.vim.org/scripts/script.php?script_id=2975") }
before do
VimDotOrg.stubs(:repository).returns({"2975" => "fugitive.vim"})
end

let(:installer) { VimDotOrg.for_url("http://www.vim.org/scripts/script.php?script_id=2975") }

context "using the vim-scripts mirror" do

it "should return a Github installer" do
installer.should be_a(Github)
end

it "uses the Github path" do
installer.path.should == "https://github.com/vim-scripts/fugitive.vim.git"
end

end

specify "the installer should not raise an exception" do

class << installer
def git_clone(arg1, arg2)
end
end

lambda { installer.install }.should_not raise_error
end

specify "the installer calculates the plugin's name" do
pending
installer.plugin_name.should == "fugitive.vim"
end

specify "provides access to the path" do
installer.path.should == "http://www.vim.org/scripts/script.php?script_id=2975"
end

end

end
5 changes: 5 additions & 0 deletions spec/spec_helper.rb
Expand Up @@ -8,10 +8,15 @@
require 'fakefs/spec_helpers'
require 'bourne'

require 'webmock/rspec'


Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}

RSpec.configure do |config|

config.include FakeFS::SpecHelpers
config.include WebMock::API
config.mock_with :mocha

def app_root
Expand Down
1 change: 1 addition & 0 deletions vimmer.gemspec
Expand Up @@ -25,6 +25,7 @@ Gem::Specification.new do |s|
s.add_development_dependency "aruba", "= 0.2.7"
s.add_development_dependency "mocha"
s.add_development_dependency "bourne"
s.add_development_dependency "webmock"

s.files = `git ls-files`.split("\n")
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
Expand Down

0 comments on commit 3f490c9

Please sign in to comment.