-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathProgram.cs
329 lines (282 loc) · 11.9 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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
using System;
using System.Collections.Generic;
using System.IO;
using Iviz.Tools;
namespace Iviz.MsgsGen
{
static class Program
{
static void Main(string[] args)
{
try
{
DoMain(args);
}
catch (Exception e)
{
Console.Error.WriteLine("EE Fatal error: " + Logger.ExceptionToString(e));
}
}
static void DoMain(string[] args)
{
if (args.Length == 0)
{
PrintHelpMessage();
return;
}
if (args.Length == 1)
{
switch (args[0])
{
case "-h":
case "--help":
PrintHelpMessage();
return;
case "-r":
case "--regenerate":
RegenerateIvizMessages();
return;
}
}
if (args.Length < 4 || (args[0] != "-o" && args[0] != "--output"))
{
Console.Error.WriteLine("EE Invalid arguments. Use -h to show usage.");
return;
}
string fullOutputPath = Path.GetFullPath(args[1]);
switch (args[2])
{
case "-p" or "--package":
{
string package = args[3];
if (args.Length < 5)
{
Console.Error.WriteLine("EE Invalid arguments. Use -h to show usage.");
return;
}
switch (args[4])
{
case "-i" or "--input":
CreateIndividualMessages(args, fullOutputPath, package);
break;
case "-if" or "--input-folder":
CreateMessagesFromFolder(args, fullOutputPath);
break;
default:
Console.Error.WriteLine($"EE Unknown argument {args[4]}. Use -h to show usage.");
break;
}
break;
}
case "-i" or "--input" or "-if" or "--input-folder":
Console.Error.WriteLine($"EE Argument {args[2]} is out of order. Use -h to show usage.");
break;
default:
Console.Error.WriteLine($"EE Argument {args[2]} is unknown or out of order. Use -h to show usage.");
break;
}
}
static void PrintHelpMessage()
{
Console.WriteLine("** Welcome to Iviz.MsgsGen, an utility to generate C# code from ROS messages.");
Console.WriteLine("** Usage:");
Console.WriteLine(" dotnet Iviz.MsgsGen.dll -h");
Console.WriteLine(" Shows this text");
Console.WriteLine(" dotnet Iviz.MsgsGen.dll -o OutputFolder -p Package -i Files... ");
Console.WriteLine(
" Converts the given files (.msg, .srv, .action) into C# messages, and writes the result in OutputFolder");
Console.WriteLine(
" dotnet Iviz.MsgsGen.dll -o OutputFolder -p Package -if InputFolder [-p Package* -if InputFolder*]...");
Console.WriteLine(
" Searches all files in the input folders (.msg, .srv, .action), converts them into C# messages, and writes the result in OutputFolder");
Console.WriteLine(" dotnet Iviz.MsgsGen.dll -r");
Console.WriteLine(" Regenerates the default Iviz message files from the ros_msgs folder");
Console.WriteLine();
}
static void CreateMessagesFromFolder(string[] args, string fullOutputPath)
{
if (args.Length < 6)
{
Console.Error.WriteLine($"EE Expected folder path after -if or --input-folder.");
return;
}
var p = new PackageInfo();
for (int i = 3; i < args.Length; i += 4)
{
Console.WriteLine();
Console.WriteLine("** Processing package '" + args[i] + "' at " + args[i + 2]);
string package = args[i];
string inputPath = args[i + 2];
string fullInputPath = Path.GetFullPath(inputPath);
if (!Directory.Exists(fullInputPath))
{
throw new FileNotFoundException($"Input path '{fullOutputPath}' does not exist.");
}
p.AddAllInPackagePath(fullInputPath, package);
}
Directory.CreateDirectory(fullOutputPath);
p.ResolveAll();
if (p.Messages.Count == 0 && p.Services.Count == 0)
{
Console.WriteLine("** No files given. Nothing to do.");
return;
}
if (p.Messages.Count != 0)
{
Directory.CreateDirectory($"{fullOutputPath}/msg/");
}
if (p.Services.Count != 0)
{
Directory.CreateDirectory($"{fullOutputPath}/srv/");
}
var usedNames = new HashSet<string>();
foreach (ClassInfo classInfo in p.Messages.Values)
{
string text = classInfo.ToCsString();
string fileName = usedNames.Contains(classInfo.Name)
? classInfo.CsPackage + classInfo.Name
: classInfo.Name;
usedNames.Add(classInfo.Name);
File.WriteAllText($"{fullOutputPath}/msg/{fileName}.cs", text);
}
foreach (ServiceInfo classInfo in p.Services.Values)
{
string text = classInfo.ToCsString();
File.WriteAllText($"{fullOutputPath}/srv/{classInfo.Name}.cs", text);
}
Console.WriteLine("** Done!");
}
static void CreateIndividualMessages(string[] args, string fullOutputPath, string package)
{
var messageFullPaths = new List<string>();
var serviceFullPaths = new List<string>();
var actionFullPaths = new List<string>();
foreach (string path in args.Skip(5))
{
string absolutePath = Path.GetFullPath(path);
if (!File.Exists(absolutePath))
{
if (Directory.Exists(absolutePath))
{
Console.WriteLine($"EE File path '{absolutePath}' refers to a directory. " +
$"Did you mean to use '-if' instead of '-i'?");
return;
}
Console.WriteLine($"EE File path '{absolutePath}' does not exist.");
return;
}
Console.WriteLine(absolutePath);
string extension = Path.GetExtension(absolutePath);
switch (extension)
{
case ".msg":
messageFullPaths.Add(absolutePath);
break;
case ".srv":
serviceFullPaths.Add(absolutePath);
break;
case ".action":
actionFullPaths.Add(absolutePath);
break;
default:
Console.WriteLine($"EE File path '{absolutePath}' has an unknown extension.");
return;
}
}
if (messageFullPaths.Count == 0 && serviceFullPaths.Count == 0 && actionFullPaths.Count == 0)
{
Console.WriteLine("** No files given. Nothing to do.");
return;
}
Directory.CreateDirectory(fullOutputPath);
if (messageFullPaths.Count != 0)
{
Directory.CreateDirectory($"{fullOutputPath}/msg/");
}
if (serviceFullPaths.Count != 0)
{
Directory.CreateDirectory($"{fullOutputPath}/srv/");
}
foreach (string fullPath in messageFullPaths)
{
ClassInfo classInfo = new ClassInfo(package, fullPath);
string classCode = classInfo.ToCsString();
string destPath = $"{fullOutputPath}/msg/{classInfo.Name}.cs";
File.WriteAllText(destPath, classCode);
}
foreach (string fullPath in serviceFullPaths)
{
ServiceInfo serviceInfo = new ServiceInfo(package, fullPath);
string classCode = serviceInfo.ToCsString();
string destPath = $"{fullOutputPath}/srv/{serviceInfo.Name}.cs";
File.WriteAllText(destPath, classCode);
}
foreach (string fullPath in actionFullPaths)
{
var actionClasses = ActionGenerator.GenerateFor(package, fullPath);
if (actionClasses == null)
{
continue;
}
foreach (ClassInfo classInfo in actionClasses)
{
string classCode = classInfo.ToCsString();
string destPath = $"{fullOutputPath}/msg/{classInfo.Name}.cs";
File.WriteAllText(destPath, classCode);
}
}
Console.WriteLine("** Done!");
}
static void RegenerateIvizMessages()
{
string rosBasePath;
string ivizMsgPaths;
string debugRosBasePath = Path.GetFullPath("../../../../ros_msgs");
string debugIvizMsgPaths = Path.GetFullPath("../../../../iviz_msgs");
string releaseRosBasePath = Path.GetFullPath("../ros_msgs");
string releaseIvizMsgPaths = Path.GetFullPath("../iviz_msgs");
Console.WriteLine("** Starting iviz_msgs_gen...");
if (Directory.Exists(debugRosBasePath) && Directory.Exists(debugIvizMsgPaths))
{
// running from an IDE
rosBasePath = debugRosBasePath;
ivizMsgPaths = debugIvizMsgPaths;
}
else if (Directory.Exists(releaseRosBasePath) && Directory.Exists(releaseIvizMsgPaths))
{
// running from the Publish folder
rosBasePath = releaseRosBasePath;
ivizMsgPaths = releaseIvizMsgPaths;
}
else
{
Console.WriteLine("EE Failed to find the iviz_msgs and ros_msgs folders. " +
"They should be in the ../ folder from where you run this.");
return;
}
PackageInfo p = new PackageInfo();
string[] packages = Directory.GetDirectories(rosBasePath);
foreach (string packageDir in packages)
{
string package = Path.GetFileName(packageDir);
p.AddAllInPackagePath(packageDir, package);
}
p.ResolveAll();
foreach (ClassInfo classInfo in p.Messages.Values)
{
string dstPackageDir = $"{ivizMsgPaths}/{classInfo.RosPackage}/msg/";
Directory.CreateDirectory(dstPackageDir);
string text = classInfo.ToCsString();
File.WriteAllText($"{dstPackageDir}{classInfo.Name}.cs", text);
}
foreach (ServiceInfo classInfo in p.Services.Values)
{
string packageDir = $"{ivizMsgPaths}/{classInfo.RosPackage}/srv/";
Directory.CreateDirectory(packageDir);
string text = classInfo.ToCsString();
File.WriteAllText($"{packageDir}{classInfo.Name}.cs", text);
}
Console.WriteLine("** Done!");
}
}
}