-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRakefile
200 lines (162 loc) · 5.27 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
require_relative 'lib/models/modou/store'
desc 'download apps as described in app-links.yml'
task :download_apps do
require "yaml"
YAML.load_file('app-links.yml')['github'].each do |app|
require "octokit"
require "dotenv"
Dotenv.load
if ENV['GITHUB_CLIENT_ID'] && ENV['GITHUB_CLIENT_SECRET']
client = Octokit::Client.new \
:client_id => ENV['GITHUB_CLIENT_ID'],
:client_secret => ENV['GITHUB_CLIENT_SECRET']
else
client = Octokit
end
latest_release = client.releases(app).first
asset = latest_release.assets.first
if File.exists?("tmp/cache/apps/#{asset.name}")
puts "#{asset.name} is already downloaded."
else
puts "#{asset.name}, #{asset.browser_download_url}"
system(ENV, "wget #{asset.browser_download_url} -O tmp/cache/apps/#{asset.name}")
end
end
end
desc 'copy and rename downloaded apps from tmp/cache/apps to data/apps'
task :release_apps do
require "yaml"
Dir['tmp/cache/apps/*'].each do |app_file|
if manifest_hash = manifest_hash_for_file(app_file)
# 0: none, 1: stable, 2: dev
channel = 0
# Stable
yml_file = Dir['data/stable/*.yml'].select do |file|
YAML.load_file(file)['package_id'] == manifest_hash['package_id']
end.first
if yml_file
channel = 1
else
# Dev
yml_file = Dir['data/dev/*.yml'].select do |file|
YAML.load_file(file)['package_id'] == manifest_hash['package_id']
end.first
if yml_file
channel = 2
end
end
if channel != 0
app_info = YAML.load_file(yml_file)
`cp #{app_file} data/apps/#{app_info['name']}-#{app_info['version']}.mpk`
tmp_folder = tmp_folder_for_app_name(app_name_for_app_file(app_file))
require "pathname"
icon_path = Pathname.new(tmp_folder).join(manifest_hash['icon'])
if File.exists?(icon_path)
`cp #{icon_path} #{icon_path_for_app(app_info)}`
end
end
else
puts "manifest.json not found for #{app_file}"
end
end
end
def icon_path_for_app(app_hash)
"data/icons/#{app_hash['name']}-#{app_hash['version']}.png"
end
def app_name_for_app_file(app_file)
app_file.split('/').last.gsub('.mpk', '').gsub('.tar.gz', '')
end
def tmp_folder_for_app_name(app_name)
"tmp/app_folder/#{app_name}"
end
def manifest_hash_for_file(app_file)
app_name = app_name_for_app_file(app_file)
tmp_folder = tmp_folder_for_app_name(app_name)
`mkdir -p #{tmp_folder}`
`tar xzf #{app_file} -C #{tmp_folder}`
if File.exists?("#{tmp_folder}/manifest.json")
require "json"
JSON(File.read("#{tmp_folder}/manifest.json"))
end
end
desc 'generate meta info for app store from downloaded apps'
task :gen_meta do
require "yaml"
require 'digest'
require 'securerandom'
Dir['tmp/cache/apps/*'].each do |app_file|
if manifest_hash = manifest_hash_for_file(app_file)
# 0: none, 1: stable, 2: dev
channel = 0
# Stable
yml_file = Dir["data/stable/*.yml"].select do |file|
YAML.load_file(file)['package_id'] == manifest_hash['package_id']
end.first
if yml_file
channel = 1
else
# Dev
yml_file = Dir["data/dev/*.yml"].select do |file|
YAML.load_file(file)['package_id'] == manifest_hash['package_id']
end.first
if yml_file
channel = 2
end
end
# defaults release to Stable
if channel == 0
app_info = merge_manifest_hash({}, manifest_hash)
app_info['id'] = SecureRandom.uuid
yml_file = "data/stable/#{manifest_hash['name']}.yml"
else
# already has definition, update it
app_info = merge_manifest_hash(YAML.load_file(yml_file), manifest_hash)
end
app_info['size'] = File.size(app_file)
app_info['md5_sum'] = Digest::MD5.file(app_file).hexdigest
if YAML.load_file(yml_file) != app_info
puts "updating #{yml_file}"
app_info['available'] = true
require "time"
app_info['updated_at'] = Time.now.utc.iso8601
File.write(yml_file, app_info.to_yaml)
end
else
puts "manifest.json not found for #{app_file}"
end
end
end
def merge_manifest_hash(app_store_hash, manifest_hash)
%w{ package_id author homepage version release_date icon description }.each do |spec|
app_store_hash[spec] = manifest_hash[spec]
end
spec_mapping = {
display_name: 'name',
email: 'author_mail',
install_location: 'location',
instructions: 'instruction',
require_os_version: 'os_version'
}
spec_mapping.each do |key, value|
app_store_hash[key.to_s] = manifest_hash[value]
end
app_store_hash
end
desc 'push packages and icons to qiniu'
task :push_to_qiniu do
require "dotenv"
Dotenv.load
`qboxrsctl login #{ENV['QINIU_ACCESS_KEY']} #{ENV['QINIU_SECRET_KEY']}`
# push mpk
Dir['data/apps/*.mpk'].each do |file|
key = file[5..-1]
`qboxrsctl put #{ENV['QINIU_BUCKET']} #{key} #{file}`
puts "#{file} pushed to http://#{ENV['QINIU_BUCKET']}.qiniudn.com/#{key}"
end
# push icons
Dir['data/icons/*.png'].each do |file|
key = file[5..-1]
`qboxrsctl put #{ENV['QINIU_BUCKET']} #{key} #{file}`
puts "#{file} pushed to http://#{ENV['QINIU_BUCKET']}.qiniudn.com/#{key}"
end
end