Skip to content

Commit 05a38c3

Browse files
authored
Improve Az.Tool.Predictor (#12922)
* Set up the dependencies on the PSReadline 2.1.0 * Initialize the env when the module is imported. - Add a script AzPredictor.ps1 to set the psreadline prediction view and source. - The script is run when the module is imported. * Fix a bug when loading settings from user profile. - The settings from user profile may not exist. In this case we should just ignore it and use the default one. * Fix issues in getting settings and send requests. - Fix the way to get the Azure PowerShell settings regarding collecting telemetry. - Avoid sending multiple requests while there is one running. * Use the NuGet pacakges from nuget.org. - Remove the local feed for those NuGet packages. * Rename namespace/assemblies/module name * Collect telemetry where we get the prediction. * Make some classes internal. * Add dependencies on Az module - This avoid the issue that our module loads Microsoft.Azure.PowerShell.Common dlls and Az tries to load it too. * Remove ps1 file that sets psreadline options. - We should instruct the users to set the options by themselves. * Remove psreadline module since it's not released. * Fix the merge issue
1 parent e940a50 commit 05a38c3

39 files changed

+328
-131
lines changed

tools/Az.Tools.AzPredictor/AzPredictor.Test/AzPredictor.Test.csproj renamed to tools/Az.Tools.Predictor/Az.Tools.Predictor.Test/Az.Tools.Predictor.Test.csproj

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,19 @@
22

33
<PropertyGroup>
44
<TargetFramework>net5.0</TargetFramework>
5-
<AssemblyName>Microsoft.Azure.PowerShell.AzPredictor.Test</AssemblyName>
6-
<RootNamespace>Microsoft.Azure.PowerShell.AzPredictor.Test</RootNamespace>
5+
<AssemblyName>Microsoft.Azure.PowerShell.Tools.AzPredictor.Test</AssemblyName>
6+
<RootNamespace>Microsoft.Azure.PowerShell.Tools.AzPredictor.Test</RootNamespace>
77
<IsPackable>false</IsPackable>
88
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
99
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
10-
<GenerateSerializationAssemblies>Auto</GenerateSerializationAssemblies>
10+
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
11+
<RepoArtifacts>$(MSBuildThisFileDirectory)</RepoArtifacts>
12+
<OutputPath>$(RepoArtifacts)..\..\..\artifacts\Tools\Az.Tools.Predictor.Test\</OutputPath>
13+
<DocumentationFile>$(OutputPath)\Microsoft.Azure.PowerShell.AzPredictor.Tools.Test.xml</DocumentationFile>
1114
</PropertyGroup>
1215

1316
<ItemGroup>
14-
<PackageReference Include="System.Management.Automation" Version="7.1.0-preview.9" />
17+
<PackageReference Include="System.Management.Automation" Version="7.1.0-preview.7" />
1518
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.6.1" />
1619
<PackageReference Include="xunit" Version="2.4.1" />
1720
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
@@ -22,7 +25,7 @@
2225
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
2326
<PrivateAssets>all</PrivateAssets>
2427
</PackageReference>
25-
<ProjectReference Include="..\AzPredictor\AzPredictor.csproj" />
28+
<ProjectReference Include="..\Az.Tools.Predictor\Az.Tools.Predictor.csproj" />
2629
</ItemGroup>
2730

2831
<ItemGroup>

tools/Az.Tools.AzPredictor/AzPredictor.Test/AzPredictorServiceTests.cs renamed to tools/Az.Tools.Predictor/Az.Tools.Predictor.Test/AzPredictorServiceTests.cs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@
1212
// limitations under the License.
1313
// ----------------------------------------------------------------------------------
1414

15-
using Microsoft.Azure.PowerShell.AzPredictor.Test.Mocks;
15+
using Microsoft.Azure.PowerShell.Tools.AzPredictor.Test.Mocks;
1616
using System.Management.Automation.Subsystem;
1717
using System.Threading;
1818
using Xunit;
1919

20-
namespace Microsoft.Azure.PowerShell.AzPredictor.Test
20+
namespace Microsoft.Azure.PowerShell.Tools.AzPredictor.Test
2121
{
2222
/// <summary>
2323
/// Tests for <see cref="AzPredictorService"/>
@@ -41,7 +41,7 @@ public AzPredictorServiceTests(ModelFixture fixture)
4141
this._suggestionsPredictor = new Predictor(this._fixture.PredictionCollection[startHistory]);
4242
this._commandsPredictor = new Predictor(this._fixture.CommandCollection);
4343

44-
this._service = new MockAzPredictorService(this._fixture.PredictionCollection[startHistory], this._fixture.CommandCollection);
44+
this._service = new MockAzPredictorService(startHistory, this._fixture.PredictionCollection[startHistory], this._fixture.CommandCollection);
4545
}
4646

4747

@@ -63,11 +63,12 @@ public void VerifyUsingSuggestion(string userInput)
6363
var predictionContext = PredictionContext.Create(userInput);
6464
var expected = this._suggestionsPredictor.Query(predictionContext.InputAst, CancellationToken.None);
6565
var actual = this._service.GetSuggestion(predictionContext.InputAst, CancellationToken.None);
66-
Assert.Equal(expected, actual);
6766
Assert.NotNull(actual);
67+
Assert.NotNull(actual.Item1);
68+
Assert.Equal(expected, actual.Item1);
69+
Assert.Equal(PredictionSource.CurrentHistory, actual.Item2);
6870
}
6971

70-
7172
/// <summary>
7273
/// Verifies that when no prediction is in the suggestion list, we'll use the command list.
7374
/// </summary>
@@ -79,8 +80,10 @@ public void VerifyUsingCommand(string userInput)
7980
var predictionContext = PredictionContext.Create(userInput);
8081
var expected = this._commandsPredictor.Query(predictionContext.InputAst, CancellationToken.None);
8182
var actual = this._service.GetSuggestion(predictionContext.InputAst, CancellationToken.None);
82-
Assert.Equal(expected, actual);
8383
Assert.NotNull(actual);
84+
Assert.NotNull(actual.Item1);
85+
Assert.Equal(expected, actual.Item1);
86+
Assert.Equal(PredictionSource.Commands, actual.Item2);
8487
}
8588

8689
/// <summary>
@@ -98,7 +101,8 @@ public void VerifyNoPrediction(string userInput)
98101
{
99102
var predictionContext = PredictionContext.Create(userInput);
100103
var actual = this._service.GetSuggestion(predictionContext.InputAst, CancellationToken.None);
101-
Assert.Null(actual);
104+
Assert.Null(actual.Item1);
105+
Assert.Equal(PredictionSource.None, actual.Item2);
102106
}
103107
}
104108
}

tools/Az.Tools.AzPredictor/AzPredictor.Test/AzPredictorTests.cs renamed to tools/Az.Tools.Predictor/Az.Tools.Predictor.Test/AzPredictorTests.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@
1212
// limitations under the License.
1313
// ----------------------------------------------------------------------------------
1414

15-
using Microsoft.Azure.PowerShell.AzPredictor.Test.Mocks;
15+
using Microsoft.Azure.PowerShell.Tools.AzPredictor.Test.Mocks;
1616
using System.Collections.Generic;
1717
using System.Linq;
1818
using System.Management.Automation.Subsystem;
1919
using System.Threading;
2020
using Xunit;
2121

22-
namespace Microsoft.Azure.PowerShell.AzPredictor.Test
22+
namespace Microsoft.Azure.PowerShell.Tools.AzPredictor.Test
2323
{
2424
/// <summary>
2525
/// Tests for <see cref="AzPredictor"/>
@@ -40,7 +40,7 @@ public AzPredictorTests(ModelFixture modelFixture)
4040
this._fixture = modelFixture;
4141
var startHistory = $"{AzPredictorConstants.CommandHistoryPlaceholder}{AzPredictorConstants.CommandConcatenator}{AzPredictorConstants.CommandHistoryPlaceholder}";
4242

43-
this._service = new MockAzPredictorService(this._fixture.PredictionCollection[startHistory], this._fixture.CommandCollection);
43+
this._service = new MockAzPredictorService(startHistory, this._fixture.PredictionCollection[startHistory], this._fixture.CommandCollection);
4444
this._telemetryClient = new MockAzPredictorTelemetryClient();
4545
this._azPredictor = new AzPredictor(this._service, this._telemetryClient);
4646
}
@@ -142,12 +142,12 @@ public void VerifySuggestion(string userInput)
142142
var actual = this._azPredictor.GetSuggestion(predictionContext, CancellationToken.None);
143143
if (actual == null)
144144
{
145-
Assert.Null(expected);
145+
Assert.Null(expected?.Item1);
146146
}
147147
else
148148
{
149149
Assert.Single(actual);
150-
Assert.Equal(expected, actual.First().SuggestionText);
150+
Assert.Equal(expected.Item1, actual.First().SuggestionText);
151151
}
152152

153153
}

tools/Az.Tools.AzPredictor/AzPredictor.Test/Mocks/MockAzPredictorService.cs renamed to tools/Az.Tools.Predictor/Az.Tools.Predictor.Test/Mocks/MockAzPredictorService.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@
1414

1515
using System.Collections.Generic;
1616

17-
namespace Microsoft.Azure.PowerShell.AzPredictor.Test.Mocks
17+
namespace Microsoft.Azure.PowerShell.Tools.AzPredictor.Test.Mocks
1818
{
1919
/// <summary>
2020
/// Mock <see cref="AzPredictorService"/> so that it doesn't do httpd request to get the commands and predictions.
2121
/// </summary>
22-
public sealed class MockAzPredictorService : AzPredictorService
22+
sealed class MockAzPredictorService : AzPredictorService
2323
{
2424
/// <summary>
2525
/// Gets or sets if a predictions is requested.
@@ -29,12 +29,14 @@ public sealed class MockAzPredictorService : AzPredictorService
2929
/// <summary>
3030
/// Constructs a new instance of <see cref="MockAzPredictorService"/>
3131
/// </summary>
32+
/// <param name="history">The history that the suggestion is for</param>
3233
/// <param name="suggestions">The suggestions collection</param>
3334
/// <param name="commands">The commands collection</param>
34-
public MockAzPredictorService(IList<string> suggestions, IList<string> commands)
35+
public MockAzPredictorService(string history, IList<string> suggestions, IList<string> commands)
3536
{
37+
SetHistory(history);
3638
SetCommandsPredictor(commands);
37-
SetSuggestionPredictor(suggestions);
39+
SetSuggestionPredictor(history, suggestions);
3840
}
3941

4042
/// <inheritdoc/>

tools/Az.Tools.AzPredictor/AzPredictor.Test/Mocks/MockAzPredictorTelemetryClient.cs renamed to tools/Az.Tools.Predictor/Az.Tools.Predictor.Test/Mocks/MockAzPredictorTelemetryClient.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
using System.Collections.Generic;
1616

17-
namespace Microsoft.Azure.PowerShell.AzPredictor.Test.Mocks
17+
namespace Microsoft.Azure.PowerShell.Tools.AzPredictor.Test.Mocks
1818
{
1919
sealed class MockAzPredictorTelemetryClient : ITelemetryClient
2020
{
@@ -46,5 +46,10 @@ public void OnSuggestionAccepted(string acceptedSuggestion)
4646
{
4747
++this.SuggestionAccepted;
4848
}
49+
50+
/// <inheritdoc/>
51+
public void OnGetSuggestion(PredictionSource predictionSource)
52+
{
53+
}
4954
}
5055
}

tools/Az.Tools.AzPredictor/AzPredictor.Test/ModelFixture.cs renamed to tools/Az.Tools.Predictor/Az.Tools.Predictor.Test/ModelFixture.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
using System.IO.Compression;
2020
using Xunit;
2121

22-
namespace Microsoft.Azure.PowerShell.AzPredictor.Test
22+
namespace Microsoft.Azure.PowerShell.Tools.AzPredictor.Test
2323
{
2424
/// <summary>
2525
/// The class to prepare model for all the tests.

tools/Az.Tools.AzPredictor/AzPredictor.Test/PredictorTests.cs renamed to tools/Az.Tools.Predictor/Az.Tools.Predictor.Test/PredictorTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
using System.Threading;
1717
using Xunit;
1818

19-
namespace Microsoft.Azure.PowerShell.AzPredictor.Test
19+
namespace Microsoft.Azure.PowerShell.Tools.AzPredictor.Test
2020
{
2121
/// <summary>
2222
/// Test cases for <see cref="Predictor" />

tools/Az.Tools.AzPredictor/AzPredictor.sln renamed to tools/Az.Tools.Predictor/Az.Tools.Predictor.sln

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ Microsoft Visual Studio Solution File, Format Version 12.00
33
# Visual Studio Version 16
44
VisualStudioVersion = 16.0.30426.262
55
MinimumVisualStudioVersion = 10.0.40219.1
6-
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AzPredictor", "AzPredictor\AzPredictor.csproj", "{E4A5F697-086C-4908-B90E-A31EE47ECF13}"
6+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Az.Tools.Predictor", "Az.Tools.Predictor\Az.Tools.Predictor.csproj", "{E4A5F697-086C-4908-B90E-A31EE47ECF13}"
77
EndProject
8-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AzPredictor.Test", "AzPredictor.Test\AzPredictor.Test.csproj", "{C7A3ED31-8F41-4643-ADCF-85DF032BD8AC}"
8+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Az.Tools.Predictor.Test", "Az.Tools.Predictor.Test\Az.Tools.Predictor.Test.csproj", "{C7A3ED31-8F41-4643-ADCF-85DF032BD8AC}"
99
EndProject
1010
Global
1111
GlobalSection(SolutionConfigurationPlatforms) = preSolution

tools/Az.Tools.AzPredictor/AzPredictor/AzPredictor.csproj renamed to tools/Az.Tools.Predictor/Az.Tools.Predictor/Az.Tools.Predictor.csproj

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
<PropertyGroup>
44
<TargetFramework>net5.0</TargetFramework>
5-
<AssemblyName>Microsoft.Azure.PowerShell.AzPredictor</AssemblyName>
5+
<AssemblyName>Microsoft.Azure.PowerShell.Tools.AzPredictor</AssemblyName>
6+
<RootNamespace>Microsoft.Azure.PowerShell.Tools.AzPredictor</RootNamespace>
67
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
7-
<RootNamespace>Microsoft.Azure.PowerShell.AzPredictor</RootNamespace>
88
<RepoArtifacts>$(MSBuildThisFileDirectory)</RepoArtifacts>
99
<OutputPath>$(RepoArtifacts)..\..\..\artifacts\Tools\Az.Tools.Predictor\</OutputPath>
1010
</PropertyGroup>
@@ -15,13 +15,21 @@
1515
</PropertyGroup>
1616

1717
<PropertyGroup>
18-
<DocumentationFile>$(OutputPath)\Microsoft.Azure.PowerShell.AzPredictor.xml</DocumentationFile>
18+
<PackageId>Az.Tools.Predictor</PackageId>
19+
<Version>0.1.0-preview</Version>
20+
<Author>Microsoft Corporation</Author>
21+
<Company>Microsoft Corporation</Company>
22+
<Description>Microsoft Azure PowerShell Predictor: Provide prediction while user types Azure PowerShell commands</Description>
23+
</PropertyGroup>
24+
25+
<PropertyGroup>
26+
<DocumentationFile>$(OutputPath)\Microsoft.Azure.PowerShell.Tools.AzPredictor.xml</DocumentationFile>
1927
<GenerateSerializationAssemblies>Auto</GenerateSerializationAssemblies>
2028
</PropertyGroup>
2129

2230
<ItemGroup>
2331
<PackageReference Include="Microsoft.ApplicationInsights" Version="2.14.0" />
24-
<PackageReference Include="System.Management.Automation" Version="7.1.0-preview.9" />
32+
<PackageReference Include="System.Management.Automation" Version="7.1.0-preview.7" />
2533
<PackageReference Include="Microsoft.Azure.PowerShell.Common" Version="1.3.23-preview" />
2634
<PackageReference Include="Microsoft.Azure.PowerShell.Authentication.Abstractions" Version="1.3.23-preview" />
2735
</ItemGroup>

tools/Az.Tools.AzPredictor/AzPredictor/Az.Tools.Predictor.psd1 renamed to tools/Az.Tools.Predictor/Az.Tools.Predictor/Az.Tools.Predictor.psd1

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Module manifest for module 'AzPredictor'
1+
# Module manifest for module 'Az.Tools.Predictor'
22
#
33
# Generated by: Microsoft Corporation
44
#
@@ -8,7 +8,7 @@
88
@{
99

1010
# Script module or binary module file associated with this manifest.
11-
#RootModule = './AzPredictor.psm1'
11+
RootModule = ''
1212

1313
# Version number of this module.
1414
ModuleVersion = '0.1.0'
@@ -29,15 +29,15 @@ CompanyName = 'Microsoft Corporation'
2929
Copyright = 'Microsoft Corporation. All rights reserved.'
3030

3131
# Description of the functionality provided by this module
32-
Description = 'Microsoft Azure PowerShell: Provide prediction while user typing Azure PowerShell commands'
32+
Description = 'Microsoft Azure PowerShell Predictor: Provide prediction while user types Azure PowerShell commands'
3333

3434
# Minimum version of the PowerShell engine required by this module
3535
PowerShellVersion = '7.1'
3636

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

40-
NestedModules = @("Microsoft.Azure.PowerShell.AzPredictor.dll")
40+
NestedModules = @("Microsoft.Azure.PowerShell.Tools.AzPredictor.dll")
4141

4242
# Format files (.ps1xml) to be loaded when importing this module
4343

@@ -62,7 +62,7 @@ PrivateData = @{
6262
ReleaseNotes = '* the first preview release'
6363

6464
# Prerelease string of this module
65-
# Prerelease = 'beta'
65+
Prerelease = 'beta1'
6666

6767
# Flag to indicate whether the module requires explicit user acceptance for install/update/save
6868
# RequireLicenseAcceptance = $false
@@ -77,10 +77,4 @@ PrivateData = @{
7777
# HelpInfo URI of this module
7878
# HelpInfoURI = ''
7979

80-
# Default prefix for commands exported from this module. Override the default prefix using Import-Module -Prefix.
81-
# DefaultCommandPrefix = ''
82-
83-
FunctionsToExport = 'PSConsoleHostPredictor'
84-
8580
}
86-

tools/Az.Tools.AzPredictor/AzPredictor/AzPredictor.cs renamed to tools/Az.Tools.Predictor/Az.Tools.Predictor/AzPredictor.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,13 @@
1818
using System.Management.Automation;
1919
using System.Management.Automation.Language;
2020
using System.Management.Automation.Subsystem;
21+
using System.Runtime.CompilerServices;
2122
using System.Text;
2223
using System.Threading;
2324

24-
namespace Microsoft.Azure.PowerShell.AzPredictor
25+
[assembly:InternalsVisibleTo("Microsoft.Azure.PowerShell.Tools.AzPredictor.Test")]
26+
27+
namespace Microsoft.Azure.PowerShell.Tools.AzPredictor
2528
{
2629
/// <summary>
2730
/// The implementation of a <see cref="ICommandPredictor"/> to provide suggestion in PSReadLine.
@@ -132,13 +135,15 @@ public List<PredictiveSuggestion> GetSuggestion(PredictionContext context, Cance
132135
var userInput = context.InputAst.Extent.Text;
133136
var result = _service.GetSuggestion(context.InputAst, cancellationToken);
134137

135-
if (result != null)
138+
if (result?.Item1 != null)
136139
{
137140
cancellationToken.ThrowIfCancellationRequested();
138-
var fullSuggestion = MergeStrings(userInput, result);
141+
var fullSuggestion = MergeStrings(userInput, result.Item1);
139142
return new List<PredictiveSuggestion>() { new PredictiveSuggestion(fullSuggestion) };
140143
}
141144

145+
this._telemetryClient.OnGetSuggestion(result?.Item2 ?? PredictionSource.None);
146+
142147
return null;
143148
}
144149

tools/Az.Tools.AzPredictor/AzPredictor/AzPredictorConstants.cs renamed to tools/Az.Tools.Predictor/Az.Tools.Predictor/AzPredictorConstants.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@
1212
// limitations under the License.
1313
// ----------------------------------------------------------------------------------
1414

15-
namespace Microsoft.Azure.PowerShell.AzPredictor
15+
namespace Microsoft.Azure.PowerShell.Tools.AzPredictor
1616
{
1717
/// <summary>
1818
/// The constants shared in the project.
1919
/// </summary>
20-
public static class AzPredictorConstants
20+
internal static class AzPredictorConstants
2121
{
2222
/// <summary>
2323
/// The value to use when the command isn't an Az command.

0 commit comments

Comments
 (0)