-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathbuild.rb
executable file
·135 lines (111 loc) · 3.2 KB
/
build.rb
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
#!/usr/bin/env ruby
# Usage:
# ./build $env
#
# env: production, development(default)
require 'yaml'
require 'json'
require 'fileutils'
require_relative 'lib/mx-plan'
module Check
# validate plan.json, return invalid count
def self.perform(filename)
count = 0
begin
json = JSON.parse(File.open(filename, 'r').read);
if json.is_a? Array
json.each do |it|
plan = MxPlan.new(it)
unless plan.valid?
puts "Invalid plan: "
puts JSON.pretty_generate(it)
puts "Details: "
puts plan.errors
count += 1
end
end
else
puts "[E] #{filename} is not an array"
end
return count
rescue JSON::ParserError => e
throw e
end
end
end
module Build
def self.perform(output_dir, deploy_path)
channels = [];
Dir.glob("plans/channel_*.json") do |filename|
channel = JSON.parse(File.open(filename, 'r').read)
channels.push channel
end
channels.each do |channel|
invalid_plan_count = 0
Dir.glob("plans/#{channel['folder']}/*.json") do |filename|
invalid_plan_count += Check.perform(filename)
end
if invalid_plan_count == 0
puts "Channel #{channel['name']} OK"
renderIndexAndPlans(output_dir, deploy_path, channel)
else
puts "Channel #{channel['name']} was not build, there're #{invalid_plan_count} invalid plans"
end
end
puts "\nComplete!\n"
end
def self.renderIndexAndPlans(output_dir, deploy_path, channel)
plans = []
version = 0;
Dir.glob("plans/#{channel['folder']}/*.json") do |filename|
json = JSON.parse(File.open(filename, 'r').read)
json.each do |it|
if it['version'] > version
version = it['version']
end
plans << MxPlan.new(it)
end
end
if version == 0
puts "Not plan to render"
end
folder = "plans/#{channel['folder']}"
url = File.join(deploy_path, folder, 'index.json')
indexFilename = File.join(output_dir, folder, 'index.json')
update_url = File.join(deploy_path, folder, "#{version}.json")
plansFilename = File.join(output_dir, folder, "#{version}.json")
index = {
name: channel['name'],
description: channel['description'],
size: plans.size,
latestVersion: version,
url: url,
updateUrl: update_url
}
mkdir(indexFilename)
File.open(indexFilename, 'w') {|f| f.write JSON.pretty_generate(index)}
File.open(plansFilename, 'w') {|f| f.write JSON.pretty_generate(plans.map(&:to_hash))}
puts indexFilename
puts plansFilename
puts "[Done] channel: #{channel['name']}"
puts ""
end
def self.mkdir(filename)
dir = File.dirname(filename)
unless File.exist?(dir)
FileUtils.mkdir_p(dir)
end
end
end
script_dir = File.expand_path('..', __FILE__)
if FileUtils.pwd != script_dir
puts "[Error] Please execute this script in #{script_dir}"
exit 1
end
env = (ARGV[0] || 'development')
if env == 'production'
config = YAML.load(File.open('./config.production.yaml', "r").read)
else
config = YAML.load(File.open('./config.yaml', "r").read)
end
Build.perform(config['output_dir'], config['deploy_path'])