-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathRakefile
204 lines (171 loc) · 5.23 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
# frozen_string_literal: true
require 'opal'
require 'json'
require 'yaml'
require 'tomlrb'
task default: :build
task build: %i[
racc
build_opal
build_opal_parser
build_core
build_game_system
build_i18n
build_i18n_json
build_i18n_list
build_test
build_game_system_list
]
def create_builder
builder = Opal::Builder.new
builder.compiler_options = {
method_missing: false
}
builder.stubs = {
# 'i18n' => {},
}
builder.append_paths(
'patched/lib',
'ruby/emurators',
*$LOAD_PATH
)
builder
end
def decleation(source)
File.write "lib/#{source}.d.ts", 'export default undefined;'
end
def build(source, prerequired: [])
puts source
builder = create_builder
builder.prerequired = prerequired if prerequired
builder.build source
opal_path = Pathname.new('bcdice/opal').relative_path_from(File.dirname(source))
File.write "lib/#{source}.js", "require('./#{opal_path}');\n\n#{builder}"
File.write "lib/#{source}.js.map", builder.source_map
decleation(source)
end
directory 'patched'
task copy: 'patched' do
cp_r Dir.glob('BCDice/*'), 'patched/'
end
task patch: [:copy] do
sh 'patch -p1 < ../patch.diff', { chdir: 'patched' }, {}
$LOAD_PATH.prepend "#{__dir__}/patched/lib"
end
task racc: [:copy] do
sh 'bundle exec rake racc', { chdir: 'patched' }, {}
end
directory 'lib/bcdice'
task build_opal: [:patch, 'lib/bcdice'] do
puts 'bcdice/opal'
builder = create_builder
builder.build('opal')
builder.build('native')
builder.build('./ruby/patch.rb')
File.write 'lib/bcdice/opal.js',
builder.to_s
File.write 'lib/bcdice/opal.js.map', builder.source_map
decleation('bcdice/opal')
end
directory 'lib/bcdice'
task build_opal_parser: [:patch, 'lib/bcdice'] do
puts 'bcdice/opal-parser'
builder = create_builder
builder.build('opal-parser')
File.write 'lib/bcdice/opal-parser.js',
builder.to_s
File.write 'lib/bcdice/opal-parser.js.map', builder.source_map
decleation('bcdice/opal-parser')
end
directory 'lib/bcdice'
task build_core: [:patch, 'lib/bcdice'] do
[
'bcdice/arithmetic_evaluator',
'bcdice/base',
'bcdice/common_command',
'bcdice/preprocessor',
'bcdice/randomizer',
'bcdice/user_defined_dice_table',
'bcdice/version'
].each { |source| build(source) }
end
directory 'lib/bcdice/game_system'
task build_game_system: 'lib/bcdice/game_system' do
index_js = "require('../opal');\nrequire('../base');\n"
File.read('patched/lib/bcdice/game_system.rb').scan(/require "([^"]+)"/).each do |m|
source = m[0]
build(source, prerequired: ['i18n'])
index_js += "require('../../#{source}');\n"
end
puts 'bcdice/game_system'
File.write 'lib/bcdice/game_system/index.js', index_js
decleation('bcdice/game_system/index')
end
directory 'lib/bcdice/i18n'
task build_i18n: 'lib/bcdice/i18n' do
i18n = {}
Dir['patched/i18n/*.yml'].each do |path|
i18n = i18n.merge(YAML.load_file(path)) do |_key, oldval, newval|
oldval.merge(newval)
end
end
File.write 'lib/bcdice/i18n/i18n.yml', YAML.dump(i18n)
File.write 'lib/bcdice/i18n/i18n.json', JSON.dump(i18n)
end
directory 'lib/bcdice/i18n'
task build_i18n_json: 'lib/bcdice/i18n' do
path_from = Pathname('patched/i18n')
Dir['patched/i18n/**/*.yml'].each do |path|
relative_path = Pathname(path).relative_path_from(path_from).to_s
next unless relative_path.split('/').length > 1
i18n = YAML.load_file(path)
file_name = File.basename(relative_path.gsub('/', '.'), '.*')
File.write "lib/bcdice/i18n/#{file_name}.json", JSON.dump(i18n)
puts "bcdice/i18n/#{file_name}.json"
end
end
directory 'lib/bcdice'
task build_i18n_list: 'lib/bcdice' do
ids = []
Dir['patched/i18n/**/'].each do |game_path|
locales = []
Dir["#{game_path}*.yml"].each do |path|
locales.push(File.basename(path, '.*'))
end
name = File.split(game_path.gsub('patched/i18n', '')).last
next unless name.length > 1
ids.push({ baseClassName: name, locales: locales })
end
File.write 'lib/bcdice/i18n_list.json', JSON.dump({ i18nList: ids })
puts 'bcdice/i18n_list.json.d.ts'
FileUtils.copy 'ts/bcdice/i18n_list.json.d.ts', 'lib/bcdice/i18n_list.json.d.ts'
end
directory 'lib/bcdice'
task build_test: 'lib/bcdice' do
puts 'bcdice/test_data.json'
tests = {}
Dir['patched/test/**/*.toml'].each do |path|
id = File.basename(path, '.toml')
tests[id] = Tomlrb.load_file(path)
end
File.write 'lib/bcdice/test_data.json', JSON.dump(tests)
end
directory 'lib/bcdice'
task build_game_system_list: [:patch, 'lib/bcdice'] do
puts 'bcdice/game_system_list.json'
require 'bcdice'
require 'bcdice/game_system'
game_systems = BCDice.all_game_systems.map do |game_system_class|
{
id: game_system_class::ID,
name: game_system_class::NAME,
className: game_system_class.name.gsub(/^.*::/, ''),
superClassName: game_system_class.superclass.name.gsub(/^.*::/, ''),
sortKey: game_system_class::SORT_KEY,
locale: game_system_class.new('none').instance_variable_get('@locale')
}
end
File.write 'lib/bcdice/game_system_list.json', JSON.dump({ gameSystems: game_systems })
puts 'bcdice/game_system_list.json.d.ts'
FileUtils.copy 'ts/bcdice/game_system_list.json.d.ts', 'lib/bcdice/game_system_list.json.d.ts'
end