gml-format: Port to LibMain

This commit is contained in:
Lucas CHOLLET 2022-01-13 21:03:08 +01:00 committed by Idan Horowitz
parent f862fabe6e
commit 9e1e80954b
Notes: sideshowbarker 2024-07-17 20:54:37 +09:00
3 changed files with 13 additions and 24 deletions

View file

@ -482,7 +482,7 @@ if (BUILD_LAGOM)
add_executable(gml-format_lagom ../../Userland/Utilities/gml-format.cpp)
set_target_properties(gml-format_lagom PROPERTIES OUTPUT_NAME gml-format)
target_link_libraries(gml-format_lagom LagomCore LagomGML)
target_link_libraries(gml-format_lagom LagomCore LagomGML LagomMain)
add_executable(js_lagom ../../Userland/Utilities/js.cpp)
set_target_properties(js_lagom PROPERTIES OUTPUT_NAME js)

View file

@ -99,7 +99,7 @@ target_link_libraries(find LibMain)
target_link_libraries(flock LibMain)
target_link_libraries(fortune LibMain)
target_link_libraries(functrace LibDebug LibX86 LibMain)
target_link_libraries(gml-format LibGUI)
target_link_libraries(gml-format LibGUI LibMain)
target_link_libraries(grep LibRegex)
target_link_libraries(gron LibMain)
target_link_libraries(groups LibMain)

View file

@ -6,12 +6,13 @@
#include <LibCore/ArgsParser.h>
#include <LibCore/File.h>
#include <LibCore/System.h>
#include <LibGUI/GMLFormatter.h>
#include <unistd.h>
#include <LibMain/Main.h>
bool format_file(StringView, bool);
ErrorOr<bool> format_file(StringView, bool);
bool format_file(StringView path, bool inplace)
ErrorOr<bool> format_file(StringView path, bool inplace)
{
auto read_from_stdin = path == "-";
RefPtr<Core::File> file;
@ -19,12 +20,7 @@ bool format_file(StringView path, bool inplace)
file = Core::File::standard_input();
} else {
auto open_mode = inplace ? Core::OpenMode::ReadWrite : Core::OpenMode::ReadOnly;
auto file_or_error = Core::File::open(path, open_mode);
if (file_or_error.is_error()) {
warnln("Could not open {}: {}", path, file_or_error.error());
return false;
}
file = file_or_error.value();
file = TRY(Core::File::open(path, open_mode));
}
auto formatted_gml = GUI::format_gml(file->read_all());
if (formatted_gml.is_null()) {
@ -46,13 +42,10 @@ bool format_file(StringView path, bool inplace)
return true;
}
int main(int argc, char** argv)
ErrorOr<int> serenity_main(Main::Arguments args)
{
#ifdef __serenity__
if (pledge("stdio rpath wpath cpath", nullptr) < 0) {
perror("pledge");
return 1;
}
TRY(Core::System::pledge("stdio rpath wpath cpath", nullptr));
#endif
bool inplace = false;
@ -62,15 +55,11 @@ int main(int argc, char** argv)
args_parser.set_general_help("Format GML files.");
args_parser.add_option(inplace, "Write formatted contents back to file rather than standard output", "inplace", 'i');
args_parser.add_positional_argument(files, "File(s) to process", "path", Core::ArgsParser::Required::No);
args_parser.parse(argc, argv);
args_parser.parse(args);
#ifdef __serenity__
if (!inplace) {
if (pledge("stdio rpath", nullptr) < 0) {
perror("pledge");
return 1;
}
}
if (!inplace)
TRY(Core::System::pledge("stdio rpath", nullptr));
#endif
unsigned exit_code = 0;
@ -78,7 +67,7 @@ int main(int argc, char** argv)
if (files.is_empty())
files.append("-");
for (auto& file : files) {
if (!format_file(file, inplace))
if (!TRY(format_file(file, inplace)))
exit_code = 1;
}