Class: Entityjs::Build

Inherits:
Object
  • Object
show all
Defined in:
lib/entityjs/commands/build.rb

Class Method Summary collapse

Class Method Details

.build_wrap(code) ⇒ Object



184
185
186
187
188
189
190
# File 'lib/entityjs/commands/build.rb', line 184

def self.build_wrap(code)

  head = Config.instance.build_head
  foot = Config.instance.build_foot

  return head+code+foot
end

.compile_entity(ignore = nil) ⇒ Object

compiles all entity source and returns it



109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/entityjs/commands/build.rb', line 109

def self.compile_entity(ignore = nil)
  out = ''
  entities = Dirc.find_entity_src(ignore)
  entities.each do |i|
    out += "\n"
    out += IO.read(i)
    out += "\n"
  end
  
  #add version
  out = out.gsub(/RE_VERSION/, Entityjs::VERSION)
  
  return out
end

.compile_eunitObject

finds all js inside public/qunit and compiles into one string



125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/entityjs/commands/build.rb', line 125

def self.compile_eunit()
  out = ''
  
  units = Dirc.find_eunit_src
  
  units.each do |i|
    out += "\n"
    out += IO.read(i)
    out += "\n"
  end
  
  return out
end

.compile_gameObject



176
177
178
179
180
181
182
# File 'lib/entityjs/commands/build.rb', line 176

def self.compile_game

  entity_src = self.compile_entity(Config.instance.build_entity_ignore+Config.instance.entity_ignore)
  scripts = self.compile_scripts(Config.instance.build_scripts_ignore+Config.instance.scripts_ignore, Config.instance.scripts_order)
  
  self.build_wrap(entity_src+scripts)
end

.compile_scripts(ignore = nil, order = nil) ⇒ Object

compiles all game source and returns it



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/entityjs/commands/build.rb', line 156

def self.compile_scripts(ignore = nil, order=nil)
  #find with short urls for proper data processing
  scripts = Dirc.find_scripts_short(ignore, order)
  
  out = ''
  
  scripts.each do |i|
    out += "\n"
    out += Compile.script_to_js(i)
    out += "\n"
  end
  
  out = Config.preprocess(out)

  #add js config
  out += self.js_config
  
  return out
end

.compile_styles(ignore = nil) ⇒ Object



139
140
141
142
143
144
145
146
147
148
149
# File 'lib/entityjs/commands/build.rb', line 139

def self.compile_styles(ignore = nil)
  styles = Dirc.find_styles(ignore)

  out = ''

  styles.each do |i|
    out += IO.read(i)
  end

  return Config.preprocess(out)
end

.generate(args = nil) ⇒ Object



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
# File 'lib/entityjs/commands/build.rb', line 9

def self.generate(args=nil)
  
  if !Dirc.game?
    return 2
  end
  
  Config.instance.reload
  
  build_folder = Config.instance.build_path
  assets_folder = Config.instance.build_assets_path
  images_folder = Config.images_folder
  sounds_folder = Config.sounds_folder
  scripts_folder = Config.scripts_folder
  
  final_name = Config.instance.build_name+'.js'
  html_name = 'play.html'
  
  puts "Building to #{build_folder}"

  #build if it doesn't exist
  Dirc.create_dir(build_folder, true)
  
  #clear directory
  #FileUtils.rm_rf("#{build_folder}/.", :secure=> true)
  
  assets_root = Dirc.game_root+'/'+Config.assets_folder
  
  #copy everything inside the assets folder
  puts "Copying assets folder to #{assets_folder}"
  FileUtils.cp_r assets_root+'/.', assets_folder
  
  #append all files into one big file
  puts "Compiling scripts"

  out = self.compile_game
  
  puts "Minifying scripts"

  out = self.minify(out)

  puts "Minifying styles"

  css = self.minify_styles(self.compile_styles(Config.instance.build_styles_ignore))

  #minify
  puts "Almost done..."

  #save
  File.open(final_name, 'w') do |f|
    
    f.write(out)
  end
  
  #save css
  File.open(Config.instance.build_styles_path+"/"+Config.instance.build_styles_name+'.css', 'w') do |f|
    f.write(css)
  end
  

  if Config.instance.build_ignore_play.nil?
    #create play.html
    puts "Creating play page"
  
  #create play.html code
  play_code = %Q(<!DOCTYPE html>
<html>
  <head>
<meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1">
<script src='#{final_name}' type='text/javascript'></script>
  </head>
  <body>
<canvas id='#{Config.instance.canvas_id}' width='#{Config.instance.width}' height='#{Config.instance.height}'>Error browser does not support canvas element.</canvas>
  </body>
</html>
)
  
    #check if local play.html exists
    if Dirc::exists?('play.html')
      #create js for html
      js = "<script src='#{final_name}' type='text/javascript'></script>"
      play_code = Page::render_play_page(:js=>js)
    end

    File.open(html_name, 'w') do |f|
      f.write(play_code)
    end
  else
    puts "Ignoring play.html"
  end
  
  puts "Build Complete!"
  puts "Build is at"
  puts "  #{File.expand_path(build_folder)}"
  
  Dirc.to_game_root
  
  return 0
end

.images_to_js(images = nil) ⇒ Object

returns all images in a js array



226
227
228
229
230
231
232
# File 'lib/entityjs/commands/build.rb', line 226

def self.images_to_js(images = nil)
  images ||= Assets.search('images')
  
  s = images.collect{|i| "'#{i}'"}.join(', ')
  
  "[#{s}]"
end

.js_config(path = nil, images = nil, sounds = nil, canvas = nil) ⇒ Object



209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'lib/entityjs/commands/build.rb', line 209

def self.js_config(path = nil, images = nil, sounds = nil, canvas = nil)
  path ||= Config.assets_folder+'/'
  images ||= self.images_to_js
  sounds ||= self.sounds_to_js
  canvas ||= Config.instance.canvas_id
  
  return %Q(
  re.load.path = \"#{path}\";
  re.assets = {
    images:#{images},
    sounds:#{sounds}
    };
  re.canvas = \"##{canvas}\";
  )
end

.minify(code, ops = {}) ⇒ Object

minifies source and returns it



193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/entityjs/commands/build.rb', line 193

def self.minify(code, ops={})
  ops[:copyright] ||= false
  ops[:license] ||= true
  
  code = Uglifier.compile(code, :copyright=>ops[:copyright])
  
  #add entity license statement
  if ops[:license].is_a? String
    code = ops[:license] + code
  elsif !ops[:license].nil?
    code = Config.instance.license + code
  end
  
  return code
end

.minify_styles(styles) ⇒ Object



151
152
153
# File 'lib/entityjs/commands/build.rb', line 151

def self.minify_styles(styles)
  return CSSMin.minify(styles)
end

.sounds_to_js(sounds = nil) ⇒ Object

returns all sounds in a js array



235
236
237
238
239
240
241
# File 'lib/entityjs/commands/build.rb', line 235

def self.sounds_to_js(sounds = nil)
  sounds ||= Assets.search('sounds')
  
  s = sounds.collect{|i| "'#{i}'"}.join(', ')
  
  "[#{s}]"
end