Skip to content

Commit

Permalink
Add support for command-line arguments to StructuredLogViewer.
Browse files Browse the repository at this point in the history
Extract base class ObservableObject that implements INotifyPropertyChanged.
Add an ability to display a custom message on the welcome screen.
  • Loading branch information
KirillOsenkov committed Aug 27, 2016
1 parent 3897e32 commit d8c970b
Show file tree
Hide file tree
Showing 10 changed files with 118 additions and 48 deletions.
12 changes: 2 additions & 10 deletions src/StructuredLogViewer/BuildParametersScreen.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,13 @@
using System;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Windows.Input;

namespace Microsoft.Build.Logging.StructuredLogger
{
public class BuildParametersScreen : INotifyPropertyChanged
public class BuildParametersScreen : ObservableObject
{
public event PropertyChangedEventHandler PropertyChanged;

public event Action BuildRequested;
public event Action CancelRequested;

protected void RaisePropertyChanged([CallerMemberName] string propertyName = null)
=> PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));

public string PrefixArguments { get; set; }
public string MSBuildArguments { get; set; }
public string PostfixArguments { get; set; }
Expand All @@ -27,5 +20,4 @@ protected void RaisePropertyChanged([CallerMemberName] string propertyName = nul
public ICommand CancelCommand => cancelCommand ?? (cancelCommand = new Command(Cancel));
private void Cancel() => CancelRequested?.Invoke();
}
}

}
12 changes: 2 additions & 10 deletions src/StructuredLogViewer/BuildProgress.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
using System.ComponentModel;
using System.Runtime.CompilerServices;

namespace Microsoft.Build.Logging.StructuredLogger
namespace Microsoft.Build.Logging.StructuredLogger
{
public class BuildProgress : INotifyPropertyChanged
public class BuildProgress : ObservableObject
{
private string progressText;
public string ProgressText
Expand Down Expand Up @@ -37,10 +34,5 @@ public string MSBuildCommandLine
}

public bool ShowCommandLine => !string.IsNullOrEmpty(MSBuildCommandLine);

public event PropertyChangedEventHandler PropertyChanged;

protected void RaisePropertyChanged([CallerMemberName] string propertyName = null)
=> PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System;
using System.Globalization;
using System.Windows;
using System.Windows.Data;

namespace StructuredLogViewer
{
public class StringEmptinessToVisibilityConverter : IValueConverter
{
public static readonly StringEmptinessToVisibilityConverter Instance = new StringEmptinessToVisibilityConverter();

public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var text = value as string;
return string.IsNullOrEmpty(text) ? Visibility.Collapsed : Visibility.Visible;
}

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
}
45 changes: 41 additions & 4 deletions src/StructuredLogViewer/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,18 @@ public MainWindow()
var uri = new Uri("StructuredLogViewer;component/themes/Generic.xaml", UriKind.Relative);
var generic = (ResourceDictionary)Application.LoadComponent(uri);
Application.Current.Resources.MergedDictionaries.Add(generic);
Loaded += MainWindow_Loaded;

DisplayWelcomeScreen();
Loaded += MainWindow_Loaded;
}

private void DisplayWelcomeScreen()
private void DisplayWelcomeScreen(string message = "")
{
this.projectFilePath = null;
this.logFilePath = null;
this.currentBuild = null;
Title = DefaultTitle;
var welcomeScreen = new WelcomeScreen();
welcomeScreen.Message = message;
SetContent(welcomeScreen);
welcomeScreen.RecentLogSelected += log => OpenLogFile(log);
welcomeScreen.RecentProjectSelected += project => BuildProject(project);
Expand All @@ -49,6 +49,43 @@ private async void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
try
{
var args = Environment.GetCommandLineArgs();
if (args.Length > 1)
{
if (args.Length > 2)
{
DisplayWelcomeScreen("Structured Log Viewer can only accept a single command-line argument: a full path to an existing log file or MSBuild project/solution.");
return;
}

var filePath = args[1];
if (!File.Exists(filePath))
{
DisplayWelcomeScreen($"File {filePath} not found.");
return;
}

if (filePath.EndsWith(".xml", StringComparison.OrdinalIgnoreCase) ||
filePath.EndsWith(".buildlog", StringComparison.OrdinalIgnoreCase))
{
OpenLogFile(filePath);
return;
}

if (filePath.EndsWith(".sln", StringComparison.OrdinalIgnoreCase) ||
filePath.EndsWith("proj", StringComparison.OrdinalIgnoreCase))
{
BuildProject(filePath);
return;
}

DisplayWelcomeScreen($"File extension not supported: {filePath}");
return;
}

DisplayWelcomeScreen();

// only check for updates if there were no command-line arguments and debugger not attached
if (Debugger.IsAttached || SettingsService.DisableUpdates)
{
return;
Expand Down Expand Up @@ -84,7 +121,7 @@ private async void MainWindow_Loaded(object sender, RoutedEventArgs e)
var welcomeScreen = mainContent.Content as WelcomeScreen;
if (welcomeScreen != null)
{
welcomeScreen.Version = ex.ToString();
welcomeScreen.Message = ex.ToString();
}
}
}
Expand Down
1 change: 1 addition & 0 deletions src/StructuredLogViewer/StructuredLogViewer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
</Compile>
<Compile Include="Controls\Command.cs" />
<Compile Include="Controls\SplitterPanel.cs" />
<Compile Include="Controls\StringEmptinessToVisibilityConverter.cs" />
<Compile Include="Controls\TreeViewExtensions.cs" />
<Compile Include="Entrypoint.cs" />
<Compile Include="ExceptionHandler.cs" />
Expand Down
24 changes: 16 additions & 8 deletions src/StructuredLogViewer/WelcomeScreen.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Windows;
using System.Windows.Input;
using StructuredLogViewer;

namespace Microsoft.Build.Logging.StructuredLogger
{
public class WelcomeScreen : INotifyPropertyChanged
public class WelcomeScreen : ObservableObject
{
private IEnumerable<string> recentLogs;
public IEnumerable<string> RecentLogs => recentLogs ?? (recentLogs = SettingsService.GetRecentLogFiles());
Expand Down Expand Up @@ -42,6 +40,21 @@ public string Version
}
}

private string message;
public string Message
{
get
{
return message;
}

set
{
message = value;
RaisePropertyChanged();
}
}

private static string GetVersion()
{
var version = Assembly.GetEntryAssembly().GetName().Version;
Expand Down Expand Up @@ -93,10 +106,5 @@ public string SelectedProject
private ICommand openLogFileCommand;
public ICommand OpenLogFileCommand => openLogFileCommand ?? (openLogFileCommand = new Command(OpenLogFile));
private void OpenLogFile() => OpenLogFileRequested?.Invoke();

public event PropertyChangedEventHandler PropertyChanged;

protected void RaisePropertyChanged([CallerMemberName] string propertyName = null)
=> PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
23 changes: 17 additions & 6 deletions src/StructuredLogViewer/themes/Generic.xaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:l="clr-namespace:Microsoft.Build.Logging.StructuredLogger;assembly=StructuredLogger"
xmlns:local="clr-namespace:Microsoft.Build.Logging.StructuredLogger">
xmlns:local="clr-namespace:Microsoft.Build.Logging.StructuredLogger"
xmlns:s="clr-namespace:StructuredLogViewer">

<Geometry x:Key="SearchGeometry">F1 M 8.5,7.0 C 7.12,7.00 6.0,5.88 6.0,4.5 C 6.0,3.12 7.12,2.0 8.5,2.0 C 9.88,2.0 11.0,3.12 11.0,4.5 C 11.0,5.88 9.88,7.0 8.5,7.0 Z M 8.5,0.0 C 6.02,0.0 4.0,2.02 4.0,4.500 C 4.0,5.23 4.19,5.9 4.49,6.5 L 0.0,11.0 L 2.0,13.0 L 6.49,8.51 C 7.1,8.81 7.77,9.0 8.5,9.0 C 11.0,9.0 13.0,7.0 13.0,4.5 C 13.0,2.02 11.0,0.0 8.5,0.0 Z</Geometry>
<BooleanToVisibilityConverter x:Key="booleanToVisibilityConverter" />
Expand Down Expand Up @@ -404,8 +405,18 @@
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid x:Name="openLogOrProjectCompartment">
<TextBlock x:Name="customMessage"
Grid.Row="0"
FontSize="16"
Foreground="Red"
Margin="10"
TextWrapping="Wrap"
Visibility="{Binding Message, Converter={x:Static s:StringEmptinessToVisibilityConverter.Instance}}"
Text="{Binding Message}"/>
<Grid x:Name="openLogOrProjectCompartment"
Grid.Row="1">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
Expand Down Expand Up @@ -446,7 +457,7 @@
</Button>
</Grid>
<Grid x:Name="recentProjects"
Grid.Row="1"
Grid.Row="2"
Visibility="{Binding ShowRecentProjects, Converter={StaticResource booleanToVisibilityConverter}}">
<GroupBox Header="Recent projects and solutions"
Margin="10">
Expand All @@ -459,7 +470,7 @@
</GroupBox>
</Grid>
<Grid x:Name="recentLogs"
Grid.Row="2"
Grid.Row="3"
Visibility="{Binding ShowRecentLogs, Converter={StaticResource booleanToVisibilityConverter}}">
<GroupBox Header="Recent logs"
Margin="10">
Expand Down Expand Up @@ -499,8 +510,8 @@
<KeyBinding Key="Enter"
Command="{Binding BuildCommand}" />
</TextBox.InputBindings>
</TextBox>
</TextBox>

<TextBlock Text="{Binding PostfixArguments}" />
</WrapPanel>
</StackPanel>
Expand Down
12 changes: 2 additions & 10 deletions src/StructuredLogger/ObjectModel/BaseNode.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
using System.ComponentModel;
using System.Runtime.CompilerServices;

namespace Microsoft.Build.Logging.StructuredLogger
namespace Microsoft.Build.Logging.StructuredLogger
{
public abstract class BaseNode : INotifyPropertyChanged
public abstract class BaseNode : ObservableObject
{
public event PropertyChangedEventHandler PropertyChanged;

/// <summary>
/// Since there can only be 1 selected node at a time, don't waste an instance field
/// just to store a bit. Store the currently selected node here and this way we save
Expand Down Expand Up @@ -36,8 +31,5 @@ public bool IsSelected
RaisePropertyChanged("IsLowRelevance");
}
}

protected void RaisePropertyChanged([CallerMemberName] string propertyName = null)
=> PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
13 changes: 13 additions & 0 deletions src/StructuredLogger/ObjectModel/ObservableObject.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System.ComponentModel;
using System.Runtime.CompilerServices;

namespace Microsoft.Build.Logging.StructuredLogger
{
public class ObservableObject : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;

protected void RaisePropertyChanged([CallerMemberName] string propertyName = null)
=> PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
1 change: 1 addition & 0 deletions src/StructuredLogger/StructuredLogger.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
<Compile Include="ObjectModel\Metadata.cs" />
<Compile Include="ObjectModel\NamedNode.cs" />
<Compile Include="ObjectModel\NameValueNode.cs" />
<Compile Include="ObjectModel\ObservableObject.cs" />
<Compile Include="ObjectModel\Parameter.cs" />
<Compile Include="ObjectModel\ParentedNode.cs" />
<Compile Include="ObjectModel\Project.cs" />
Expand Down

0 comments on commit d8c970b

Please sign in to comment.