Skip to content

Commit 0a3ef55

Browse files
authored
Report the resolved path when writing scripts and document the base for --output (#39566)
Part of #32752
1 parent 7ebdd78 commit 0a3ef55

8 files changed

Lines changed: 48 additions & 16 deletions

File tree

‎src/dotnet-ef/Properties/Resources.Designer.cs‎

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/dotnet-ef/Properties/Resources.resx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@
352352
<value>Don't generate SQL transaction statements.</value>
353353
</data>
354354
<data name="OutputDescription" xml:space="preserve">
355-
<value>The file to write the result to.</value>
355+
<value>The file to write the result to. Relative paths are resolved from the current directory.</value>
356356
</data>
357357
<data name="OutputDirDescription" xml:space="preserve">
358358
<value>The directory to put files in. Paths are relative to the project or file-based app directory.</value>

‎src/ef/Commands/DbContextScriptCommand.cs‎

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,15 @@ protected override int Execute(string[] args)
2020
}
2121
else
2222
{
23-
var output = _output.Value()!;
24-
if (WorkingDir!.HasValue())
25-
{
26-
output = Path.Combine(WorkingDir.Value()!, output);
27-
}
23+
var output = ResolveOutputPath(_output.Value()!, WorkingDir!.Value());
2824

2925
var directory = Path.GetDirectoryName(output);
3026
if (!string.IsNullOrEmpty(directory))
3127
{
3228
Directory.CreateDirectory(directory);
3329
}
3430

35-
Reporter.WriteVerbose(Resources.WritingFile(_output.Value()));
31+
Reporter.WriteInformation(Resources.WritingFile(output));
3632
File.WriteAllText(output, sql, Encoding.UTF8);
3733
}
3834

‎src/ef/Commands/MigrationsScriptCommand.cs‎

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,15 @@ protected override int Execute(string[] args)
2626
}
2727
else
2828
{
29-
var output = _output.Value()!;
30-
if (WorkingDir!.HasValue())
31-
{
32-
output = Path.Combine(WorkingDir.Value()!, output);
33-
}
29+
var output = ResolveOutputPath(_output.Value()!, WorkingDir!.Value());
3430

3531
var directory = Path.GetDirectoryName(output);
3632
if (!string.IsNullOrEmpty(directory))
3733
{
3834
Directory.CreateDirectory(directory);
3935
}
4036

41-
Reporter.WriteVerbose(Resources.WritingFile(_output.Value()));
37+
Reporter.WriteInformation(Resources.WritingFile(output));
4238
File.WriteAllText(output, sql, Encoding.UTF8);
4339
}
4440

‎src/ef/Commands/ProjectCommandBase.cs‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ internal abstract class ProjectCommandBase : EFCommandBase
2525
protected CommandOption? StartupAssembly { get; private set; }
2626
protected CommandOption? StartupProject { get; private set; }
2727
protected CommandOption? WorkingDir { get; private set; }
28+
29+
// Relative --output paths are resolved against the working directory (the directory the tool was invoked from),
30+
// unlike --output-dir, which is resolved against the project directory.
31+
internal static string ResolveOutputPath(string output, string? workingDir)
32+
=> Path.GetFullPath(string.IsNullOrEmpty(workingDir) ? output : Path.Combine(workingDir, output));
2833
protected CommandOption? Framework { get; private set; }
2934
protected CommandOption? Configuration { get; private set; }
3035

‎src/ef/Properties/Resources.Designer.cs‎

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/ef/Properties/Resources.resx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@
346346
<value>Options: {options}</value>
347347
</data>
348348
<data name="OutputDescription" xml:space="preserve">
349-
<value>The file to write the result to.</value>
349+
<value>The file to write the result to. Relative paths are resolved from the current directory.</value>
350350
</data>
351351
<data name="OutputDirDescription" xml:space="preserve">
352352
<value>The directory to put files in. Paths are relative to the project or file-based app directory.</value>

‎test/ef.Tests/OutputPathTest.cs‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using Microsoft.EntityFrameworkCore.Tools.Commands;
5+
6+
namespace Microsoft.EntityFrameworkCore.Tools;
7+
8+
public class OutputPathTest
9+
{
10+
private static readonly string Root = Path.GetFullPath(Path.Combine(Path.GetTempPath(), "efroot"));
11+
12+
[Fact]
13+
public void Relative_output_is_resolved_from_the_working_directory()
14+
=> Assert.Equal(
15+
Path.Combine(Root, "src", "Startup", "Scripts", "Idempotent.sql"),
16+
ProjectCommandBase.ResolveOutputPath(Path.Combine("Scripts", "Idempotent.sql"), Path.Combine(Root, "src", "Startup")));
17+
18+
[Fact]
19+
public void Absolute_output_ignores_the_working_directory()
20+
{
21+
var output = Path.Combine(Root, "out", "script.sql");
22+
23+
Assert.Equal(output, ProjectCommandBase.ResolveOutputPath(output, Path.Combine(Root, "src", "Startup")));
24+
}
25+
26+
[Fact]
27+
public void Relative_output_without_working_directory_is_resolved_from_the_current_directory()
28+
=> Assert.Equal(Path.GetFullPath("script.sql"), ProjectCommandBase.ResolveOutputPath("script.sql", workingDir: null));
29+
30+
[Fact]
31+
public void Dot_segments_are_normalized()
32+
=> Assert.Equal(
33+
Path.Combine(Root, "src", "Migrations", "script.sql"),
34+
ProjectCommandBase.ResolveOutputPath(Path.Combine("..", "Migrations", "script.sql"), Path.Combine(Root, "src", "Startup")));
35+
}

0 commit comments

Comments
 (0)