Skip to content

Commit

Permalink
make the input library to lazy loaded and no longer swallow the excep…
Browse files Browse the repository at this point in the history
…tion (#3382)
  • Loading branch information
ArcturusZhang committed May 17, 2024
1 parent 546f65c commit c7a75ac
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,18 @@ public CodeModelPlugin(GeneratorContext context)
{
_instance = this;
Configuration = context.Configuration;
InputLibrary = new InputLibrary(Instance.Configuration.OutputDirectory);
_inputLibrary = new(() => new InputLibrary(Instance.Configuration.OutputDirectory));
}

private Lazy<InputLibrary> _inputLibrary;

// Extensibility points to be implemented by a plugin
public abstract ApiTypes ApiTypes { get; }
public abstract CodeWriterExtensionMethods CodeWriterExtensionMethods { get; }
public abstract TypeFactory TypeFactory { get; }
public abstract ExtensibleSnippets ExtensibleSnippets { get; }
public abstract OutputLibrary OutputLibrary { get; }
public InputLibrary InputLibrary { get; }
public InputLibrary InputLibrary => _inputLibrary.Value;
public virtual TypeProviderWriter GetWriter(CodeWriter writer, TypeProvider provider) => new(writer, provider);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,11 @@ public void LoadPlugin(string outputDirectory)
using DirectoryCatalog directoryCatalog = new(AppContext.BaseDirectory);
using (CompositionContainer container = new(directoryCatalog))
{
try
container.ComposeExportedValue(new GeneratorContext(Configuration.Load(outputDirectory)));
var plugin = container.GetExportedValue<CodeModelPlugin>();
if (plugin == null)
{
container.ComposeExportedValue(new GeneratorContext(Configuration.Load(outputDirectory)));
var plugin = container.GetExportedValue<CodeModelPlugin>();
if (plugin == null)
{
throw new InvalidOperationException($"Cannot find exported value in current directory {AppContext.BaseDirectory}.");
}
}
catch (Exception ex)
{
throw new InvalidOperationException($"Failed to load client model plugin from {AppContext.BaseDirectory}.", ex);
throw new InvalidOperationException($"Cannot find exported value in current directory {AppContext.BaseDirectory}.");
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,12 @@ private static async Task<int> Run(CommandLineOptions options, GeneratorRunner r
{
await runner.RunAsync(options);
}
catch (Exception ex)
catch (Exception e)
{
Console.Error.WriteLine($"Error: {ex.Message}: {ex.StackTrace}");
Console.Error.WriteLine(e.Message);
Console.Error.WriteLine(e.StackTrace);
return 1;
}

Console.Error.WriteLine("Shutting Down");
return 0;
}
}
Expand Down

0 comments on commit c7a75ac

Please sign in to comment.