lib/entityjs/commands/build.rb in entityjs-0.3.1 vs lib/entityjs/commands/build.rb in entityjs-0.3.2

- old
+ new

@@ -32,35 +32,23 @@ #build if it doesn't exist Dirc.create_dir('builds', true) #create new directory - if Dir.exists?(name) + if File.directory?(name) return 3 end - assets_root = Dirc.game_root+'/' + assets_root = Dirc.game_root+'/'+assets_folder Dir.mkdir(name) Dir.chdir(name) - - #copy images and sounds into assets - Dirc.create_dir(assets_folder) - Dir.chdir(assets_folder) do - - if Dir.exists? assets_root+images_folder - puts "Copying images" - FileUtils.cp_r assets_root+images_folder, 'images' - end - - if Dir.exists? assets_root+sounds_folder - puts "Copying sounds" - FileUtils.cp_r assets_root+sounds_folder, 'sounds' - end - - end + #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) @@ -119,38 +107,90 @@ out = out.gsub(/\$VERSION/, Entityjs::VERSION) return out end + #finds all js inside public/qunit and compiles into one string + 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 + #compiles all game source and returns it def self.compile_scripts(ignore = nil, order=nil) - scripts = Dirc.find_scripts(ignore, order) + #find with short urls for proper data processing + scripts = Dirc.find_scripts_short(ignore, order) out = '' scripts.each do |i| out += "\n" - out += IO.read(i) + out += Compile.script_to_js(i) out += "\n" end - #add levels, animations etc data - out += Assets.to_js + #add js config + out += self.js_config return out end #minifies source and returns it def self.minify(code, license=true) code = Uglifier.compile(code, :copyright=>false) #add entity license statement - if license + if license.is_a? String + code = license + code + elsif license code = Config.instance.license + code end return code + end + + 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 + + #returns all images in a js array + def self.images_to_js(images = nil) + images ||= Assets.search('images') + + s = images.collect{|i| "'#{i}'"}.join(', ') + + "[#{s}]" + end + + #returns all sounds in a js array + def self.sounds_to_js(sounds = nil) + sounds ||= Assets.search('sounds') + + s = sounds.collect{|i| "'#{i}'"}.join(', ') + + "[#{s}]" end end end \ No newline at end of file