Serenity Operating System
1/*
2 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice, this
9 * list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
22 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#include <AK/JsonArray.h>
28#include <AK/JsonObject.h>
29#include <AK/JsonValue.h>
30#include <AK/LogStream.h>
31#include <AK/StringBuilder.h>
32#include <LibCore/File.h>
33#include <stdio.h>
34
35int main(int argc, char** argv)
36{
37 if (argc != 2) {
38 printf("usage: %s <form-file>\n", argv[0]);
39 return 0;
40 }
41
42 auto file = Core::File::construct(argv[1]);
43 if (!file->open(Core::IODevice::ReadOnly)) {
44 fprintf(stderr, "Error: Cannot open %s: %s\n", argv[1], file->error_string());
45 return 1;
46 }
47
48 auto file_contents = file->read_all();
49 auto json = JsonValue::from_string(file_contents);
50
51 if (!json.is_object()) {
52 fprintf(stderr, "Malformed input\n");
53 return 1;
54 }
55
56 auto name = json.as_object().get("name").to_string();
57 auto widgets = json.as_object().get("widgets");
58
59 if (!widgets.is_array()) {
60 fprintf(stderr, "Malformed input\n");
61 return 1;
62 }
63
64 dbg() << "#pragma once";
65
66 widgets.as_array().for_each([&](auto& value) {
67 const JsonObject& widget_object = value.as_object();
68 auto class_name = widget_object.get("class").to_string();
69 StringBuilder builder;
70 auto parts = class_name.split(':');
71 builder.append(parts.last());
72 dbg() << "#include <LibGUI/" << builder.to_string() << ".h>";
73 });
74
75 dbg() << "struct UI_" << name << " {";
76 dbg() << " RefPtr<GUI::Widget> main_widget;";
77
78 widgets.as_array().for_each([&](auto& value) {
79 ASSERT(value.is_object());
80 const JsonObject& widget_object = value.as_object();
81 auto name = widget_object.get("name").to_string();
82 auto class_name = widget_object.get("class").to_string();
83 dbg() << " RefPtr<" << class_name << "> " << name << ";";
84 });
85
86 dbg() << " UI_" << name << "();";
87
88 dbg() << "};";
89
90 dbg() << "UI_" << name << "::UI_" << name << "()";
91 dbg() << "{";
92
93 dbg() << " main_widget = GUI::Widget::construct();";
94 dbg() << " main_widget->set_fill_with_background_color(true);";
95
96 widgets.as_array().for_each([&](auto& value) {
97 ASSERT(value.is_object());
98 const JsonObject& widget_object = value.as_object();
99 auto name = widget_object.get("name").to_string();
100 auto class_name = widget_object.get("class").to_string();
101 dbg() << " " << name << " = main_widget->add<" << class_name << ">();";
102
103 widget_object.for_each_member([&](auto& property_name, const JsonValue& property_value) {
104 if (property_name == "class")
105 return;
106
107 String value;
108
109 if (property_value.is_null())
110 value = "{}";
111 else
112 value = property_value.serialized<StringBuilder>();
113
114 dbg() << " " << name << "->set_" << property_name << "(" << value << ");";
115 });
116
117 dbg() << "";
118 });
119 dbg() << "}";
120
121 return 0;
122}