Skip to content
This repository has been archived by the owner on Dec 21, 2024. It is now read-only.

Commit

Permalink
Merge pull request #34 from StrangeRanger/dev
Browse files Browse the repository at this point in the history
Major refactoring for underlining modularity and performance
  • Loading branch information
StrangeRanger authored Jan 11, 2024
2 parents 354c83c + 1ab6cb3 commit e26d117
Show file tree
Hide file tree
Showing 15 changed files with 403 additions and 220 deletions.
3 changes: 3 additions & 0 deletions Docs/Figma/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Figma Demo

This contains pictures/screenshots of the pre-coding design and demo of the application. Its purpose was to create a concept for what the gui might and should look like.
42 changes: 0 additions & 42 deletions FAFB-PowerShell-Tool.Tests/CommandTest.cs

This file was deleted.

66 changes: 66 additions & 0 deletions FAFB-PowerShell-Tool.Tests/GuiCommandTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
using FAFB_PowerShell_Tool.PowerShell;

namespace FAFB_PowerShell_Tool.Tests;

[TestClass]
public class GuiCommandTest
{
[TestMethod]
public void CommandNameIsCorrect()
{
InternalCommand internalCommand = new("Get-ADUser");
Assert.AreEqual("Get-ADUser", internalCommand.CommandName);
}

[TestMethod]
public void CommandNameThrowsArgumentExceptionWhenNull()
{
Assert.ThrowsException<ArgumentException>(() => new GuiCommand(null!));
}

[TestMethod]
public void CommandNameThrowsArgumentExceptionWhenWhitespace()
{
Assert.ThrowsException<ArgumentException>(() => new GuiCommand(""));
Assert.ThrowsException<ArgumentException>(() => new GuiCommand(" "));
Assert.ThrowsException<ArgumentException>(() => new GuiCommand(string.Empty));
}

[TestMethod]
public void PossibleParametersThrowsInvalidOperationExceptionWhenEmpty()
{
GuiCommand guiCommand = new("Get-ADUser");
Assert.ThrowsException<InvalidOperationException>(() => guiCommand.PossibleParameters);
}

[TestMethod]
public void PossibleParametersReturnsCorrectly()
{
GuiCommand guiCommand = new("Get-ADUser");
guiCommand.LoadCommandParametersAsync().Wait();
Assert.IsTrue(guiCommand.PossibleParameters.Contains("-Identity"));
Assert.IsTrue(guiCommand.PossibleParameters.Count > 0);
}

[TestMethod]
public void PossibleParametersReturnsCorrectlyWhenCalledTwice()
{
GuiCommand guiCommand = new("Get-ADUser");
guiCommand.LoadCommandParametersAsync().Wait();
guiCommand.LoadCommandParametersAsync().Wait();
Assert.IsTrue(guiCommand.PossibleParameters.Contains("-Identity"));
Assert.IsTrue(guiCommand.PossibleParameters.Count > 0);
Assert.AreEqual(guiCommand.PossibleParameters.Count, guiCommand.PossibleParameters.Distinct().Count());
}

[TestMethod]
public void TestParameters()
{
GuiCommand guiCommand = new("Get-ADUser")
{
Parameters = new[] {"-Identity", "FAFB-Admin"}
};
Assert.AreEqual(guiCommand.Parameters[0], "-Identity");
Assert.AreEqual(guiCommand.Parameters[1], "FAFB-Admin");
}
}
10 changes: 3 additions & 7 deletions FAFB-PowerShell-Tool.Tests/PowerShellExecutorTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,9 @@ namespace FAFB_PowerShell_Tool.Tests;
[TestClass]
public class PowerShellExecutorTest
{
private readonly PowerShellExecutor _powerShellExecutor = new();

[TestMethod]
public void ThrowArgumentNullExceptionWhenCommandTextIsNullOrWhitespace()
public void ExecuteGeneratesStdOut()
{
Assert.ThrowsException<ArgumentException>(() => _powerShellExecutor.Execute(null!));
Assert.ThrowsException<ArgumentException>(() => _powerShellExecutor.Execute(""));
Assert.ThrowsException<ArgumentException>(() => _powerShellExecutor.Execute(" "));

}
}
}
57 changes: 39 additions & 18 deletions FAFB-PowerShell-Tool/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,17 @@
<Grid Grid.Row="1">
<!-- Define Columns for resizable area -->
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" MinWidth="1" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="3*" />
<ColumnDefinition Width="*" MinWidth="150" />
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="3*" MinWidth="150"/>
</Grid.ColumnDefinitions>

<!-- Left Side Query Bar (Content in first column) -->
<Grid x:Name="LeftSideQueryGrid" Grid.Column="0">
<Grid.BindingGroup>
<BindingGroup/>
</Grid.BindingGroup>

<Grid.Background>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#FF616161" Offset="0"/>
Expand All @@ -43,8 +44,8 @@
</Grid.Background>

<!-- Predefined Queries -->
<StackPanel x:Name="ButtonStackPanel">
<Button x:Name="SampleOne" Content="Sample 1" VerticalAlignment="Top" Height="49" Grid.Row="0"/>
<StackPanel x:Name="ButtonStackPanel" >
<Button x:Name="SampleOne" Content="Sample 1" VerticalAlignment="Top" Height="49"/>
<Button x:Name="SampleTwo" Content="Sample 2" Height="48"/>
<Button x:Name="SampleThree" Content="Sample 3" Height="50"/>
</StackPanel>
Expand All @@ -58,23 +59,43 @@
<Border BorderThickness="1" BorderBrush="Black" RenderTransformOrigin="0.516,0.595">
<TabControl>
<!-- Tab Items -->
<TabItem Header="Main">
<Grid Background="White">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="91*"/>
<ColumnDefinition Width="100*"/>
<ColumnDefinition Width="446*"/>
</Grid.ColumnDefinitions>
<!--<Button x:Name="ExecutionButton" Content="Execute" Click="MExecutionButton" HorizontalAlignment="Left" Margin="206,88,0,0" VerticalAlignment="Top" Height="49" Width="140" Grid.Column="2"/>-->
</Grid>
<TabItem Header="Query Information">
<!--<Grid Background="White">
<Button x:Name="ExecutionButton" Content="Execute" Click="MExecutionButton" HorizontalAlignment="Left" Margin="206,88,0,0" VerticalAlignment="Top" Height="49" Width="140" Grid.Column="2"/>
</Grid>-->
</TabItem>
<!-- Tab Items -->
<TabItem Header="Query Builder">
<Grid Background="#FFE5E5E5">
<ComboBox x:Name="ComboBoxCommandList" SelectionChanged="MComboBoxSelectionChanged" HorizontalAlignment="Left" Margin="26,48,0,0" VerticalAlignment="Top" Width="216" BorderBrush="#FFD41818" Background="Black" IsEditable="True" />
<ComboBox x:Name="ComboBoxCommandParameterList" HorizontalAlignment="Left" Margin="278,48,0,0" VerticalAlignment="Top" Width="212" IsEditable="True"/>
<Button x:Name="SaveQueryButton" Click="MSaveQueryButton" Content="Save Query" HorizontalAlignment="Left" Margin="459,275,0,0" VerticalAlignment="Top" Height="29" Width="83" />
<Grid Background="#FFE5E5E5" MinHeight="100">
<!-- Define Rows -->
<Grid.RowDefinitions>
<!-- TODO: Figure out why the bottom row isn't stopping at the specified min height!!! -->
<RowDefinition Height="Auto" MinHeight="100"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="5"/>
<RowDefinition Height="3*" MinHeight="100"/>
</Grid.RowDefinitions>

<Grid Grid.Row="0">
<!-- Existing Controls -->
<ComboBox x:Name="ComboBoxCommandList" SelectionChanged="MComboBoxSelectionChanged" HorizontalAlignment="Left" Margin="26,48,0,0" VerticalAlignment="Top" Width="216" BorderBrush="#FFD41818" Background="Black" IsEditable="True" Grid.Row="0"/>
<ComboBox x:Name="ComboBoxCommandParameterList" HorizontalAlignment="Left" Margin="278,48,0,0" VerticalAlignment="Top" Width="212" IsEditable="True" Grid.Row="0"/>
</Grid>

<Grid Grid.Row="1" Margin="12,16">
<Button x:Name="SaveQueryButton" Click="MSaveQueryButton" Content="Save Query" HorizontalAlignment="Right" VerticalAlignment="Top" Height="29" Width="83"/>
</Grid>

<!-- GridSplitter for resizing console area in the second row -->
<GridSplitter Height="5" HorizontalAlignment="Stretch" VerticalAlignment="Bottom" Background="White" Grid.Row="2"/>

<!-- TODO: Figure out why I can't get the console to be resizable to the min height... -->
<Grid Grid.Row="3">
<!-- TextBox as Console Output -->
<TextBox x:Name="ConsoleOutputTextBox" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="10" IsReadOnly="True" TextWrapping="Wrap" VerticalScrollBarVisibility="Auto"/>
</Grid>
</Grid>

</TabItem>
<!-- Tab Items -->
<TabItem Header="Script Editor" HorizontalAlignment="Center" >
Expand Down
67 changes: 54 additions & 13 deletions FAFB-PowerShell-Tool/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,30 @@
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Management.Automation;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using FAFB_PowerShell_Tool.PowerShell;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Xml.Serialization;
using System.Text.Json;
using System.Net.Security;

namespace FAFB_PowerShell_Tool;

public partial class MainWindow
{
// Creates an instance to be able to serialize the queries
CustomQueries CQ = new CustomQueries();

public MainWindow()
{
InitializeComponent();
Loaded += MainWindow_Loaded; // Allows for async method to be called in the Loaded event.
}

/// <summary>
/// This method is used to populate the first ComboBox with the commands that are available.
/// </summary>
Expand All @@ -23,11 +33,13 @@ public MainWindow()
private async void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
ComboBox comboBoxCommandList = ComboBoxCommandList;
ObservableCollection<Command> list = await ActiveDirectoryCommands.GetActiveDirectoryCommands();
ObservableCollection<GuiCommand> list = await ActiveDirectoryCommands.GetActiveDirectoryCommands();
comboBoxCommandList.ItemsSource = list;
comboBoxCommandList.DisplayMemberPath = "CommandName";

FillCustomQueries();
}

/// <summary>
/// This method is used to populate the second ComboBox with the parameters of the selected command.
/// </summary>
Expand All @@ -37,40 +49,69 @@ private async void MComboBoxSelectionChanged(object sender, SelectionChangedEven
{
ComboBox comboBox = sender as ComboBox ?? throw new InvalidOperationException();

if (comboBox.SelectedItem is not Command selectedCommand) { return; }

if (comboBox.SelectedItem is not GuiCommand selectedCommand)
{
return;
}

// Set the ItemsSource for the parameters ComboBox.
ComboBox comboBoxParameters = ComboBoxCommandParameterList;
await selectedCommand.LoadCommandParametersAsync(); // Lazy loading.
await selectedCommand.LoadCommandParametersAsync(); // Lazy loading.
comboBoxParameters.ItemsSource = selectedCommand.PossibleParameters;
}

// TODO: Test to see if this works as it should...
// TODO: Make the buttons able to save a command to them and have a universal class maybe for executing them
private void MSaveQueryButton(object sender, RoutedEventArgs e)
{
// Try to get the content within the drop downs
try
{
// Get the Command
Command? commandName = ComboBoxCommandList.SelectedValue as Command;
GuiCommand? command = ComboBoxCommandList.SelectedValue as GuiCommand;
// string commandParameters = cmbParameters.Text;

Button newButton = new()
{
Button newButton = new() {

Content = "Special Command",
Height = 48
};

// Adds the query to the Queries serializable list
CQ.Queries.Add(command.CommandName);

Check warning on line 80 in FAFB-PowerShell-Tool/MainWindow.xaml.cs

View workflow job for this annotation

GitHub Actions / build (x64)

Dereference of a possibly null reference.

Check warning on line 80 in FAFB-PowerShell-Tool/MainWindow.xaml.cs

View workflow job for this annotation

GitHub Actions / build (x64)

Dereference of a possibly null reference.

Check warning on line 80 in FAFB-PowerShell-Tool/MainWindow.xaml.cs

View workflow job for this annotation

GitHub Actions / build (x64)

Dereference of a possibly null reference.

Check warning on line 80 in FAFB-PowerShell-Tool/MainWindow.xaml.cs

View workflow job for this annotation

GitHub Actions / build (x64)

Dereference of a possibly null reference.

Check warning on line 80 in FAFB-PowerShell-Tool/MainWindow.xaml.cs

View workflow job for this annotation

GitHub Actions / build (x64)

Dereference of a possibly null reference.

Check warning on line 80 in FAFB-PowerShell-Tool/MainWindow.xaml.cs

View workflow job for this annotation

GitHub Actions / build (x64)

Dereference of a possibly null reference.
// Adds the button in the stack panel
ButtonStackPanel.Children.Add(newButton);
// Saves the Queries to the file
CQ.SerializeMethod();
}
catch (Exception ex)
{
Console.Write(ex);
}
}
/// <summary>
/// This Method is to fill the left had side query bar
/// </summary>
private void FillCustomQueries()
{
try
{
CQ.LoadData();

foreach (string cQuery in CQ.Queries)
{
Button newButton = new() { Content = "Special Command", Height = 48 };

ButtonStackPanel.Children.Add(newButton);
}
}
catch (Exception ex)
{
Trace.WriteLine(ex);
}
}

// TODO: Test to see if this works as it should...
private void ExecuteScriptEditorButton(object sender, RoutedEventArgs e)
/*private void ExecuteScriptEditorButton(object sender, RoutedEventArgs e)
{
string scriptEditorText = ScriptEditorTextBox.Text;
PowerShellExecutor powerShellExecutor = new();
Expand All @@ -93,5 +134,5 @@ private void ExecuteScriptEditorButton(object sender, RoutedEventArgs e)
{
MessageBoxOutput.Show(ex.Message, MessageBoxOutput.OutputType.InternalError);
}
}
}
}*/
}
Loading

0 comments on commit e26d117

Please sign in to comment.