public CompilerResults Compile(CompileOptions compileOptions) { CompilerResults result = new CompilerResults(); GameLoader loader = new GameLoader(); UpdateStatus(string.Format("Compiling {0} to {1}", compileOptions.Filename, compileOptions.OutputFolder)); if (!loader.Load(compileOptions.Filename)) { result.Errors = loader.Errors; } else { UpdateStatus("Loaded successfully"); result.Warnings = loader.Warnings; result.Success = true; var substitutionText = GetSubstitutionText(loader, compileOptions.Profile); UpdateStatus("Copying dependencies"); result.IndexHtml = CopyDependenciesToOutputFolder(compileOptions.OutputFolder, substitutionText, compileOptions.DebugMode, compileOptions.Profile, compileOptions.Minify, loader, compileOptions); string saveData = string.Empty; UpdateStatus("Saving"); GameSaver saver = new GameSaver(loader.Elements); saver.Progress += saver_Progress; saveData = saver.Save(); UpdateStatus("Copying resources"); CopyResourcesToOutputFolder(loader.ResourcesFolder, compileOptions.OutputFolder); saveData += GetEmbeddedHtmlFileData(loader.ResourcesFolder); string saveJs = System.IO.Path.Combine(compileOptions.OutputFolder, "game.js"); saveData = System.IO.File.ReadAllText(saveJs) + saveData; if (compileOptions.Minify) { var minifier = new Microsoft.Ajax.Utilities.Minifier(); saveData = minifier.MinifyJavaScript(saveData, new Microsoft.Ajax.Utilities.CodeSettings { MacSafariQuirks = true, RemoveUnneededCode = true, LocalRenaming = Microsoft.Ajax.Utilities.LocalRenaming.CrunchAll }); var encoding = (Encoding)Encoding.ASCII.Clone(); encoding.EncoderFallback = new Microsoft.Ajax.Utilities.JSEncoderFallback(); using (var writer = new System.IO.StreamWriter(saveJs, false, encoding)) { writer.Write(saveData); } } else { System.IO.File.WriteAllText(saveJs, saveData); } UpdateStatus("Finished"); } return(result); }
public void StartCompile(CompileOptions compileOptions) { try { CompilerResults results = Compile(compileOptions); if (CompileFinished != null) { CompileFinished(this, results); } } catch (Exception ex) { CompilerResults results = new CompilerResults { Success = false, Errors = new List <string> { ex.ToString() } }; if (CompileFinished != null) { CompileFinished(this, results); } } }
// TO DO: Different profiles have different dependencies, so want to only copy the required files private string CopyDependenciesToOutputFolder(string outputFolder, Dictionary<string, string> substitutionText, bool debugMode, string profile, bool minify, GameLoader loader, CompileOptions options) { string indexHtm = Copy("index.htm", _resourcesFolder, outputFolder, options, loader, substitutionText, debugMode: debugMode, outputFilename: "index.html"); Copy("style.css", _resourcesFolder, outputFolder, options, loader, substitutionText); Copy("jquery-ui-1.8.16.custom.css", _resourcesFolder, outputFolder, options, loader, substitutionText); Copy("game.js", _resourcesFolder, outputFolder, options, loader, substitutionText, debugMode); string jsFolder = System.IO.Path.Combine(_resourcesFolder, "js"); string outputJsFolder = System.IO.Path.Combine(outputFolder, "js"); System.IO.Directory.CreateDirectory(outputJsFolder); Copy("jquery.min.js", jsFolder, outputJsFolder, options, loader); Copy("jquery-ui*.js", jsFolder, outputJsFolder, options, loader); Copy("xregexp*.js", jsFolder, outputJsFolder, options, loader); Copy("jjmenu.js", jsFolder, outputJsFolder, options, loader); Copy("bootstrap*.js", jsFolder, outputJsFolder, options, loader); Copy("*.css", jsFolder, outputJsFolder, options, loader); Copy("bootstrap*.css", _resourcesFolder, outputFolder, options, loader, substitutionText); string imagesFolder = System.IO.Path.Combine(_resourcesFolder, "images"); string outputImagesFolder = System.IO.Path.Combine(outputFolder, "images"); System.IO.Directory.CreateDirectory(outputImagesFolder); Copy("*.png", imagesFolder, outputImagesFolder, options, loader, binary: true); return indexHtm; }
private string Copy(string filename, string sourceFolder, string outputFolder, CompileOptions options, GameLoader loader, Dictionary<string, string> substitutionText = null, bool debugMode = false, string outputFilename = null, bool binary = false) { if (filename.Contains("*")) { string[] files = System.IO.Directory.GetFiles(sourceFolder, filename); foreach (string file in files) { string resultPath = System.IO.Path.Combine(outputFolder, System.IO.Path.GetFileName(file)); CopyInternal(file, resultPath, substitutionText, debugMode, binary, options.Profile, loader, options.Gamebook); } return null; } else { if (outputFilename == null) outputFilename = filename; string sourcePath = System.IO.Path.Combine(sourceFolder, filename); string resultPath = System.IO.Path.Combine(outputFolder, outputFilename); return CopyInternal(sourcePath, resultPath, substitutionText, debugMode, binary, options.Profile, loader, options.Gamebook); } }
public void StartCompile(CompileOptions compileOptions) { try { CompilerResults results = Compile(compileOptions); if (CompileFinished != null) { CompileFinished(this, results); } } catch (Exception ex) { CompilerResults results = new CompilerResults { Success = false, Errors = new List<string> { ex.ToString() } }; if (CompileFinished != null) { CompileFinished(this, results); } } }
public CompilerResults Compile(CompileOptions compileOptions) { CompilerResults result = new CompilerResults(); GameLoader loader = new GameLoader(); UpdateStatus(string.Format("Compiling {0} to {1}", compileOptions.Filename, compileOptions.OutputFolder)); if (!loader.Load(compileOptions.Filename)) { result.Errors = loader.Errors; } else { UpdateStatus("Loaded successfully"); result.Warnings = loader.Warnings; result.Success = true; var substitutionText = GetSubstitutionText(loader, compileOptions.Profile); UpdateStatus("Copying dependencies"); result.IndexHtml = CopyDependenciesToOutputFolder(compileOptions.OutputFolder, substitutionText, compileOptions.DebugMode, compileOptions.Profile, compileOptions.Minify, loader, compileOptions); string saveData = string.Empty; UpdateStatus("Saving"); GameSaver saver = new GameSaver(loader.Elements); saver.Progress += saver_Progress; saveData = saver.Save(); UpdateStatus("Copying resources"); CopyResourcesToOutputFolder(loader.ResourcesFolder, compileOptions.OutputFolder); saveData += GetEmbeddedHtmlFileData(loader.ResourcesFolder); string saveJs = System.IO.Path.Combine(compileOptions.OutputFolder, "game.js"); saveData = System.IO.File.ReadAllText(saveJs) + saveData; if (compileOptions.Minify) { var minifier = new Microsoft.Ajax.Utilities.Minifier(); saveData = minifier.MinifyJavaScript(saveData, new Microsoft.Ajax.Utilities.CodeSettings { MacSafariQuirks = true, RemoveUnneededCode = true, LocalRenaming = Microsoft.Ajax.Utilities.LocalRenaming.CrunchAll }); var encoding = (Encoding)Encoding.ASCII.Clone(); encoding.EncoderFallback = new Microsoft.Ajax.Utilities.JSEncoderFallback(); using (var writer = new System.IO.StreamWriter(saveJs, false, encoding)) { writer.Write(saveData); } } else { System.IO.File.WriteAllText(saveJs, saveData); } UpdateStatus("Finished"); } return result; }
private string Copy(string filename, string sourceFolder, string outputFolder, CompileOptions options, GameLoader loader, Dictionary <string, string> substitutionText = null, bool debugMode = false, string outputFilename = null, bool binary = false) { if (filename.Contains("*")) { string[] files = System.IO.Directory.GetFiles(sourceFolder, filename); foreach (string file in files) { string resultPath = System.IO.Path.Combine(outputFolder, System.IO.Path.GetFileName(file)); CopyInternal(file, resultPath, substitutionText, debugMode, binary, options.Profile, loader, options.Gamebook); } return(null); } else { if (outputFilename == null) { outputFilename = filename; } string sourcePath = System.IO.Path.Combine(sourceFolder, filename); string resultPath = System.IO.Path.Combine(outputFolder, outputFilename); return(CopyInternal(sourcePath, resultPath, substitutionText, debugMode, binary, options.Profile, loader, options.Gamebook)); } }
// TO DO: Different profiles have different dependencies, so want to only copy the required files private string CopyDependenciesToOutputFolder(string outputFolder, Dictionary <string, string> substitutionText, bool debugMode, string profile, bool minify, GameLoader loader, CompileOptions options) { string indexHtm = Copy("index.htm", _resourcesFolder, outputFolder, options, loader, substitutionText, debugMode: debugMode, outputFilename: "index.html"); Copy("style.css", _resourcesFolder, outputFolder, options, loader, substitutionText); Copy("jquery-ui-1.8.16.custom.css", _resourcesFolder, outputFolder, options, loader, substitutionText); Copy("game.js", _resourcesFolder, outputFolder, options, loader, substitutionText, debugMode); string jsFolder = System.IO.Path.Combine(_resourcesFolder, "js"); string outputJsFolder = System.IO.Path.Combine(outputFolder, "js"); System.IO.Directory.CreateDirectory(outputJsFolder); Copy("jquery.min.js", jsFolder, outputJsFolder, options, loader); Copy("jquery-ui*.js", jsFolder, outputJsFolder, options, loader); Copy("xregexp*.js", jsFolder, outputJsFolder, options, loader); Copy("jjmenu.js", jsFolder, outputJsFolder, options, loader); Copy("bootstrap*.js", jsFolder, outputJsFolder, options, loader); Copy("*.css", jsFolder, outputJsFolder, options, loader); Copy("bootstrap*.css", _resourcesFolder, outputFolder, options, loader, substitutionText); string imagesFolder = System.IO.Path.Combine(_resourcesFolder, "images"); string outputImagesFolder = System.IO.Path.Combine(outputFolder, "images"); System.IO.Directory.CreateDirectory(outputImagesFolder); Copy("*.png", imagesFolder, outputImagesFolder, options, loader, binary: true); return(indexHtm); }
private void cmdCompile_Click(object sender, RoutedEventArgs e) { cmdCompile.IsEnabled = false; progress.Visibility = Visibility.Visible; SaveValues(); txtOutput.Text = ""; progress.Value = 0; TaskbarItemInfo.ProgressValue = 0; TaskbarItemInfo.ProgressState = System.Windows.Shell.TaskbarItemProgressState.Normal; var options = new CompileOptions { Filename = txtSource.Text, OutputFolder = txtDestination.Text, DebugMode = (bool)chkDebug.IsChecked, Profile = cmbProfile.Text, Minify = (bool)chkMinify.IsChecked, Gamebook = (bool)chkGamebook.IsChecked, }; Thread newThread = new Thread(() => compiler.StartCompile(options)); newThread.Start(); }