|
4 | 4 |
|
5 | 5 | #include "LuaFormat.h" |
6 | 6 | #include "CodeService/Config/LuaEditorConfig.h" |
7 | | -#include "LuaParser/LuaParser.h" |
8 | 7 | #include "Util/format.h" |
9 | | -#include "CodeService/LuaFormatter.h" |
10 | 8 | #include "Util/CommandLine.h" |
11 | 9 | #include "LuaWorkspaceFormat.h" |
12 | 10 | #include "Util/StringUtil.h" |
13 | 11 |
|
14 | 12 | // https://stackoverflow.com/questions/1598985/c-read-binary-stdin |
15 | 13 | #ifdef _WIN32 |
| 14 | + |
16 | 15 | # include <io.h> |
17 | 16 | # include <fcntl.h> |
| 17 | + |
18 | 18 | # define SET_BINARY_MODE() _setmode(_fileno(stdin), _O_BINARY);\ |
19 | | - _setmode(_fileno(stdout), _O_BINARY) |
| 19 | + _setmode(_fileno(stdout), _O_BINARY) |
20 | 20 | #else |
21 | 21 | # define SET_BINARY_MODE() ((void)0) |
22 | 22 | #endif |
23 | 23 |
|
24 | | - |
25 | | -int main(int argc, char** argv) |
26 | | -{ |
27 | | - CommandLine cmd; |
28 | | - cmd.SetUsage( |
29 | | - "Usage:\n" |
30 | | - "CodeFormat [check/format] [options]\n" |
31 | | - "for example:\n" |
32 | | - "\tCodeFormat check -w . -d\n" |
33 | | - "\tCodeFormat format -f test.lua -d\n" |
34 | | - "\tCodeFormat check -w . -d --ignores \"Test/*.lua;src/**.lua\"\n" |
35 | | - "\tCodeFormat check -w . -d --ignores-file \".gitignore\"\n" |
36 | | - ); |
37 | | - cmd.AddTarget("format") |
38 | | - .Add<std::string>("file", "f", "Specify the input file") |
39 | | - .Add<bool>("overwrite", "ow", "Format overwrite the input file") |
40 | | - .Add<std::string>("workspace", "w", |
41 | | - "Specify workspace directory,if no input file is specified, bulk formatting is performed") |
42 | | - .Add<bool>("stdin", "i", "Read from stdin and specify read size") |
43 | | - .Add<std::string>("config", "c", |
44 | | - "Specify .editorconfig file, it decides on the effect of formatting") |
45 | | - .Add<bool>("detect-config", "d", |
46 | | - "Configuration will be automatically detected,\n" |
47 | | - "\t\tIf this option is set, the config option has no effect ") |
48 | | - .Add<std::string>("outfile", "o", |
49 | | - "Specify output file") |
50 | | - .Add<std::string>("ignores-file", "igf", |
51 | | - "Specify which files to ignore through configuration file,for example \".gitignore\"" |
52 | | - ) |
53 | | - .Add<std::string>("ignores", "ig", |
54 | | - "Use file wildcards to specify how to ignore files\n" |
55 | | - "\t\tseparated by ';'" |
56 | | - ) |
57 | | - .EnableKeyValueArgs(); |
58 | | - cmd.AddTarget("check") |
59 | | - .Add<std::string>("file", "f", "Specify the input file") |
60 | | - .Add<std::string>("workspace", "w", |
61 | | - "Specify workspace directory, if no input file is specified, bulk checking is performed") |
62 | | - .Add<std::string>("config", "c", |
63 | | - "Specify editorconfig file, it decides on the effect of formatting or diagnosis") |
64 | | - .Add<bool>("detect-config", "d", |
65 | | - "Configuration will be automatically detected,\n" |
66 | | - "\t\tIf this option is set, the config option has no effect") |
67 | | - .Add<bool>("diagnosis-as-error", "DAE", "if exist error or diagnosis info , return -1") |
68 | | - .Add<std::string>("ignores-file", "igf", |
69 | | - "Specify which files to ignore through configuration file,for example \".gitignore\"" |
70 | | - ) |
71 | | - .Add<std::string>("ignores", "ig", |
72 | | - "Use file wildcards to specify how to ignore files\n" |
73 | | - "\t\tseparated by ';'" |
74 | | - ) |
75 | | - // .Add<std::string>("output", "o", |
76 | | - // "Diagnostic messages are output to file or standard io" |
77 | | - // "\t\toptional filename/stdout/stderr, default is stderr") |
78 | | - .EnableKeyValueArgs(); |
79 | | - |
80 | | - |
81 | | - if (!cmd.Parse(argc, argv)) |
82 | | - { |
83 | | - cmd.PrintUsage(); |
84 | | - return -1; |
85 | | - } |
86 | | - |
87 | | - if (cmd.HasOption("file") || cmd.HasOption("stdin")) |
88 | | - { |
89 | | - auto luaFormat = std::make_shared<LuaFormat>(); |
90 | | - if (cmd.HasOption("file") && !cmd.HasOption("stdin")) |
91 | | - { |
92 | | - if (!luaFormat->SetInputFile(cmd.Get<std::string>("file"))) |
93 | | - { |
94 | | - std::cerr << Util::format("Can not find file {}", cmd.Get<std::string>("file")) << std::endl; |
95 | | - return -1; |
96 | | - } |
97 | | - } |
98 | | - else if (cmd.HasOption("stdin") && !cmd.HasOption("file")) |
99 | | - { |
100 | | - SET_BINARY_MODE(); |
101 | | - if (!luaFormat->ReadFromStdin()) |
102 | | - { |
103 | | - return -1; |
104 | | - } |
105 | | - } |
106 | | - else |
107 | | - { |
108 | | - std::cerr << "Either --file or --stdin must be specified." << std::endl; |
109 | | - return -1; |
110 | | - } |
111 | | - |
112 | | - if (cmd.HasOption("outfile")) |
113 | | - { |
114 | | - luaFormat->SetOutputFile(cmd.Get<std::string>("outfile")); |
115 | | - } |
116 | | - else if (cmd.HasOption("overwrite")) |
117 | | - { |
118 | | - luaFormat->SetOutputFile(cmd.Get<std::string>("file")); |
119 | | - } |
120 | | - |
121 | | - if (cmd.Get<bool>("detect-config")) |
122 | | - { |
123 | | - if (cmd.HasOption("workspace")) |
124 | | - { |
125 | | - luaFormat->AutoDetectConfig(std::filesystem::path(cmd.Get<std::string>("workspace"))); |
126 | | - } |
127 | | - else |
128 | | - { |
129 | | - luaFormat->AutoDetectConfig(); |
130 | | - } |
131 | | - } |
132 | | - else if (cmd.HasOption("config")) |
133 | | - { |
134 | | - luaFormat->SetConfigPath(cmd.Get<std::string>("config")); |
135 | | - } |
136 | | - |
137 | | - luaFormat->SetDefaultOptions(cmd.GetKeyValueOptions()); |
138 | | - |
139 | | - if (cmd.GetTarget() == "format") |
140 | | - { |
141 | | - if (!luaFormat->Reformat()) |
142 | | - { |
143 | | - std::cerr << Util::format("Exist lua syntax error") << std::endl; |
144 | | - return -1; |
145 | | - } |
146 | | - } |
147 | | - else if (cmd.GetTarget() == "check") |
148 | | - { |
149 | | - if (!luaFormat->Check() && cmd.Get<bool>("diagnosis-as-error")) |
150 | | - { |
151 | | - return -1; |
152 | | - } |
153 | | - } |
154 | | - } |
155 | | - else if (cmd.HasOption("workspace")) |
156 | | - { |
157 | | - LuaWorkspaceFormat workspaceFormat(cmd.Get<std::string>("workspace")); |
158 | | - |
159 | | - if (cmd.Get<bool>("detect-config")) |
160 | | - { |
161 | | - workspaceFormat.SetAutoDetectConfig(true); |
162 | | - } |
163 | | - else if (cmd.HasOption("config")) |
164 | | - { |
165 | | - workspaceFormat.SetConfigPath(cmd.Get<std::string>("config")); |
166 | | - } |
167 | | - |
168 | | - if (cmd.HasOption("ignores")) |
169 | | - { |
170 | | - auto ignores = cmd.Get<std::string>("ignores"); |
171 | | - auto patterns = string_util::Split(ignores, ";"); |
172 | | - for (auto pattern : patterns) |
173 | | - { |
174 | | - auto patternNoSpace = string_util::TrimSpace(pattern); |
175 | | - if (!patternNoSpace.empty()) |
176 | | - { |
177 | | - workspaceFormat.AddIgnores(patternNoSpace); |
178 | | - } |
179 | | - } |
180 | | - } |
181 | | - |
182 | | - if (cmd.HasOption("ignores-file")) |
183 | | - { |
184 | | - workspaceFormat.AddIgnoresByFile(cmd.Get<std::string>("ignores-file")); |
185 | | - } |
186 | | - |
187 | | - workspaceFormat.SetKeyValues(cmd.GetKeyValueOptions()); |
188 | | - |
189 | | - if (cmd.GetTarget() == "format") |
190 | | - { |
191 | | - workspaceFormat.ReformatWorkspace(); |
192 | | - } |
193 | | - else if (cmd.GetTarget() == "check") |
194 | | - { |
195 | | - if (!workspaceFormat.CheckWorkspace() && cmd.Get<bool>("diagnosis-as-error")) |
196 | | - { |
197 | | - return -1; |
198 | | - } |
199 | | - } |
200 | | - } |
201 | | - return 0; |
| 24 | +int main(int argc, char **argv) { |
| 25 | + CommandLine cmd; |
| 26 | + cmd.SetUsage( |
| 27 | + "Usage:\n" |
| 28 | + "CodeFormat [check/format] [options]\n" |
| 29 | + "for example:\n" |
| 30 | + "\tCodeFormat check -w . -d\n" |
| 31 | + "\tCodeFormat format -f test.lua -d\n" |
| 32 | + "\tCodeFormat check -w . -d --ignores \"Test/*.lua;src/**.lua\"\n" |
| 33 | + "\tCodeFormat check -w . -d --ignores-file \".gitignore\"\n" |
| 34 | + ); |
| 35 | + cmd.AddTarget("format") |
| 36 | + .Add<std::string>("file", "f", "Specify the input file") |
| 37 | + .Add<bool>("overwrite", "ow", "Format overwrite the input file") |
| 38 | + .Add<std::string>("workspace", "w", |
| 39 | + "Specify workspace directory,if no input file is specified, bulk formatting is performed") |
| 40 | + .Add<bool>("stdin", "i", "Read from stdin") |
| 41 | + .Add<std::string>("config", "c", |
| 42 | + "Specify .editorconfig file, it decides on the effect of formatting") |
| 43 | + .Add<bool>("detect-config", "d", |
| 44 | + "Configuration will be automatically detected,\n" |
| 45 | + "\t\tIf this option is set, the config option has no effect ") |
| 46 | + .Add<std::string>("outfile", "o", |
| 47 | + "Specify output file") |
| 48 | + .Add<std::string>("ignores-file", "igf", |
| 49 | + "Specify which files to ignore through configuration file,for example \".gitignore\"" |
| 50 | + ) |
| 51 | + .Add<std::string>("ignores", "ig", |
| 52 | + "Use file wildcards to specify how to ignore files\n" |
| 53 | + "\t\tseparated by ';'" |
| 54 | + ) |
| 55 | + .EnableKeyValueArgs(); |
| 56 | + cmd.AddTarget("check") |
| 57 | + .Add<std::string>("file", "f", "Specify the input file") |
| 58 | + .Add<std::string>("workspace", "w", |
| 59 | + "Specify workspace directory, if no input file is specified, bulk checking is performed") |
| 60 | + .Add<std::string>("config", "c", |
| 61 | + "Specify editorconfig file, it decides on the effect of formatting or diagnosis") |
| 62 | + .Add<bool>("detect-config", "d", |
| 63 | + "Configuration will be automatically detected,\n" |
| 64 | + "\t\tIf this option is set, the config option has no effect") |
| 65 | + .Add<bool>("diagnosis-as-error", "DAE", "if exist error or diagnosis info , return -1") |
| 66 | + .Add<std::string>("ignores-file", "igf", |
| 67 | + "Specify which files to ignore through configuration file,for example \".gitignore\"" |
| 68 | + ) |
| 69 | + .Add<std::string>("ignores", "ig", |
| 70 | + "Use file wildcards to specify how to ignore files\n" |
| 71 | + "\t\tseparated by ';'" |
| 72 | + ) |
| 73 | + .EnableKeyValueArgs(); |
| 74 | + |
| 75 | + |
| 76 | + if (!cmd.Parse(argc, argv)) { |
| 77 | + cmd.PrintUsage(); |
| 78 | + return -1; |
| 79 | + } |
| 80 | + |
| 81 | + LuaFormat format; |
| 82 | + if (cmd.HasOption("file") || cmd.HasOption("stdin")) { |
| 83 | + format.SetWorkMode(WorkMode::File); |
| 84 | + if (cmd.HasOption("file") && !cmd.HasOption("stdin")) { |
| 85 | + if (!format.SetInputFile(cmd.Get<std::string>("file"))) { |
| 86 | + std::cerr << util::format("Can not find file {}", cmd.Get<std::string>("file")) << std::endl; |
| 87 | + return -1; |
| 88 | + } |
| 89 | + } else if (cmd.HasOption("stdin") && !cmd.HasOption("file")) { |
| 90 | + SET_BINARY_MODE(); |
| 91 | + if (!format.ReadFromStdin()) { |
| 92 | + return -1; |
| 93 | + } |
| 94 | + } else { |
| 95 | + std::cerr << "Either --file or --stdin must be specified." << std::endl; |
| 96 | + return -1; |
| 97 | + } |
| 98 | + |
| 99 | + if (cmd.HasOption("outfile")) { |
| 100 | + format.SetOutputFile(cmd.Get<std::string>("outfile")); |
| 101 | + } else if (cmd.HasOption("overwrite")) { |
| 102 | + format.SetOutputFile(cmd.Get<std::string>("file")); |
| 103 | + } |
| 104 | + } else { |
| 105 | + format.SetWorkMode(WorkMode::Workspace); |
| 106 | + } |
| 107 | + |
| 108 | + if (cmd.HasOption("workspace")) { |
| 109 | + format.SetWorkspace(cmd.Get<std::string>("workspace")); |
| 110 | + } |
| 111 | + |
| 112 | + if (cmd.HasOption("ignores")) { |
| 113 | + auto ignores = cmd.Get<std::string>("ignores"); |
| 114 | + auto patterns = string_util::Split(ignores, ";"); |
| 115 | + for (auto pattern: patterns) { |
| 116 | + auto patternNoSpace = string_util::TrimSpace(pattern); |
| 117 | + if (!patternNoSpace.empty()) { |
| 118 | + format.AddIgnores(patternNoSpace); |
| 119 | + } |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + if (cmd.HasOption("ignores-file")) { |
| 124 | + format.AddIgnoresByFile(cmd.Get<std::string>("ignores-file")); |
| 125 | + } |
| 126 | + |
| 127 | + if (cmd.Get<bool>("detect-config")) { |
| 128 | + format.AutoDetectConfig(); |
| 129 | + } else if (cmd.HasOption("config")) { |
| 130 | + format.SetConfigPath(cmd.Get<std::string>("config")); |
| 131 | + } else { |
| 132 | + format.SetDefaultStyle(cmd.GetKeyValueOptions()); |
| 133 | + } |
| 134 | + |
| 135 | + if (cmd.GetTarget() == "format") { |
| 136 | + if (!format.Reformat()) { |
| 137 | + std::cerr << util::format("Exist lua syntax error") << std::endl; |
| 138 | + return -1; |
| 139 | + } |
| 140 | + } else if (cmd.GetTarget() == "check") { |
| 141 | + if (!format.Check() && cmd.Get<bool>("diagnosis-as-error")) { |
| 142 | + return -1; |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + return 0; |
202 | 147 | } |
0 commit comments