-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.rb
executable file
·57 lines (44 loc) · 1.3 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
#!/usr/bin/env ruby
# frozen_string_literal: true
require 'pathname'
require 'rubygems'
require 'bundler/setup'
require 'zip'
LIBRARY_NAME = 'hoard'
def main
ensure_directory_exists('dist/lib')
build "lib/#{LIBRARY_NAME}.rb", "dist/lib/#{LIBRARY_NAME}.rb"
zip_output
end
def ensure_directory_exists(path)
Pathname(path).mkpath
end
def build(input_filename, output_filename)
input_content = File.read input_filename
result_content = replace_require_statements_with_file_contents input_content
File.write(output_filename, result_content)
end
def replace_require_statements_with_file_contents(string)
result = string
result = replace_require_statements(result) while require_statement? result
result
end
def require_statement?(string)
REQUIRE_STATEMENT =~ string
end
def replace_require_statements(string)
string.gsub(REQUIRE_STATEMENT) do
File.read Regexp.last_match[:filename]
end
end
REQUIRE_STATEMENT = /^require '(?<filename>[^']+)'$/
def zip_output
zip_file = Pathname("dist/#{LIBRARY_NAME}.zip")
zip_file.delete if zip_file.exist?
Zip::File.open(zip_file, Zip::File::CREATE) do |zipfile|
zipfile.add "lib/#{LIBRARY_NAME}.rb", "dist/lib/#{LIBRARY_NAME}.rb"
zipfile.add 'Smaug.toml', 'Smaug.toml'
end
Pathname('dist/lib').rmtree
end
main if __FILE__ == $PROGRAM_NAME