Skip to content

Commit

Permalink
Merge pull request #196 from WildernessLabs/dominque-V1Lazy
Browse files Browse the repository at this point in the history
Lazy load settings and ensure template installation runs on a background thread.
  • Loading branch information
CartBlanche authored Oct 31, 2024
2 parents 3eaed90 + 4b87a07 commit b8697df
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 38 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/dotnet.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: VS4Win Extension
env:
IDE_TOOLS_RELEASE_VERSION: 1.9.9.1
IDE_TOOLS_RELEASE_VERSION: 1.9.9.2

on:
push:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<PackageManifest Version="2.0.0" xmlns="http://schemas.microsoft.com/developer/vsx-schema/2011" xmlns:d="http://schemas.microsoft.com/developer/vsx-schema-design/2011">
<Metadata>
<Identity Id="VS_Meadow_Extension.ProjectType..fe358059-9487-4fe7-a01a-4b67b8231321" Version="1.9.9.1" Language="en-US" Publisher="Wilderness Labs" />
<Identity Id="VS_Meadow_Extension.ProjectType..fe358059-9487-4fe7-a01a-4b67b8231321" Version="1.9.9.2" Language="en-US" Publisher="Wilderness Labs" />
<DisplayName>VS 2019 Tools for Meadow</DisplayName>
<Description xml:space="preserve">Tools for developing Meadow applications</Description>
<Icon>wildernesslabs_icon.png</Icon>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<PackageManifest Version="2.0.0" xmlns="http://schemas.microsoft.com/developer/vsx-schema/2011" xmlns:d="http://schemas.microsoft.com/developer/vsx-schema-design/2011">
<Metadata>
<Identity Id="VS_Meadow_Extension.2022.d5eb772d-2173-4795-b60b-67929a9bf12d" Version="1.9.9.1" Language="en-US" Publisher="Wilderness Labs" />
<Identity Id="VS_Meadow_Extension.2022.d5eb772d-2173-4795-b60b-67929a9bf12d" Version="1.9.9.2" Language="en-US" Publisher="Wilderness Labs" />
<DisplayName>VS 2022 Tools for Meadow</DisplayName>
<Description xml:space="preserve">Tools for developing Meadow applications</Description>
<Icon>wildernesslabs_icon.png</Icon>
Expand Down
2 changes: 1 addition & 1 deletion VS_Meadow_Extension/VS_Meadow_Extension.Shared/Globals.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Meadow
{
public static class Globals
{
public const string AssemblyVersion = "1.9.9.1";
public const string AssemblyVersion = "1.9.9.2";

public const string MeadowCapability = "Meadow";

Expand Down
100 changes: 66 additions & 34 deletions VS_Meadow_Extension/VS_Meadow_Extension.Shared/MeadowPackage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ public sealed class MeadowPackage : AsyncPackage

public static bool DebugOrDeployInProgress { get; set; } = false;

private MeadowSettings meadowSettings = new MeadowSettings(Globals.SettingsFilePath);
private Lazy<MeadowSettings> meadowSettingsLazy;
private MeadowSettings MeadowSettings => meadowSettingsLazy.Value;

/// <summary>
/// Initializes a new instance of the <see cref="MeadowPackage"/> class.
Expand All @@ -53,7 +54,6 @@ public MeadowPackage()
// initialization is the Initialize method.
}


#region Package Members

/// <summary>
Expand All @@ -67,13 +67,16 @@ protected override async Task InitializeAsync(CancellationToken cancellationToke
{
await base.InitializeAsync(cancellationToken, progress);

// Make settings loading lazy
meadowSettingsLazy = new Lazy<MeadowSettings>(() => new MeadowSettings(Globals.SettingsFilePath));

// Ensure Install dependencies is off loaded to a background thread
_ = InstallDependencies(cancellationToken);

// When initialized asynchronously, the current thread may be a background thread at this point.
// Do any initialization that requires the UI thread after switching to the UI thread.
await JoinableTaskFactory.SwitchToMainThreadAsync(cancellationToken);

// Install dependencies
await InstallDependencies();

// Add our command handlers for menu (commands must be declared in the .vsct file)
if (await GetServiceAsync(typeof(IMenuCommandService)) is OleMenuCommandService mcs)
{
Expand Down Expand Up @@ -105,10 +108,10 @@ private async void OnMeadowDeviceListCombo(object sender, EventArgs e)
{
string deviceTarget = string.Empty;

bool IsSavedValueInPortList = IsValueInPortList(portList, meadowSettings.DeviceTarget);
bool IsSavedValueInPortList = IsValueInPortList(portList, MeadowSettings.DeviceTarget);
if (IsSavedValueInPortList)
{
deviceTarget = meadowSettings.DeviceTarget;
deviceTarget = MeadowSettings.DeviceTarget;
}

Marshal.GetNativeVariantForObject(deviceTarget, vOut);
Expand Down Expand Up @@ -194,59 +197,88 @@ private static bool IsValueInPortList(IList<string> portList, string newChoice)

private void SaveDeviceChoiceToSettings(string newChoice)
{
meadowSettings.DeviceTarget = newChoice;
meadowSettings.Save();
MeadowSettings.DeviceTarget = newChoice;
MeadowSettings.Save();
}

private async Task InstallDependencies()
private async Task InstallDependencies(CancellationToken cancellationToken)
{
// No point installing if we don't have an internet connection
if (NetworkInterface.GetIsNetworkAvailable())
if (!NetworkInterface.GetIsNetworkAvailable())
{
//string templateName = "Meadow";
// Check if the package is installed
//if (!await IsTemplateInstalled(templateName))
{
string packageName = "WildernessLabs.Meadow.Template";
return;
}

// Install the package.
// If an update is available it should update it automagically.
if (!await InstallPackage(packageName))
{
// Unable to install ProjectTemplates Throw Up a Message??
}
//string templateName = "Meadow";
// Check if the package is installed
//if (!await IsTemplateInstalled(templateName))
{
string packageName = "WildernessLabs.Meadow.Template";

// Install the package.
// If an update is available it should update it automagically.
if (!await InstallPackage(packageName, cancellationToken))
{
// Unable to install ProjectTemplates Throw Up a Message??
}
}

}

private async Task<bool> InstallPackage(string packageName)
private async Task<bool> InstallPackage(string packageName, CancellationToken cancellationToken)
{
return await StartDotNetProcess("new install", packageName);
return await StartDotNetProcess("new install", packageName, cancellationToken);
}

private async Task<bool> IsTemplateInstalled(string templateName)
private async Task<bool> IsTemplateInstalled(string templateName, CancellationToken cancellationToken)
{
return await StartDotNetProcess("new list", templateName);
return await StartDotNetProcess("new list", templateName, cancellationToken);
}

private async Task<bool> StartDotNetProcess(string command, string parameters)
private async Task<bool> StartDotNetProcess(string command, string parameters, CancellationToken cancellationToken)
{
return await Task.Run(async () =>
using (var process = new System.Diagnostics.Process())
{
System.Diagnostics.Process process = new System.Diagnostics.Process();
process.StartInfo.FileName = "dotnet";
process.StartInfo.Arguments = $"{command} {parameters}";
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.UseShellExecute = false;
process.StartInfo.CreateNoWindow = true;

var outputBuilder = string.Empty;
var errorBuilder = string.Empty;

// Event handlers for async output reading
process.OutputDataReceived += (sender, args) => outputBuilder += Environment.NewLine + args.Data;
process.ErrorDataReceived += (sender, args) => errorBuilder += Environment.NewLine + args.Data;

process.Start();

string output = await process.StandardOutput.ReadToEndAsync();
process.WaitForExit();
process.BeginOutputReadLine();
process.BeginErrorReadLine();

// Wait for the process to complete asynchronously
await Task.Run(() =>
{
process.WaitForExit();
}, cancellationToken);

/* TODO Maybe we should log the output and error to a file
var output = outputBuilder.ToString();
var errorOutput = errorBuilder.ToString(); */

// Check if the package name exists in the output
return output.Contains(parameters);
});
if (process.ExitCode == 0)
{
// Process completed successfully
return true;
}
else
{
// Process failed
return false;
}
}
}
}

Expand Down
4 changes: 4 additions & 0 deletions overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ For step by step instructions on using this extension, [check out the tutorial](

## Release Notes

### 1.9.9.2

- Lazy load settings and ensure Template installation happens in a background thread.

### 1.9.9.1

- Update NoLink list
Expand Down

0 comments on commit b8697df

Please sign in to comment.