Class: Entityjs::Build

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

Class Method Summary collapse

Class Method Details

.compile_entity(ignore = nil) ⇒ Object

compiles all entity source and returns it



97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/entityjs/commands/build.rb', line 97

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(/\$VERSION/, Entityjs::VERSION)
  
  return out
end

.compile_eunitObject

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



113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/entityjs/commands/build.rb', line 113

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_scripts(ignore = nil, order = nil) ⇒ Object

compiles all game source and returns it



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/entityjs/commands/build.rb', line 128

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
  
  #add js config
  out += self.js_config
  
  return out
end

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

def self.generate(name=nil)
  
  if !Dirc.game?
    return 2
  end
  
  Config.instance.reload
  
  if name.nil? || name.empty?
    date = Time.now.strftime('%s')
    name = "build-#{date}"
  else
    name = name.first
  end
  
  builds_folder = Config.builds_folder
  assets_folder = Config.assets_folder
  images_folder = Config.images_folder
  sounds_folder = Config.sounds_folder
  scripts_folder = Config.scripts_folder
  
  final_name = 'game.min.js'
  html_name = 'play.html'
  
  #build if it doesn't exist
  Dirc.create_dir('builds', true)
  
  #create new directory
  if File.directory?(name)
    return 3
  end
  
  assets_root = Dirc.game_root+'/'+assets_folder
  
  Dir.mkdir(name)
  Dir.chdir(name)
  
  #copy everything inside the assets folder
  puts "Copying assets folder"
  FileUtils.cp_r assets_root, assets_folder
  
  #append all files into one big file
  puts "Compiling code"
  
  entity_src = self.compile_entity(Config.instance.entity_ignore)
  scripts = self.compile_scripts(Config.instance.scripts_ignore, Config.instance.scripts_order)
  
  out = entity_src+scripts
  
  #minify
  puts "Almost done..."
  
  #save
  File.open(final_name, 'w') do |f|
    
    f.write(self.minify(out))
    
    f.close
  end
  
  #create play.html
  puts "Creating play page"
  
  File.open(html_name, 'w') do |f|
    f.write(%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>
))
    f.close
  end
  
  puts "Successfully built!"
  puts "Build is at"
  puts "  #{builds_folder}/#{name}"
  
  Dirc.to_game_root
  
  return 0
end

.images_to_js(images = nil) ⇒ Object

returns all images in a js array



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

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



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

def self.js_config(path = nil, images = nil, sounds = nil, canvas = nil)
  path ||= 'assets/'
  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, license = true) ⇒ Object

minifies source and returns it



147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/entityjs/commands/build.rb', line 147

def self.minify(code, license=true)
  
  code = Uglifier.compile(code, :copyright=>false)
  
  #add entity license statement
  if license.is_a? String
    code = license + code
  elsif license
    code = Config.instance.license + code
  end
  
  return code
end

.sounds_to_js(sounds = nil) ⇒ Object

returns all sounds in a js array



187
188
189
190
191
192
193
# File 'lib/entityjs/commands/build.rb', line 187

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