Builder improvements.

This commit is contained in:
José Pedro 2018-03-27 00:41:51 +01:00
parent 1480bfbca2
commit 985385527a
No known key found for this signature in database
GPG key ID: B8247B9301707B83
4 changed files with 248 additions and 175 deletions

View file

@ -0,0 +1,40 @@
using System.ComponentModel;
using System.Text;
namespace Cosmos.Build.Builder.Models
{
internal class Section : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public string Name { get; }
public string Log => _logBuilder.ToString();
public bool HasLoggedErrors { get; private set; }
private StringBuilder _logBuilder;
public Section(string name)
{
Name = name;
_logBuilder = new StringBuilder();
}
public void LogMessage(string message)
{
_logBuilder.AppendLine(message);
OnPropertyChanged(nameof(Log));
}
public void SetError()
{
if (!HasLoggedErrors)
{
HasLoggedErrors = true;
OnPropertyChanged(nameof(HasLoggedErrors));
}
}
private void OnPropertyChanged(string propertyName) =>
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}

View file

@ -1,9 +1,12 @@
using System; using System;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text; using System.Text;
using System.Windows; using System.Windows;
using System.Windows.Input; using System.Windows.Input;
using Cosmos.Build.Builder.Collections; using Cosmos.Build.Builder.Collections;
using Cosmos.Build.Builder.Models;
namespace Cosmos.Build.Builder.ViewModels namespace Cosmos.Build.Builder.ViewModels
{ {
@ -14,6 +17,9 @@ namespace Cosmos.Build.Builder.ViewModels
public ICommand CopyCommand { get; } public ICommand CopyCommand { get; }
public ObservableFixedSizeStack<string> TailItems { get; } public ObservableFixedSizeStack<string> TailItems { get; }
public ObservableCollection<Section> Sections { get; }
public Section CurrentSection => Sections.LastOrDefault();
public StringBuilder LogBuilder { get; set; } public StringBuilder LogBuilder { get; set; }
@ -22,6 +28,7 @@ namespace Cosmos.Build.Builder.ViewModels
CopyCommand = new Command(this); CopyCommand = new Command(this);
TailItems = new ObservableFixedSizeStack<string>(TailItemCount); TailItems = new ObservableFixedSizeStack<string>(TailItemCount);
Sections = new ObservableCollection<Section>();
} }
private class Command : ICommand private class Command : ICommand

View file

@ -12,6 +12,9 @@
Title="Cosmos Kit Builder" Title="Cosmos Kit Builder"
WindowStartupLocation="CenterScreen" WindowStartupLocation="CenterScreen"
DataContext="{StaticResource MainWindowViewModel}"> DataContext="{StaticResource MainWindowViewModel}">
<Window.Resources>
<BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />
</Window.Resources>
<DockPanel> <DockPanel>
@ -28,13 +31,13 @@
FontSize="18" FontSize="18"
Foreground="Blue" Foreground="Blue"
FontWeight="ExtraBold" FontWeight="ExtraBold"
Text="Current Task Log Tail" /> Text="{Binding CurrentSection.Name, StringFormat='Current Task Log Tail - {0}', FallbackValue='Current Task Log Tail'}" />
<ItemsControl DockPanel.Dock="Top" <ItemsControl DockPanel.Dock="Top"
Background="Black" Background="Black"
BorderBrush="DarkGray" BorderBrush="DarkGray"
BorderThickness="0.5" BorderThickness="0.5"
FontSize="16" FontSize="16"
Foreground="DarkGreen" Foreground="Green"
Margin="0,4" Margin="0,4"
ItemsSource="{Binding TailItems}"> ItemsSource="{Binding TailItems}">
<ItemsControl.ItemTemplate> <ItemsControl.ItemTemplate>
@ -51,8 +54,37 @@
<Rectangle DockPanel.Dock="Top" <Rectangle DockPanel.Dock="Top"
Height="5" Height="5"
Fill="LightGray"/> Fill="LightGray"/>
<ScrollViewer HorizontalScrollBarVisibility="Visible"> <ScrollViewer HorizontalScrollBarVisibility="Auto"
<StackPanel Name="spnlLog" /> VerticalScrollBarVisibility="Auto">
<ItemsControl ItemsSource="{Binding Sections}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<HeaderedContentControl>
<HeaderedContentControl.Header>
<ToggleButton x:Name="sectionToggleButton"
Background="LightGray"
FontSize="18"
FontWeight="Bold"
HorizontalContentAlignment="Left"
Content="{Binding Name}">
<ToggleButton.Style>
<Style TargetType="ToggleButton">
<Setter Property="Foreground" Value="Green" />
<Style.Triggers>
<DataTrigger Binding="{Binding HasLoggedErrors}" Value="True">
<Setter Property="Foreground" Value="Red" />
</DataTrigger>
</Style.Triggers>
</Style>
</ToggleButton.Style>
</ToggleButton>
</HeaderedContentControl.Header>
<TextBlock Visibility="{Binding IsChecked, ElementName=sectionToggleButton, Converter={StaticResource BooleanToVisibilityConverter}}"
Text="{Binding Log}" />
</HeaderedContentControl>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</ScrollViewer> </ScrollViewer>
</DockPanel> </DockPanel>

View file

@ -3,51 +3,50 @@ using System.IO;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Windows; using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Threading; using System.Windows.Threading;
using Cosmos.Build.Builder.Models;
using Cosmos.Build.Installer; using Cosmos.Build.Installer;
namespace Cosmos.Build.Builder.Views { namespace Cosmos.Build.Builder.Views
public partial class MainWindow : Window { {
public partial class MainWindow : Window
{
private static readonly string[] NewLineStringArray = new string[] { Environment.NewLine }; private static readonly string[] NewLineStringArray = new string[] { Environment.NewLine };
string mCosmosDir; string mCosmosDir;
string mSetupPath; string mSetupPath;
// Needs updating with each new release. // Needs updating with each new release.
int mReleaseNo = 106027; int mReleaseNo = 106027;
string mTailCaption;
public MainWindow()
public MainWindow() { {
InitializeComponent(); InitializeComponent();
mTailCaption = tblkTail.Text + " - ";
} }
bool mPreventAutoClose = false; bool mPreventAutoClose = false;
TextBlock mSection;
TextBlock mContent;
StringBuilder mClipboard = new StringBuilder(); StringBuilder mClipboard = new StringBuilder();
DispatcherTimer mCloseTimer; DispatcherTimer mCloseTimer;
public bool Build()
public bool Build() { {
Log.LogLine += new Log.LogLineHandler(Log_LogLine); Log.LogLine += new Log.LogLineHandler(Log_LogLine);
Log.LogSection += new Log.LogSectionHandler(Log_LogSection); Log.LogSection += new Log.LogSectionHandler(Log_LogSection);
Log.LogError += new Log.LogErrorHandler(Log_LogError); Log.LogError += new Log.LogErrorHandler(Log_LogError);
if (App.IsUserKit) { if (App.IsUserKit)
{
mReleaseNo = Int32.Parse(DateTime.Now.ToString("yyyyMMdd")); mReleaseNo = Int32.Parse(DateTime.Now.ToString("yyyyMMdd"));
} }
if (mPreventAutoClose) { if (mPreventAutoClose)
{
return true; return true;
} }
var xTask = new CosmosTask(mCosmosDir, mReleaseNo); var xTask = new CosmosTask(mCosmosDir, mReleaseNo);
var xThread = new System.Threading.Thread(delegate () { var xThread = new System.Threading.Thread(delegate ()
{
xTask.Run(); xTask.Run();
ThreadDone(); ThreadDone();
}); });
@ -56,18 +55,26 @@ namespace Cosmos.Build.Builder.Views {
return true; return true;
} }
void ThreadDone() { void ThreadDone()
Dispatcher.Invoke(DispatcherPriority.Normal, (Action)delegate () { {
if (App.StayOpen == false) { Dispatcher.Invoke(DispatcherPriority.Normal, (Action)delegate ()
{
if (App.StayOpen == false)
{
mCloseTimer = new DispatcherTimer(); mCloseTimer = new DispatcherTimer();
mCloseTimer.Interval = TimeSpan.FromSeconds(5); mCloseTimer.Interval = TimeSpan.FromSeconds(5);
mCloseTimer.Tick += delegate { mCloseTimer.Tick += delegate
{
mCloseTimer.Stop(); mCloseTimer.Stop();
if (mPreventAutoClose) { if (mPreventAutoClose)
if (WindowState == WindowState.Minimized) { {
if (WindowState == WindowState.Minimized)
{
WindowState = WindowState.Normal; WindowState = WindowState.Normal;
} }
} else { }
else
{
Close(); Close();
} }
}; };
@ -76,83 +83,62 @@ namespace Cosmos.Build.Builder.Views {
}); });
} }
void ClearTail() { void ClearTail() => DoOnViewModel(vm => vm.TailItems.Clear());
if (DataContext is ViewModels.MainWindowViewModel viewModel)
{
viewModel.TailItems.Clear();
}
}
void Log_LogError() { void Log_LogError()
Dispatcher.Invoke(DispatcherPriority.Normal, (Action)delegate () { {
Dispatcher.Invoke(DispatcherPriority.Normal, (Action)delegate ()
{
ClearTail(); ClearTail();
mSection.Foreground = Brushes.Red; DoOnViewModel(vm => vm.CurrentSection?.SetError());
mContent.Visibility = Visibility.Visible;
mPreventAutoClose = true; mPreventAutoClose = true;
}); });
} }
void Log_LogLine(string aLine) { void Log_LogLine(string aLine)
Dispatcher.Invoke(DispatcherPriority.Normal, (Action)delegate () { {
Dispatcher.Invoke(DispatcherPriority.Normal, (Action)delegate ()
{
WriteTail(aLine); WriteTail(aLine);
mClipboard.AppendLine(aLine); DoOnViewModel(vm => vm.CurrentSection?.LogMessage(aLine));
mContent.Inlines.Add(aLine); mClipboard.AppendLine(aLine);
mContent.Inlines.Add(new LineBreak());
}); });
} }
void Log_LogSection(string aLine) { void Log_LogSection(string name)
Dispatcher.Invoke(DispatcherPriority.Normal, (Action)delegate () { {
Title = aLine; Dispatcher.Invoke(DispatcherPriority.Normal, (Action)delegate ()
{
DoOnViewModel(vm => vm.Sections.Add(new Section(name)));
ClearTail(); ClearTail();
tblkTail.Text = mTailCaption + aLine;
mClipboard.AppendLine(); mClipboard.AppendLine();
mClipboard.AppendLine(new string('=', aLine.Length)); mClipboard.AppendLine(new string('=', name.Length));
mClipboard.AppendLine(aLine); mClipboard.AppendLine(name);
mClipboard.AppendLine(new string('=', aLine.Length)); mClipboard.AppendLine(new string('=', name.Length));
mClipboard.AppendLine(); mClipboard.AppendLine();
mSection = new TextBlock();
mSection.Text = aLine;
mSection.Background = Brushes.LightGray;
mSection.Foreground = Brushes.Green;
mSection.FontSize = 18;
mSection.FontWeight = FontWeights.Bold;
mSection.MouseUp += new MouseButtonEventHandler(mSection_MouseUp);
spnlLog.Children.Add(mSection);
mContent = new TextBlock();
mContent.Visibility = Visibility.Collapsed;
spnlLog.Children.Add(mContent);
mSection.Tag = mContent;
}); });
} }
void mSection_MouseUp(object sender, MouseButtonEventArgs e) { void WriteTail(string aText) => DoOnViewModel(
var xSection = (TextBlock)sender; vm =>
var xContent = (TextBlock)xSection.Tag;
xContent.Visibility = xContent.Visibility == Visibility.Visible ? Visibility.Collapsed : Visibility.Visible;
mPreventAutoClose = true;
}
void WriteTail(string aText) {
if (DataContext is ViewModels.MainWindowViewModel viewModel)
{ {
foreach (var line in aText.Split(NewLineStringArray, StringSplitOptions.None)) foreach (var line in aText.Split(NewLineStringArray, StringSplitOptions.None))
{ {
viewModel.TailItems.Push(line); vm.TailItems.Push(line);
}
}
} }
});
protected bool mLoaded = false; protected bool mLoaded = false;
void Window_Loaded(object sender, RoutedEventArgs e) { void Window_Loaded(object sender, RoutedEventArgs e)
if (!App.mArgs.Any()) { {
if (!App.mArgs.Any())
{
MessageBox.Show("Builder not meant to be called directly. Use install.bat instead.", "Error", MessageBoxButton.OK, MessageBoxImage.Error); MessageBox.Show("Builder not meant to be called directly. Use install.bat instead.", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
Close(); Close();
return; return;
@ -160,26 +146,34 @@ namespace Cosmos.Build.Builder.Views {
mLoaded = true; mLoaded = true;
if (DataContext is ViewModels.MainWindowViewModel viewModel) DoOnViewModel(vm => vm.LogBuilder = mClipboard);
{
viewModel.LogBuilder = mClipboard;
}
string xAppPath = AppContext.BaseDirectory; string xAppPath = AppContext.BaseDirectory;
mCosmosDir = Path.GetFullPath(xAppPath + @"..\..\..\..\..\"); mCosmosDir = Path.GetFullPath(xAppPath + @"..\..\..\..\..\");
mSetupPath = Path.Combine(mCosmosDir, @"Setup\Output\" + CosmosTask.GetSetupName(mReleaseNo) + ".exe"); mSetupPath = Path.Combine(mCosmosDir, @"Setup\Output\" + CosmosTask.GetSetupName(mReleaseNo) + ".exe");
if (!Build()) { if (!Build())
{
Close(); Close();
} }
} }
private void Window_SizeChanged(object sender, SizeChangedEventArgs e) { private void Window_SizeChanged(object sender, SizeChangedEventArgs e)
{
// User had non minimized window, or maximized it, or otherwise manually intervened. // User had non minimized window, or maximized it, or otherwise manually intervened.
// Even if starting minimized, this event gets called with Normal before load. // Even if starting minimized, this event gets called with Normal before load.
// This is why we have mLoaded. // This is why we have mLoaded.
if (mLoaded && WindowState != WindowState.Minimized) { if (mLoaded && WindowState != WindowState.Minimized)
{
mPreventAutoClose = true; mPreventAutoClose = true;
} }
} }
private void DoOnViewModel(Action<ViewModels.MainWindowViewModel> action)
{
if (DataContext is ViewModels.MainWindowViewModel viewModel)
{
action(viewModel);
}
}
} }
} }