2023-04-24 23:46:55 -04:00
|
|
|
// Iterates through the `all-types` DTS to find types that meet a variety of criteria, such as:
|
|
|
|
// - type size
|
|
|
|
// - field types at given offsets
|
|
|
|
// - parent-types
|
|
|
|
// - ...
|
|
|
|
|
|
|
|
#include <queue>
|
|
|
|
|
|
|
|
#include "common/formatter/formatter.h"
|
|
|
|
#include "common/log/log.h"
|
|
|
|
#include "common/util/FileUtil.h"
|
|
|
|
#include "common/util/json_util.h"
|
|
|
|
#include "common/util/string_util.h"
|
2024-02-04 08:40:43 -05:00
|
|
|
#include "common/util/term_util.h"
|
2023-04-24 23:46:55 -04:00
|
|
|
#include "common/util/unicode_util.h"
|
|
|
|
|
|
|
|
#include "decompiler/util/DecompilerTypeSystem.h"
|
|
|
|
#include "tree_sitter/api.h"
|
|
|
|
|
|
|
|
#include "third-party/CLI11.hpp"
|
|
|
|
#include "third-party/fmt/core.h"
|
|
|
|
#include "third-party/json.hpp"
|
|
|
|
|
|
|
|
int main(int argc, char** argv) {
|
|
|
|
ArgumentGuard u8_guard(argc, argv);
|
|
|
|
|
|
|
|
bool check = false;
|
|
|
|
bool write_inplace = false;
|
|
|
|
bool write_newfile = false;
|
|
|
|
std::string file_path = "";
|
|
|
|
std::string config_path = "";
|
|
|
|
|
|
|
|
lg::initialize();
|
|
|
|
|
|
|
|
CLI::App app{"OpenGOAL Formatter"};
|
|
|
|
app.add_flag("-c,--check", check,
|
|
|
|
"If on, will just do a dry-run and fail if something isn't formatted correctly");
|
|
|
|
app.add_flag("-w,--write", write_inplace,
|
|
|
|
"Whether to write the formatted results directly back into the original file");
|
|
|
|
app.add_flag("-n,--new", write_newfile,
|
|
|
|
"Whether to write the formatted results into a new file in the same directory, "
|
|
|
|
"useful for testing");
|
|
|
|
app.add_option("-f,--file", file_path, "Input file path");
|
|
|
|
app.add_option("--config", config_path, "Config file path");
|
|
|
|
app.validate_positionals();
|
2024-02-04 08:40:43 -05:00
|
|
|
define_common_cli_arguments(app);
|
2023-04-24 23:46:55 -04:00
|
|
|
CLI11_PARSE(app, argc, argv);
|
|
|
|
|
2024-02-04 08:40:43 -05:00
|
|
|
if (_cli_flag_disable_ansi) {
|
|
|
|
lg::disable_ansi_colors();
|
|
|
|
}
|
|
|
|
|
2023-04-24 23:46:55 -04:00
|
|
|
// TODO - support recursing directories
|
|
|
|
// Read in source code
|
|
|
|
const auto source_code = file_util::read_text_file(file_path);
|
|
|
|
const auto result = formatter::format_code(source_code);
|
|
|
|
|
2023-11-11 17:51:55 -05:00
|
|
|
if (result) {
|
2023-04-24 23:46:55 -04:00
|
|
|
// TODO - i don't like this implementation, return a new string instead
|
2023-11-11 17:51:55 -05:00
|
|
|
if (write_inplace) {
|
2023-05-28 13:22:00 -04:00
|
|
|
file_util::write_text_file(file_path, result.value());
|
2023-11-11 17:51:55 -05:00
|
|
|
} else if (write_newfile) {
|
|
|
|
if (str_util::replace(file_path, ".gc", ".new.gc")) {
|
|
|
|
file_util::write_text_file(file_path, result.value());
|
|
|
|
}
|
2023-04-24 23:46:55 -04:00
|
|
|
}
|
2024-02-04 08:40:43 -05:00
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
lg::error("Could not format file");
|
|
|
|
return 1;
|
2023-04-24 23:46:55 -04:00
|
|
|
}
|
|
|
|
}
|