Skip to content

Commit

Permalink
Merge pull request #161 from webmaster442/next
Browse files Browse the repository at this point in the history
Next
  • Loading branch information
webmaster442 authored May 23, 2022
2 parents 4e70a7c + f893fef commit f07babd
Show file tree
Hide file tree
Showing 45 changed files with 661 additions and 319 deletions.
9 changes: 8 additions & 1 deletion BookGen.Core/FsUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -183,12 +183,19 @@ public static bool WriteFile(this FsPath target, ILog log, params string[] conte
}
}

public static string ReadFile(this FsPath path, ILog log)
public static string ReadFile(this FsPath path, ILog log, bool appendExtraLine = false)
{
try
{
using (var reader = File.OpenText(path.ToString()))
{
if (appendExtraLine)
{
StringBuilder buffer = new StringBuilder(reader.ReadToEnd());
buffer.AppendLine();
return buffer.ToString();
}

return reader.ReadToEnd();
}
}
Expand Down
10 changes: 5 additions & 5 deletions BookGen.Gui/ArgumentParser/ArgumentParser.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//-----------------------------------------------------------------------------
// (c) 2020-2021 Ruzsinszki Gábor
// (c) 2020-2022 Ruzsinszki Gábor
// This code is licensed under MIT license (see LICENSE for details)
//-----------------------------------------------------------------------------

Expand All @@ -21,7 +21,7 @@ public static class ArgumentParser
private static int _filled = 0;
private static int _required = 0;

public static bool ParseArguments<T>(string[] args, T targetClass) where T : ArgumentsBase
public static bool ParseArguments<T>(IReadOnlyList<string> args, T targetClass) where T : ArgumentsBase
{
_properties.Clear();
_filled = 0;
Expand Down Expand Up @@ -71,14 +71,14 @@ private static string ParseSwitch(string current)
return current;
}

private static void WalkArgsAndFillClass<T>(string[] args, ref T targetClass) where T : ArgumentsBase
private static void WalkArgsAndFillClass<T>(IReadOnlyList<string> args, ref T targetClass) where T : ArgumentsBase
{
int i = 0;
bool nextIsswitch, currentIsSwitch;
while (i < args.Length)
while (i < args.Count)
{
string current = args[i].ToLower();
string next = (i + 1) < args.Length ? args[i + 1].ToLower() : string.Empty;
string next = (i + 1) < args.Count ? args[i + 1].ToLower() : string.Empty;
nextIsswitch = next.StartsWith("-");
currentIsSwitch = current.StartsWith("-");

Expand Down
1 change: 1 addition & 0 deletions BookGen.Gui/BookGen.Gui.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Web.WebView2" Version="1.0.1210.39" />
<PackageReference Include="Terminal.Gui" Version="1.5.0" />
</ItemGroup>

Expand Down
6 changes: 4 additions & 2 deletions BookGen.Gui/ConsoleUi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,11 @@ public void ResumeUi()

public void ExitApp()
{
if (Application.Current != null)
{
Application.Current.Running = false;
}
SuspendUi();
Dispose();
Environment.Exit(0);
}

public void UpdateBindingsToModel()
Expand Down
12 changes: 12 additions & 0 deletions BookGen.Launch/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
// This code is licensed under MIT license (see LICENSE for details)
//-----------------------------------------------------------------------------

using BookGen.ShellHelper;
using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Navigation;
using System.Windows.Shell;

namespace BookGen.Launch
Expand Down Expand Up @@ -36,5 +38,15 @@ public static void UpdateJumplist(IEnumerable<string> items)
}
list.Apply();
}

protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
var rerminalInstall = TerminalProfileInstaller.TryInstall();
if (rerminalInstall == false)
{
MessageBox.Show(Launch.Properties.Resources.TerminalProfileInstallFail);
}
}
}
}
5 changes: 3 additions & 2 deletions BookGen.Launch/BookGen.Launch.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<OutputType>WinExe</OutputType>
<TargetFramework>net6.0-windows</TargetFramework>
<UseWPF>true</UseWPF>
<ApplicationIcon>Icon.ico</ApplicationIcon>
<ApplicationIcon>..\Icon.ico</ApplicationIcon>
<OutputPath>..\bin\$(Configuration)\</OutputPath>
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
<Nullable>enable</Nullable>
Expand All @@ -15,7 +15,7 @@

<ItemGroup>
<PackageReference Include="AvalonEdit" Version="6.1.3.50" />
<PackageReference Include="Microsoft.Web.WebView2" Version="1.0.1210.30" />
<PackageReference Include="Microsoft.Web.WebView2" Version="1.0.1210.39" />
<PackageReference Include="Ookii.Dialogs.Wpf" Version="5.0.1" />
</ItemGroup>

Expand All @@ -24,6 +24,7 @@
<ProjectReference Include="..\BookGen.Core\BookGen.Core.csproj" />
<ProjectReference Include="..\BookGen.Gui\BookGen.Gui.csproj" />
<ProjectReference Include="..\BookGen.Resources\BookGen.Resources.csproj" />
<ProjectReference Include="..\BookGen.ShellHelper\BookGen.ShellHelper.csproj" />
</ItemGroup>

<ItemGroup>
Expand Down
30 changes: 5 additions & 25 deletions BookGen.Launch/Code/ProcessCommandBase.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
//-----------------------------------------------------------------------------
// (c) 2021 Ruzsinszki Gábor
// (c) 2021-2022 Ruzsinszki Gábor
// This code is licensed under MIT license (see LICENSE for details)
//-----------------------------------------------------------------------------

using BookGen.Launch.Properties;
using BookGen.ShellHelper;
using BookGen.ShellHelper.Domain;
using System;
using System.ComponentModel;
using System.Diagnostics;
Expand All @@ -16,35 +18,13 @@ namespace BookGen.Launch.Code
internal abstract class ProcessCommandBase : ICommand
{
public event EventHandler? CanExecuteChanged;
protected InstallStatus InstallStatus;

protected readonly bool isWindowsTerminalInstalled;
protected readonly bool isVSCodeInstalled;
protected readonly bool isPsCoreInstalled;

protected const string WindowsTerminalExe = "wt.exe";
protected const string PowershellExe = "powershell.exe";
protected const string PowershellCoreExe = "ps.exe";
protected const string VsCodeExe = "code.cmd";

protected ProcessCommandBase()
{
string[] pathDirs = Environment.GetEnvironmentVariable("path")?.Split(';') ?? Array.Empty<string>();
foreach (var dir in pathDirs)
{
if (string.IsNullOrEmpty(dir)) continue;

string? terminalExecutable = Path.Combine(dir, WindowsTerminalExe);
string? vsCodeExecutable = Path.Combine(dir, VsCodeExe);
string? psCoreExecutable = Path.Combine(dir, PowershellCoreExe);
if (File.Exists(terminalExecutable))
isWindowsTerminalInstalled = true;

if (File.Exists(vsCodeExecutable))
isVSCodeInstalled = true;

if (File.Exists(psCoreExecutable))
isPsCoreInstalled = true;
}
InstallStatus = InstallDetector.GetInstallStatus();
}

public bool CanExecute(object? parameter)
Expand Down
7 changes: 4 additions & 3 deletions BookGen.Launch/Code/RunVsCodeCommand.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
//-----------------------------------------------------------------------------
// (c) 2021 Ruzsinszki Gábor
// (c) 2021-2022 Ruzsinszki Gábor
// This code is licensed under MIT license (see LICENSE for details)
//-----------------------------------------------------------------------------

using BookGen.ShellHelper;
using System.IO;
using System.Windows;

Expand All @@ -12,7 +13,7 @@ internal sealed class RunVsCodeCommand : ProcessCommandBase
{
public override bool CanExecute(string? folder)
{
return isVSCodeInstalled
return InstallStatus.IsVSCodeInstalled
&& Directory.Exists(folder);
}

Expand All @@ -25,7 +26,7 @@ public override void Execute(string? folder)
return;
}

RunProgram(VsCodeExe, folder);
RunProgram(InstallDetector.VsCodeExe, folder);
}
}
}
51 changes: 9 additions & 42 deletions BookGen.Launch/Code/StartShellCommand.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
//-----------------------------------------------------------------------------
// (c) 2021 Ruzsinszki Gábor
// (c) 2021-2022 Ruzsinszki Gábor
// This code is licensed under MIT license (see LICENSE for details)
//-----------------------------------------------------------------------------

using BookGen.Resources;
using BookGen.ShellHelper;
using System;
using System.IO;
using System.Windows;
Expand All @@ -12,45 +12,12 @@ namespace BookGen.Launch.Code
{
internal sealed class StartShellCommand : ProcessCommandBase
{
private bool TryCreateShellScript(string folder, out string shellScriptPath)
{
try
{
#pragma warning disable S5445 // Insecure temporary file creation methods should not be used
string? name = Path.GetTempFileName();
#pragma warning restore S5445 // Insecure temporary file creation methods should not be used
name = Path.ChangeExtension(name, ".ps1");

using (var file = File.CreateText(name))
{
var dn = ResourceHandler.GetResourceFile<KnownFile>("Powershell/completer.dn.ps1");
var bg = ResourceHandler.GetResourceFile<KnownFile>("Powershell/completer.ps1");
var sh = ResourceHandler.GetResourceFile<KnownFile>("Powershell/shell.ps1");

file.WriteLine("$env:Path += \";{0}\"", AppContext.BaseDirectory);
file.WriteLine("Set-Location \"{0}\"", folder);
file.WriteLine(dn);
file.WriteLine(bg);
file.WriteLine("Remove-Item \"{0}\"", name);
file.WriteLine(sh);
}

shellScriptPath = name;
return true;
}
catch (IOException)
{
shellScriptPath = string.Empty;
return false;
}
}

private bool Launch(string shellScript)
{
if (isWindowsTerminalInstalled && Properties.Settings.Default.UseWindowsTerminal)
return RunProgram(WindowsTerminalExe, $"new-tab -p \"Powershell\" --title \"BookGen shell\" --colorScheme \"Campbell Powershell\" powershell.exe -ExecutionPolicy Bypass -NoExit -File \"{shellScript}\"");
else if (isPsCoreInstalled)
return RunProgram(PowershellCoreExe, $"-NoExit -File \"{shellScript}\"");
if (InstallStatus.IsWindowsTerminalInstalled && Properties.Settings.Default.UseWindowsTerminal)
return RunProgram(InstallDetector.WindowsTerminalExe, $"new-tab -p \"Powershell\" --title \"BookGen shell\" --colorScheme \"Campbell Powershell\" powershell.exe -ExecutionPolicy Bypass -NoExit -File \"{shellScript}\"");
else if (InstallStatus.IsPsCoreInstalled)
return RunProgram(InstallDetector.PowershellCoreExe, $"-NoExit -File \"{shellScript}\"");
else
return RunProgram(PowershellExe, $"-ExecutionPolicy Bypass -NoExit -File \"{shellScript}\"");
}
Expand All @@ -70,14 +37,14 @@ public override void Execute(string? folder)
return;
}

string _shellScript;
if (!TryCreateShellScript(folder, out _shellScript))
string shellScript = Path.Combine(AppContext.BaseDirectory, "BookGenShell.ps1");
if (!File.Exists(shellScript))
{
Message(Properties.Resources.ShellScriptWriteFail, MessageBoxImage.Error);
return;
}

if (!Launch(_shellScript))
if (!Launch(shellScript))
{
Message(Properties.Resources.ShellScriptStartFail, MessageBoxImage.Error);
}
Expand Down
Binary file removed BookGen.Launch/Icon.ico
Binary file not shown.
9 changes: 9 additions & 0 deletions BookGen.Launch/Properties/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions BookGen.Launch/Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,10 @@
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<data name="ClearRecentList" xml:space="preserve">
<value>Do you want to clear the recent files list?</value>
Expand Down Expand Up @@ -147,4 +147,7 @@
<data name="ShellScriptWriteFail" xml:space="preserve">
<value>Failed to write start script. Application will exit.</value>
</data>
<data name="TerminalProfileInstallFail" xml:space="preserve">
<value>Windows Terminal profile installation failed </value>
</data>
</root>
6 changes: 0 additions & 6 deletions BookGen.Resources/BookGen.Resources.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,6 @@
<None Remove="JsCss\simplemde.min.js" />
<None Remove="JsCss\SinglePage.css" />
<None Remove="JsCss\turbolinks.js" />
<None Remove="Powershell\completer.dn.ps1" />
<None Remove="Powershell\completer.ps1" />
<None Remove="Powershell\shell.ps1" />
<None Remove="Powershell\updater.ps1" />
<None Include="..\LICENCE">
<Pack>True</Pack>
Expand Down Expand Up @@ -77,9 +74,6 @@
<EmbeddedResource Include="JsCss\prism.js" />
<EmbeddedResource Include="JsCss\SinglePage.css" />
<EmbeddedResource Include="JsCss\turbolinks.js" />
<EmbeddedResource Include="Powershell\completer.dn.ps1" />
<EmbeddedResource Include="Powershell\completer.ps1" />
<EmbeddedResource Include="Powershell\shell.ps1" />
<EmbeddedResource Include="Powershell\updater.ps1" />
</ItemGroup>

Expand Down
7 changes: 0 additions & 7 deletions BookGen.Resources/Powershell/completer.dn.ps1

This file was deleted.

14 changes: 0 additions & 14 deletions BookGen.Resources/Powershell/completer.ps1

This file was deleted.

7 changes: 0 additions & 7 deletions BookGen.Resources/Powershell/shell.ps1

This file was deleted.

2 changes: 1 addition & 1 deletion BookGen.Setup/version.iss
Original file line number Diff line number Diff line change
@@ -1 +1 @@
#define MyAppVersion "2022.03.02"
#define MyAppVersion "2022.05.23"
14 changes: 14 additions & 0 deletions BookGen.ShellHelper/Domain/InstallStatus.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//-----------------------------------------------------------------------------
// (c) 2022 Ruzsinszki Gábor
// This code is licensed under MIT license (see LICENSE for details)
//-----------------------------------------------------------------------------

namespace BookGen.ShellHelper.Domain
{
public sealed record class InstallStatus
{
public bool IsWindowsTerminalInstalled { get; init; }
public bool IsVSCodeInstalled { get; init; }
public bool IsPsCoreInstalled { get; init; }
}
}
Loading

0 comments on commit f07babd

Please sign in to comment.