public override void ExportProject(Dictionary <string, FileOutput> output, IList <LibraryForExport> libraries, ResourceDatabase resourceDatabase, Options options) { Dictionary <string, string> replacements = this.GenerateReplacementDictionary(options, resourceDatabase); TemplateReader templates = new TemplateReader(new PkgAwareFileUtil(), this); TemplateSet vmTemplates = templates.GetVmTemplates(); string functions = vmTemplates.GetText("functions.php"); string structs = vmTemplates.GetText("structs.php"); string byteCode = ConvertStringToVariableSetterFile(resourceDatabase.ByteCodeFile.TextContent, "_CRAYON_BYTE_CODE"); string resourceManifest = ConvertStringToVariableSetterFile(resourceDatabase.ResourceManifestFile.TextContent, "_CRAYON_RESOURCE_MANIFEST"); output["crayon_gen/bytecode.php"] = FileOutput.OfString(byteCode); output["crayon_gen/resource_manifest.php"] = FileOutput.OfString(resourceManifest); output["crayon_gen/functions.php"] = FileOutput.OfString(functions); output["crayon_gen/structs.php"] = FileOutput.OfString(structs); output["index.php"] = FileOutput.OfString(this.LoadTextResource("Resources/index.php", replacements)); output[".htaccess"] = FileOutput.OfString(this.LoadTextResource("Resources/htaccess.txt", replacements)); List <string> libsIncluder = new List <string>() { "<?php" }; foreach (LibraryForExport library in libraries.Where(lib => lib.HasNativeCode)) { foreach (string key in library.ExportEntities.Keys) { foreach (ExportEntity entity in library.ExportEntities[key]) { switch (key) { case "COPY_CODE": string target = entity.Values["target"]; string fileContent = entity.FileOutput.TextContent; int lastLine = fileContent.LastIndexOf('\n'); fileContent = fileContent.Substring(0, lastLine); // trim off '?>' fileContent = fileContent.TrimEnd() + string.Join("\n", new string[] { "", "", "\t$_CRAYON_LIBS['" + library.Name + "'] = crayon_generateFunctionLookup('CrayonLibWrapper_" + library.Name + "');", "?>", }); output["crayon_gen/" + target] = FileOutput.OfString(fileContent); libsIncluder.Add("\trequire 'crayon_gen/" + target + "';"); break; default: throw new System.NotImplementedException(); } } } } libsIncluder.Add("?>"); output["crayon_gen/libs.php"] = FileOutput.OfString(string.Join("\n", libsIncluder)); }
private void ExportInterpreter( TemplateReader templateReader, string baseDir, Dictionary <string, FileOutput> output) { TemplateSet vmTemplates = templateReader.GetVmTemplates(); foreach (string structKey in vmTemplates.GetPaths("structs/")) { string structFileName = structKey.Substring(structKey.LastIndexOf('/') + 1); string structName = System.IO.Path.GetFileNameWithoutExtension(structFileName); output[baseDir + "Structs/" + structName + ".cs"] = new FileOutput() { Type = FileOutputType.Text, TextContent = vmTemplates.GetText(structKey), }; } output[baseDir + "Vm/CrayonWrapper.cs"] = new FileOutput() { Type = FileOutputType.Text, TextContent = vmTemplates.GetText("CrayonWrapper.cs"), }; }
public override void ExportProject( Dictionary <string, FileOutput> output, IList <LibraryForExport> libraries, ResourceDatabase resourceDatabase, Options options) { List <string> jsExtraHead = new List <string>() { options.GetStringOrEmpty(ExportOptionKey.JS_HEAD_EXTRAS) }; if (options.GetBool(ExportOptionKey.JS_FULL_PAGE)) { jsExtraHead.Add( "<script type=\"text/javascript\">" + "C$common$globalOptions['fullscreen'] = true;" + "</script>"); } options.SetOption(ExportOptionKey.JS_HEAD_EXTRAS, string.Join("\n", jsExtraHead)); Dictionary <string, string> replacements = this.GenerateReplacementDictionary(options, resourceDatabase); TemplateReader templateReader = new TemplateReader(new PkgAwareFileUtil(), this); TemplateSet vmTemplates = templateReader.GetVmTemplates(); output["vm.js"] = new FileOutput() { Type = FileOutputType.Text, TextContent = vmTemplates.GetText("vm.js"), }; List <LibraryForExport> librariesWithCode = new List <LibraryForExport>(); foreach (LibraryForExport library in libraries.Where(lib => lib.HasNativeCode)) { string libraryName = library.Name; TemplateSet libTemplates = templateReader.GetLibraryTemplates(library); List <string> libraryLines = new List <string>(); libraryLines.Add(libTemplates.GetText("gen/lib_" + libraryName.ToLowerInvariant() + ".js")); libraryLines.Add(""); libraryLines.Add("C$common$scrapeLibFuncNames('" + libraryName.ToLowerInvariant() + "');"); libraryLines.Add(""); // add helper functions after the scrape. foreach (string jsHelperFile in libTemplates.GetPaths("source/", ".js")) { libraryLines.Add(libTemplates.GetText(jsHelperFile)); libraryLines.Add(""); } output["libs/lib_" + libraryName.ToLowerInvariant() + ".js"] = new FileOutput() { Type = FileOutputType.Text, TextContent = string.Join(this.NL, libraryLines), }; librariesWithCode.Add(library); } Dictionary <string, string> htmlReplacements = new Dictionary <string, string>(replacements); replacements["JS_LIB_INCLUSIONS"] = GenerateJsLibInclusionHtml(output.Keys); this.CopyResourceAsText(output, "index.html", "Resources/HostHtml.txt", replacements); this.CopyResourceAsText(output, "test_server.py", "Resources/TestServerPy.txt", replacements); this.CopyResourceAsText(output, "common.js", "Resources/Common.txt", replacements); System.Text.StringBuilder resourcesJs = new System.Text.StringBuilder(); foreach (FileOutput textResource in resourceDatabase.TextResources) { resourcesJs.Append("C$common$addTextRes("); resourcesJs.Append(StringTokenUtil.ConvertStringValueToCode(textResource.CanonicalFileName)); resourcesJs.Append(", "); resourcesJs.Append(StringTokenUtil.ConvertStringValueToCode(textResource.TextContent)); resourcesJs.Append(");\n"); } foreach (FileOutput fontResource in resourceDatabase.FontResources) { resourcesJs.Append("C$common$addBinaryRes("); resourcesJs.Append(StringTokenUtil.ConvertStringValueToCode(fontResource.CanonicalFileName)); resourcesJs.Append(", '"); resourcesJs.Append(Base64.ToBase64(fontResource.GetFinalBinaryContent())); resourcesJs.Append("');\n"); } FileOutput imageSheetManifest = resourceDatabase.ImageSheetManifestFile; resourcesJs.Append("C$common$addTextRes('image_sheets.txt', "); resourcesJs.Append(imageSheetManifest == null ? "''" : StringTokenUtil.ConvertStringValueToCode(imageSheetManifest.TextContent)); resourcesJs.Append(");\n"); resourcesJs.Append("C$common$resourceManifest = "); resourcesJs.Append(StringTokenUtil.ConvertStringValueToCode(resourceDatabase.ResourceManifestFile.TextContent)); resourcesJs.Append(";\n"); string filePrefix = options.GetStringOrNull(ExportOptionKey.JS_FILE_PREFIX); if (filePrefix != null) { resourcesJs.Append("C$common$jsFilePrefix = "); resourcesJs.Append(StringTokenUtil.ConvertStringValueToCode(filePrefix)); resourcesJs.Append(";\n"); } output["resources.js"] = new FileOutput() { Type = FileOutputType.Text, TextContent = resourcesJs.ToString(), }; output["bytecode.js"] = new FileOutput() { Type = FileOutputType.Text, TextContent = "C$bytecode = " + StringTokenUtil.ConvertStringValueToCode(resourceDatabase.ByteCodeFile.TextContent) + ";", }; foreach (string imageResourceFile in resourceDatabase.ImageSheetFiles.Keys) { FileOutput file = resourceDatabase.ImageSheetFiles[imageResourceFile]; output["resources/images/" + imageResourceFile] = file; } foreach (FileOutput audioResourceFile in resourceDatabase.AudioResources) { output["resources/audio/" + audioResourceFile.CanonicalFileName] = audioResourceFile; } // TODO: minify JavaScript across all of output dictionary }
public override void ExportProject( Dictionary <string, FileOutput> output, IList <LibraryForExport> libraries, ResourceDatabase resourceDatabase, Options options) { Dictionary <string, string> replacements = this.GenerateReplacementDictionary(options, resourceDatabase); string srcPath = "src"; string srcPackagePath = srcPath + "/" + replacements["JAVA_PACKAGE"].Replace('.', '/') + "/"; List <string> imports = new List <string>() { "import java.util.ArrayList;", "import java.util.HashMap;", "import org.crayonlang.interpreter.Interpreter;", "import org.crayonlang.interpreter.TranslationHelper;", "import org.crayonlang.interpreter.ResourceReader;", "import org.crayonlang.interpreter.structs.*;", }; imports.Sort(); TemplateReader templateReader = new TemplateReader(new PkgAwareFileUtil(), this); foreach (LibraryForExport library in libraries.Where(t => t.HasNativeCode)) { TemplateSet libraryTemplates = templateReader.GetLibraryTemplates(library); string libraryPath = srcPath + "/org/crayonlang/libraries/" + library.Name.ToLowerInvariant(); foreach (string templatePath in libraryTemplates.GetPaths("gen/").Concat(libraryTemplates.GetPaths("source/"))) { string code = libraryTemplates.GetText(templatePath); string realPath = templatePath.Substring(templatePath.IndexOf('/') + 1); output[libraryPath + "/" + realPath] = new FileOutput() { Type = FileOutputType.Text, TextContent = code, TrimBomIfPresent = realPath.EndsWith(".java"), }; } } TemplateSet vmTemplates = templateReader.GetVmTemplates(); output["src/org/crayonlang/interpreter/vm/CrayonWrapper.java"] = new FileOutput() { Type = FileOutputType.Text, TextContent = vmTemplates.GetText("CrayonWrapper.java"), TrimBomIfPresent = true, }; foreach (string structKey in vmTemplates.GetPaths("structs/")) { string structName = System.IO.Path.GetFileNameWithoutExtension(structKey); string structCode = vmTemplates.GetText(structKey); output["src/org/crayonlang/interpreter/structs/" + structName + ".java"] = new FileOutput() { Type = FileOutputType.Text, TextContent = structCode, TrimBomIfPresent = true, }; } // common Java helper files this.CopyResourceAsText(output, "src/org/crayonlang/interpreter/LibraryInstance.java", "Resources/LibraryInstance.java", replacements); this.CopyResourceAsText(output, "src/org/crayonlang/interpreter/TranslationHelper.java", "Resources/TranslationHelper.java", replacements); this.CopyResourceAsText(output, "src/org/crayonlang/interpreter/PlatformTranslationHelper.java", "Resources/PlatformTranslationHelper.java", replacements); // java-app specific files this.CopyResourceAsText(output, "src/org/crayonlang/interpreter/LibraryLoader.java", "Resources/LibraryLoader.java", replacements); this.CopyResourceAsText(output, "src/org/crayonlang/interpreter/ResourceReader.java", "Resources/ResourceReader.java", replacements); this.CopyResourceAsText(output, srcPackagePath + "/Main.java", "Resources/Main.java", replacements); this.CopyResourceAsText(output, "build.xml", "Resources/Build.xml", replacements); output["resources/manifest.txt"] = resourceDatabase.ResourceManifestFile; output["resources/bytecode.txt"] = resourceDatabase.ByteCodeFile; if (resourceDatabase.ImageSheetManifestFile != null) { output["resources/imagesheetmanifest.txt"] = resourceDatabase.ImageSheetManifestFile; } foreach (string imageSheetFileName in resourceDatabase.ImageSheetFiles.Keys) { FileOutput imageSheetFile = resourceDatabase.ImageSheetFiles[imageSheetFileName]; output["resources/images/" + imageSheetFileName] = imageSheetFile; } foreach (FileOutput audioResource in resourceDatabase.AudioResources) { output["resources/audio/" + audioResource.CanonicalFileName] = audioResource; } foreach (FileOutput textResource in resourceDatabase.TextResources) { output["resources/text/" + textResource.CanonicalFileName] = textResource; } foreach (FileOutput fontResource in resourceDatabase.FontResources) { output["resources/ttf/" + fontResource.CanonicalFileName] = fontResource; } IEnumerable <FileOutput> javaFiles = output.Keys .Where(filename => filename.ToLowerInvariant().EndsWith(".java")) .Select(filename => output[filename]); foreach (FileOutput file in javaFiles) { file.TrimBomIfPresent = true; } }
public override void ExportProject( Dictionary <string, FileOutput> output, IList <LibraryForExport> libraries, ResourceDatabase resourceDatabase, Options options) { Dictionary <string, string> replacements = this.GenerateReplacementDictionary(options, resourceDatabase); TemplateReader templates = new TemplateReader(new PkgAwareFileUtil(), this); TemplateSet vmTemplates = templates.GetVmTemplates(); output["code/vm.py"] = new FileOutput() { Type = FileOutputType.Text, TextContent = string.Join(this.NL, new string[] { this.LoadTextResource("Resources/header.txt", replacements), this.LoadTextResource("Resources/TranslationHelper.txt", replacements), this.LoadTextResource("Resources/LibraryRegistry.txt", replacements), this.LoadTextResource("Resources/ResourceReader.txt", replacements), vmTemplates.GetText("vm.py"), }), }; foreach (LibraryForExport library in libraries) { if (library.HasNativeCode) { TemplateSet libTemplates = new TemplateReader(new PkgAwareFileUtil(), this).GetLibraryTemplates(library); string libraryName = library.Name; List <string> libraryLines = new List <string>(); libraryLines.Add("import math"); libraryLines.Add("import os"); libraryLines.Add("import random"); libraryLines.Add("import sys"); libraryLines.Add("import time"); libraryLines.Add("import inspect"); libraryLines.Add("from code.vm import *"); libraryLines.Add(""); foreach (string genCodePath in libTemplates.GetPaths("gen/")) { libraryLines.Add(libTemplates.GetText(genCodePath)); libraryLines.Add(""); } libraryLines.Add(""); libraryLines.Add("_moduleInfo = ('" + libraryName + "', dict(inspect.getmembers(sys.modules[__name__])))"); libraryLines.Add(""); foreach (string genCodePath in libTemplates.GetPaths("source/")) { libraryLines.Add(libTemplates.GetText(genCodePath)); libraryLines.Add(""); } output["code/lib_" + libraryName.ToLowerInvariant() + ".py"] = new FileOutput() { Type = FileOutputType.Text, TextContent = string.Join(this.NL, libraryLines), }; } } output["main.py"] = new FileOutput() { Type = FileOutputType.Text, TextContent = this.LoadTextResource("Resources/main.txt", replacements), }; output["code/__init__.py"] = new FileOutput() { Type = FileOutputType.Text, TextContent = "", }; output["res/bytecode.txt"] = resourceDatabase.ByteCodeFile; output["res/resource_manifest.txt"] = resourceDatabase.ResourceManifestFile; if (resourceDatabase.ImageSheetManifestFile != null) { output["res/image_sheet_manifest.txt"] = resourceDatabase.ImageSheetManifestFile; } foreach (FileOutput image in resourceDatabase.ImageResources) { output["res/images/" + image.CanonicalFileName] = image; } foreach (string imageSheetFile in resourceDatabase.ImageSheetFiles.Keys) { output["res/images/" + imageSheetFile] = resourceDatabase.ImageSheetFiles[imageSheetFile]; } foreach (FileOutput sound in resourceDatabase.AudioResources) { output["res/audio/" + sound.CanonicalFileName] = sound; } foreach (FileOutput textResource in resourceDatabase.TextResources) { output["res/text/" + textResource.CanonicalFileName] = textResource; } foreach (FileOutput fontResource in resourceDatabase.FontResources) { output["res/ttf/" + fontResource.CanonicalFileName] = fontResource; } }
public override void ExportProject( Dictionary <string, FileOutput> output, BuildData buildData, ExportProperties exportProperties) { List <string> jsExtraHead = new List <string>() { exportProperties.JsHeadExtras ?? "" }; if (exportProperties.JsFullPage) { jsExtraHead.Add( "<script type=\"text/javascript\">" + "C$common$globalOptions['fullscreen'] = true;" + "</script>"); } if (buildData.UsesU3) { Dictionary <string, FileOutput> u3Files = new Dictionary <string, FileOutput>(); foreach (string file in noriFiles) { this.CopyResourceAsText(u3Files, file, "ResourcesU3/" + file, new Dictionary <string, string>()); } List <string> newFile = new List <string>(); foreach (string file in noriFiles) { newFile.Add("// From " + file); newFile.Add(u3Files[file].TextContent.Trim()); } output["u3.js"] = new FileOutput() { Type = FileOutputType.Text, TextContent = string.Join("\n", newFile), }; jsExtraHead.Add("<script src=\"u3.js\"></script>"); } exportProperties.JsHeadExtras = string.Join("\n", jsExtraHead); ResourceDatabase resDb = buildData.CbxBundle.ResourceDB; Dictionary <string, string> replacements = this.GenerateReplacementDictionary(exportProperties, buildData); TemplateReader templateReader = new TemplateReader(new PkgAwareFileUtil(), this, null); TemplateSet vmTemplates = templateReader.GetVmTemplates(); output["vm.js"] = new FileOutput() { Type = FileOutputType.Text, TextContent = vmTemplates.GetText("vm.js"), }; Dictionary <string, string> htmlReplacements = new Dictionary <string, string>(replacements); this.CopyResourceAsText(output, "index.html", "Resources/HostHtml.txt", replacements); this.CopyResourceAsText(output, "test_server.py", "Resources/TestServerPy.txt", replacements); this.CopyResourceAsText(output, "common.js", "Resources/Common.txt", replacements); System.Text.StringBuilder resourcesJs = new System.Text.StringBuilder(); string[] fileNames = resDb.FlatFileNames; FileOutput[] files = resDb.FlatFiles; for (int i = 0; i < files.Length; i++) { FileOutput file = files[i]; string name = fileNames[i]; if (file.Type == FileOutputType.Text) { resourcesJs.Append("C$common$addTextRes("); resourcesJs.Append(ConvertStringValueToCode(name)); resourcesJs.Append(", "); resourcesJs.Append(ConvertStringValueToCode(file.TextContent)); resourcesJs.Append(");\n"); } else if (name.EndsWith(".png") || name.EndsWith(".jpg")) { output["resources/" + name] = file; } else if (name.EndsWith(".wav") || name.EndsWith(".ogg") || name.EndsWith(".mp3")) { output["resources/" + name] = file; } else { resourcesJs.Append("C$common$addBinaryRes("); resourcesJs.Append(ConvertStringValueToCode(name)); resourcesJs.Append(", '"); resourcesJs.Append(System.Convert.ToBase64String(file.GetFinalBinaryContent())); resourcesJs.Append("');\n"); } } resourcesJs.Append("C$common$resourceManifest = "); resourcesJs.Append(ConvertStringValueToCode(resDb.ResourceManifestFile.TextContent)); resourcesJs.Append(";\n"); string filePrefix = exportProperties.JsFilePrefix; if (filePrefix != null) { resourcesJs.Append("C$common$jsFilePrefix = "); resourcesJs.Append(ConvertStringValueToCode(filePrefix)); resourcesJs.Append(";\n"); } string imageManifest = resDb.ImageResourceManifestFile.TextContent; imageManifest = ConvertStringValueToCode(imageManifest); resourcesJs.Append("C$common$imageManifest = " + imageManifest + ";\n"); output["resources.js"] = new FileOutput() { Type = FileOutputType.Text, TextContent = resourcesJs.ToString(), }; output["bytecode.js"] = new FileOutput() { Type = FileOutputType.Text, TextContent = "C$bytecode = " + ConvertStringValueToCode(buildData.CbxBundle.ByteCode) + ";", }; if (exportProperties.HasIcon) { this.GenerateIconFile(output, "favicon.ico", exportProperties); } // TODO: minify JavaScript across all of output dictionary }