private async void ProjectBuildStartCommand_Executed(object sender, ExecutedRoutedEventArgs e) { // Force update of script interface ProjectBuildStartButton.Focus(); if (!Engine.TryEnterLock()) { return; } try { // Get current project Project p = Model.CurMainTree.Script.Project; Model.BuildTreeItems.Clear(); ProjectTreeItemModel treeRoot = MainViewModel.PopulateOneTreeItem(p.MainScript, null, null); MainViewModel.ScriptListToTreeViewModel(p, p.ActiveScripts, false, treeRoot); Model.BuildTreeItems.Add(treeRoot); Model.CurBuildTree = null; EngineState s = new EngineState(p, Logger, Model); s.SetOptions(Global.Setting); s.SetCompat(p.Compat); Engine.WorkingEngine = new Engine(s); // Build Start, Switch to Build View Model.SwitchNormalBuildInterface = false; // Turn on progress ring Model.WorkInProgress = true; // Set StatusBar Text using (CancellationTokenSource ct = new CancellationTokenSource()) { Task printStatus = MainViewModel.PrintBuildElapsedStatus($"Building {p.ProjectName}...", s, ct.Token); // Run int buildId = await Engine.WorkingEngine.Run($"Project {p.ProjectName}"); #if DEBUG Logger.ExportBuildLog(LogExportType.Text, Path.Combine(s.BaseDir, "LogDebugDump.txt"), buildId, new BuildLogOptions { IncludeComments = true, IncludeMacros = true, ShowLogFlags = true, }); #endif // Cancel and wait until PrintBuildElapsedStatus stops ct.Cancel(); await printStatus; } // Turn off progress ring Model.WorkInProgress = false; // Build ended, Switch to Normal View Model.SwitchNormalBuildInterface = true; Model.BuildTreeItems.Clear(); Model.DisplayScript(Model.CurMainTree.Script); // Report elapsed time string reason = s.RunResultReport(); if (reason != null) { Model.StatusBarText = $"{p.ProjectName} build stopped by {reason}. ({s.Elapsed:h\\:mm\\:ss})"; } else { Model.StatusBarText = $"{p.ProjectName} build finished. ({s.Elapsed:h\\:mm\\:ss})"; } if (Global.Setting.General.ShowLogAfterBuild && LogWindow.Count == 0) { // Open BuildLogWindow LogDialog = new LogWindow(1); LogDialog.Show(); } } finally { Engine.WorkingEngine = null; Engine.ExitLock(); CommandManager.InvalidateRequerySuggested(); } }
private async void CodeBoxRunCommand_Executed(object sender, ExecutedRoutedEventArgs e) { CodeBoxRunButton.Focus(); _m.CanExecuteCommand = false; try { // Save CodeBox first _m.SaveCodeBox(); // Run Engine if (Engine.TryEnterLock()) { try { Project project = _m.CurrentProject; Script sc = project.LoadScriptRuntime(_m.CodeFile, new LoadScriptRuntimeOptions { IgnoreMain = true }); MainViewModel mainModel = Global.MainViewModel; mainModel.BuildTreeItems.Clear(); mainModel.SwitchNormalBuildInterface = false; mainModel.WorkInProgress = true; EngineState s = new EngineState(sc.Project, Global.Logger, mainModel, EngineMode.RunMainAndOne, sc); s.SetOptions(Global.Setting); s.SetCompat(sc.Project.Compat); Engine.WorkingEngine = new Engine(s); // Set StatusBar Text using (CancellationTokenSource ct = new CancellationTokenSource()) { Task printStatus = MainViewModel.PrintBuildElapsedStatus("Running CodeBox...", s, ct.Token); await Engine.WorkingEngine.Run($"CodeBox - {project.ProjectName}"); // Cancel and Wait until PrintBuildElapsedStatus stops ct.Cancel(); await printStatus; } // Turn off progress ring mainModel.WorkInProgress = false; // Build ended, Switch to Normal View mainModel.SwitchNormalBuildInterface = true; mainModel.BuildTreeItems.Clear(); // Report elapsed build time string haltReason = s.RunResultReport(); if (haltReason != null) { mainModel.StatusBarText = $"CodeBox took {s.Elapsed:h\\:mm\\:ss}, stopped by {haltReason}"; } else { mainModel.StatusBarText = $"CodeBox took {s.Elapsed:h\\:mm\\:ss}"; } s.MainViewModel.DisplayScript(mainModel.CurMainTree.Script); if (Global.Setting.General.ShowLogAfterBuild && LogWindow.Count == 0) { // Open BuildLogWindow Application.Current?.Dispatcher?.Invoke(() => { if (!(Application.Current.MainWindow is MainWindow w)) { return; } w.LogDialog = new LogWindow(1); w.LogDialog.Show(); }); } } finally { Engine.WorkingEngine = null; Engine.ExitLock(); } } } finally { _m.CanExecuteCommand = true; CommandManager.InvalidateRequerySuggested(); CodeBoxInputTextBox.Focus(); } }