-
Notifications
You must be signed in to change notification settings - Fork 127
/
Rakefile
201 lines (172 loc) · 5.42 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
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# Rakefile
#
# Tasks for managing dot_vim.
require 'open-uri'
require 'openssl'
require 'rubygems'
require 'json'
PLUGIN_LIST_TAG = '## Plugin List'
PLUGIN_LIST_NOTE = 'Generated by `rake update_readme`'
README_FILE = 'README.md'
PLUGINS_FOLDER = 'plug_plugins'
LINES_WITHOUT_CONFIG = 4
PLUGINS_HEADER = <<-HEADER.chomp
| Stars#{9.times.map{' '}.join('')} | **Plugin** | **Description** |
| :------- | :--------- | :-------------- |
HEADER
FILES_TO_LINK = %w{vimrc gvimrc}
task :default => ['vim:link']
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
neovim_config_file = File.expand_path("~/.vim/nvimrc");
neovim_dot_file = File.expand_path("~/.config/nvim/init.vim")
if File.exists? neovim_dot_file
puts "#{neovim_dot_file} already exists, skipping link."
else
File.symlink(neovim_config_file, neovim_dot_file)
puts "Created link for nvimrc in your home folder."
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
desc 'Update the list of plugins in README.md'
task :update_readme do
delete_old_plugins_from_readme
add_plugins_to_readme(plugins)
end
# ----------------------------------------
# Helper Methods
# ----------------------------------------
# Just takes an array of strings that resolve to plugins
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#{PLUGINS_HEADER}")
plugin_rows = plugins
.uniq { |p| p[:name] }
.sort {|x,y|
x[:stars] <=> y[:stars] or x[:name].downcase <=> y[:name].downcase
}
.reverse
.map{|p| table_row(p) }
lines.insert(index+2, plugin_rows)
lines << "\n_That's #{plugins.length} plugins, holy crap._"
lines << "\n_#{PLUGIN_LIST_NOTE} on #{Time.now.strftime('%Y/%m/%d')}._\n\n"
write_lines_to_readme(lines)
else
puts "Error: Plugin List Tag (#{PLUGIN_LIST_TAG}) not found"
end
end
def table_row(plugin)
p = plugin
[
"| #{p[:stars_text]} |",
"[#{p[:name]}](#{p[:uri]})",
p[:config?] ? " [:page_facing_up:](#{p[:config_file]})" : nil,
'|',
"#{p[:description]} |"
].compact.join
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 plugin files.
def plugin_files
Dir.glob("./#{PLUGINS_FOLDER}/**.vim")
end
# Returns an array of hashes containing info for each plugin.
def plugins
plugins = plugin_files.map do |filename|
lines = File.new(filename).readlines
sub_plugins = lines.reduce([]) do |memo, line|
if line =~ /^\s*Plug\s+["'](.+)["']/
plugin_info = fetch_plugin_info($1, File.basename(filename))
plugin_info[:config?] = lines.length > LINES_WITHOUT_CONFIG
memo << plugin_info
end
memo
end
print '.'
sub_plugins
end
plugins.flatten
end
# Returns a hash of info for a plugin based on it's Plug link.
def fetch_plugin_info(plug_link, filename)
info = {}
github_user = ''
github_repo = ''
if /(?<user>[a-zA-Z0-9\-]*)\/(?<name>[a-zA-Z0-9\-\._]*)/ =~ plug_link
github_user = user
github_repo = name
else
github_user = 'vim-scripts'
github_repo = plug_link
end
info[:user] = github_user
info[:name] = github_repo
info[:uri] = "https://github.com/#{github_user}/#{github_repo}"
info[:config_file_name] =
/\.vim$/ =~ github_repo ? github_repo : "#{github_repo}.vim"
info[:config_file] = "#{PLUGINS_FOLDER}/#{filename}"
plugin_info = repo_info(github_user, github_repo)
info[:description] = plugin_info['description'] && plugin_info['description'].strip
info[:stars] = plugin_info['stargazers_count']
info[:stars_text] = "![Star count](https://flat.badgen.net/github/stars/#{github_user}/#{github_repo}?label=★&color=black)"
info
end
def comma_number(number)
number.to_s.gsub(/(\d)(?=(\d\d\d)+(?!\d))/, "\\1,")
end
def repo_info(user, name)
response = ''
api_url = "https://api.github.com/repos/#{user}/#{name}"
# Without a GitHub Client / Secret token you will only be able to make 60
# requests per hour, meaning you can only update the readme once.
# Read more here http://developer.github.com/v3/#rate-limiting.
if not ENV['GITHUB_TOKEN']
throw "Missing GITHUB_TOKEN"
end
api_url += "?access_token=#{ENV['GITHUB_TOKEN']}"
begin
response = open(api_url, "Authorization" => "token #{ENV['GITHUB_TOKEN']}").read
rescue OpenURI::HTTPError => error
message = "Problem fetching #{user}/#{name}."
puts message + error.message
response = error.io
puts response.string
return {'description' => message}
end
JSON.parse(response)
end