Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create workloads enumerator lazily in dotnet-new host #28677

Merged
merged 3 commits into from
Oct 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/Cli/dotnet/commands/dotnet-new/NewCommandParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,10 @@ private static CliTemplateEngineHost CreateHost(bool disableSdkTemplates, bool d
builtIns.Add((typeof(ITemplateConstraintFactory), new ProjectCapabilityConstraintFactory()));
builtIns.Add((typeof(MSBuildEvaluator), new MSBuildEvaluator(outputDirectory: outputPath?.FullName, projectPath: projectPath?.FullName)));
}
builtIns.Add((typeof(IWorkloadsInfoProvider), new WorkloadsInfoProvider(new WorkloadInfoHelper())));

builtIns.Add((typeof(IWorkloadsInfoProvider), new WorkloadsInfoProvider(
new Lazy<IWorkloadsRepositoryEnumerator>(() => new WorkloadInfoHelper())))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we consider just lazy implementation of IWorkloadsRepositoryEnumerator instead?
just a though

);
builtIns.Add((typeof(ISdkInfoProvider), new SdkInfoProvider()));

string? preferredLangEnvVar = Environment.GetEnvironmentVariable(PrefferedLangEnvVarName);
Expand Down
6 changes: 3 additions & 3 deletions src/Cli/dotnet/commands/dotnet-new/WorkloadsInfoProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,18 @@ namespace Microsoft.DotNet.Tools.New
{
internal class WorkloadsInfoProvider : IWorkloadsInfoProvider
{
private readonly IWorkloadsRepositoryEnumerator _workloadsRepositoryEnumerator;
private readonly Lazy<IWorkloadsRepositoryEnumerator> _workloadsRepositoryEnumerator;
public Guid Id { get; } = Guid.Parse("{F8BA5B13-7BD6-47C8-838C-66626526817B}");

public WorkloadsInfoProvider(IWorkloadsRepositoryEnumerator workloadsRepositoryEnumerator)
public WorkloadsInfoProvider(Lazy<IWorkloadsRepositoryEnumerator> workloadsRepositoryEnumerator)
{
_workloadsRepositoryEnumerator = workloadsRepositoryEnumerator;
}

public Task<IEnumerable<WorkloadInfo>> GetInstalledWorkloadsAsync(CancellationToken cancellationToken)
{
return Task.FromResult(
_workloadsRepositoryEnumerator.InstalledAndExtendedWorkloads.Select(w => new WorkloadInfo(w.Id, w.Description))
_workloadsRepositoryEnumerator.Value.InstalledAndExtendedWorkloads.Select(w => new WorkloadInfo(w.Id, w.Description))
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -946,7 +946,7 @@ public static NetSdkMsiInstallerClient Create(
string tempDirPath = null,
RestoreActionConfig restoreActionConfig = null)
{
TimestampedFileLogger logger = new(Path.Combine(Path.GetTempPath(), $"Microsoft.NET.Workload_{DateTime.Now:yyyyMMdd_HHmmss}.log"));
TimestampedFileLogger logger = new(Path.Combine(Path.GetTempPath(), $"Microsoft.NET.Workload_{Environment.ProcessId}_{DateTime.Now:yyyyMMdd_HHmmss}.log"));
InstallClientElevationContext elevationContext = new(logger);

if (nugetPackageDownloader == null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using Xunit;
using System.Linq;
using System.Collections.Generic;
using System;

namespace Microsoft.DotNet.Cli.New.Tests
{
Expand Down Expand Up @@ -44,7 +45,7 @@ public void InstalledWorkloads_ShouldReturnExpectedWorkloads()
currentSdkVersion: "1.2.3",
workloadRecordRepo: repoMock.Object,
workloadResolver: resolverMock.Object);
IWorkloadsInfoProvider wp = new WorkloadsInfoProvider(workloadsEnumerator);
IWorkloadsInfoProvider wp = new WorkloadsInfoProvider(new Lazy<IWorkloadsRepositoryEnumerator>(workloadsEnumerator));

// Act
var workloads = wp.GetInstalledWorkloadsAsync(default).Result;
Expand Down