-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRakefile
85 lines (69 loc) · 2.04 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
require 'open-uri'
require 'yaml'
desc 'Build site into docs directory'
task :build do
jekyll_build
end
desc 'Update index.md and spec folder based on versions in _config.yml'
task :update do
config = YAML.load_file('_config.yml')
current_version = config['current_version']
versions = config['versions']
remove_all_specs(config['update'])
puts ''
puts 'Fetching configured spec versions:'
versions.each do |version|
spec = fetch_spec(version, config['update'])
if current_version == version
write_file('index.md', spec[:body], " (#{version})")
end
filename = File.join(config['update']['output_dir'], version)
write_file("#{filename}.md", spec[:body])
write_file("#{filename}.svg", spec[:diagram]) if spec[:diagram]
end
jekyll_build
end
def jekyll_build
puts 'Rebuilding output into docs directory...'
exec 'jekyll build --destination docs && touch docs/.nojekyll'
end
def write_file(file, content, comment = nil)
puts " - #{file}#{comment}"
File.write(file, content)
end
def fetch_spec(version, config)
document = get(build_file_url('document', version, config))
if config['files']['diagram']
diagram = get(build_file_url('diagram', version, config))
img_tag = config['img_tpl'].gsub('{{file}}', "#{version}.svg")
document.gsub!(/\A(.*\n=+\n)/, "\\1\n#{img_tag}\n")
end
title = document.split("\n", 2).first
body = config['body_tpl'].gsub('{{content}}', document)
.gsub('{{title}}', title)
.gsub('{{version}}', version)
{
version: version,
title: title,
body: body,
diagram: diagram
}
end
def build_file_url(file, version, config)
config['url_tpl']
.gsub('{{version}}', version)
.gsub('{{file}}', config['files'][file])
end
def get(url)
URI.parse(url).read
rescue OpenURI::HTTPError
nil
end
def remove_all_specs(config)
puts ''
puts 'Removing existing spec files:'
Dir["#{config['output_dir']}/*"].each do |file|
puts " #{file.gsub(File.dirname(__FILE__), '')}"
File.delete(file)
end
end