forked from olivernn/moonwalkers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rakefile
90 lines (71 loc) · 1.87 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
require 'rake'
require 'json'
require 'erb'
require 'open3'
require 'webrick'
class Template
Bio = Struct.new(:id, :name, :body)
def self.render(corpus_path, template_path)
new(corpus_path, template_path).render
end
def initialize(corpus_path, template_path)
@corpus_path = corpus_path
@template_path = template_path
end
def bios
corpus.map do |attrs|
Bio.new(*attrs.values_at('id', 'name', 'body'))
end
end
def render
b = binding
template.result(b)
end
private
attr_reader :corpus_path, :template_path
def corpus
@corpus ||= JSON.parse(File.read(corpus_path))
end
def template
@template ||= ERB.new(File.read(template_path))
end
end
directory "docs"
file 'docs/corpus.json' => ['docs', *Rake::FileList['bios/*.txt']] do |t|
corpus = t.sources.grep(/\.txt$/)
.map do |path|
{
id: path.pathmap('%n'),
name: path.pathmap('%n').gsub('_', ' '),
body: File.read(path),
}
end
File.open(t.name, 'w') do |f|
f << JSON.generate(corpus)
end
end
file 'docs/index.html' => ['docs/corpus.json', 'templates/index.html.erb'] do |t|
File.open(t.name, 'w') do |f|
f << Template.render(*t.sources)
end
end
file 'docs/index.json' => ['docs/corpus.json'] do |t|
Open3.popen2('./build-index') do |stdin, stdout, wt|
IO.copy_stream(t.source, stdin)
stdin.close
IO.copy_stream(stdout, t.name)
end
end
file 'docs/index.js' => [*Rake::FileList['src/*.js']] do |t|
sh "./node_modules/.bin/webpack src/main.js #{t.name}"
end
file 'docs/main.css' => ['docs', 'styles/main.css'] do |t|
cp 'styles/main.css', 'docs/main.css'
end
task :default => ['docs/index.json', 'docs/index.html', 'docs/index.js', 'docs/main.css']
task :server do
WEBrick::HTTPServer.new(:Host => '0.0.0.0', :Port => 8000, :DocumentRoot => Dir.pwd + "/docs").start
end
task :clean do
rm_rf 'docs'
end