require "opal" require "fileutils" require_relative "../../version" class AuthorEngine class OpalExporter def initialize(project_file:) @project_file = project_file save(export) end def project_name name = File.basename(@project_file, ".authorengine") return name.split("_").map {|n| n.capitalize}.join(" ") end # Rebuild author_engine runtime if it doesn't exist or if its out of date def build_authorengine? authorengine_runtime = "#{export_directory}/js/author_engine.js" if File.exists?(authorengine_runtime) file = File.open(authorengine_runtime) version = file.first.gsub("/", "").strip file.close AuthorEngine::VERSION != version else true end end def stylesheet %{ @font-face { font-family: Connection; src: url('fonts/Connection.otf'); } @font-face { font-family: ConnectionBold; src: url('fonts/ConnectionBold.otf'); } body { margin: 0; padding: 0; background: #222; } #canvas { display: block; margin: 0 auto; cursor: none; } #loading { font-family: Connection, sans-serif; color: white; text-align: center; position: absolute; top: 0; bottom: 0; left: 0; right: 0; width: 50%; height: 30%; margin: auto; } } end def project file = File.read(@project_file) %{ var projectString = `#{file}`; } end def author_engine_runtime program = %{ require "author_engine/opal" `var callback = function(){ \#{AuthorEngine::GameRunner.new(`projectString`).show} }; if ( document.readyState === "complete" || (document.readyState !== "loading" && !document.documentElement.doScroll) ) { callback(); } else { document.addEventListener("DOMContentLoaded", callback); }` } puts "Transpiling to JavaScript using Opal..." author_engine_builder = nil if build_authorengine? puts " Building AuthorEngine runtime..." author_engine_builder = Opal::Builder.new base_path = File.expand_path("../../../..", __FILE__) author_engine_builder.append_paths("#{base_path}") author_engine_builder.build_require("author_engine/opal") else puts " Skipping AuthorEngine runtime. Already exists and up to date (v#{AuthorEngine::VERSION})..." end author_engine_js = nil if author_engine_builder author_engine_js = author_engine_builder.build_str(program, "(inline)").to_s end return author_engine_js end def template %{