1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 12:47:35 +00:00

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
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) add_executable(gml-format_lagom ../../Userland/Utilities/gml-format.cpp)
set_target_properties(gml-format_lagom PROPERTIES OUTPUT_NAME gml-format) 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) add_executable(js_lagom ../../Userland/Utilities/js.cpp)
set_target_properties(js_lagom PROPERTIES OUTPUT_NAME js) 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(flock LibMain)
target_link_libraries(fortune LibMain) target_link_libraries(fortune LibMain)
target_link_libraries(functrace LibDebug LibX86 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(grep LibRegex)
target_link_libraries(gron LibMain) target_link_libraries(gron LibMain)
target_link_libraries(groups LibMain) target_link_libraries(groups LibMain)

View file

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