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

Refine the client-namespace-conflict diagnostic #6161

Merged
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
resolve comments
  • Loading branch information
ArcturusZhang committed Feb 28, 2025
commit 375498823d40e277352a9b75ded06c35a3f27c03
Original file line number Diff line number Diff line change
@@ -11,7 +11,7 @@ internal static class PluginInitializer
public static void Initialize()
{
PluginHandler pluginHandler = new PluginHandler();
pluginHandler.LoadPlugin(null!, new CommandLineOptions
pluginHandler.LoadPlugin(new CommandLineOptions
{
OutputDirectory = Path.Combine(Directory.GetParent(Assembly.GetExecutingAssembly().Location)!.FullName, "Projects", "Model"),
PluginName = "CodeModelPlugin"
Original file line number Diff line number Diff line change
@@ -47,7 +47,7 @@ public CodeModelPlugin(GeneratorContext context)
Configuration = context.Configuration;
_inputLibrary = new InputLibrary(Configuration.OutputDirectory);
TypeFactory = new TypeFactory();
Emitter = null!;
Emitter = new Emitter(Console.OpenStandardOutput());
}

// for mocking
@@ -60,7 +60,7 @@ protected CodeModelPlugin()
internal bool IsNewProject { get; set; }
private InputLibrary _inputLibrary;

public virtual Emitter Emitter { get; internal set; }
public virtual Emitter Emitter { get; }

// Extensibility points to be implemented by a plugin
public virtual TypeFactory TypeFactory { get; }
Original file line number Diff line number Diff line change
@@ -8,10 +8,10 @@ namespace Microsoft.TypeSpec.Generator
{
internal class GeneratorRunner
{
public async Task RunAsync(Emitter emitter, CommandLineOptions options)
public async Task RunAsync(CommandLineOptions options)
{
PluginHandler pluginHandler = new();
pluginHandler.LoadPlugin(emitter, options);
pluginHandler.LoadPlugin(options);

var csharpGen = new CSharpGen();
await csharpGen.ExecuteAsync();
Original file line number Diff line number Diff line change
@@ -5,24 +5,23 @@
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;
using Microsoft.TypeSpec.Generator.EmitterRpc;

namespace Microsoft.TypeSpec.Generator
{
internal class PluginHandler
{
public void LoadPlugin(Emitter emitter, CommandLineOptions options)
public void LoadPlugin(CommandLineOptions options)
{
using DirectoryCatalog directoryCatalog = new(AppContext.BaseDirectory);
using CompositionContainer container = new(directoryCatalog);

container.ComposeExportedValue(new GeneratorContext(Configuration.Load(options.OutputDirectory)));
container.ComposeParts(this);

SelectPlugin(emitter, options);
SelectPlugin(options);
}

internal void SelectPlugin(Emitter emitter, CommandLineOptions options)
internal void SelectPlugin(CommandLineOptions options)
{
bool loaded = false;
foreach (var plugin in Plugins!)
@@ -31,7 +30,6 @@ internal void SelectPlugin(Emitter emitter, CommandLineOptions options)
{
CodeModelPlugin.Instance = plugin.Value;
CodeModelPlugin.Instance.IsNewProject = options.IsNewProject;
CodeModelPlugin.Instance.Emitter = emitter;
loaded = true;
CodeModelPlugin.Instance.Configure();
break;
Original file line number Diff line number Diff line change
@@ -31,17 +31,15 @@ public static async Task<int> Main(string[] args)

private static async Task<int> Run(CommandLineOptions options, GeneratorRunner runner)
{
using var emitter = new Emitter(Console.OpenStandardOutput());

if (options.ShouldDebug)
{
emitter.Debug("Attempting to attach debugger..");
Console.Error.WriteLine("Attempting to attach debugger..");
Debugger.Launch();
}

try
{
await runner.RunAsync(emitter, options);
await runner.RunAsync(options);
}
catch (Exception e)
{
@@ -50,6 +48,8 @@ private static async Task<int> Run(CommandLineOptions options, GeneratorRunner r
return 1;
}

(CodeModelPlugin.Instance.Emitter as IDisposable)?.Dispose();

return 0;
}
}
Original file line number Diff line number Diff line change
@@ -23,7 +23,7 @@ public void SelectPluginFindsMatchingPlugin()
};
CommandLineOptions options = new() { PluginName = "MockPlugin" };

Assert.DoesNotThrow(() => pluginHandler.SelectPlugin(null!, options));
Assert.DoesNotThrow(() => pluginHandler.SelectPlugin(options));
mockPlugin.Verify(p => p.Configure(), Times.Once);
}

@@ -41,7 +41,7 @@ public void SelectPluginThrowsWhenNoMatch()
};
CommandLineOptions options = new() { PluginName = "NonExistentPlugin" };

Assert.Throws<System.InvalidOperationException>(() => pluginHandler.SelectPlugin(null!, options));
Assert.Throws<System.InvalidOperationException>(() => pluginHandler.SelectPlugin(options));
mockPlugin.Verify(p => p.Configure(), Times.Never);
}
}
Loading