This repository has been archived by the owner on Feb 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
140 lines (125 loc) · 4.1 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
using ConanSharpmake;
using System;
using System.Diagnostics;
using System.Text.RegularExpressions;
public static class Globals
{
// Namespace under which packages would be generated
public const string DefaultPackageNamespace = "ConanPackages";
}
internal class ConsoleParser
{
private Dictionary<string, string> mParsed = new Dictionary<string, string>();
public IDictionary<string, string> Parsed
{
get
{
return mParsed;
}
}
public ConsoleParser(string[] args)
{
foreach (string arg in args)
{
bool isArg = arg.StartsWith("--");
if (!isArg)
{
continue;
}
int equals = arg.IndexOf('=');
if (equals != -1)
{
string key = arg.Substring(2, equals - 2);
string val = arg.Substring(equals + 1, arg.Length - equals - 1);
mParsed[key] = val;
}
else
{
string key = arg.Substring(2, equals);
mParsed[key] = "";
}
}
}
public string GetOrDefault(string key, string def = "")
{
if (Parsed.ContainsKey(key))
{
return Parsed[key];
}
return def;
}
public bool GetOrDefault(string key, bool def)
{
if (Parsed.ContainsKey(key))
{
string lowerCase = Parsed[key].Trim();
if (lowerCase == "true")
{
return true;
}
if (lowerCase == "false")
{
return false;
}
bool parseSucceded = int.TryParse(lowerCase, out int outInt);
if (parseSucceded)
{
return outInt > 0;
}
}
return def;
}
}
internal class Program
{
private static void Main(string[] args)
{
var parser = new ConsoleParser(args);
if (parser.Parsed.Count == 0)
{
Console.WriteLine("ConanSharpmake - utility to generate Sharpmake projects from conanbuildinfo.json.");
Console.WriteLine("Syntax:");
Console.WriteLine(" ConanSharpmake --inputPath=<Path> --outputPath=<Path> [--namespace=<Namespace>]");
Console.WriteLine();
Console.WriteLine("Parameters:");
Console.WriteLine(" inputPath - directory where conanbuildinfo.json is located.");
Console.WriteLine(" outputPath - directory where *.sharpmake.cs files would be written.");
Console.WriteLine(" namespace - [optional] namespace under which packages would be generated (ConanPackages by default).");
Environment.Exit(-1);
return;
}
string inputPath = parser.GetOrDefault("inputPath", "");
if (string.IsNullOrWhiteSpace(inputPath))
{
throw new Exception("Parameter inputPath is not set.");
}
else
{
string absolutePath = Path.GetFullPath(inputPath);
if (!Directory.Exists(absolutePath))
{
throw new Exception("Parameter inputPath does not lead to a directory.");
}
}
string outputPath = parser.GetOrDefault("outputPath", "");
if (string.IsNullOrWhiteSpace(outputPath))
{
throw new Exception("Parameter outputPath is not set.");
}
string packageNamespace = parser.GetOrDefault("namespace", Globals.DefaultPackageNamespace);
if (parser.Parsed.ContainsKey("namespace"))
{
Regex namespaceMatcher = new Regex(@"[A-Za-z_][A-Za-z0-9_]*");
if (!namespaceMatcher.IsMatch(packageNamespace))
{
throw new Exception("Parameter namespace is invalid");
}
}
GeneratorParameters parameters = new GeneratorParameters();
parameters.WorkingDirectory = inputPath;
parameters.OutputDirectory = outputPath;
parameters.PackageNamespace = packageNamespace;
var generator = new Generator();
generator.Generate(parameters);
}
}