require 'fileutils' module Entityjs class Build def self.generate(name=nil) if !Dirc.game? return 2 end Config.instance.reload license = Config.instance.license 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 #build if it doesn't exist Dirc.create_dir('builds', true) #create new directory if Dir.exists?(name) return 3 end assets_root = Dirc.game_root+'/' 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 #append all files into one big file puts "Compiling code" out = '' scripts = Dirc.find_scripts(Config.instance.scripts_ignore, Config.instance.scripts_order) entities = Dirc.find_entity_src(Config.instance.entity_ignore) entities.each do |i| out += "\n" out += IO.read(i) out += "\n" end #add version out = out.gsub(/\$VERSION/, Entityjs::VERSION) scripts.each do |i| out += "\n" out += IO.read(i) out += "\n" end #add levels, animations etc data out += Assets.to_js #minify puts "Almost done..." #save File.open('game.min.js', 'w') do |f| f.write(license) f.write(Uglifier.compile(out, :copyright=>false)) f.close end #create play.html puts "Creating play page" File.open('play.html', 'w') do |f| f.write(%Q( Error browser does not support canvas element. )) f.close end puts "Successfully built!" puts "Build is at" puts " #{builds_folder}/#{name}" Dirc.to_game_root return 0 end end end