Add Generate button

This commit is contained in:
2021-12-04 15:27:04 +01:00
parent 6df7c2885b
commit 52b7e10da1
4 changed files with 79 additions and 7 deletions

View File

@@ -8,7 +8,7 @@ using System.Threading.Tasks;
namespace ExeLauncher.GUI namespace ExeLauncher.GUI
{ {
public record ApplicationModel : INotifyPropertyChanged public class ApplicationModel : INotifyPropertyChanged
{ {
private int _number = 1; private int _number = 1;
public int Number public int Number
@@ -70,7 +70,7 @@ namespace ExeLauncher.GUI
} }
} }
public event PropertyChangedEventHandler? PropertyChanged; public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged([CallerMemberName] string propertyName = "") private void NotifyPropertyChanged([CallerMemberName] string propertyName = "")
{ {

View File

@@ -2,8 +2,7 @@
<PropertyGroup> <PropertyGroup>
<OutputType>WinExe</OutputType> <OutputType>WinExe</OutputType>
<TargetFramework>net6.0-windows</TargetFramework> <TargetFramework>net48</TargetFramework>
<Nullable>enable</Nullable>
<UseWPF>true</UseWPF> <UseWPF>true</UseWPF>
</PropertyGroup> </PropertyGroup>

View File

@@ -13,6 +13,7 @@
<StackPanel Orientation="Horizontal" HorizontalAlignment="Right" VerticalAlignment="Center" Margin="4,0"> <StackPanel Orientation="Horizontal" HorizontalAlignment="Right" VerticalAlignment="Center" Margin="4,0">
<Button HorizontalAlignment="Right" Margin="0,8" Padding="6,4" Content="Remove" Click="Button_Remove" /> <Button HorizontalAlignment="Right" Margin="0,8" Padding="6,4" Content="Remove" Click="Button_Remove" />
<Button HorizontalAlignment="Right" Margin="4,8" Padding="6,4" Content="Add" Click="Button_Add" /> <Button HorizontalAlignment="Right" Margin="4,8" Padding="6,4" Content="Add" Click="Button_Add" />
<Button HorizontalAlignment="Right" Margin="8,8,4,8" Padding="6,4" Content="Generate" Click="Button_Generate" />
</StackPanel> </StackPanel>
</Grid> </Grid>
<!--Apps--> <!--Apps-->

View File

@@ -15,6 +15,7 @@ using System.Windows.Media;
using System.Windows.Media.Imaging; using System.Windows.Media.Imaging;
using System.Windows.Navigation; using System.Windows.Navigation;
using System.Windows.Shapes; using System.Windows.Shapes;
using Microsoft.Win32;
namespace ExeLauncher.GUI namespace ExeLauncher.GUI
{ {
@@ -23,9 +24,9 @@ namespace ExeLauncher.GUI
/// </summary> /// </summary>
public partial class MainWindow : Window, INotifyPropertyChanged public partial class MainWindow : Window, INotifyPropertyChanged
{ {
public event PropertyChangedEventHandler? PropertyChanged; public event PropertyChangedEventHandler PropertyChanged;
private string _appName = ""; private string _appName = "Run";
public string ApplicationName public string ApplicationName
{ {
@@ -59,9 +60,13 @@ namespace ExeLauncher.GUI
public ObservableCollection<ApplicationModel> Applications { get; set; } public ObservableCollection<ApplicationModel> Applications { get; set; }
private readonly ExeGenerator _exeGenerator;
public MainWindow() public MainWindow()
{ {
Applications = new() _exeGenerator = new ExeGenerator();
Applications = new ObservableCollection<ApplicationModel>()
{ {
new ApplicationModel() new ApplicationModel()
}; };
@@ -87,6 +92,73 @@ namespace ExeLauncher.GUI
} }
} }
private void Button_Generate(object sender, RoutedEventArgs e)
{
if (!CheckApplications())
{
MessageBox.Show("Path and working directory cannot be empty!", "Invalid path or working directory", MessageBoxButton.OK, MessageBoxImage.Error);
return;
}
Generate();
}
private void Generate()
{
var dialog = new SaveFileDialog()
{
AddExtension = true,
DefaultExt = ".exe",
FileName = $"{ApplicationName}.exe",
Filter = "Executable|*.exe",
OverwritePrompt = true
};
if (dialog.ShowDialog() == true)
{
var paths = string.Join("$", Applications.Select(app => app.Path));
var arguments = string.Join("$", Applications.Select(app => app.Arguments));
var workingDirectories = string.Join("$", Applications.Select(app => app.WorkingDirectory));
var errors = _exeGenerator.CreateExecutable(dialog.FileName, paths, arguments, workingDirectories, ApplicationIconPath);
if (errors != null)
{
var message = GenerateErrorMessage(errors);
MessageBox.Show(message, "Compilation error", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
}
private string GenerateErrorMessage(System.CodeDom.Compiler.CompilerErrorCollection errors)
{
var builder = new StringBuilder();
builder.AppendLine("An error occured while compiling the executable: ");
foreach (var compilerError in errors)
{
builder.AppendLine($"\t{compilerError}");
builder.AppendLine();
}
return builder.ToString();
}
private bool CheckApplications()
{
foreach (var app in Applications)
{
if (string.IsNullOrWhiteSpace(app.Path) || string.IsNullOrWhiteSpace(app.WorkingDirectory))
{
return false;
}
}
return true;
}
private void NotifyPropertyChanged([CallerMemberName] string propertyName = "") private void NotifyPropertyChanged([CallerMemberName] string propertyName = "")
{ {
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));