forked from yanghuan/CSharp.lua
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
136 lines (122 loc) · 5.19 KB
/
Copy pathProgram.cs
File metadata and controls
136 lines (122 loc) · 5.19 KB
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
/*
Copyright 2017 YANG Huan (sy.yanghuan@gmail.com).
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.Contracts;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CSharpLua {
class Program {
private const string kHelpCmdString = @"Usage: CSharp.lua [-s srcfolder] [-d dstfolder]
Arguments
-s : can be a directory where all cs files will be compiled, or a list of files, using ';' or ',' to separate
-d : destination directory, will put the out lua files
Options
-h : show the help message and exit
-l : libraries referenced, use ';' to separate
if the library is a module, which is compiled by CSharp.lua with -module argument, the last character needs to be '!' in order to mark
-m : meta files, like System.xml, use ';' to separate
-csc : csc.exe command arguments, use ' ' or '\t' to separate
-c : support classic lua version(5.1), default support 5.3
-a : attributes need to export, use ';' to separate, if ""-a"" only, all attributes will be exported
-e : enums need to export, use ';' to separate, if ""-e"" only, all enums will be exported
-p : do not use debug.setmetatable, in some Addon/Plugin environment debug object cannot be used
-metadata : export all metadata, use @CSharpLua.Metadata annotations for precise control
-module : the currently compiled assembly needs to be referenced, it's useful for multiple module compiled
-inline-property: inline some single-line properties
";
public static void Main(string[] args) {
if (args.Length > 0) {
try {
var cmds = Utility.GetCommandLines(args);
if (cmds.ContainsKey("-h")) {
ShowHelpInfo();
return;
}
var sw = new Stopwatch();
sw.Start();
string input = cmds.GetArgument("-s");
string output = cmds.GetArgument("-d");
string lib = cmds.GetArgument("-l", true);
string meta = cmds.GetArgument("-m", true);
bool isClassic = cmds.ContainsKey("-c");
string atts = cmds.GetArgument("-a", true);
if (atts == null && cmds.ContainsKey("-a")) {
atts = string.Empty;
}
string enums = cmds.GetArgument("-e", true);
if (enums == null && cmds.ContainsKey("-e")) {
enums = string.Empty;
}
string csc = GetCSCArgument(args);
bool isPreventDebugObject = cmds.ContainsKey("-p");
bool isExportMetadata = cmds.ContainsKey("-metadata");
bool isModule = cmds.ContainsKey("-module");
bool isInlineSimpleProperty = cmds.ContainsKey("-inline-property");
Compiler c = new Compiler(input, output, lib, meta, csc, isClassic, atts, enums) {
IsExportMetadata = isExportMetadata,
IsModule = isModule,
IsInlineSimpleProperty = isInlineSimpleProperty,
IsPreventDebugObject = isPreventDebugObject,
};
c.Compile();
Console.WriteLine($"Compiled Success, cost {sw.Elapsed.TotalSeconds}s");
} catch (CmdArgumentException e) {
Console.Error.WriteLine(e.Message);
ShowHelpInfo();
Environment.ExitCode = -1;
} catch (CompilationErrorException e) {
Console.Error.WriteLine(e.Message);
Environment.ExitCode = -1;
} catch (Exception e) {
Console.Error.WriteLine(e.ToString());
Environment.ExitCode = -1;
}
} else {
ShowHelpInfo();
Environment.ExitCode = -1;
}
}
private static void ShowHelpInfo() {
Console.Error.WriteLine(kHelpCmdString);
}
private static HashSet<string> arguments_;
private static bool IsArgumentKey(string key) {
if (arguments_ == null) {
arguments_ = new HashSet<string>();
string[] lines = kHelpCmdString.Split(new string[] { "\r\n", "\n", "\r" }, StringSplitOptions.RemoveEmptyEntries);
foreach (string line in lines) {
if (line.StartsWith('-')) {
char[] chars = line.TakeWhile(i => !char.IsWhiteSpace(i)).ToArray();
arguments_.Add(new string(chars));
}
}
}
return arguments_.Contains(key);
}
private static string GetCSCArgument(string[] args) {
int index = args.IndexOf("-csc");
if (index != -1) {
var remains = args.Skip(index + 1);
int end = remains.IndexOf(IsArgumentKey);
if (end != -1) {
remains = remains.Take(end);
}
return string.Join(" ", remains);
}
return null;
}
}
}