diff options
author | Sam Atkins <atkinssj@serenityos.org> | 2022-03-08 15:00:27 +0000 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2022-03-09 23:06:30 +0100 |
commit | b7bb86462bf218f4aadfc9f6c4192a0649a29c6b (patch) | |
tree | 63c88e0c29687f731414a7e8386674f2dc16009d /Meta/Lagom | |
parent | e986331a4ffae5c1398dbb20d5ccffc2f90e58e0 (diff) | |
download | serenity-b7bb86462bf218f4aadfc9f6c4192a0649a29c6b.zip |
Meta: Generate CSS::MediaFeatureID enum
This works largely the same as the PropertyID and ValueID generators,
but using LibMain, Core::Stream, and TRY().
Rather than have a MediaFeatureID::Invalid, I decided to return an
Optional. We'll see if that turns out better or not. :^)
Diffstat (limited to 'Meta/Lagom')
4 files changed, 140 insertions, 4 deletions
diff --git a/Meta/Lagom/Tools/CodeGenerators/LibWeb/CMakeLists.txt b/Meta/Lagom/Tools/CodeGenerators/LibWeb/CMakeLists.txt index a757779a76..e9963fc06e 100644 --- a/Meta/Lagom/Tools/CodeGenerators/LibWeb/CMakeLists.txt +++ b/Meta/Lagom/Tools/CodeGenerators/LibWeb/CMakeLists.txt @@ -1,8 +1,10 @@ set(SOURCES "") # avoid pulling SOURCES from parent scope -lagom_tool(Generate_CSS_PropertyID_h SOURCES Generate_CSS_PropertyID_h.cpp) -lagom_tool(Generate_CSS_PropertyID_cpp SOURCES Generate_CSS_PropertyID_cpp.cpp) -lagom_tool(Generate_CSS_ValueID_h SOURCES Generate_CSS_ValueID_h.cpp) -lagom_tool(Generate_CSS_ValueID_cpp SOURCES Generate_CSS_ValueID_cpp.cpp) +lagom_tool(Generate_CSS_MediaFeatureID_h SOURCES Generate_CSS_MediaFeatureID_h.cpp LIBS LagomMain) +lagom_tool(Generate_CSS_MediaFeatureID_cpp SOURCES Generate_CSS_MediaFeatureID_cpp.cpp LIBS LagomMain) +lagom_tool(Generate_CSS_PropertyID_h SOURCES Generate_CSS_PropertyID_h.cpp) +lagom_tool(Generate_CSS_PropertyID_cpp SOURCES Generate_CSS_PropertyID_cpp.cpp) +lagom_tool(Generate_CSS_ValueID_h SOURCES Generate_CSS_ValueID_h.cpp) +lagom_tool(Generate_CSS_ValueID_cpp SOURCES Generate_CSS_ValueID_cpp.cpp) add_subdirectory(WrapperGenerator) diff --git a/Meta/Lagom/Tools/CodeGenerators/LibWeb/Generate_CSS_MediaFeatureID_cpp.cpp b/Meta/Lagom/Tools/CodeGenerators/LibWeb/Generate_CSS_MediaFeatureID_cpp.cpp new file mode 100644 index 0000000000..88c6e156a9 --- /dev/null +++ b/Meta/Lagom/Tools/CodeGenerators/LibWeb/Generate_CSS_MediaFeatureID_cpp.cpp @@ -0,0 +1,69 @@ +/* + * Copyright (c) 2022, Sam Atkins <atkinssj@serenityos.org> + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include "GeneratorUtil.h" +#include <AK/SourceGenerator.h> +#include <AK/StringBuilder.h> +#include <LibMain/Main.h> + +ErrorOr<int> serenity_main(Main::Arguments arguments) +{ + if (arguments.argc != 2) { + warnln("usage: {} <path/to/CSS/MediaFeatures.json", arguments.strings[0]); + return 1; + } + + auto json = TRY(read_entire_file_as_json(arguments.strings[1])); + VERIFY(json.is_object()); + + StringBuilder builder; + SourceGenerator generator { builder }; + generator.append(R"~~~(#include <LibWeb/CSS/MediaFeatureID.h> + +namespace Web::CSS { + +Optional<MediaFeatureID> media_feature_id_from_string(StringView string) +{)~~~"); + + json.as_object().for_each_member([&](auto& name, auto&) { + auto member_generator = generator.fork(); + member_generator.set("name", name); + member_generator.set("name:titlecase", title_casify(name)); + member_generator.append(R"~~~( + if (string.equals_ignoring_case("@name@"sv)) + return MediaFeatureID::@name:titlecase@; +)~~~"); + }); + + generator.append(R"~~~( + return {}; +} + +char const* string_from_media_feature_id(MediaFeatureID media_feature_id) +{ + switch (media_feature_id) {)~~~"); + + json.as_object().for_each_member([&](auto& name, auto&) { + auto member_generator = generator.fork(); + member_generator.set("name", name); + member_generator.set("name:titlecase", title_casify(name)); + member_generator.append(R"~~~( + case MediaFeatureID::@name:titlecase@: + return "@name@";)~~~"); + }); + + generator.append(R"~~~( + } + VERIFY_NOT_REACHED(); +} + +} +)~~~"); + + outln("{}", generator.as_string_view()); + + return 0; +} diff --git a/Meta/Lagom/Tools/CodeGenerators/LibWeb/Generate_CSS_MediaFeatureID_h.cpp b/Meta/Lagom/Tools/CodeGenerators/LibWeb/Generate_CSS_MediaFeatureID_h.cpp new file mode 100644 index 0000000000..b94288345a --- /dev/null +++ b/Meta/Lagom/Tools/CodeGenerators/LibWeb/Generate_CSS_MediaFeatureID_h.cpp @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2022, Sam Atkins <atkinssj@serenityos.org> + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include "GeneratorUtil.h" +#include <AK/SourceGenerator.h> +#include <AK/StringBuilder.h> +#include <LibMain/Main.h> + +ErrorOr<int> serenity_main(Main::Arguments arguments) +{ + if (arguments.argc != 2) { + warnln("usage: {} <path/to/CSS/MediaFeatures.json", arguments.strings[0]); + return 1; + } + + auto json = TRY(read_entire_file_as_json(arguments.strings[1])); + VERIFY(json.is_object()); + + StringBuilder builder; + SourceGenerator generator { builder }; + generator.append(R"~~~(#pragma once + +#include <AK/StringView.h> +#include <AK/Traits.h> + +namespace Web::CSS { + +enum class MediaFeatureID {)~~~"); + + json.as_object().for_each_member([&](auto& name, auto&) { + auto member_generator = generator.fork(); + member_generator.set("name:titlecase", title_casify(name)); + member_generator.append(R"~~~( + @name:titlecase@,)~~~"); + }); + + generator.append(R"~~~( +}; + +Optional<MediaFeatureID> media_feature_id_from_string(StringView); +char const* string_from_media_feature_id(MediaFeatureID); + +} +)~~~"); + + outln("{}", generator.as_string_view()); + + return 0; +} diff --git a/Meta/Lagom/Tools/CodeGenerators/LibWeb/GeneratorUtil.h b/Meta/Lagom/Tools/CodeGenerators/LibWeb/GeneratorUtil.h index b1f20c5d68..7d68df56ce 100644 --- a/Meta/Lagom/Tools/CodeGenerators/LibWeb/GeneratorUtil.h +++ b/Meta/Lagom/Tools/CodeGenerators/LibWeb/GeneratorUtil.h @@ -1,13 +1,16 @@ /* * Copyright (c) 2019-2021, Andreas Kling <kling@serenityos.org> + * Copyright (c) 2022, Samuel Atkins <atkinssj@serenityos.org> * * SPDX-License-Identifier: BSD-2-Clause */ #pragma once +#include <AK/JsonObject.h> #include <AK/String.h> #include <AK/Vector.h> +#include <LibCore/Stream.h> #include <ctype.h> String title_casify(String const& dashy_name) @@ -45,3 +48,13 @@ String camel_casify(StringView dashy_name) } return builder.to_string(); } + +ErrorOr<JsonValue> read_entire_file_as_json(StringView filename) +{ + auto file = TRY(Core::Stream::File::open(filename, Core::Stream::OpenMode::Read)); + auto json_size = TRY(file->size()); + auto json_data = TRY(ByteBuffer::create_uninitialized(json_size)); + if (!file->read_or_error(json_data.bytes())) + return Error::from_string_literal("Failed to read json file."); + return JsonValue::from_string(json_data); +} |