-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConvertCommand.cs
89 lines (73 loc) · 3.11 KB
/
ConvertCommand.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.IO;
using System.Xml.Linq;
using Microsoft.DotNet.ProjectModel;
using Microsoft.Extensions.Cli.Utils;
using Newtonsoft.Json;
namespace GuardRex.JsonToCsprojConverter
{
public class ConvertCommand
{
private readonly string _projectPath;
private readonly string _framework;
private readonly string _configuration;
public ConvertCommand(string framework, string configuration, string projectPath)
{
_projectPath = projectPath;
_framework = framework;
_configuration = configuration;
}
public int Run()
{
var applicationBasePath = GetApplicationBasePath();
var projectJsonPath = Path.Combine(applicationBasePath, "project2.json");
var projectContext = GetProjectContext(applicationBasePath, _framework);
var appName = projectContext.ProjectFile.GetCompilerOptions(projectContext.TargetFramework, _configuration).OutputName;
var appCsprojPath = Path.Combine(applicationBasePath, appName + ".csproj");
string projectJsonContent = string.Empty;
if (File.Exists(projectJsonPath))
{
try
{
Reporter.Output.WriteLine($"Reading project.json file at '{projectJsonPath}'");
projectJsonContent = File.ReadAllText(projectJsonPath);
XDocument doc = (XDocument)JsonConvert.DeserializeXNode(projectJsonContent);
using (var f = new FileStream(appCsprojPath, FileMode.Create))
{
doc.Save(f);
}
Reporter.Output.WriteLine("Conversion of project.json successful");
}
catch (System.Exception ex)
{
Reporter.Output.WriteLine($"Exception reading file: '{ex.Message}'");
}
}
else
{
Reporter.Output.WriteLine($"No project.json found. Skipping '{projectJsonPath}'");
}
return 0;
}
private string GetApplicationBasePath()
{
if (!string.IsNullOrEmpty(_projectPath))
{
var fullProjectPath = Path.GetFullPath(_projectPath);
return Path.GetFileName(fullProjectPath) == "project.json"
? Path.GetDirectoryName(fullProjectPath)
: fullProjectPath;
}
return Directory.GetCurrentDirectory();
}
private static ProjectContext GetProjectContext(string applicationBasePath, string framework)
{
var project = ProjectReader.GetProject(Path.Combine(applicationBasePath, "project.json"));
return new ProjectContextBuilder()
.WithProject(project)
.WithTargetFramework(framework)
.Build();
}
}
}