Skip to content

Commit

Permalink
Scaffolding extension (#5723)
Browse files Browse the repository at this point in the history
Supports Azure/azure-sdk-for-net#47071 by
adding extensibility so we can generate csprojs in the azure plugin that
are different.

Partially fixes #4065
  • Loading branch information
m-nash authored Jan 23, 2025
1 parent 6d9761d commit cd0880d
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,7 @@ public async Task ExecuteAsync()
// Write project scaffolding files
if (CodeModelPlugin.Instance.IsNewProject)
{
var scaffolding = new NewProjectScaffolding();
await scaffolding.Execute();
await CodeModelPlugin.Instance.TypeFactory.CreateNewProjectScaffolding().Execute();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@

namespace Microsoft.Generator.CSharp.Primitives
{
//TODO Keeping internal for now until we define the extensibility points https://github.com/microsoft/typespec/issues/4065
internal class NewProjectScaffolding
//TODO Need to define the rest of the extensibility points https://github.com/microsoft/typespec/issues/4065
public class NewProjectScaffolding
{
public async Task<bool> Execute()
{
Expand All @@ -35,7 +35,7 @@ private async Task WriteProjectFiles()
{
await File.WriteAllBytesAsync(
Path.Combine(CodeModelPlugin.Instance.Configuration.ProjectDirectory, $"{CodeModelPlugin.Instance.Configuration.RootNamespace}.csproj"),
Encoding.UTF8.GetBytes(NormalizeLineEndings(GetSrcCSProj())));
Encoding.UTF8.GetBytes(NormalizeLineEndings(GetSourceProjectFileContent())));
}

private string NormalizeLineEndings(string content)
Expand All @@ -47,12 +47,12 @@ private async Task WriteSolutionFiles()
{
await File.WriteAllBytesAsync(
Path.Combine(CodeModelPlugin.Instance.Configuration.OutputDirectory, $"{CodeModelPlugin.Instance.Configuration.RootNamespace}.sln"),
Encoding.UTF8.GetBytes(NormalizeLineEndings(GetSln())));
Encoding.UTF8.GetBytes(NormalizeLineEndings(GetSolutionFileContent())));
}

private string GetSrcCSProj()
protected virtual string GetSourceProjectFileContent()
{
var builder = new CSProjWriter()
var builder = new CSharpProjectWriter()
{
Description = $"This is the {CodeModelPlugin.Instance.Configuration.RootNamespace} client library for developing .NET applications with rich experience.",
AssemblyTitle = $"SDK Code Generation {CodeModelPlugin.Instance.Configuration.RootNamespace}",
Expand All @@ -70,13 +70,13 @@ private string GetSrcCSProj()
return builder.Write();
}

private static readonly IReadOnlyList<CSProjWriter.CSProjDependencyPackage> _unbrandedDependencyPackages = new CSProjWriter.CSProjDependencyPackage[]
private static readonly IReadOnlyList<CSharpProjectWriter.CSProjDependencyPackage> _unbrandedDependencyPackages = new CSharpProjectWriter.CSProjDependencyPackage[]
{
new("System.ClientModel", "1.1.0-beta.4"),
new("System.Text.Json", "8.0.5")
};

private string GetSln()
protected virtual string GetSolutionFileContent()
{
string slnContent = @"Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,11 @@ protected virtual IReadOnlyList<TypeProvider> CreateSerializationsCore(InputType
return [];
}

public virtual NewProjectScaffolding CreateNewProjectScaffolding()
{
return new NewProjectScaffolding();
}

private readonly struct EnumCacheKey
{
public InputEnumType EnumType { get; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@

namespace Microsoft.Generator.CSharp;

internal class CSProjWriter
public class CSharpProjectWriter
{
public CSProjWriter()
public CSharpProjectWriter()
{
ProjectReferences = new List<CSProjDependencyPackage>();
PackageReferences = new List<CSProjDependencyPackage>();
Expand Down Expand Up @@ -137,7 +137,7 @@ public string Write()
return builder.ToString();
}

private static readonly IEnumerable<PropertyInfo> _properties = typeof(CSProjWriter).GetProperties(BindingFlags.Public | BindingFlags.Instance);
private static readonly IEnumerable<PropertyInfo> _properties = typeof(CSharpProjectWriter).GetProperties(BindingFlags.Public | BindingFlags.Instance);

private void WriteProperties(XmlWriter writer)
{
Expand Down

0 comments on commit cd0880d

Please sign in to comment.