|
3 | 3 |
|
4 | 4 | #nullable enable |
5 | 5 |
|
| 6 | +using System.Collections.Immutable; |
6 | 7 | using System.CommandLine; |
| 8 | +using System.IO; |
7 | 9 | using Microsoft.DotNet.Cli; |
8 | 10 | using Microsoft.DotNet.Cli.Utils; |
9 | 11 | using Microsoft.TemplateEngine.Cli.Commands; |
@@ -35,13 +37,29 @@ public override int Execute() |
35 | 37 | throw new GracefulException(LocalizableStrings.DirectoryAlreadyExists, targetDirectory); |
36 | 38 | } |
37 | 39 |
|
| 40 | + // Find directives (this can fail, so do this before creating the target directory). |
| 41 | + var sourceFile = VirtualProjectBuildingCommand.LoadSourceFile(file); |
| 42 | + var directives = VirtualProjectBuildingCommand.FindDirectives(sourceFile); |
| 43 | + |
38 | 44 | Directory.CreateDirectory(targetDirectory); |
39 | 45 |
|
40 | | - string projectFile = Path.Join(targetDirectory, Path.GetFileNameWithoutExtension(file) + ".csproj"); |
41 | | - string projectFileText = VirtualProjectBuildingCommand.GetNonVirtualProjectFileText(); |
42 | | - File.WriteAllText(path: projectFile, contents: projectFileText); |
| 46 | + var targetFile = Path.Join(targetDirectory, Path.GetFileName(file)); |
| 47 | + |
| 48 | + // If there were any directives, remove them from the file. |
| 49 | + if (directives.Length != 0) |
| 50 | + { |
| 51 | + VirtualProjectBuildingCommand.RemoveDirectivesFromFile(directives, sourceFile.Text, targetFile); |
| 52 | + File.Delete(file); |
| 53 | + } |
| 54 | + else |
| 55 | + { |
| 56 | + File.Move(file, targetFile); |
| 57 | + } |
43 | 58 |
|
44 | | - File.Move(file, Path.Join(targetDirectory, Path.GetFileName(file))); |
| 59 | + string projectFile = Path.Join(targetDirectory, Path.GetFileNameWithoutExtension(file) + ".csproj"); |
| 60 | + using var stream = File.Open(projectFile, FileMode.Create, FileAccess.Write); |
| 61 | + using var writer = new StreamWriter(stream, Encoding.UTF8); |
| 62 | + VirtualProjectBuildingCommand.WriteProjectFile(writer, directives); |
45 | 63 |
|
46 | 64 | return 0; |
47 | 65 | } |
|
0 commit comments