Skip to content

Commit

Permalink
Merge pull request #106 from ironmansoftware/IMSHostRemote
Browse files Browse the repository at this point in the history
Remote IMS Host Package.
  • Loading branch information
adamdriscoll authored Jan 2, 2025
2 parents 6789789 + 0900f46 commit 94fbe51
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 17 deletions.
2 changes: 0 additions & 2 deletions PowerShellTools.2022/packages.lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -1796,7 +1796,6 @@
"dependencies": {
"Microsoft.Win32.Registry": "[4.5.0, )",
"Newtonsoft.Json": "[9.0.1, )",
"PowerShellProTools.SharedCommands": "[1.0.0, )",
"PowerShellProTools.VSCode": "[1.0.0, )",
"PowerShellStandard.Library": "[5.1.0, )",
"Serilog.Sinks.File": "[5.0.0, )"
Expand All @@ -1823,7 +1822,6 @@
"FormDesigner.Generator": "[1.0.0, )",
"ICSharpCode.Decompiler": "[7.0.0.6488, )",
"Newtonsoft.Json": "[9.0.1, )",
"PowerShellProTools.SharedCommands": "[1.0.0, )",
"PowerShellStandard.Library": "[5.1.0, )"
}
},
Expand Down
2 changes: 0 additions & 2 deletions PowerShellTools.Test/packages.lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -1195,7 +1195,6 @@
"dependencies": {
"Microsoft.Win32.Registry": "[4.5.0, )",
"Newtonsoft.Json": "[9.0.1, )",
"PowerShellProTools.SharedCommands": "[1.0.0, )",
"PowerShellProTools.VSCode": "[1.0.0, )",
"PowerShellStandard.Library": "[5.1.0, )",
"Serilog.Sinks.File": "[5.0.0, )"
Expand All @@ -1222,7 +1221,6 @@
"FormDesigner.Generator": "[1.0.0, )",
"ICSharpCode.Decompiler": "[7.0.0.6488, )",
"Newtonsoft.Json": "[9.0.1, )",
"PowerShellProTools.SharedCommands": "[1.0.0, )",
"PowerShellStandard.Library": "[5.1.0, )"
}
},
Expand Down
2 changes: 0 additions & 2 deletions PowerShellTools/packages.lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -1135,7 +1135,6 @@
"dependencies": {
"Microsoft.Win32.Registry": "[4.5.0, )",
"Newtonsoft.Json": "[9.0.1, )",
"PowerShellProTools.SharedCommands": "[1.0.0, )",
"PowerShellProTools.VSCode": "[1.0.0, )",
"PowerShellStandard.Library": "[5.1.0, )",
"Serilog.Sinks.File": "[5.0.0, )"
Expand All @@ -1162,7 +1161,6 @@
"FormDesigner.Generator": "[1.0.0, )",
"ICSharpCode.Decompiler": "[7.0.0.6488, )",
"Newtonsoft.Json": "[9.0.1, )",
"PowerShellProTools.SharedCommands": "[1.0.0, )",
"PowerShellStandard.Library": "[5.1.0, )"
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,11 @@
<None Remove="FodyWeavers.xml" />
<None Remove="GetBoundParameters.ps1" />
<None Remove="Hosts\Console\app.manifest" />
<None Remove="Hosts\IronmanPowerShellHost.exe" />
<None Remove="Hosts\IronmanPowerShellHost7.exe" />
<None Remove="Obfuscar.Console.exe" />
</ItemGroup>

<ItemGroup>
<Content Include="Hosts\IronmanPowerShellHost.exe">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<EmbeddedResource Include="..\poshtools.version.txt" Link="poshtools.version.txt" />
</ItemGroup>

<ItemGroup>
Expand Down
83 changes: 77 additions & 6 deletions PowerShellToolsPro.Packager/UpdateHostStage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,8 @@
using AsmResolver.IO;
using AsmResolver.PE;
using AsmResolver.PE.DotNet.Builder;
using AsmResolver.PE.File;
using AsmResolver.PE.File.Headers;
using AsmResolver.PE.Win32Resources;
using AsmResolver.PE.Win32Resources.Builder;
using AsmResolver.PE.Win32Resources.Icon;
using AsmResolver.PE.Win32Resources.Version;
using PowerShellToolsPro.Packager.Config;
using System;
Expand All @@ -22,11 +19,77 @@
using System.Xml.Serialization;
using PSPackager;
using System.Security.Cryptography.X509Certificates;
using System.Net.Http;
using System.Threading.Tasks;
using System.Reflection;

namespace PowerShellToolsPro.Packager
{
public class UpdateHostStage : Stage
{
private async Task DownloadHostAsync()
{
var version = GetVersion();

string url = $"https://github.com/ironmansoftware/powershell-pro-tools/releases/download/host-{version}/host.zip";
string localAppData = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
string targetDirectory = Path.Combine(localAppData, "IronmanPowerShellHost", version);

Directory.CreateDirectory(targetDirectory);

string tempZipPath = Path.Combine(Path.GetTempPath(), "tempDownload.zip");

try
{
await DownloadFileAsync(url, tempZipPath);
ZipFile.ExtractToDirectory(tempZipPath, targetDirectory);
}
catch (Exception ex)
{
WriteErrorMessage($"Failed to download host: {ex.Message}");
}
finally
{
if (File.Exists(tempZipPath))
{
File.Delete(tempZipPath);
}
}
}

static async Task DownloadFileAsync(string url, string destinationPath)
{
using (HttpClient client = new HttpClient())
{
using (HttpResponseMessage response = await client.GetAsync(url, HttpCompletionOption.ResponseHeadersRead))
{
response.EnsureSuccessStatusCode();

using (Stream contentStream = await response.Content.ReadAsStreamAsync(),
fileStream = new FileStream(destinationPath, FileMode.Create, FileAccess.Write, FileShare.None))
{
await contentStream.CopyToAsync(fileStream);
}
}
}
}

private static string GetVersion()
{
Assembly assembly = Assembly.GetExecutingAssembly();
using (Stream stream = assembly.GetManifestResourceStream("PowershellToolsPro.Packager.poshtools.version.txt"))
{
if (stream == null)
{
throw new FileNotFoundException($"Embedded resource 'poshtools.version.txt' not found.");
}
using (StreamReader reader = new StreamReader(stream))
{
return reader.ReadToEnd().Trim();
}
}
}

public override StageResult Execute(PackageProcess process, StageResult previousStage)
{
var hostDownloadService = new HostDownloadService();
Expand All @@ -36,15 +99,23 @@ public override StageResult Execute(PackageProcess process, StageResult previous

WriteDebugMessage($"Generating assembly");

var version = GetVersion();

if (string.IsNullOrEmpty(hostPath))
{
var parentPath = Path.GetDirectoryName(GetType().Assembly.Location);
hostPath = Path.Combine(parentPath, "Hosts");
string localAppData = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
string targetDirectory = Path.Combine(localAppData, "IronmanPowerShellHost", version);

if (!Directory.Exists(targetDirectory))
{
DownloadHostAsync().Wait();
}

switch (process.Config.Package.Host)
{
case PowerShellHosts.IronmanPowerShellHost:
case PowerShellHosts.IronmanPowerShellWinFormsHost:
hostPath = Path.Combine(hostPath, "IronmanPowerShellHost.exe");
hostPath = Path.Combine(targetDirectory, "IronmanPowerShellHost.exe");
break;
default:
return new StageResult(false);
Expand Down

0 comments on commit 94fbe51

Please sign in to comment.