Skip to content

Commit

Permalink
Don't use PowerShell SDK which fails in single file app
Browse files Browse the repository at this point in the history
  • Loading branch information
t1m0thyj committed Apr 3, 2022
1 parent f1dec64 commit 0836e34
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 19 deletions.
51 changes: 34 additions & 17 deletions src/ScriptManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Management.Automation;
using System.Text;
using System.Threading.Tasks;

namespace WinDynamicDesktop
Expand Down Expand Up @@ -57,26 +57,43 @@ public static void RunScripts(ScriptArgs args)
}
}

private static void RunScript(string path, ScriptArgs args)
private static async void RunScript(string path, ScriptArgs args)
{
using (var ps = PowerShell.Create())
Process proc = new Process();
string command = BuildCommandString(Path.GetFileName(path), args);
proc.StartInfo = new ProcessStartInfo("powershell.exe", "-NoProfile -ExecutionPolicy Bypass -Command \"" + command + "\"")
{
ps.AddScript("Set-ExecutionPolicy Bypass -Scope Process -Force");
ps.AddScript(File.ReadAllText(path));
ps.AddParameter("daySegment2", args.daySegment2);
ps.AddParameter("daySegment4", args.daySegment4 ?? -1);
// TODO Provide all the image paths to scripts, not just first one
ps.AddParameter("imagePath", args.imagePaths[0]);
ps.AddParameter("nightMode", JsonConfig.settings.darkMode);
ps.Invoke();
CreateNoWindow = true,
RedirectStandardError = true,
UseShellExecute = false,
WindowStyle = ProcessWindowStyle.Hidden,
WorkingDirectory = Path.GetDirectoryName(path)
};

if (ps.Streams.Error.Count > 0)
{
MessageDialog.ShowWarning(string.Format(_("Error(s) running PowerShell script '{0}':\n\n{1}"), path,
string.Join("\n\n", ps.Streams.Error.ReadAll().Select((er) => er.Exception.ToString()))),
_("Script Error"));
var errors = new StringBuilder();
proc.ErrorDataReceived += (sender, e) => {
if (!string.IsNullOrEmpty(e.Data)) {
errors.Append(e.Data + "\n");
}
};
proc.Start();
proc.BeginErrorReadLine();
await proc.WaitForExitAsync();

if (proc.ExitCode != 0)
{
MessageDialog.ShowWarning(string.Format(_("Error(s) running PowerShell script '{0}':\n\n{1}"), path,
errors), _("Script Error"));
}
}

private static string BuildCommandString(string filename, ScriptArgs args)
{
return "& \\\".\\" + filename + "\\\"" +
" -daySegment2 " + args.daySegment2.ToString() +
" -daySegment4 " + (args.daySegment4 ?? -1).ToString() +
" -imagePath \\\"" + args.imagePaths[0] + "\\\"" +
" -nightMode " + Convert.ToInt32(JsonConfig.settings.darkMode);
}
}
}
3 changes: 1 addition & 2 deletions src/WinDynamicDesktop.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<PackageId>WinDynamicDesktop</PackageId>
<Product>WinDynamicDesktop</Product>
<Copyright>Copyright © 2022 Timothy Johnson</Copyright>
<Version>5.0.0</Version>
<Version>5.0.1</Version>
<ApplicationIcon>resources\WinDynamicDesktop.ico</ApplicationIcon>
<UseWindowsForms>true</UseWindowsForms>
<UseWPF>true</UseWPF>
Expand All @@ -28,7 +28,6 @@
<PackageReference Include="DesktopBridge.Helpers" Version="1.2.2" />
<PackageReference Include="Fody" Version="6.6.0" IncludeAssets="runtime; build; native; contentfiles; analyzers; buildtransitive" PrivateAssets="all" />
<PackageReference Include="GeoTimeZone" Version="4.1.0" />
<PackageReference Include="Microsoft.PowerShell.SDK" Version="7.2.2" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="NGettext" Version="0.6.7" />
<PackageReference Include="PropertyChanged.Fody" Version="3.4.0" PrivateAssets="all" />
Expand Down

0 comments on commit 0836e34

Please sign in to comment.