forked from bronson/vim-update-bundles
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvim-update-bundles.old
executable file
·175 lines (145 loc) · 4.97 KB
/
vim-update-bundles.old
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
#!/usr/bin/env ruby
# Reads the bundles you want installed out of your $HOME/.vimrc file,
# then synchronizes .vim/bundles to match, downloading new repositories
# as needed. It also deletes bundles that are no longer used.
#
# To specify a bundle in your .vimrc, just add a line like this:
# " BUNDLE: git://git.wincent.com/command-t.git
# If you want a branch other than 'master', add the branch name on the end:
# " BUNDLE: git://github.com/vim-ruby/vim-ruby.git noisy
# Or tag or sha1: (this results in a detached head, see 'git help checkout')
# " BUNDLE: git://github.com/bronson/vim-closebuffer.git 0.2
# " BUNDLE: git://github.com/tpope/vim-rails.git 42bb0699
require 'fileutils'
require 'open-uri'
def gitroot
# return root of git repository
`readlink -fn ./$(git rev-parse --show-cdup)`
end
def dotvim *path
# return vim's runtimepath folder
# make sure this is set to your vim folder
# normally ".vim"
File.join ENV['HOME'], '.dotfiles/vim', *path
end
def bundlepath *path
# path to vim's bundle folder relative to git's root
# make sure this is set correctly
File.join 'vim/bundle', *path
end
def ensure_dir dir
Dir.mkdir dir unless test ?d, dir
end
def print_bundle dir, doc
version = date = ''
FileUtils.cd(dir) do
version = `git describe --tags 2>/dev/null`.chomp
version = "n/a" if version == ''
date = `git log -1 --pretty=format:%ai`.chomp
end
doc.printf " - %-30s %-22s %s\n", "|#{dir}|", version, date.split(' ').first
end
def clone_bundle dir, url, tagstr
puts "adding submodule #{dir} from #{url}#{tagstr}"
FileUtils.cd(gitroot) {
`git submodule add #{url} #{bundlepath(dir)}`
unless system("grep -q \"doc/tags\" \"#{bundlepath(dir)}/.git/info/exclude\"")
puts" exclude #{dir}'s helptags from git..."
open("#{bundlepath(dir)}/.git/info/exclude", "a") do |f|
f << "\ndoc/tags\n"
end
end
}
end
def download_bundle dir, url, tag, doc
tagstr = " at #{tag}" if tag
if test ?d, dir
remote = '' # shame FileUtils.cd doesn't return the retval of the block
FileUtils.cd(dir) { remote = `git config --get remote.origin.url`.chomp }
if remote == url
puts "updating #{dir} from #{url}#{tagstr}"
FileUtils.cd(dir) { `git pull` }
else
puts "repo has changed from #{remote} to #{url}"
remove_bundle dir
clone_bundle dir, url, tagstr
end
else
clone_bundle dir, url, tagstr
end
FileUtils.cd(dir) { `git checkout -q #{tag || 'master'}` }
print_bundle(dir, doc)
end
def read_vimrc
File.open("#{ENV['HOME']}/.vimrc") do |file|
file.each_line { |line| yield line }
end
end
def remove_bundle dir
trash_dir = dotvim("Trashed-Bundles")
ensure_dir trash_dir
puts "Erasing #{dir}, find it in #{trash_dir}"
FileUtils.mv dir, trash_dir
end
def update_bundles doc
existing_bundles = Dir['*']
dir = nil
read_vimrc do |line|
if line =~ /^\s*"\s*BUNDLE:\s*(.*)$/
url, tag = $1.split
dir = url.split('/').last.gsub(/^vim-|\.git$/, '').gsub(/\.vim$/, '')
download_bundle dir, url, tag, doc
existing_bundles.delete dir
elsif line =~ /^\s*"\s*BUNDLE-COMMAND:\s*(.*)$/
raise "BUNDLE-COMMAND must come after BUNDLE" if dir.nil?
puts " running: #{$1}"
FileUtils.cd(dir) { system($1) }
end
end
existing_bundles.each { |dir| remove_bundle(dir) }
FileUtils.cd(gitroot) {
puts "initializing submodules"
`git submodule init`
puts "updating submodules"
`git submodule update`
}
end
def download_file url, file
open(url) do |r|
File.open(file, 'w') do |w|
w.write(r.read)
end
end
end
def update_bundles_and_docs
File.open(dotvim('doc', 'bundles.txt'), "w") do |doc|
doc.printf "%-32s %s %32s\n\n", "*bundles.txt*", "Bundles", "Version 0.1"
doc.puts "These are the bundles installed on your system, along with their\n" +
"versions and release dates. Downloaded on #{Time.now}.\n\n" +
"A version number of 'n/a' means upstream hasn't tagged any releases.\n"
bundle_dir = dotvim('bundle')
ensure_dir bundle_dir
FileUtils.cd(bundle_dir) { update_bundles(doc) }
doc.puts "\n"
end
end
def create_new_vim_environment
puts 'Creating a new Vim environment.'
puts "Downloading default vimrc..."
ensure_dir dotvim
download_file "http://github.com/steeef/vim-update-bundles/raw/master/vimrc-starter", dotvim('vimrc')
`ln -s '#{dotvim 'vimrc'}' '#{ENV['HOME']}/.vimrc'`
puts "Downloading Pathogen..."
ensure_dir dotvim('autoload')
download_file "http://github.com/tpope/vim-pathogen/raw/master/autoload/pathogen.vim", dotvim('autoload', 'pathogen.vim')
puts 'Done! Now edit your ~/.vimrc file.'
end
if ensure_dir dotvim
create_new_vim_environment
else
ensure_dir dotvim('doc')
update_bundles_and_docs
puts "updating helptags..."
`vim -e -c "call pathogen#helptags()" -c q`
puts "done! Start Vim and type ':help bundles' to see what you have installed."
end