forked from dotnet/sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProjectConvertCommand.cs
48 lines (37 loc) · 1.67 KB
/
ProjectConvertCommand.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
#nullable enable
using System.CommandLine;
using Microsoft.DotNet.Cli;
using Microsoft.DotNet.Cli.Utils;
using Microsoft.TemplateEngine.Cli.Commands;
namespace Microsoft.DotNet.Tools.Project.Convert;
internal sealed class ProjectConvertCommand : CommandBase
{
private readonly string _file;
private readonly string? _outputDirectory;
public ProjectConvertCommand(ParseResult parseResult) : base(parseResult)
{
_file = parseResult.GetValue(ProjectConvertCommandParser.FileArgument) ?? string.Empty;
_outputDirectory = parseResult.GetValue(SharedOptions.OutputOption)?.FullName;
}
public override int Execute()
{
string file = Path.GetFullPath(_file);
if (!VirtualProjectBuildingCommand.IsValidEntryPointPath(file))
{
throw new GracefulException(LocalizableStrings.InvalidFilePath, file);
}
string targetDirectory = _outputDirectory ?? Path.ChangeExtension(file, null);
if (Directory.Exists(targetDirectory))
{
throw new GracefulException(LocalizableStrings.DirectoryAlreadyExists, targetDirectory);
}
Directory.CreateDirectory(targetDirectory);
string projectFile = Path.Join(targetDirectory, Path.GetFileNameWithoutExtension(file) + ".csproj");
string projectFileText = VirtualProjectBuildingCommand.GetNonVirtualProjectFileText();
File.WriteAllText(path: projectFile, contents: projectFileText);
File.Move(file, Path.Join(targetDirectory, Path.GetFileName(file)));
return 0;
}
}