jak-project/tools/formatter/main.cpp
Tyler Wilding 60db0e5ef9
deps: update fmt to latest version (#3403)
This updates `fmt` to the latest version and moves to just being a copy
of their repo to make updating easier (no editing their cmake / figuring
out which files to minimally include).

The motivation for this is now that we switched to C++ 20, there were a
ton of deprecated function usages that is going away in future compiler
versions. This gets rid of all those warnings.
2024-03-05 22:11:52 -05:00

73 lines
2.2 KiB
C++

// 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"
#include "common/util/term_util.h"
#include "common/util/unicode_util.h"
#include "decompiler/util/DecompilerTypeSystem.h"
#include "tree_sitter/api.h"
#include "fmt/core.h"
#include "third-party/CLI11.hpp"
#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();
define_common_cli_arguments(app);
CLI11_PARSE(app, argc, argv);
if (_cli_flag_disable_ansi) {
lg::disable_ansi_colors();
}
// 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);
if (result) {
// TODO - i don't like this implementation, return a new string instead
if (write_inplace) {
file_util::write_text_file(file_path, result.value());
} else if (write_newfile) {
if (str_util::replace(file_path, ".gc", ".new.gc")) {
file_util::write_text_file(file_path, result.value());
}
}
return 0;
} else {
lg::error("Could not format file");
return 1;
}
}