-
Notifications
You must be signed in to change notification settings - Fork 2
/
Rakefile
171 lines (135 loc) · 3.85 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
# encoding: utf-8
require 'json'
require 'pathname'
require 'rake/clean'
require 'yaml'
EXIT_OK = 0
PROJECT = 'jailang'
PROJECT_ROOT = File.dirname(__FILE__)
def exec_with_echo(cmd)
puts(cmd)
stdout = %x[#{cmd}]
puts(stdout) unless stdout.empty?
$?.exitstatus
end
def fail(message)
abort("‼ #{message}")
end
def try(cmd, failure_message)
fail(failure_message) if (exec_with_echo(cmd) != EXIT_OK)
end
def read_yaml(file)
File.file?(file) ? YAML.load(File.read(file)) : {}
end
def write_yaml(file, config)
IO.write(file, config.to_yaml)
end
def relative_path(source, target)
Pathname(target).relative_path_from(Pathname(source)).to_s
end
def docs_dir
File.join(PROJECT_ROOT, 'docs')
end
def jekyll_cmd(verb)
"bundle exec jekyll #{verb} --source #{docs_dir} --layouts #{docs_dir}/_layouts"
end
def jekyll_profile
jekyll_cmd('build --profile')
end
def jekyll_build
jekyll_cmd('build')
end
def jekyll_watch
jekyll_cmd('serve --watch --incremental')
end
def project_config_file
File.join(docs_dir, '_config.yml')
end
def project_version
config = read_yaml(project_config_file)
config['project']['version']
end
def update_project_version(config_file, new_value)
# update config and write to file
config = read_yaml(config_file)
config['project']['version'] = new_value
write_yaml(config_file, config)
end
def time_to_seconds(time)
counts = time.split(':')
fail('too many time components, expected 3 or fewer (h:m:s, m:s, or s)') unless counts.length <= 3
seconds = [1, 60, 3600].take(counts.length).reverse
accumulator = counts.map.with_index { |v,i| v.to_i * seconds[i] }
accumulator.reduce(:+)
end
[
# no files to clean .. yet
].each { |f| CLEAN.include(f) }
Rake::Task[:clean].clear_comments()
Rake::Task[:clean].add_description([
"removes intermediate files to ensure a clean build",
"running now would delete the following:\n #{CLEAN.resolve.join("\n ")}",
].join("\n"))
[
'_site',
].each { |f| CLOBBER.include(f) }
Rake::Task[:clobber].clear_comments()
Rake::Task[:clobber].add_description([
"removes all generated artifacts to restore project to checkout-like state",
"removes the following folders:\n #{CLOBBER.join("\n ")}",
].join("\n"))
task :default => [:list_targets]
task :list_targets do |t, args|
a = "#{PROJECT} v#{project_version} Rakefile"
b = "running on Ruby #{RUBY_VERSION}"
puts "#{a} #{b}"
system('rake -T')
end
desc [
"calls jekyll to generate, serve, watch and regenerate the docs site",
" cmd: #{jekyll_watch}",
].join("\n")
task :docs do |t, args|
begin # run jekyll
puts jekyll_watch
system(jekyll_watch)
rescue Exception => e # capture interrupt signal from the process
puts ' (stop)'
end
end
desc [
"calls jekyll to build with profiling on, saves output to file",
" cmd: #{jekyll_profile}",
].join("\n")
task :prof do |t, args|
file = "#{Time.now.strftime('%Y%m%d%H%M%S')}.prof"
cmd = "#{jekyll_profile} > #{file}"
puts cmd
system(cmd)
puts "[#{t.name}] task completed, data written to '#{file}'"
end
desc [
"reports project version",
].join("\n")
task :version do |t, args|
puts "#{PROJECT} v#{project_version}"
end
desc [
"updates the project version number",
"(this is reflected in the generated docs footer)",
"changes the following files:",
" '#{relative_path(Pathname.pwd, project_config_file)}'",
].join("\n")
task :set_version, [:v] do |t, args|
args.with_defaults(:v => nil)
fail('please provide a version string') unless (args.v && args.v.length > 0)
update_project_version(project_config_file, args.v)
puts "[#{t.name}] task completed, #{PROJECT} updated to #{project_version}"
end
desc [
"converts h:m:s or m:s to seconds for youtube time marks",
].join("\n")
task :time, [:t] do |t, args|
fail('please provide a time string as h:m:s') unless (args.t && args.t.length > 0)
puts "t=#{time_to_seconds(args.t)}"
end