forked from mutewinter/dot_vim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rakefile
134 lines (110 loc) · 3.23 KB
/
Rakefile
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
# Rakefile
#
# Simple tasks for managing my .vim files
require 'open-uri'
require 'openssl'
require 'json'
PLUGIN_LIST_TAG = '## Plugin List'
PLUGIN_LIST_NOTE = '_Note: Auto generated by `rake plugins:update_readme`_'
VIMRC_FILE = 'vimrc'
README_FILE = 'README.md'
FILES_TO_LINK = %w{vimrc gvimrc}
namespace :vim do
desc 'Create symlinks'
task :link do
begin
FILES_TO_LINK.each do |file|
dot_file = File.expand_path("~/.#{file}")
if File.exists? dot_file
puts "#{dot_file} already exists, skipping link."
else
File.symlink(".vim/#{file}", dot_file)
puts "Created link for #{file} in your home folder."
end
end
rescue NotImplementedError
puts "File.symlink not supported, you must do it manually."
if RUBY_PLATFORM.downcase =~ /(mingw|win)(32|64)/
puts 'Windows 7 use mklink, e.g.'
puts ' mklink _vimrc .vim\vimrc'
end
end
end
end
namespace :plugins do
desc 'Update the list of plugins in README.md'
task :update_readme do
plugins = parse_plugins_from_vimrc
delete_old_plugins_from_readme
add_plugins_to_readme(plugins)
end
end
# ----------------------------------------
# Helper Methods
# ----------------------------------------
# Just takes an array of strings that resolve to plugins from Vundle
def add_plugins_to_readme(plugins = [])
lines = File.readlines(README_FILE).map{|l| l.chomp}
index = lines.index(PLUGIN_LIST_TAG)
unless index.nil?
lines.insert(index+1, "\n#{PLUGIN_LIST_NOTE}\n\n")
lines.insert(index+2, plugins.map{|p| " * [#{p[:name]}](#{p[:uri]}) - #{p[:description]}"})
write_lines_to_readme(lines)
else
puts "Error: Plugin List Tag (#{PLUGIN_LIST_TAG}) not found"
end
end
def delete_old_plugins_from_readme
lines = []
File.readlines(README_FILE).map do |line|
line.chomp!
lines << line
if line == PLUGIN_LIST_TAG
break
end
end
write_lines_to_readme(lines)
end
def write_lines_to_readme(lines)
readme_file = File.open(README_FILE, 'w')
readme_file << lines.join("\n")
readme_file.close
end
# Returns an array of plugins denoted with Bundle
def parse_plugins_from_vimrc
plugins = []
File.new(VIMRC_FILE).each do |line|
if line =~ /^Bundle\s+["'](.+)["']/
plugins << convert_to_link_hash($1)
end
end
plugins
end
# Converts a Vundle link to a URI
def convert_to_link_hash(link)
link_hash = {}
if link =~ /([a-zA-Z0-9\-]*)\/([a-zA-Z0-9\-\._]*)/
user = $1
name = $2
link_hash[:user] = user
link_hash[:name] = name
link_hash[:uri] = "https://github.com/#{user}/#{name}"
link_hash[:description] = fetch_github_repo_description(user, name)
else
name = link
link_hash[:name] = name
link_hash[:uri] = "https://github.com/vim-scripts/#{name}"
link_hash[:description] = fetch_github_repo_description('vim-scripts', name)
end
link_hash
end
def fetch_github_repo_description(user, name)
response = ''
if RUBY_VERSION < '1.9'
response = open("https://api.github.com/repos/#{user}/#{name}").read
else
response = open("https://api.github.com/repos/#{user}/#{name}", :ssl_verify_mode => OpenSSL::SSL::VERIFY_NONE).read
end
repo = JSON.parse response
repo['description']
end