forked from mutewinter/dot_vim
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Rakefile
164 lines (140 loc) · 4.38 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
# 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_ins'
LINES_WITHOUT_CONFIG = 4
PLUGINS_HEADER = <<-HEADER.chomp
| Stars____ | **Plugin** | **Description** |
| --------: | :--------- | :-------------- |
HEADER
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 from Plug
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
.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_For a grand total of #{plugins.length} plugins!_"
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)
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 GitHub link.
def fetch_plugin_info(github_link)
info = {}
github_user = ''
github_repo = ''
if /(?<user>[a-zA-Z0-9\-]*)\/(?<name>[a-zA-Z0-9\-\._]*)/ =~ github_link
github_user = user
github_repo = name
else
github_user = 'vim-scripts'
github_repo = github_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}/#{info[:config_file_name]}"
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] = "#{comma_number(plugin_info['stargazers_count'])} ★"
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 ENV['GITHUB_OAUTH_TOKEN']
api_url += "?access_token=#{ENV['GITHUB_OAUTH_TOKEN']}"
end
begin
if RUBY_VERSION < '1.9'
response = open(api_url).read
else
response = open(api_url, :ssl_verify_mode => OpenSSL::SSL::VERIFY_NONE).read
end
rescue Exception => e
message = "Problem fetching #{user}/#{name}."
puts message + e.message
return {'description' => message}
end
JSON.parse(response)
end