2
2
// The .NET Foundation licenses this file to you under the MIT license.
3
3
4
4
using System . CommandLine ;
5
+ using System . Diagnostics . CodeAnalysis ;
5
6
using Microsoft . Build . Evaluation ;
6
7
using Microsoft . DotNet . Cli . Commands . Run ;
7
8
using Microsoft . DotNet . Cli . Utils ;
@@ -17,17 +18,14 @@ internal sealed class ProjectConvertCommand(ParseResult parseResult) : CommandBa
17
18
18
19
public override int Execute ( )
19
20
{
21
+ // Check the entry point file path.
20
22
string file = Path . GetFullPath ( _file ) ;
21
23
if ( ! VirtualProjectBuildingCommand . IsValidEntryPointPath ( file ) )
22
24
{
23
25
throw new GracefulException ( CliCommandStrings . InvalidFilePath , file ) ;
24
26
}
25
27
26
- string targetDirectory = _outputDirectory ?? Path . ChangeExtension ( file , null ) ;
27
- if ( Directory . Exists ( targetDirectory ) )
28
- {
29
- throw new GracefulException ( CliCommandStrings . DirectoryAlreadyExists , targetDirectory ) ;
30
- }
28
+ string targetDirectory = DetermineOutputDirectory ( file ) ;
31
29
32
30
// Find directives (this can fail, so do this before creating the target directory).
33
31
var sourceFile = VirtualProjectBuildingCommand . LoadSourceFile ( file ) ;
@@ -36,28 +34,37 @@ public override int Execute()
36
34
// Find other items to copy over, e.g., default Content items like JSON files in Web apps.
37
35
var includeItems = FindIncludedItems ( ) . ToList ( ) ;
38
36
39
- Directory . CreateDirectory ( targetDirectory ) ;
37
+ bool dryRun = _parseResult . GetValue ( ProjectConvertCommandParser . DryRunOption ) ;
38
+
39
+ CreateDirectory ( targetDirectory ) ;
40
40
41
41
var targetFile = Path . Join ( targetDirectory , Path . GetFileName ( file ) ) ;
42
42
43
- // If there were any directives, remove them from the file.
44
- if ( directives . Length != 0 )
43
+ // Process the entry point file.
44
+ if ( dryRun )
45
45
{
46
- VirtualProjectBuildingCommand . RemoveDirectivesFromFile ( directives , sourceFile . Text , targetFile ) ;
47
- File . Delete ( file ) ;
46
+ Reporter . Output . WriteLine ( CliCommandStrings . ProjectConvertWouldCopyFile , file , targetFile ) ;
47
+ Reporter . Output . WriteLine ( CliCommandStrings . ProjectConvertWouldConvertFile , targetFile ) ;
48
48
}
49
49
else
50
50
{
51
- File . Move ( file , targetFile ) ;
51
+ VirtualProjectBuildingCommand . RemoveDirectivesFromFile ( directives , sourceFile . Text , targetFile ) ;
52
52
}
53
53
54
54
// Create project file.
55
55
string projectFile = Path . Join ( targetDirectory , Path . GetFileNameWithoutExtension ( file ) + ".csproj" ) ;
56
- using var stream = File . Open ( projectFile , FileMode . Create , FileAccess . Write ) ;
57
- using var writer = new StreamWriter ( stream , Encoding . UTF8 ) ;
58
- VirtualProjectBuildingCommand . WriteProjectFile ( writer , directives , isVirtualProject : false ) ;
56
+ if ( dryRun )
57
+ {
58
+ Reporter . Output . WriteLine ( CliCommandStrings . ProjectConvertWouldCreateFile , projectFile ) ;
59
+ }
60
+ else
61
+ {
62
+ using var stream = File . Open ( projectFile , FileMode . Create , FileAccess . Write ) ;
63
+ using var writer = new StreamWriter ( stream , Encoding . UTF8 ) ;
64
+ VirtualProjectBuildingCommand . WriteProjectFile ( writer , directives , isVirtualProject : false ) ;
65
+ }
59
66
60
- // Copy over included items.
67
+ // Copy or move over included items.
61
68
foreach ( var item in includeItems )
62
69
{
63
70
string targetItemFullPath = Path . Combine ( targetDirectory , item . RelativePath ) ;
@@ -69,12 +76,39 @@ public override int Execute()
69
76
}
70
77
71
78
string targetItemDirectory = Path . GetDirectoryName ( targetItemFullPath ) ! ;
72
- Directory . CreateDirectory ( targetItemDirectory ) ;
73
- File . Copy ( item . FullPath , targetItemFullPath ) ;
79
+ CreateDirectory ( targetItemDirectory ) ;
80
+ CopyFile ( item . FullPath , targetItemFullPath ) ;
74
81
}
75
82
76
83
return 0 ;
77
84
85
+ void CreateDirectory ( string path )
86
+ {
87
+ if ( dryRun )
88
+ {
89
+ if ( ! Directory . Exists ( path ) )
90
+ {
91
+ Reporter . Output . WriteLine ( CliCommandStrings . ProjectConvertWouldCreateDirectory , path ) ;
92
+ }
93
+ }
94
+ else
95
+ {
96
+ Directory . CreateDirectory ( path ) ;
97
+ }
98
+ }
99
+
100
+ void CopyFile ( string source , string target )
101
+ {
102
+ if ( dryRun )
103
+ {
104
+ Reporter . Output . WriteLine ( CliCommandStrings . ProjectConvertWouldCopyFile , source , target ) ;
105
+ }
106
+ else
107
+ {
108
+ File . Copy ( source , target ) ;
109
+ }
110
+ }
111
+
78
112
IEnumerable < ( string FullPath , string RelativePath ) > FindIncludedItems ( )
79
113
{
80
114
string entryPointFileDirectory = PathUtility . EnsureTrailingSlash ( Path . GetDirectoryName ( file ) ! ) ;
@@ -118,4 +152,42 @@ public override int Execute()
118
152
}
119
153
}
120
154
}
155
+
156
+ private string DetermineOutputDirectory ( string file )
157
+ {
158
+ string defaultValue = Path . ChangeExtension ( file , null ) ;
159
+ string defaultValueRelative = Path . GetRelativePath ( relativeTo : Environment . CurrentDirectory , defaultValue ) ;
160
+ string targetDirectory = _outputDirectory
161
+ ?? TryAskForOutputDirectory ( defaultValueRelative )
162
+ ?? defaultValue ;
163
+ if ( Directory . Exists ( targetDirectory ) )
164
+ {
165
+ throw new GracefulException ( CliCommandStrings . DirectoryAlreadyExists , targetDirectory ) ;
166
+ }
167
+
168
+ return targetDirectory ;
169
+ }
170
+
171
+ private string ? TryAskForOutputDirectory ( string defaultValueRelative )
172
+ {
173
+ return InteractiveConsole . Ask < string ? > (
174
+ string . Format ( CliCommandStrings . ProjectConvertAskForOutputDirectory , defaultValueRelative ) ,
175
+ _parseResult ,
176
+ ( path , out result , [ NotNullWhen ( returnValue : false ) ] out error) =>
177
+ {
178
+ if ( Directory . Exists ( path ) )
179
+ {
180
+ result = null ;
181
+ error = string . Format ( CliCommandStrings . DirectoryAlreadyExists , Path . GetFullPath ( path ) ) ;
182
+ return false ;
183
+ }
184
+
185
+ result = path is null ? null : Path . GetFullPath ( path ) ;
186
+ error = null ;
187
+ return true ;
188
+ } ,
189
+ out var result )
190
+ ? result
191
+ : null ;
192
+ }
121
193
}
0 commit comments