Skip to content

Commit

Permalink
Clear output path by default (microsoft#3616)
Browse files Browse the repository at this point in the history
Updates the generator to clear the generated output folder by default.

fixes: microsoft#3469
  • Loading branch information
jorgerangel-msft authored Jun 18, 2024
1 parent 41e8505 commit 8e38930
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Generator.CSharp.Providers;

namespace Microsoft.Generator.CSharp
{
internal sealed class CSharpGen
{
private const string ConfigurationFileName = "Configuration.json";
private const string CodeModelFileName = "tspCodeModel.json";
private const string GeneratedFolderName = "Generated";

private static readonly string[] _filesToKeep = [ConfigurationFileName, CodeModelFileName];

Expand All @@ -24,12 +24,13 @@ public async Task ExecuteAsync()
{
GeneratedCodeWorkspace.Initialize();
var outputPath = CodeModelPlugin.Instance.Configuration.OutputDirectory;
var generatedTestOutputPath = Path.Combine(outputPath, "..", "..", "tests", "Generated");
var generatedSourceOutputPath = ParseGeneratedSourceOutputPath(outputPath);
var generatedTestOutputPath = Path.Combine(outputPath, "..", "..", "tests", GeneratedFolderName);

GeneratedCodeWorkspace workspace = await GeneratedCodeWorkspace.Create();

var output = CodeModelPlugin.Instance.OutputLibrary;
Directory.CreateDirectory(Path.Combine(outputPath, "src", "Generated", "Models"));
Directory.CreateDirectory(Path.Combine(generatedSourceOutputPath, "Models"));
List<Task> generateFilesTasks = new();

foreach (var model in output.Models)
Expand Down Expand Up @@ -57,7 +58,7 @@ public async Task ExecuteAsync()
generateFilesTasks.Add(workspace.AddGeneratedFile(CodeModelPlugin.Instance.GetWriter(client).Write()));
}

Directory.CreateDirectory(Path.Combine(outputPath, "src", "Generated", "Internal"));
Directory.CreateDirectory(Path.Combine(generatedSourceOutputPath, "Internal"));
foreach (var type in output.Types)
{
generateFilesTasks.Add(workspace.AddGeneratedFile(CodeModelPlugin.Instance.GetWriter(type).Write()));
Expand All @@ -68,7 +69,7 @@ public async Task ExecuteAsync()

if (CodeModelPlugin.Instance.Configuration.ClearOutputFolder)
{
DeleteDirectory(outputPath, _filesToKeep);
DeleteDirectory(generatedSourceOutputPath, _filesToKeep);
DeleteDirectory(generatedTestOutputPath, _filesToKeep);
}

Expand All @@ -91,13 +92,15 @@ public async Task ExecuteAsync()
/// </summary>
/// <param name="outputPath">The output path.</param>
/// <returns>The parsed output path string.</returns>
internal static string ParseOutputPath(string outputPath)
internal static string ParseGeneratedSourceOutputPath(string outputPath)
{
if (!outputPath.EndsWith("src", StringComparison.Ordinal) && !outputPath.EndsWith("src/", StringComparison.Ordinal))
{
outputPath = Path.Combine(outputPath, "src");
}

outputPath = Path.Combine(outputPath, GeneratedFolderName);

return outputPath;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ internal static Configuration Load(string outputPath, string? json = null)
{ Options.UseModelNamespace, true },
{ Options.GenerateModelFactory, true },
{ Options.GenerateSampleProject, true },
{ Options.ClearOutputFolder, false },
{ Options.ClearOutputFolder, true },
{ Options.GenerateTestProject, false }
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@ namespace Microsoft.Generator.CSharp.Tests
public class CSharpGenTests
{
private readonly string _mocksFolder = "./Mocks";

// Validates that the output path is parsed correctly when provided
[Test]
public void TestGetOutputPath_OutputPathProvided()
{
var outputPath = "./outputDir";
var parsedOutputPath = CSharpGen.ParseOutputPath(outputPath).Replace("\\", "/");
var expectedPath = $"{outputPath}/src";
var parsedOutputPath = CSharpGen.ParseGeneratedSourceOutputPath(outputPath);
var expectedPath = Path.Combine(outputPath, "src", "Generated");
var areEqual = string.Equals(expectedPath, parsedOutputPath, StringComparison.OrdinalIgnoreCase);

Assert.IsTrue(areEqual);
Expand All @@ -35,8 +36,8 @@ public void TestGetOutputPath_OutputPathProvided()
public void TestGetConfigurationInputFilePath_DefaultPath()
{
var outputPath = "";
var parsedOutputPath = CSharpGen.ParseOutputPath(outputPath).Replace("\\", "/");
var expectedPath = $"src";
var parsedOutputPath = CSharpGen.ParseGeneratedSourceOutputPath(outputPath);
var expectedPath = Path.Combine("src", "Generated");
var areEqual = string.Equals(expectedPath, parsedOutputPath, StringComparison.OrdinalIgnoreCase);

Assert.IsTrue(areEqual);
Expand Down Expand Up @@ -84,15 +85,14 @@ public void TestCSharpGen_ValidPlugin()

private void TestOutputPathAppended(string outputPath, string expectedPath)
{
var srcPath = "/src";
var srcPath = "src";

outputPath += srcPath;
outputPath = Path.Combine(outputPath, srcPath);


var parsedOutputPath = CSharpGen.ParseOutputPath(outputPath);
var cleanedOutputPath = parsedOutputPath.Replace("\\", "/");
var parsedOutputPath = CSharpGen.ParseGeneratedSourceOutputPath(outputPath);

var areEqual = string.Equals(expectedPath, cleanedOutputPath, StringComparison.OrdinalIgnoreCase);
var areEqual = string.Equals(expectedPath, parsedOutputPath, StringComparison.OrdinalIgnoreCase);

Assert.IsTrue(areEqual);
}
Expand Down

0 comments on commit 8e38930

Please sign in to comment.