diff --git a/src/dotnet-scaffolding/dotnet-scaffold-aspnet/Common/Constants.cs b/src/dotnet-scaffolding/dotnet-scaffold-aspnet/Common/Constants.cs index c00fc0f1c..6372bf032 100644 --- a/src/dotnet-scaffolding/dotnet-scaffold-aspnet/Common/Constants.cs +++ b/src/dotnet-scaffolding/dotnet-scaffold-aspnet/Common/Constants.cs @@ -38,4 +38,15 @@ public class Identity public const string UserClassName = "ApplicationUser"; public const string DbContextName = "NewIdentityDbContext"; } + + public class DotnetCommands + { + public const string RazorPageCommandName = "page"; + public const string RazorPageCommandOutput = "Pages"; + public const string RazorComponentCommandName = "razorcomponent"; + public const string RazorComponentCommandOutput = "Components"; + public const string ViewCommandName = "view"; + public const string ViewCommandOutput = "Views"; + public const string ControllerCommandOutput = "Controllers"; + } } diff --git a/src/dotnet-scaffolding/dotnet-scaffold-aspnet/Program.cs b/src/dotnet-scaffolding/dotnet-scaffold-aspnet/Program.cs index 94ef9abea..a0dca6a2e 100644 --- a/src/dotnet-scaffolding/dotnet-scaffold-aspnet/Program.cs +++ b/src/dotnet-scaffolding/dotnet-scaffold-aspnet/Program.cs @@ -39,7 +39,7 @@ public static void Main(string[] args) var context = config.Context; step.ProjectPath = context.GetOptionResult(projectOption); step.FileName = context.GetOptionResult(fileNameOption); - step.CommandName = "razorcomponent"; + step.CommandName = Constants.DotnetCommands.RazorComponentCommandName; }); builder.AddScaffolder("razorview-empty") @@ -54,7 +54,7 @@ public static void Main(string[] args) var context = config.Context; step.ProjectPath = context.GetOptionResult(projectOption); step.FileName = context.GetOptionResult(fileNameOption); - step.CommandName = "view"; + step.CommandName = Constants.DotnetCommands.ViewCommandName; }); builder.AddScaffolder("razorpage-empty") @@ -70,7 +70,7 @@ public static void Main(string[] args) step.ProjectPath = context.GetOptionResult(projectOption); step.NamespaceName = Path.GetFileNameWithoutExtension(step.ProjectPath); step.FileName = context.GetOptionResult(fileNameOption); - step.CommandName = "page"; + step.CommandName = Constants.DotnetCommands.RazorPageCommandName; }); builder.AddScaffolder("apicontroller") diff --git a/src/dotnet-scaffolding/dotnet-scaffold-aspnet/ScaffoldSteps/DotnetNewScaffolderStep.cs b/src/dotnet-scaffolding/dotnet-scaffold-aspnet/ScaffoldSteps/DotnetNewScaffolderStep.cs index a2bd47ade..ceada6048 100644 --- a/src/dotnet-scaffolding/dotnet-scaffold-aspnet/ScaffoldSteps/DotnetNewScaffolderStep.cs +++ b/src/dotnet-scaffolding/dotnet-scaffold-aspnet/ScaffoldSteps/DotnetNewScaffolderStep.cs @@ -6,6 +6,7 @@ using Microsoft.DotNet.Scaffolding.Internal.CliHelpers; using Microsoft.DotNet.Scaffolding.Internal.Services; using Microsoft.DotNet.Scaffolding.Internal.Telemetry; +using Microsoft.DotNet.Tools.Scaffold.AspNet.Common; using Microsoft.DotNet.Tools.Scaffold.AspNet.ScaffoldSteps.Settings; using Microsoft.DotNet.Tools.Scaffold.AspNet.Telemetry; using Microsoft.Extensions.Logging; @@ -54,13 +55,21 @@ public override Task ExecuteAsync(ScaffolderContext context, CancellationT private bool InvokeDotnetNew(DotnetNewStepSettings stepSettings) { - var projectBasePath = Path.GetDirectoryName(stepSettings.Project); - //using the project name from the csproj path as namespace currently. - //to evaluate the correct namespace is a lot of overhead for a simple 'dotnet new' operation - //TODO maybe change this? + var outputDirectory = Path.GetDirectoryName(stepSettings.Project); + //invoking a command with a specific output folder, if not, use the default output folder (project root) + if (!string.IsNullOrEmpty(outputDirectory) && + OutputFolders.TryGetValue(stepSettings.CommandName, out var folderName)) + { + outputDirectory = Path.Combine(outputDirectory, folderName); + if (!_fileSystem.DirectoryExists(outputDirectory)) + { + _fileSystem.CreateDirectory(outputDirectory); + } + } + var projectName = Path.GetFileNameWithoutExtension(stepSettings.Project); - if (Directory.Exists(projectBasePath) && - !string.IsNullOrEmpty(projectBasePath)) + if (!string.IsNullOrEmpty(outputDirectory) && + _fileSystem.DirectoryExists(outputDirectory)) { //arguments for 'dotnet new {settings.CommandName}' var args = new List() @@ -69,7 +78,7 @@ private bool InvokeDotnetNew(DotnetNewStepSettings stepSettings) "--name", stepSettings.Name, "--output", - projectBasePath + outputDirectory }; if (!string.IsNullOrEmpty(stepSettings.NamespaceName)) @@ -113,4 +122,11 @@ private bool InvokeDotnetNew(DotnetNewStepSettings stepSettings) CommandName = CommandName }; } + + private readonly static Dictionary OutputFolders = new() + { + { Constants.DotnetCommands.RazorPageCommandName, Constants.DotnetCommands.RazorPageCommandOutput }, + { Constants.DotnetCommands.RazorComponentCommandName, Constants.DotnetCommands.RazorComponentCommandOutput }, + { Constants.DotnetCommands.ViewCommandName, Constants.DotnetCommands.ViewCommandOutput } + }; } diff --git a/src/dotnet-scaffolding/dotnet-scaffold-aspnet/ScaffoldSteps/EmptyControllerScaffolderStep.cs b/src/dotnet-scaffolding/dotnet-scaffold-aspnet/ScaffoldSteps/EmptyControllerScaffolderStep.cs index 0dd87aafe..69bd16e4a 100644 --- a/src/dotnet-scaffolding/dotnet-scaffold-aspnet/ScaffoldSteps/EmptyControllerScaffolderStep.cs +++ b/src/dotnet-scaffolding/dotnet-scaffold-aspnet/ScaffoldSteps/EmptyControllerScaffolderStep.cs @@ -56,10 +56,26 @@ public override Task ExecuteAsync(ScaffolderContext context, CancellationT private bool InvokeDotnetNew(EmptyControllerStepSettings settings) { - var projectBasePath = Path.GetDirectoryName(settings.Project); + var outputDirectory = Path.GetDirectoryName(settings.Project); + if (!string.IsNullOrEmpty(outputDirectory)) + { + outputDirectory = Path.Combine(outputDirectory, "Controllers"); + if (!_fileSystem.DirectoryExists(outputDirectory)) + { + _fileSystem.CreateDirectory(outputDirectory); + } + } + else + { + _logger.LogError("Failed to determine output path."); + return false; + } + var projectName = Path.GetFileNameWithoutExtension(settings.Project); var actionsParameter = settings.Actions ? "--actions" : string.Empty; - if (Directory.Exists(projectBasePath) && !string.IsNullOrEmpty(projectName)) + if (!string.IsNullOrEmpty(outputDirectory) && + _fileSystem.DirectoryExists(outputDirectory) && + !string.IsNullOrEmpty(projectName)) { //arguments for 'dotnet new {settings.CommandName}' var args = new List() @@ -68,7 +84,7 @@ private bool InvokeDotnetNew(EmptyControllerStepSettings settings) "--name", settings.Name, "--output", - projectBasePath, + outputDirectory, "--namespace", projectName, }; diff --git a/src/dotnet-scaffolding/dotnet-scaffold-aspnet/ScaffoldSteps/ValidateEfControllerStep.cs b/src/dotnet-scaffolding/dotnet-scaffold-aspnet/ScaffoldSteps/ValidateEfControllerStep.cs index 12997b735..d4b896877 100644 --- a/src/dotnet-scaffolding/dotnet-scaffold-aspnet/ScaffoldSteps/ValidateEfControllerStep.cs +++ b/src/dotnet-scaffolding/dotnet-scaffold-aspnet/ScaffoldSteps/ValidateEfControllerStep.cs @@ -209,7 +209,7 @@ public override async Task ExecuteAsync(ScaffolderContext context, Cancell ProjectInfo = projectInfo, ModelInfo = modelInfo, DbContextInfo = dbContextInfo, - ControllerOutputPath = projectDirectory + ControllerOutputPath = Path.Combine(projectDirectory, AspNetConstants.DotnetCommands.ControllerCommandOutput) }; if (scaffoldingModel.ProjectInfo is not null && scaffoldingModel.ProjectInfo.CodeService is not null)