Skip to content

Commit

Permalink
WIP: Pickles GUI: Generate equivalent PowerShell or Windows Console c…
Browse files Browse the repository at this point in the history
…ommand line

Fixes #90
  • Loading branch information
Martin Gondermann committed Oct 4, 2018
1 parent 63e5953 commit 7de933d
Show file tree
Hide file tree
Showing 7 changed files with 316 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="CLICommandGenerator.cs" company="PicklesDoc">
// Copyright 2011 Jeffrey Cameron
// Copyright 2012-present PicklesDoc team and community contributors
//
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// </copyright>
// --------------------------------------------------------------------------------------------------------------------

using System.Text;

using PicklesDoc.Pickles.UserInterface.Settings;

namespace PicklesDoc.Pickles.UserInterface.CommandGeneration
{
public class CLICommandGenerator : CommandGeneratorBase
{
protected override string GenerateSingleCommandLine(
MainModel model,
string outputDirectory,
DocumentationFormat documentationFormat,
string selectedLanguage)
{
var result = new StringBuilder("pickles.exe");
result.AppendFormat(" --feature-directory=\"{0}\"", model.FeatureDirectory);
result.AppendFormatIfNotEmpty(" --output-directory=\"{0}\"", outputDirectory);
result.AppendFormatIfNotEmpty(" --system-under-test-name={0}", model.ProjectName);
result.AppendFormatIfNotEmpty(" --system-under-test-version={0}", model.ProjectVersion);

if (model.IncludeTestResults)
{
result.AppendFormatIfNotEmpty(" --link-results-file=\"{0}\"", model.TestResultsFile);

if (model.TestResultsFormat != TestResultsFormat.NUnit)
{
result.AppendFormat(" --test-results-format={0}", model.TestResultsFormat.ToString().ToLowerInvariant());
}
}

result.AppendFormatIfNotEmpty(" --language={0}", selectedLanguage);

if (documentationFormat != DocumentationFormat.Html)
{
result.AppendFormat(" --documentation-format={0}", documentationFormat.ToString().ToLowerInvariant());
}

if (model.IncludeExperimentalFeatures)
{
result.Append(" --include-experimental-features");
}

if (!model.EnableComments)
{
result.Append(" --enableComments=false");
}

result.AppendFormatIfNotEmpty(" --excludeTags=", model.ExcludeTags);
result.AppendFormatIfNotEmpty(" --hideTags=", model.HideTags);

return result.ToString();
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="CommandGeneratorBase.cs" company="PicklesDoc">
// Copyright 2011 Jeffrey Cameron
// Copyright 2012-present PicklesDoc team and community contributors
//
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// </copyright>
// --------------------------------------------------------------------------------------------------------------------

using System;
using System.Globalization;
using System.IO;
using System.Linq;

using PicklesDoc.Pickles.UserInterface.Settings;

namespace PicklesDoc.Pickles.UserInterface.CommandGeneration
{
public abstract class CommandGeneratorBase
{
public string Generate(MainModel model)
{
return (from documentationFormat in model.DocumentationFormats
let outputDirectory = model.CreateDirectoryForEachOutputFormat
? model.OutputDirectory + Path.DirectorySeparatorChar + documentationFormat
: model.OutputDirectory
let selectedLanguage = CultureInfo
.GetCultures(CultureTypes.NeutralCultures)
.Where(c => c.LCID == model.SelectedLanguageLcid)
.Select(l => l.TwoLetterISOLanguageName)
.FirstOrDefault()
?.ToLowerInvariant()
select this.GenerateSingleCommandLine(model, outputDirectory, documentationFormat, selectedLanguage))
.Aggregate(string.Empty, (c, n) => c + Environment.NewLine + n)
.Trim();
}

protected abstract string GenerateSingleCommandLine(
MainModel model,
string outputDirectory,
DocumentationFormat documentationFormat,
string selectedLanguage);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="PowerShellCommandGenerator.cs" company="PicklesDoc">
// Copyright 2011 Jeffrey Cameron
// Copyright 2012-present PicklesDoc team and community contributors
//
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// </copyright>
// --------------------------------------------------------------------------------------------------------------------

using System;
using System.Text;

using PicklesDoc.Pickles.UserInterface.Settings;

namespace PicklesDoc.Pickles.UserInterface.CommandGeneration
{
public class PowerShellCommandGenerator : CommandGeneratorBase
{
/// <inheritdoc />
protected override string GenerateSingleCommandLine(
MainModel model,
string outputDirectory,
DocumentationFormat documentationFormat,
string selectedLanguage)
{
var result = new StringBuilder("Pickle-Features")
.AppendFormat(" -FeatureDirectory \"{0}\"", model.FeatureDirectory)
.AppendFormatIfNotEmpty(" -OutputDirectory \"{0}\"", outputDirectory)
.AppendFormatIfNotEmpty(" -SystemUnderTestName {0}", model.ProjectName)
.AppendFormatIfNotEmpty(" -SystemUnderTestVersion {0}", model.ProjectVersion);

if (model.IncludeTestResults)
{
result.AppendFormatIfNotEmpty(" -TestResultsFile \"{0}\"", model.TestResultsFile);

if (model.TestResultsFormat != TestResultsFormat.NUnit)
{
result.AppendFormat(" -TestResultsFormat {0}", model.TestResultsFormat.ToString().ToLowerInvariant());
}
}

if (!string.Equals(selectedLanguage, "en", StringComparison.InvariantCultureIgnoreCase))
{
result.AppendFormatIfNotEmpty(" --language={0}", selectedLanguage);
}

if (documentationFormat != DocumentationFormat.Html)
{
result.AppendFormat(" --DocumentationFormat {0}", documentationFormat.ToString());
}

if (model.IncludeExperimentalFeatures)
{
result.Append(" -IncludeExperimentalFeatures");
}

if (!model.EnableComments)
{
result.Append(" -EnableComments false");
}

result.AppendFormatIfNotEmpty(" -ExcludeTags ", model.ExcludeTags);
result.AppendFormatIfNotEmpty(" -HideTags ", model.HideTags);

return result.ToString();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="StringBuilderExtensions.cs" company="PicklesDoc">
// Copyright 2011 Jeffrey Cameron
// Copyright 2012-present PicklesDoc team and community contributors
//
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// </copyright>
// --------------------------------------------------------------------------------------------------------------------

using System.Text;

namespace PicklesDoc.Pickles.UserInterface.CommandGeneration
{
public static class StringBuilderExtensions
{
public static StringBuilder AppendFormatIfNotEmpty(this StringBuilder stringBuilder, string format, string value)
{
return string.IsNullOrWhiteSpace(value)
? stringBuilder
: stringBuilder.AppendFormat(format, value);
}
}
}
12 changes: 12 additions & 0 deletions src/Pickles/Pickles.UserInterface/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
Expand Down Expand Up @@ -121,6 +122,17 @@
<mahapps:ToggleSwitch Header="Include Experimental Features" Grid.RowSpan="2" Grid.Row="3" Grid.Column="5" Grid.ColumnSpan="1" IsChecked="{Binding Path=IncludeExperimentalFeatures, Mode=TwoWay}" Margin="5,0,0,5" />

<Button Command="{Binding GeneratePickles}" Content="Generate" Grid.Row="11" Grid.Column="4" HorizontalAlignment="Right" VerticalAlignment="Center" FontSize="24" Padding="15" Grid.ColumnSpan="2" Margin="0,0,20,20" />

<Grid Grid.Row="12" Grid.Column="4" Grid.ColumnSpan="2">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>

<Button Command="{Binding GeneratePowerShellCommand}" Content="PowerShell Command" Grid.Column="1" HorizontalAlignment="Right" VerticalAlignment="Center" Margin="10 0 10 10"/>
<Button Command="{Binding GenerateCLICommand}" Content="CLI Command" Grid.Column="2" HorizontalAlignment="Right" VerticalAlignment="Center" Margin="10 0 20 10"/>
</Grid>
<nlogViewer:NlogViewer x:Name="logCtrl" Grid.Column="0" Grid.ColumnSpan="6" Grid.Row="13
" LevelWidth="auto" ExceptionWidth="auto" MessageWidth="auto" />

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@
<Compile Include="..\VersionInfo.cs">
<Link>VersionInfo.cs</Link>
</Compile>
<Compile Include="CommandGeneration\CLICommandGenerator.cs" />
<Compile Include="CommandGeneration\CommandGeneratorBase.cs" />
<Compile Include="CommandGeneration\PowerShellCommandGenerator.cs" />
<Compile Include="CommandGeneration\StringBuilderExtensions.cs" />
<Compile Include="Mvvm\NotifySelectionChangedCollection.cs" />
<Compile Include="Settings\DataDirectoryDeriver.cs" />
<Compile Include="Settings\IMainModelSerializer.cs" />
Expand Down
57 changes: 57 additions & 0 deletions src/Pickles/Pickles.UserInterface/ViewModel/MainViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,15 @@
using System.Globalization;
using System.IO.Abstractions;
using System.Linq;
using System.Windows;
using System.Windows.Input;

using Autofac;

using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using PicklesDoc.Pickles.ObjectModel;
using PicklesDoc.Pickles.UserInterface.CommandGeneration;
using PicklesDoc.Pickles.UserInterface.Mvvm;
using PicklesDoc.Pickles.UserInterface.Settings;

Expand Down Expand Up @@ -63,6 +65,10 @@ public class MainViewModel : ViewModelBase

private readonly RelayCommand generateCommand;

private readonly RelayCommand generatePowerShellCommandCommand;

private readonly RelayCommand generateCLICommandCommand;

private readonly RelayCommand openOutputDirectory;

private readonly IMainModelSerializer mainModelSerializer;
Expand Down Expand Up @@ -132,6 +138,8 @@ public MainViewModel(IMainModelSerializer mainModelSerializer, IFileSystem fileS
this.browseForOutputFolderCommand = new RelayCommand(this.DoBrowseForOutputFolder);
this.browseForTestResultsFileCommand = new RelayCommand(this.DoBrowseForTestResultsFile);
this.generateCommand = new RelayCommand(this.DoGenerate, this.CanGenerate);
this.generatePowerShellCommandCommand = new RelayCommand(this.DoGeneratePowerShellCommand, this.CanGenerate);
this.generateCLICommandCommand = new RelayCommand(this.DoGenerateCLICommand, this.CanGenerate);
this.openOutputDirectory = new RelayCommand(this.DoOpenOutputDirectory, this.CanOpenOutputDirectory);

this.PropertyChanged += this.MainWindowViewModelPropertyChanged;
Expand Down Expand Up @@ -231,6 +239,16 @@ public ICommand GeneratePickles
get { return this.generateCommand; }
}

public ICommand GeneratePowerShellCommand
{
get { return this.generatePowerShellCommandCommand; }
}

public ICommand GenerateCLICommand
{
get { return this.generateCLICommandCommand; }
}

public ICommand BrowseForFeatureFolder
{
get { return this.browseForFeatureFolderCommand; }
Expand Down Expand Up @@ -467,6 +485,8 @@ private void MainWindowViewModelPropertyChanged(object sender, PropertyChangedEv
case "IsDocumentationFormatValid":
{
this.generateCommand.RaiseCanExecuteChanged();
this.generatePowerShellCommandCommand.RaiseCanExecuteChanged();
this.generateCLICommandCommand.RaiseCanExecuteChanged();
break;
}
}
Expand All @@ -485,6 +505,43 @@ private bool CanGenerate()
&& this.isLanguageValid;
}

private void DoGenerateCommandLine(CommandGeneratorBase generator)
{
var model = this.CreateMainModel();
var commands = generator.Generate(model);
Clipboard.SetText(commands);
MessageBox.Show("Copied the following commands into the clipboard:\n\n" + commands, "Info", MessageBoxButton.OK, MessageBoxImage.Information);
}

private void DoGeneratePowerShellCommand()
{
DoGenerateCommandLine(new PowerShellCommandGenerator());
}

private void DoGenerateCLICommand()
{
DoGenerateCommandLine(new CLICommandGenerator());
}

private MainModel CreateMainModel() =>
new MainModel
{
FeatureDirectory = this.featureFolder,
OutputDirectory = this.outputFolder,
ExcludeTags = this.excludeTags,
HideTags = this.HideTags,
CreateDirectoryForEachOutputFormat = this.createDirectoryForEachOutputFormat,
DocumentationFormats = this.documentationFormats.Selected.ToArray(),
ProjectName = this.projectName,
ProjectVersion = this.projectVersion,
IncludeTestResults = this.includeTests,
TestResultsFile = this.testResultsFile,
TestResultsFormat = this.selectedTestResultsFormat,
SelectedLanguageLcid = this.selectedLanguage.LCID,
EnableComments = this.enableComments,
IncludeExperimentalFeatures = this.includeExperimentalFeatures
};

private void DoGenerate()
{
this.IsRunning = true;
Expand Down

0 comments on commit 7de933d

Please sign in to comment.