Skip to content

Commit 9e31243

Browse files
committed
Added first commands
0 parents  commit 9e31243

File tree

9 files changed

+194
-0
lines changed

9 files changed

+194
-0
lines changed

bin/vim-bundle

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/usr/bin/env ruby
2+
3+
require 'vim_bundle'
4+
5+
VimBundle::CLI.execute
6+
7+
# vim: syntax=ruby

lib/vim_bundle.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
require 'vim_bundle/methods'
2+
require 'vim_bundle/cli'
3+
require 'vim_bundle/commands'
4+

lib/vim_bundle/cli.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
module VimBundle
2+
module CLI
3+
4+
class << self
5+
def execute
6+
command = VimBundle.commands.include?(ARGV[0]) ? ARGV[0] : 'help'
7+
VimBundle::Commands.send(command)
8+
end
9+
end
10+
11+
end
12+
end

lib/vim_bundle/commands.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
module VimBundle
2+
module Commands
3+
extend self
4+
end
5+
end
6+
7+
Dir[File.join(File.dirname(__FILE__), "commands", "*.rb")].each do |file|
8+
require file
9+
10+
VimBundle.commands.push File.basename(file, ".rb")
11+
end

lib/vim_bundle/commands/help.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
module VimBundle
2+
module Commands
3+
def help
4+
puts <<-HELP
5+
USAGE: #{$0} [command]
6+
init: install pathogen plugin
7+
sample: show sample config file
8+
HELP
9+
end
10+
end
11+
end
12+

lib/vim_bundle/commands/init.rb

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
require 'net/http'
2+
require 'fileutils'
3+
4+
module VimBundle
5+
module Commands
6+
7+
def init
8+
pathogen_file = File.join(VimBundle.vim_home, 'autoload', 'pathogen.vim')
9+
10+
puts "Installing #{pathogen_file} ..."
11+
12+
FileUtils.mkdir_p(File.dirname(pathogen_file))
13+
14+
File.open(pathogen_file, "w") { |f| f.write Net::HTTP.get(URI.parse(pathogen_url)) }
15+
16+
puts <<-DATA
17+
DONE. Please add to your .vimrc next lines:
18+
19+
call pathogen#helptags()
20+
call pathogen#runtime_append_all_bundles()
21+
22+
DATA
23+
end
24+
25+
def pathogen_url
26+
"http://github.com/tpope/vim-pathogen/raw/master/autoload/pathogen.vim"
27+
end
28+
end
29+
end

lib/vim_bundle/commands/sample.rb

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
module VimBundle
2+
module Commands
3+
def sample_config
4+
{
5+
'bundles' => [
6+
{
7+
'dir' => 'IndentAnything',
8+
'url' => 'http://www.vim.org/scripts/script.php?script_id=10228',
9+
'type' => 'tgz'
10+
},
11+
{
12+
'dir' => 'JavascriptIndent',
13+
'url' => 'http://www.vim.org/scripts/script.php?script_id=11838',
14+
'type' => 'vim',
15+
'subdir' => 'indent'
16+
},
17+
{
18+
'dir' => 'BlockComment',
19+
'url' => 'http://www.vim.org/scripts/script.php?script_id=1387',
20+
'type' => 'vim',
21+
'subdir' => 'plugin'
22+
},
23+
{
24+
'dir' => 'vim-cucumber',
25+
'url' => 'git://github.com/tpope/vim-cucumber.git',
26+
'type' => 'git'
27+
}
28+
]
29+
}
30+
end
31+
32+
def sample
33+
puts self.sample_config.to_yaml
34+
end
35+
end
36+
end

lib/vim_bundle/commands/update.rb

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
require 'net/http'
2+
require 'fileutils'
3+
4+
module VimBundle
5+
module Commands
6+
def update
7+
bundles = VimBundle.config['bundles']
8+
9+
abort("No bundles defined") if blank_value?(bundles)
10+
11+
self.erase_bundles
12+
self.bundles.each { |bundle| install_bundle(bundle) }
13+
end
14+
15+
protected
16+
17+
def erase_bundles
18+
FileUtils.rm_rf(VimBundle.dir)
19+
File.mkdir(VimBundle.dir)
20+
end
21+
22+
def install_bundle(bundle)
23+
%w(dir url).each do |key|
24+
abort("bundle #{key} is not specified") if self.blank_value?(bundle[key])
25+
end
26+
27+
puts "Updating bundle #{bundle['dir']} ..."
28+
29+
case bundle['type']
30+
when 'git':
31+
32+
when 'tgz':
33+
dir = File.join(VimBundle.dir, bundle['dir'])
34+
35+
FileUtils.mkdir_p(dir)
36+
system("curl -s #{bundle['url']} | tar xz -C #{dir}") || abort("Failed")
37+
38+
when 'vim':
39+
abort("bundle subdir is not specified") if self.blank_value?(bundle['subdir'])
40+
41+
filepath = File.join(VimBundle.dir, bundle['dir'], bundle['subdir'], bundle['dir'] + ".vim")
42+
43+
FileUtils.mkdir_p(File.dirname(filepath))
44+
File.open(filepath, 'w') { |f| f << Net::HTTP.get(URI.parse(bundle['url'])) }
45+
46+
else
47+
abort("Unsupported #{bundle['type'].inspect} file type")
48+
end
49+
end
50+
51+
def blank_value?(value)
52+
value.nil? || value.empty?
53+
end
54+
end
55+
end
56+

lib/vim_bundle/methods.rb

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
require "yaml"
2+
3+
module VimBundle
4+
module Methods
5+
def vim_home
6+
File.join(ENV['HOME'], '.vim')
7+
end
8+
9+
def dir
10+
File.join(self.vim_home, 'bundle')
11+
end
12+
13+
def default_config_file
14+
File.join(self.vim_home, 'bundle.yml')
15+
end
16+
17+
def config
18+
@config ||= YAML.load_file(default_config_file)
19+
end
20+
21+
def commands
22+
@commands ||= []
23+
end
24+
end
25+
26+
extend Methods
27+
end

0 commit comments

Comments
 (0)