Skip to content

Use dependency injection in AzDev #27457

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

Closed
wants to merge 1 commit into from
Closed
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
2 changes: 2 additions & 0 deletions tools/AzDev/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ This module is designed to help developers of Azure PowerShell modules. It provi

All the cmdlets in this module are prefixed with `Dev-` to avoid conflicts with other modules.

Like many other tools, this module targets `net8.0` so always run it in PowerShell 7.2 or later.

- [Quick start](#quick-start)
- [Features](#features)
- [Repo inventory](#repo-inventory)
Expand Down
2 changes: 1 addition & 1 deletion tools/AzDev/Tests/Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.4" />
<PackageReference Include="TestableIO.System.IO.Abstractions.TestingHelpers" Version="22.0.11" />
<PackageReference Include="TestableIO.System.IO.Abstractions.TestingHelpers" Version="22.0.12" />
</ItemGroup>

<ItemGroup>
Expand Down
16 changes: 10 additions & 6 deletions tools/AzDev/src/AzDev.csproj
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<OutputType>library</OutputType>
<TargetFramework>net8.0</TargetFramework>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<WarningsAsErrors />
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="9.0.3" />
<PackageReference Include="PowerShellStandard.Library" Version="5.1.1" />
<PackageReference Include="System.Text.Json" Version="8.0.5" />
<PackageReference Include="TestableIO.System.IO.Abstractions.Wrappers" Version="21.0.22" />
<PackageReference Include="YamlDotNet" Version="16.1.0" />
<PackageReference Include="System.Text.Json" Version="9.0.3" />
<PackageReference Include="TestableIO.System.IO.Abstractions.Wrappers" Version="22.0.12" />
<PackageReference Include="YamlDotNet" Version="16.3.0" />
</ItemGroup>

</Project>
14 changes: 5 additions & 9 deletions tools/AzDev/src/Cmdlets/DevCmdletBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,32 +60,28 @@ internal Codebase Codebase
/// <summary>
/// Gets the context provider. Use <see cref="Context"/> property to get the context.
/// </summary>
internal IContextProvider ContextProvider => AzDevModule.GetComponent<IContextProvider>(nameof(IContextProvider));
internal IContextProvider ContextProvider => AzDevModule.GetService<IContextProvider>();

/// <summary>
/// Gets the codebase provider. Use <see cref="Codebase"/> property to get the codebase.
/// </summary>
internal ICodebaseProvider CodebaseProvider => AzDevModule.GetComponent<ICodebaseProvider>(nameof(ICodebaseProvider));
internal ICodebaseProvider CodebaseProvider => AzDevModule.GetService<ICodebaseProvider>();

public DevCmdletBase()
{
}

public void OnImport()
{
var contextProvider = new DefaultContextProvider(Constants.DevContextFilePath);
var codebaseProvider = new DefaultCodebaseProvider(contextProvider);

AzDevModule.SetComponent<IContextProvider>(nameof(IContextProvider), contextProvider);
AzDevModule.SetComponent<ICodebaseProvider>(nameof(ICodebaseProvider), codebaseProvider);
AzDevModule.Initialize();
}

protected override void BeginProcessing()
{
base.BeginProcessing();
ILogger logger = new PSCmdletLogger(this);
AzDevModule.GetComponent<IContextProvider>(nameof(IContextProvider)).SetLogger(logger);
AzDevModule.GetComponent<ICodebaseProvider>(nameof(ICodebaseProvider)).SetLogger(logger);
ContextProvider.SetLogger(logger);
CodebaseProvider.SetLogger(logger);
}

protected T SelectFrom<T>(string message, IEnumerable<T> options, bool retryIfInvalid = true)
Expand Down
28 changes: 11 additions & 17 deletions tools/AzDev/src/Services/AzDevModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,37 +13,31 @@
// ----------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using AzDev.Models;
using Microsoft.Extensions.DependencyInjection;

[assembly: InternalsVisibleTo("Tests")]

namespace AzDev.Services
{
internal static class AzDevModule
{
private static readonly IDictionary<string, object> Components;
private static IServiceProvider _serviceProvider;

static AzDevModule()
public static void Initialize()
{
Components = new Dictionary<string, object>();
}
var services = new ServiceCollection();

public static T GetComponent<T>(string key)
{
if (Components[key] is T t)
{
return t;
}
else
{
throw new ArgumentException($"Mismatching type. Expect [{typeof(T)}]. Got [{Components[key].GetType()}].");
}
services.AddSingleton<IContextProvider, DefaultContextProvider>(sp => new DefaultContextProvider(Constants.DevContextFilePath));
services.AddSingleton<ICodebaseProvider, DefaultCodebaseProvider>(sp => new DefaultCodebaseProvider(sp.GetRequiredService<IContextProvider>()));

_serviceProvider = services.BuildServiceProvider();
}

public static void SetComponent<T>(string key, T value)
public static T GetService<T>() where T : class
{
Components[key] = value;
return _serviceProvider.GetService<T>();
}
}
}
2 changes: 1 addition & 1 deletion tools/AzDev/src/Services/DefaultContextProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ namespace AzDev.Services
{
internal class DefaultContextProvider : IContextProvider
{
private ILogger _logger;
private ILogger _logger = new NoopLogger();
private readonly string _contextFilePath;
private IFileSystem _fileSystem;
private DevContext _cachedContext;
Expand Down