forked from sketchplugins/plugin-directory
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRakefile
175 lines (143 loc) · 4.92 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
require 'json'
require 'time'
require "./lib/plugin-directory-utils"
GITHUB_AUTH_TOKEN = `git config com.bohemiancoding.qa.token`.strip
USERNAME = `git config github.user`.strip
def fix_plugin_title plugin
if (plugin['name'] == plugin['title'] && !(IGNORE.include? plugin['title'])) || plugin['title'] == nil
puts "— #{plugin['name']} - #{plugin['title']}: Plugin title is wrong, fixing"
plugin['title'] = titlefy(plugin['name'])
end
end
def get_plugins_from_json
data = IO.read('plugins.json')
data.force_encoding('utf-8')
JSON.parse(data)
end
def is_plugin_too_old? plugin
if plugin['lastUpdated']
last_update = Time.parse(plugin['lastUpdated'])
return (Time.now - last_update) > 60_000_000
else
return false
end
end
desc "Clones all repositories to the 'clones' folder"
task :clone do
mkdir "clones" unless File.directory? "clones"
get_plugins_from_json.each do |plugin|
name = plugin['name']
owner = plugin['owner']
url = "https://github.com/#{owner}/#{name}"
system("git clone #{url} clones/#{owner}-#{name}")
end
end
desc "Updates all clones in the 'clones' folder"
task :update do
get_plugins_from_json.each do |plugin|
name = plugin['name']
owner = plugin['owner']
url = "https://github.com/#{owner}/#{name}"
if File.directory? "clones/#{owner}-#{name}"
puts "Updating #{owner}-#{name} to latest version"
system("cd clones/#{owner}-#{name}/ && git up")
else
puts "Cloning #{owner}-#{name} to latest version"
system("git clone #{url} clones/#{owner}-#{name}")
end
end
end
desc "Generate README.md from plugins.json"
task :readme do
plugins = get_plugins_from_json
output = <<EOF
# Sketch Plugin Directory
A list of Sketch plugins hosted at GitHub, in alphabetical order.
**Note:** if you want to add yours, just open an issue with the URL, or send a pull request.
EOF
plugins.sort_by { |k| [ (k["title"] ? k["title"].downcase : k["name"].downcase), (k["owner"] ? k["owner"].downcase : k["author"].downcase) ] }.each do |plugin|
if is_plugin_too_old? plugin
next
end
if plugin['hidden'] == true
next
end
name = plugin['name']
owner = plugin['owner']
author = plugin['author'] || owner
title = plugin['title'] || name
url = plugin['homepage'] || "https://github.com/#{owner.downcase}/#{name.downcase}"
desc = plugin['description'].strip
output << "- [#{title}](#{url}), by #{author}:"
if !desc.empty?
output << " #{desc}"
end
output << "\n"
end
output << "\n\n## Sorted by last update (newest on top)\n\n"
# plugins.reject { |k| k["lastUpdated"] == nil }.sort_by { |k| Date.parse(k["lastUpdated"]).strftime("%s").to_i }.reverse.each do |plugin|
plugins.reject { |k| k["lastUpdated"] == nil }.sort_by { |k| Time.parse(k["lastUpdated"]) }.reverse.each do |plugin|
if is_plugin_too_old? plugin
next
end
if plugin['hidden'] == true
next
end
name = plugin['name']
owner = plugin['owner']
author = plugin['author'] || owner
title = plugin['title'] || name
url = plugin['homepage'] || "https://github.com/#{owner.downcase}/#{name.downcase}"
desc = plugin['description'].strip
output << "- [#{title}](#{url}), by #{author}:"
if !desc.empty?
output << " #{desc}"
end
output << "\n"
end
IO.write('README.md',output)
end
desc "Fix plugin titles"
task :fixtitles do
json_data = get_plugins_from_json
json_data.each do |plugin|
fix_plugin_title plugin
end
File.open("plugins-titles-fixed.json","w") do |f|
f.write(JSON.pretty_generate(json_data, :indent => " "))
end
end
desc "Update `lastUpdated` field for all plugin in JSON"
task :lastUpdated do
require 'octokit'
client = Octokit::Client.new(:access_token => GITHUB_AUTH_TOKEN)
json_data = get_plugins_from_json
json_data.each do |plugin|
# Only check for last push date for plugins with a repo
if plugin['owner'] && plugin['name']
puts "Updating #{titlefy(plugin['name'])}"
plugin_url = plugin['owner'] + "/" + plugin['name']
repo = client.repo(plugin_url)
user = client.user(plugin['owner'])
puts "— Plugin was updated at #{repo.pushed_at}"
plugin['lastUpdated'] = repo.pushed_at
# if plugin['name'] == plugin['title'] && plugin['title'] == nil
# puts "— Plugin title is wrong, fixing"
# plugin['title'] = titlefy(plugin['name'])
# end
end
puts
end
File.open("plugins-new.json","w") do |f|
f.write(JSON.pretty_generate(json_data, :indent => " "))
end
end
desc "List authors"
task :authors do
plugins = get_plugins_from_json
authors = plugins.collect { |plugin| (plugin['author'] ? plugin['author'].downcase + " (" + plugin['owner'].downcase + ")" : plugin['owner'].downcase ) }.uniq.sort
puts authors
puts "\n" + authors.size.to_s + " unique authors."
end
desc "Default: generate README.md from plugin"
task :default => :readme