Skip to content

Commit

Permalink
issue #2428, issue #2405: add synchronization with Optional SDK Workl…
Browse files Browse the repository at this point in the history
…oad templates.

Call the mock SDK API from the package Microsoft.Internal.DotNet.MockTemplateLocator.
  • Loading branch information
genalt committed Aug 12, 2020
1 parent 633dff0 commit 5a50c36
Show file tree
Hide file tree
Showing 6 changed files with 127 additions and 3 deletions.
2 changes: 1 addition & 1 deletion build.cmd
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
@echo off
powershell -ExecutionPolicy ByPass -NoProfile -command "& """%~dp0eng\common\Build.ps1""" -build -restore -pack -test %*"
exit /b %ErrorLevel%
exit /b %ErrorLevel%
24 changes: 24 additions & 0 deletions src/Microsoft.TemplateEngine.Cli/HiveSynchronizationException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;

namespace Microsoft.TemplateEngine.Cli
{
public sealed class HiveSynchronizationException : Exception
{
public HiveSynchronizationException(string message, string version)
: base(message)
{
SdkVersion = version;
}

public HiveSynchronizationException(string message, string version, Exception innerException)
: base(message, innerException)
{
SdkVersion = version;
}

public string SdkVersion { get; }
}
}
24 changes: 22 additions & 2 deletions src/Microsoft.TemplateEngine.Cli/LocalizableStrings.Designer.cs

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

6 changes: 6 additions & 0 deletions src/Microsoft.TemplateEngine.Cli/LocalizableStrings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,12 @@
<data name="NoTemplatesMatchName" xml:space="preserve">
<value>No templates matched the input template name: {0}.</value>
</data>
<data name="OptionalWorkloadsSynchronized" xml:space="preserve">
<value>The list of templates was synchronized with the Optional SDK Workloads.</value>
</data>
<data name="OptionalWorkloadsSyncFailed" xml:space="preserve">
<value>Error during synchronization with the Optional SDK Workloads.</value>
</data>
<data name="NoUpdates" xml:space="preserve">
<value>No updates were found.</value>
</data>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

<ItemGroup>
<PackageReference Include="Microsoft.DotNet.Cli.CommandLine" />
<PackageReference Include="Microsoft.Internal.DotNet.MockTemplateLocator" Version="5.0.100-rc.1.20405.5" />
<PackageReference Include="System.Diagnostics.Process" />
<PackageReference Include="Newtonsoft.Json" />
</ItemGroup>
Expand Down
73 changes: 73 additions & 0 deletions src/Microsoft.TemplateEngine.Cli/New3Command.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.DotNet.TemplateLocator;
using Microsoft.TemplateEngine.Abstractions;
using Microsoft.TemplateEngine.Abstractions.Mount;
using Microsoft.TemplateEngine.Abstractions.TemplateUpdates;
Expand Down Expand Up @@ -447,6 +448,19 @@ private async Task<CreationResultStatus> ExecuteAsync()
return CreationResultStatus.Success;
}

try
{
bool isHiveUpdated = SyncOptionalWorkloads();
if (isHiveUpdated)
{
Reporter.Output.WriteLine(LocalizableStrings.OptionalWorkloadsSynchronized);
}
}
catch (HiveSynchronizationException hiex)
{
Reporter.Error.WriteLine(hiex.Message.Bold().Red());
}

bool forceCacheRebuild = _commandInput.HasDebuggingFlag("--debug:rebuildcache");
try
{
Expand Down Expand Up @@ -508,6 +522,65 @@ private bool ConfigureLocale()
return true;
}

private bool SyncOptionalWorkloads()
{
bool isHiveUpdated = false;
bool isCustomHive = _commandInput.HasDebuggingFlag("--debug:ephemeral-hive") || _commandInput.HasDebuggingFlag("--debug:custom-hive");

if (!isCustomHive)
{
string sdkVersion = EnvironmentSettings.Host.Version;

try
{
List<InstallationRequest> owInstallationRequests = new List<InstallationRequest>();
Dictionary<string, string> owInstalledPkgs = new Dictionary<string, string>(); // packageId -> packageVersion
TemplateLocator optionalWorkloadLocator = new TemplateLocator();

IReadOnlyCollection<IOptionalSdkTemplatePackageInfo> owPkgsToSync = optionalWorkloadLocator.GetDotnetSdkTemplatePackages(sdkVersion);

foreach (IInstallUnitDescriptor descriptor in _settingsLoader.InstallUnitDescriptorCache.Descriptors.Values)
{

if (descriptor.IsPartOfAnOptionalWorkload)
{
if (!descriptor.Details.TryGetValue("Version", out string pkgVersion))
{
pkgVersion = string.Empty;
}
owInstalledPkgs.Add(descriptor.Identifier, pkgVersion);
}
}

foreach (IOptionalSdkTemplatePackageInfo packageInfo in owPkgsToSync)
{
if (!owInstalledPkgs.TryGetValue(packageInfo.TemplatePackageId, out string version) ||
version != packageInfo.TemplateVersion)
{
isHiveUpdated = true;
owInstallationRequests.Add(new InstallationRequest(packageInfo.Path, isPartOfAnOptionalWorkload: true));
}
}

if (owInstallationRequests.Count != 0)
{
Installer.InstallPackages(owInstallationRequests);
}

/* TODO: add comparison of Optional SDK Workload templates from Hive and returned by TemplateLocator
* implement templates removal if they are not returned by API anymore
*/

}
catch (Exception ex)
{
throw new HiveSynchronizationException(LocalizableStrings.OptionalWorkloadsSyncFailed, sdkVersion, ex);
}
}

return isHiveUpdated;
}

private bool Initialize()
{
bool ephemeralHiveFlag = _commandInput.HasDebuggingFlag("--debug:ephemeral-hive");
Expand Down

0 comments on commit 5a50c36

Please sign in to comment.