-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathFmtCommand.cpp
More file actions
403 lines (332 loc) · 9.29 KB
/
Copy pathFmtCommand.cpp
File metadata and controls
403 lines (332 loc) · 9.29 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
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
/**
*
* @file FmtCommand.cpp
* @author Gaspard Kirira
*
* Copyright 2025, Gaspard Kirira. All rights reserved.
* https://github.com/vixcpp/vix
* Use of this source code is governed by a MIT license
* that can be found in the License file.
*
* Vix.cpp
*/
#include <vix/cli/commands/FmtCommand.hpp>
#include <vix/cli/util/Ui.hpp>
#include <algorithm>
#include <cstdlib>
#include <filesystem>
#include <iostream>
#include <string>
#include <vector>
namespace fs = std::filesystem;
namespace vix::commands
{
namespace
{
struct FmtOptions
{
bool check = false;
bool quiet = false;
std::vector<std::string> ignore;
std::vector<fs::path> inputs;
};
static bool is_cpp_file(const fs::path &p)
{
const std::string ext = p.extension().string();
return ext == ".cpp" ||
ext == ".hpp" ||
ext == ".h" ||
ext == ".cc" ||
ext == ".cxx" ||
ext == ".hh" ||
ext == ".hxx";
}
static std::string shell_quote(const std::string &s)
{
#ifdef _WIN32
std::string out = "\"";
for (char c : s)
{
if (c == '"')
out += "\\\"";
else
out += c;
}
out += "\"";
return out;
#else
std::string out = "'";
for (char c : s)
{
if (c == '\'')
out += "'\\''";
else
out += c;
}
out += "'";
return out;
#endif
}
static bool has_clang_format()
{
#ifdef _WIN32
return std::system("where clang-format >nul 2>&1") == 0;
#else
return std::system("which clang-format > /dev/null 2>&1") == 0;
#endif
}
static bool path_matches_ignore(const fs::path &p, const std::vector<std::string> &ignore)
{
const std::string generic = p.generic_string();
for (const auto &item : ignore)
{
if (item.empty())
continue;
if (generic == item)
return true;
if (generic.find(item) != std::string::npos)
return true;
}
return false;
}
static void add_file_if_valid(
const fs::path &p,
const std::vector<std::string> &ignore,
std::vector<fs::path> &files)
{
std::error_code ec;
const bool exists = fs::exists(p, ec);
if (ec || !exists)
return;
if (!fs::is_regular_file(p, ec))
return;
if (!is_cpp_file(p))
return;
if (path_matches_ignore(p, ignore))
return;
files.push_back(p);
}
static void collect_from_directory(
const fs::path &root,
const std::vector<std::string> &ignore,
std::vector<fs::path> &files)
{
std::error_code ec;
if (!fs::exists(root, ec) || ec)
return;
fs::recursive_directory_iterator it(
root,
fs::directory_options::skip_permission_denied,
ec);
fs::recursive_directory_iterator end;
while (!ec && it != end)
{
const fs::path current = it->path();
if (path_matches_ignore(current, ignore))
{
if (it->is_directory(ec))
it.disable_recursion_pending();
++it;
continue;
}
if (it->is_regular_file(ec) && is_cpp_file(current))
files.push_back(current);
++it;
}
}
static std::vector<fs::path> collect_files(const FmtOptions &options)
{
std::vector<fs::path> files;
if (!options.inputs.empty())
{
for (const auto &input : options.inputs)
{
std::error_code ec;
if (!fs::exists(input, ec) || ec)
continue;
if (fs::is_directory(input, ec))
{
collect_from_directory(input, options.ignore, files);
}
else
{
add_file_if_valid(input, options.ignore, files);
}
}
}
else
{
collect_from_directory("src", options.ignore, files);
collect_from_directory("include", options.ignore, files);
}
std::sort(files.begin(), files.end());
files.erase(std::unique(files.begin(), files.end()), files.end());
return files;
}
static bool parse_args(const std::vector<std::string> &args, FmtOptions &options)
{
for (std::size_t i = 0; i < args.size(); ++i)
{
const std::string &arg = args[i];
if (arg == "-h" || arg == "--help")
return false;
if (arg == "--check")
{
options.check = true;
continue;
}
if (arg == "-q" || arg == "--quiet")
{
options.quiet = true;
continue;
}
if (arg == "--ignore")
{
if (i + 1 >= args.size())
{
vix::cli::util::err_line(std::cerr, "missing value for --ignore");
return false;
}
options.ignore.push_back(args[++i]);
continue;
}
if (arg.rfind("--ignore=", 0) == 0)
{
options.ignore.push_back(arg.substr(std::string("--ignore=").size()));
continue;
}
if (!arg.empty() && arg[0] == '-')
{
vix::cli::util::err_line(std::cerr, "unknown option: " + arg);
return false;
}
options.inputs.push_back(arg);
}
return true;
}
static int run_check(const std::vector<fs::path> &files, bool quiet)
{
std::size_t failed = 0;
for (const auto &file : files)
{
const std::string cmd =
"clang-format --dry-run --Werror " + shell_quote(file.string()) + " > /dev/null 2>&1";
const int rc = std::system(cmd.c_str());
if (rc != 0)
{
++failed;
if (!quiet)
vix::cli::util::warn_line(std::cout, file.string());
}
}
if (failed == 0)
{
if (!quiet)
vix::cli::util::ok_line(std::cout, "All files are properly formatted");
return 0;
}
if (!quiet)
{
vix::cli::util::one_line_spacer(std::cout);
vix::cli::util::err_line(
std::cout,
std::to_string(failed) + " file(s) need formatting");
}
return 1;
}
static int run_format(const std::vector<fs::path> &files, bool quiet)
{
std::size_t formatted = 0;
std::size_t failed = 0;
for (const auto &file : files)
{
const std::string cmd =
"clang-format -i " + shell_quote(file.string());
const int rc = std::system(cmd.c_str());
if (rc == 0)
{
++formatted;
if (!quiet)
vix::cli::util::info_line(std::cout, file.string());
}
else
{
++failed;
if (!quiet)
vix::cli::util::err_line(std::cout, "failed: " + file.string());
}
}
if (!quiet)
{
vix::cli::util::one_line_spacer(std::cout);
if (failed == 0)
{
vix::cli::util::ok_line(
std::cout,
"Formatted " + std::to_string(formatted) + " file(s)");
}
else
{
vix::cli::util::warn_line(
std::cout,
"Formatted " + std::to_string(formatted) +
" file(s), failed on " + std::to_string(failed) + " file(s)");
}
}
return failed == 0 ? 0 : 1;
}
} // namespace
int FmtCommand::run(const std::vector<std::string> &args)
{
FmtOptions options;
if (!parse_args(args, options))
return help();
if (!has_clang_format())
{
vix::cli::util::err_line(std::cerr, "clang-format not found");
vix::cli::util::warn_line(std::cerr, "Install clang-format to use 'vix fmt'");
return 1;
}
if (!options.quiet)
vix::cli::util::section(std::cout, "Format");
const auto files = collect_files(options);
if (files.empty())
{
if (!options.quiet)
vix::cli::util::warn_line(std::cout, "No C++ files found");
return 0;
}
if (!options.quiet)
{
if (options.check)
vix::cli::util::info_line(std::cout, "checking " + std::to_string(files.size()) + " file(s)");
else
vix::cli::util::info_line(std::cout, "formatting " + std::to_string(files.size()) + " file(s)");
}
if (options.check)
return run_check(files, options.quiet);
return run_format(files, options.quiet);
}
int FmtCommand::help()
{
std::cout
<< "Usage: vix fmt [OPTIONS] [files]...\n\n"
<< "Format C++ source files using clang-format.\n\n"
<< "Arguments:\n"
<< " [files]... Files or directories to format\n\n"
<< "Options:\n"
<< " --check Check if files are formatted\n"
<< " --ignore <path> Ignore a file or path pattern\n"
<< " -q, --quiet Suppress non-essential output\n"
<< " -h, --help Show this help message\n\n"
<< "Behavior:\n"
<< " If no files are provided, vix fmt scans src/ and include/.\n"
<< " If a .clang-format file exists, clang-format uses it automatically.\n\n"
<< "Examples:\n"
<< " vix fmt\n"
<< " vix fmt src include\n"
<< " vix fmt main.cpp --check\n"
<< " vix fmt src --ignore=build --ignore=vendor\n";
return 0;
}
} // namespace vix::commands