Skip to content

Commit 822b212

Browse files
authored
Reduce the dependencies on Azure powershell common. (#13113)
- That assembly and its dependencies causes a conflict if we load this module and Az.Accounts module. Though we can work around by sepcifying Az as a dependencies on this module, that'll cause long loading time. - The reason of loading that assembly is to get the setting about Azure PowerShell data collection. We can copy part of the code to load that file, so that we don't need that assembly as the dependency.
1 parent e182978 commit 822b212

File tree

6 files changed

+6
-77
lines changed

6 files changed

+6
-77
lines changed

tools/Az.Tools.Predictor/Az.Tools.Predictor/Az.Tools.Predictor.csproj

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,6 @@
3434
<ItemGroup>
3535
<PackageReference Include="Microsoft.ApplicationInsights" Version="2.14.0" />
3636
<PackageReference Include="System.Management.Automation" Version="7.1.0-preview.7" />
37-
<PackageReference Include="Microsoft.Azure.PowerShell.Common" Version="1.3.23-preview" />
38-
<PackageReference Include="Microsoft.Azure.PowerShell.Authentication.Abstractions" Version="1.3.23-preview" />
3937
</ItemGroup>
4038

4139
<ItemGroup>

tools/Az.Tools.Predictor/Az.Tools.Predictor/Az.Tools.Predictor.psd1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ Description = 'Microsoft Azure PowerShell Predictor: Provide prediction while us
3535
PowerShellVersion = '7.1'
3636

3737
# Modules that must be imported into the global environment prior to importing this module
38-
RequiredModules = @(@{ModuleName = 'Az'; ModuleVersion = '3.0.0'; })
38+
# RequiredModules = @()
3939

4040
NestedModules = @("Microsoft.Azure.PowerShell.Tools.AzPredictor.dll")
4141

tools/Az.Tools.Predictor/Az.Tools.Predictor/AzPredictorConstants.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ internal static class AzPredictorConstants
6767
/// <summary>
6868
/// The azure profile directory name.
6969
/// </summary>
70+
// See AzureDirectoryName in https://github.com/Azure/azure-powershell/blob/master/src/Accounts/Authentication/Properties/Resources.resx
7071
public const string AzureProfileDirectoryName = ".Azure";
7172
}
7273
}

tools/Az.Tools.Predictor/Az.Tools.Predictor/AzPredictorTelemetryClient.cs

Lines changed: 2 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@
1414

1515
using Microsoft.ApplicationInsights;
1616
using Microsoft.ApplicationInsights.Extensibility;
17-
using Microsoft.Azure.Commands.Common.Authentication;
18-
using Microsoft.Azure.Commands.Common.Authentication.Abstractions;
17+
using Microsoft.Azure.PowerShell.Tools.AzPredictor.Profile;
1918
using Newtonsoft.Json;
2019
using System;
2120
using System.Collections.Generic;
@@ -30,40 +29,6 @@ namespace Microsoft.Azure.PowerShell.Tools.AzPredictor
3029
/// </summary>
3130
sealed class AzPredictorTelemetryClient : ITelemetryClient
3231
{
33-
/// <summary>
34-
/// A simple session class that provides neccessary information to get the profile.
35-
/// </summary>
36-
private sealed class TelemetrySession : AzureSession
37-
{
38-
/// <summary>
39-
/// Constructs a new instance of <see cref="TelemetrySession" />
40-
/// </summary>
41-
public TelemetrySession()
42-
{
43-
DataStore = new DiskDataStore();
44-
ProfileDirectory = Path.Combine(
45-
Environment.GetFolderPath(Environment.SpecialFolder.UserProfile),
46-
AzPredictorConstants.AzureProfileDirectoryName);
47-
}
48-
49-
/// <inheritdoc/>
50-
public override TraceLevel AuthenticationLegacyTraceLevel
51-
{
52-
get => TraceLevel.Off;
53-
set { }
54-
}
55-
56-
/// <inheritdoc/>
57-
public override TraceListenerCollection AuthenticationTraceListeners => Trace.Listeners;
58-
59-
/// <inheritdoc/>
60-
public override SourceLevels AuthenticationTraceSourceLevel
61-
{
62-
get => SourceLevels.Off;
63-
set { }
64-
}
65-
}
66-
6732
private const string TelemetryEventPrefix = "Az.Tools.Predictor";
6833

6934
/// <inheritdoc/>
@@ -74,31 +39,6 @@ public override SourceLevels AuthenticationTraceSourceLevel
7439

7540
private readonly TelemetryClient _telemetryClient;
7641

77-
private object lockObject = new object();
78-
private AzurePSDataCollectionProfile _cachedProfile;
79-
80-
private AzurePSDataCollectionProfile DataCollectionProfile
81-
{
82-
get
83-
{
84-
if (_cachedProfile != null)
85-
{
86-
return _cachedProfile;
87-
}
88-
89-
lock (lockObject)
90-
{
91-
if (_cachedProfile == null)
92-
{
93-
var controller = DataCollectionController.Create(new TelemetrySession());
94-
_cachedProfile = controller.GetProfile(() => { });
95-
}
96-
97-
return _cachedProfile;
98-
}
99-
}
100-
}
101-
10242
/// <summary>
10343
/// Constructs a new instance of <see cref="AzPredictorTelemetryClient"/>
10444
/// </summary>
@@ -255,9 +195,7 @@ public void OnGetSuggestionError(Exception e)
255195
/// <returns>true if allowed</returns>
256196
private bool IsDataCollectionAllowed()
257197
{
258-
if (DataCollectionProfile != null &&
259-
DataCollectionProfile.EnableAzureDataCollection.HasValue &&
260-
DataCollectionProfile.EnableAzureDataCollection.Value)
198+
if (AzurePSDataCollectionProfile.Instance.EnableAzureDataCollection == true)
261199
{
262200
return true;
263201
}

tools/Az.Tools.Predictor/Az.Tools.Predictor/Predictor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public Predictor(IList<string> modelPredictions, ParameterValuePredictor paramet
4040
this._parameterValuePredictor = parameterValuePredictor;
4141
this._predictions = new List<Prediction>();
4242

43-
foreach (var predictionTextRaw in modelPredictions)
43+
foreach (var predictionTextRaw in modelPredictions ?? Enumerable.Empty<string>())
4444
{
4545
var predictionText = EscapePredictionText(predictionTextRaw);
4646
Ast ast = Parser.ParseInput(predictionText, out Token[] tokens, out _);

tools/Az.Tools.Predictor/build.proj

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,13 @@
2222

2323
<Target Name="Build">
2424
<PropertyGroup>
25-
<BuildAction Condition="'$(Configuration)' != 'Release'">build</BuildAction>
26-
<BuildAction Condition="'$(Configuration)' == 'Release'">publish</BuildAction>
25+
<BuildAction>build</BuildAction>
2726
</PropertyGroup>
2827
<Message Text="Build $(ModuleName)" />
2928
<MakeDir Directories="$(ArtifactFolder)/$(ModuleName)" />
3029
<Exec Command="dotnet --version" />
3130

3231
<Exec Command="dotnet $(BuildAction) $(ModuleSolutionFile) -c $(Configuration)" />
33-
34-
<ItemGroup>
35-
<PublishedFiles Include="$(ArtifactFolder)/$(ModuleName)/publish/**/*" />
36-
</ItemGroup>
37-
38-
<Move SourceFiles="@(PublishedFiles)" DestinationFiles="@(PublishedFiles->'$(ArtifactFolder)/$(ModuleName)/%(RecursiveDir)%(FileName)%(Extension)')" />
39-
<RemoveDir Directories="$(ArtifactFolder)/$(ModuleName)/publish" Condition="'$(Configuration)' == 'Release'" />
4032
</Target>
4133

4234
<Target Name="Test" DependsOnTargets="Build">

0 commit comments

Comments
 (0)