lib/entityjs/commands/build.rb in entityjs-0.4.1 vs lib/entityjs/commands/build.rb in entityjs-0.4.2

- old
+ new

@@ -4,91 +4,103 @@ module Entityjs class Build - def self.generate(name=nil) + def self.generate(args=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 + 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 = 'game.min.js' + 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('builds', true) + Dirc.create_dir(build_folder, true) - #create new directory - if File.directory?(name) - return 3 - end + #clear directory + #FileUtils.rm_rf("#{build_folder}/.", :secure=> true) - assets_root = Dirc.game_root+'/'+assets_folder + assets_root = Dirc.game_root+'/'+Config.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 + puts "Copying assets folder to #{assets_folder}" + FileUtils.cp_r assets_root+'/.', assets_folder #append all files into one big file - puts "Compiling code" + puts "Compiling scripts" + + out = self.compile_game - 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 - + 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(self.minify(out)) - - f.close + f.write(out) end - #create play.html - puts "Creating play page" + #save css + File.open(Config.instance.build_styles_path+"/"+Config.instance.build_styles_name+'.css', 'w') do |f| + f.write(css) + end - File.open(html_name, 'w') do |f| - f.write(%Q(<!DOCTYPE html> + + 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> -)) - f.close +) + + #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 "Successfully built!" + puts "Build Complete!" puts "Build is at" - puts " #{builds_folder}/#{name}" + puts " #{File.expand_path(build_folder)}" Dirc.to_game_root return 0 end @@ -102,11 +114,11 @@ out += IO.read(i) out += "\n" end #add version - out = out.gsub(/\$VERSION/, Entityjs::VERSION) + out = out.gsub(/RE_VERSION/, Entityjs::VERSION) return out end #finds all js inside public/qunit and compiles into one string @@ -121,11 +133,27 @@ out += "\n" end return out end - + + 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 + + def self.minify_styles(styles) + return CSSMin.minify(styles) + end + #compiles all game source and returns it def self.compile_scripts(ignore = nil, order=nil) #find with short urls for proper data processing scripts = Dirc.find_scripts_short(ignore, order) @@ -135,32 +163,52 @@ 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 + 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 + + def self.build_wrap(code) + + head = Config.instance.build_head + foot = Config.instance.build_foot + + return head+code+foot + end + #minifies source and returns it - def self.minify(code, license=true) + def self.minify(code, ops={}) + ops[:copyright] ||= false + ops[:license] ||= true - code = Uglifier.compile(code, :copyright=>false) + code = Uglifier.compile(code, :copyright=>ops[:copyright]) #add entity license statement - if license.is_a? String - code = license + code - elsif license + if ops[:license].is_a? String + code = ops[:license] + code + elsif !ops[:license].nil? code = Config.instance.license + code end return code end def self.js_config(path = nil, images = nil, sounds = nil, canvas = nil) - path ||= 'assets/' + path ||= Config.assets_folder+'/' images ||= self.images_to_js sounds ||= self.sounds_to_js canvas ||= Config.instance.canvas_id return %Q( \ No newline at end of file