-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathProgram.cs
180 lines (156 loc) · 5.86 KB
/
Program.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
using System;
using System.IO;
using Newtonsoft.Json.Linq;
namespace FixProjectJsonWarnings
{
class Program
{
private static readonly string[] _packOptionProps = new[] { "repository", "tags", "licenseUrl", "projectUrl", };
static int Main(string[] args)
{
if (args.Length != 1)
{
Console.WriteLine("Usage FixProjectJsonWarnings [repo-root]");
return 1;
}
var dir = args[0];
foreach (var file in Directory.EnumerateFiles(dir, "project.json", SearchOption.AllDirectories))
{
var content = File.ReadAllText(file);
var replaced = ModifyProject(content);
if (replaced != content)
{
File.WriteAllText(file, replaced, System.Text.Encoding.UTF8);
}
}
return 0;
}
private static string ModifyProject(string content)
{
var root = JObject.Parse(content);
RenameCompilationOptions(root);
foreach (var item in _packOptionProps)
{
MoveToPackOptions(root, item);
}
MovePackIncludeToPackOptions(root);
MoveContent(root);
MoveResourcesToBuild(root);
MoveCompileToBuildOptions(root);
return root.ToString();
}
private static void MovePackIncludeToPackOptions(JObject root)
{
var packInclude = root.Property("packInclude");
if (packInclude != null)
{
var packOptions = GetOrAddProperty(root, "packOptions", packInclude);
var files = GetOrAddProperty(packOptions, "files", null);
files.Add(new JProperty("mappings", packInclude.Value));
packInclude.Remove();
}
}
private static void MoveResourcesToBuild(JObject root)
{
var resourceItems = new[]
{
Tuple.Create("resource", "include"),
Tuple.Create("namedResource", "mappings"),
};
foreach (var item in resourceItems)
{
var property = root.Property(item.Item1);
if (property != null)
{
var build = GetOrAddProperty(root, "buildOptions", property);
var resource = (JObject)build["embed"];
if (resource == null)
{
resource = new JObject();
build["embed"] = resource;
}
resource[item.Item2] = property.Value;
property.Remove();
}
}
}
private static void MoveContent(JObject root)
{
var contentItems = new[]
{
Tuple.Create("content", "include"),
Tuple.Create("contentExclude", "exclude"),
Tuple.Create("contentFiles", "includeFiles")
};
foreach (var item in contentItems)
{
var property = root.Property(item.Item1);
if (property != null)
{
var publishInclude = GetOrAddProperty(root, "publishOptions", property);
publishInclude[item.Item2] = property.Value;
var buildOptions = GetOrAddProperty(root, "buildOptions", property);
var copyToOutput = GetOrAddProperty(buildOptions, "copyToOutput", null);
copyToOutput[item.Item2] = property.Value;
property.Remove();
}
}
}
private static void MoveToPackOptions(JObject root, string item)
{
var property = root.Property(item);
if (property != null)
{
JObject packOptions = GetOrAddProperty(root, "packOptions", property);
property.Remove();
packOptions.Add(property);
}
}
private static JObject GetOrAddProperty(JObject root, string propertyName, JProperty insertAfter)
{
var newProperty = (JObject)root[propertyName];
if (newProperty == null)
{
newProperty = new JObject();
var property = new JProperty(propertyName, newProperty);
if (insertAfter != null)
{
insertAfter.AddAfterSelf(property);
}
else
{
root.Add(property);
}
}
return newProperty;
}
private static void RenameCompilationOptions(JObject root)
{
var compilationOptions = root.Property("compilationOptions");
if (compilationOptions != null)
{
compilationOptions.Replace(new JProperty("buildOptions", compilationOptions.Value));
}
foreach (var item in ((JObject)root["frameworks"]).Properties())
{
var tfmCompilationOptions = ((JObject)item.Value).Property("compilationOptions");
if (tfmCompilationOptions != null)
{
tfmCompilationOptions.Replace(new JProperty("buildOptions", tfmCompilationOptions.Value));
}
}
}
private static void MoveCompileToBuildOptions(JObject root)
{
var compile = root.Property("compile");
if (compile == null)
{
return;
}
var buildOptions = GetOrAddProperty(root, "buildOptions", compile);
var buildCompile = GetOrAddProperty(buildOptions, "compile", null);
buildCompile["include"] = compile.Value;
compile.Remove();
}
}
}