2021-01-03 21:17:48 +03:00
|
|
|
/*
|
2021-04-22 23:51:19 +03:00
|
|
|
* Copyright (c) 2021, Linus Groh <linusg@serenityos.org>
|
2021-01-03 21:17:48 +03:00
|
|
|
*
|
2021-04-22 11:24:48 +03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2021-01-03 21:17:48 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <LibCore/ArgsParser.h>
|
2023-02-09 05:02:46 +03:00
|
|
|
#include <LibCore/File.h>
|
2022-01-13 23:03:08 +03:00
|
|
|
#include <LibCore/System.h>
|
2022-02-02 20:11:29 +03:00
|
|
|
#include <LibGUI/GML/Formatter.h>
|
2022-01-13 23:03:08 +03:00
|
|
|
#include <LibMain/Main.h>
|
2021-01-03 21:17:48 +03:00
|
|
|
|
2022-09-14 14:45:09 +03:00
|
|
|
static ErrorOr<bool> format_file(StringView path, bool inplace)
|
2021-01-03 21:17:48 +03:00
|
|
|
{
|
|
|
|
auto read_from_stdin = path == "-";
|
2023-02-09 05:02:46 +03:00
|
|
|
auto open_mode = (inplace && !read_from_stdin) ? Core::File::OpenMode::ReadWrite : Core::File::OpenMode::Read;
|
|
|
|
auto file = TRY(Core::File::open_file_or_standard_stream(path, open_mode));
|
2022-09-14 14:45:09 +03:00
|
|
|
|
2022-12-11 19:49:00 +03:00
|
|
|
auto contents = TRY(file->read_until_eof());
|
2022-02-12 20:16:13 +03:00
|
|
|
auto formatted_gml_or_error = GUI::GML::format_gml(contents);
|
|
|
|
if (formatted_gml_or_error.is_error()) {
|
|
|
|
warnln("Failed to parse GML: {}", formatted_gml_or_error.error());
|
2021-01-03 21:17:48 +03:00
|
|
|
return false;
|
|
|
|
}
|
2022-02-12 20:16:13 +03:00
|
|
|
auto formatted_gml = formatted_gml_or_error.release_value();
|
2021-01-03 21:17:48 +03:00
|
|
|
if (inplace && !read_from_stdin) {
|
2022-02-12 20:28:22 +03:00
|
|
|
if (formatted_gml == contents)
|
|
|
|
return true;
|
2023-01-22 07:09:11 +03:00
|
|
|
TRY(file->seek(0, SeekMode::SetPosition));
|
2022-09-14 14:45:09 +03:00
|
|
|
TRY(file->truncate(0));
|
2023-03-01 19:24:50 +03:00
|
|
|
TRY(file->write_until_depleted(formatted_gml.bytes()));
|
2021-01-03 21:17:48 +03:00
|
|
|
} else {
|
|
|
|
out("{}", formatted_gml);
|
|
|
|
}
|
2022-02-19 02:11:11 +03:00
|
|
|
return formatted_gml == contents;
|
2021-01-03 21:17:48 +03:00
|
|
|
}
|
|
|
|
|
2022-01-13 23:03:08 +03:00
|
|
|
ErrorOr<int> serenity_main(Main::Arguments args)
|
2021-01-03 21:17:48 +03:00
|
|
|
{
|
2022-04-04 02:16:06 +03:00
|
|
|
TRY(Core::System::pledge("stdio rpath wpath cpath"));
|
2021-01-03 21:17:48 +03:00
|
|
|
|
|
|
|
bool inplace = false;
|
2023-12-16 17:19:34 +03:00
|
|
|
Vector<ByteString> files;
|
2021-01-03 21:17:48 +03:00
|
|
|
|
|
|
|
Core::ArgsParser args_parser;
|
|
|
|
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);
|
2022-01-13 23:03:08 +03:00
|
|
|
args_parser.parse(args);
|
2021-01-03 21:17:48 +03:00
|
|
|
|
2022-01-13 23:03:08 +03:00
|
|
|
if (!inplace)
|
2022-04-04 02:16:06 +03:00
|
|
|
TRY(Core::System::pledge("stdio rpath"));
|
2021-01-03 21:17:48 +03:00
|
|
|
|
|
|
|
if (files.is_empty())
|
|
|
|
files.append("-");
|
2022-02-19 02:11:11 +03:00
|
|
|
|
|
|
|
auto formatting_changed = false;
|
2021-01-03 21:17:48 +03:00
|
|
|
for (auto& file : files) {
|
2022-01-13 23:03:08 +03:00
|
|
|
if (!TRY(format_file(file, inplace)))
|
2022-02-19 02:11:11 +03:00
|
|
|
formatting_changed = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (formatting_changed) {
|
|
|
|
dbgln("Some GML formatting issues were encountered.");
|
|
|
|
return 1;
|
2021-01-03 21:17:48 +03:00
|
|
|
}
|
|
|
|
|
2022-02-19 02:11:11 +03:00
|
|
|
return 0;
|
2021-01-03 21:17:48 +03:00
|
|
|
}
|