Skip to content

Commit

Permalink
fix output paths for a bunch of scaffolders (#3072)
Browse files Browse the repository at this point in the history
* fix output paths for a bunch of scaffolders

* fixing mvc and api controller w/ ef output paths

* minor fix

* remove unused vars
  • Loading branch information
deepchoudhery authored Dec 3, 2024
1 parent 93a8e06 commit 06ced25
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 14 deletions.
11 changes: 11 additions & 0 deletions src/dotnet-scaffolding/dotnet-scaffold-aspnet/Common/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}
}
6 changes: 3 additions & 3 deletions src/dotnet-scaffolding/dotnet-scaffold-aspnet/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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")
Expand All @@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -54,13 +55,21 @@ public override Task<bool> 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<string>()
Expand All @@ -69,7 +78,7 @@ private bool InvokeDotnetNew(DotnetNewStepSettings stepSettings)
"--name",
stepSettings.Name,
"--output",
projectBasePath
outputDirectory
};

if (!string.IsNullOrEmpty(stepSettings.NamespaceName))
Expand Down Expand Up @@ -113,4 +122,11 @@ private bool InvokeDotnetNew(DotnetNewStepSettings stepSettings)
CommandName = CommandName
};
}

private readonly static Dictionary<string, string> OutputFolders = new()
{
{ Constants.DotnetCommands.RazorPageCommandName, Constants.DotnetCommands.RazorPageCommandOutput },
{ Constants.DotnetCommands.RazorComponentCommandName, Constants.DotnetCommands.RazorComponentCommandOutput },
{ Constants.DotnetCommands.ViewCommandName, Constants.DotnetCommands.ViewCommandOutput }
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,26 @@ public override Task<bool> 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<string>()
Expand All @@ -68,7 +84,7 @@ private bool InvokeDotnetNew(EmptyControllerStepSettings settings)
"--name",
settings.Name,
"--output",
projectBasePath,
outputDirectory,
"--namespace",
projectName,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ public override async Task<bool> 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)
Expand Down

0 comments on commit 06ced25

Please # to comment.