-
Notifications
You must be signed in to change notification settings - Fork 5
/
Rakefile
261 lines (222 loc) · 6.63 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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
require "nokogiri"
require "erb"
require 'sqlite3'
class Index
attr_accessor :db
def initialize(path)
@db = SQLite3::Database.new path
end
def drop
@db.execute <<-SQL
DROP TABLE IF EXISTS searchIndex
SQL
end
def create
db.execute <<-SQL
CREATE TABLE searchIndex(id INTEGER PRIMARY KEY, name TEXT, type TEXT, path TEXT)
SQL
db.execute <<-SQL
CREATE UNIQUE INDEX anchor ON searchIndex (name, type, path)
SQL
end
def reset
drop
create
end
def insert(type, path)
doc = Nokogiri::HTML(File.open(path).read)
name = doc.title.sub(" - Terraform by HashiCorp", "").sub(/.*: (.*)/, "\\1")
@db.execute <<-SQL, name: name, type: type, path: path
INSERT OR IGNORE INTO searchIndex (name, type, path)
VALUES(:name, :type, :path)
SQL
end
end
task default: [:clean, :build, :setup, :copy, :create_index, :package]
task :clean do
rm_rf "build"
rm_rf "Terraform.docset"
end
task :build do
config_extensions = ["activate :relative_assets", "set :relative_links, true", "set :strip_index_file, false"]
File.open("config.rb", "a") do |f|
config_extensions.each do |ce|
if File.readlines("config.rb").grep(Regexp.new ce).size == 0
f.puts ce
end
end
end
sh "bundle"
sh "bundle exec middleman build"
end
task :setup do
mkdir_p "Terraform.docset/Contents/Resources/Documents"
# Icon
# at older docs there is no retina icon
if File::exist? "source/assets/images/favicons/favicon-16x16.png" and File::exist? "source/assets/images/favicons/favicon-32x32.png"
cp "source/assets/images/favicons/favicon-16x16.png", "Terraform.docset/icon.png"
cp "source/assets/images/favicons/favicon-32x32.png", "Terraform.docset/[email protected]"
elsif File::exists? "source/assets/images/favicon.png"
cp "source/assets/images/favicon.png", "Terraform.docset/icon.png"
else
cp "source/images/favicon.png", "Terraform.docset/icon.png"
end
# Info.plist
File.open("Terraform.docset/Contents/Info.plist", "w") do |f|
f.write <<-XML
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleIdentifier</key>
<string>terraform</string>
<key>CFBundleName</key>
<string>Terraform</string>
<key>DocSetPlatformFamily</key>
<string>terraform</string>
<key>isDashDocset</key>
<true/>
<key>DashDocSetFamily</key>
<string>dashtoc</string>
<key>dashIndexFilePath</key>
<string>docs/index.html</string>
<key>DashDocSetFallbackURL</key>
<string>https://www.terraform.io/</string>
</dict>
</plist>
XML
end
end
task :copy do
file_list = []
Dir.chdir("build") { file_list = Dir.glob("**/*").sort }
file_list.each do |path|
source = "build/#{path}"
target = "Terraform.docset/Contents/Resources/Documents/#{path}"
case
when File.stat(source).directory?
mkdir_p target
when source.match(/\.gz$/)
next
when source.match(/\.html$/)
doc = Nokogiri::HTML(File.open(source).read)
doc.title = doc.title.sub(" - Terraform by HashiCorp", "")
doc.xpath("//a[contains(@class, 'anchor')]").each do |e|
a = Nokogiri::XML::Node.new "a", doc
a["class"] = "dashAnchor"
a["name"] = "//apple_ref/cpp/%{type}/%{name}" %
{type: "Section", name: ERB::Util.url_encode(e.parent.children.last.text.strip)}
e.previous = a
end
doc.xpath('//script').each do |script|
if script.text != ""
script.remove
end
end
doc.xpath("id('header')").each do |e|
e.remove
end
doc.xpath("//div[contains(@class, 'mega-nav-sandbox')]").each do |e|
e.remove
end
doc.xpath("//div[contains(@class, 'docs-sidebar')]").each do |e|
e.parent.remove
end
doc.xpath("id('docs-sidebar')").each do |e|
e.remove
end
doc.xpath("id('footer')").each do |e|
e.remove
end
doc.xpath('//div[@id="inner"]/h1').each do |e|
e["style"] = "margin-top: 0px"
end
doc.xpath("//div[contains(@role, 'main')]").each do |e|
e["style"] = "width: 100%"
end
File.open(target, "w") { |f| f.write doc }
else
cp source, target
end
end
end
task :create_index do
index = Index.new("Terraform.docset/Contents/Resources/docSet.dsidx")
index.reset
Dir.chdir("Terraform.docset/Contents/Resources/Documents") do
# example
Dir.glob("intro/examples/**/*")
.find_all{ |f| File.stat(f).file? }.each do |path|
index.insert "Sample", path
end
# getting-started
Dir.glob("intro/getting-started/**/*")
.find_all{ |f| File.stat(f).file? }.each do |path|
index.insert "Guide", path
end
# backends
Dir.glob("docs/backends/**/*")
.find_all{ |f| File.stat(f).file? }.each do |path|
index.insert "Environment", path
end
# configuration
Dir.glob("docs/configuration/**/*")
.find_all{ |f| File.stat(f).file? }.each do |path|
index.insert "Setting", path
end
# commands
Dir.glob("docs/commands/**/*")
.find_all{ |f| File.stat(f).file? }.each do |path|
index.insert "Command", path
end
# import
Dir.glob("docs/import/**/*")
.find_all{ |f| File.stat(f).file? }.each do |path|
index.insert "Section", path
end
# state
Dir.glob("docs/state/**/*")
.find_all{ |f| File.stat(f).file? }.each do |path|
index.insert "Instance", path
end
# providers
Dir.glob("docs/providers/**/*")
.find_all{ |f| File.stat(f).file? }.each do |path|
maybe_type_code = path.split("/").reverse.drop(1).first
if maybe_type_code == "r"
type = "Resource"
elsif maybe_type_code == "d"
type = "Directive"
else
type = "Provider"
end
index.insert type, path
end
# provisioners
Dir.glob("docs/provisioners/**/*")
.find_all{ |f| File.stat(f).file? }.each do |path|
index.insert "Provisioner", path
end
# modules
Dir.glob("docs/modules/**/*")
.find_all{ |f| File.stat(f).file? }.each do |path|
index.insert "Module", path
end
# plugins
Dir.glob("docs/plugins/**/*")
.find_all{ |f| File.stat(f).file? }.each do |path|
index.insert "Plugin", path
end
# internals
Dir.glob("docs/internals/**/*")
.find_all{ |f| File.stat(f).file? }.each do |path|
index.insert "Protocol", path
end
end
end
task :import do
sh "open Terraform.docset"
end
task :package do
sh "tar --exclude='.DS_Store' -cvzf Terraform.tgz Terraform.docset"
end