Serenity Operating System
at hosted 124 lines 4.3 kB view raw
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 AK::dbg_raw(true); 38 39 if (argc != 2) { 40 printf("usage: %s <form-file>\n", argv[0]); 41 return 0; 42 } 43 44 auto file = Core::File::construct(argv[1]); 45 if (!file->open(Core::IODevice::ReadOnly)) { 46 fprintf(stderr, "Error: Cannot open %s: %s\n", argv[1], file->error_string()); 47 return 1; 48 } 49 50 auto file_contents = file->read_all(); 51 auto json = JsonValue::from_string(file_contents); 52 53 if (!json.is_object()) { 54 fprintf(stderr, "Malformed input\n"); 55 return 1; 56 } 57 58 auto name = json.as_object().get("name").to_string(); 59 auto widgets = json.as_object().get("widgets"); 60 61 if (!widgets.is_array()) { 62 fprintf(stderr, "Malformed input\n"); 63 return 1; 64 } 65 66 out() << "#pragma once"; 67 68 widgets.as_array().for_each([&](auto& value) { 69 const JsonObject& widget_object = value.as_object(); 70 auto class_name = widget_object.get("class").to_string(); 71 StringBuilder builder; 72 auto parts = class_name.split(':'); 73 builder.append(parts.last()); 74 out() << "#include <LibGUI/" << builder.to_string() << ".h>"; 75 }); 76 77 out() << "struct UI_" << name << " {"; 78 out() << " RefPtr<GUI::Widget> main_widget;"; 79 80 widgets.as_array().for_each([&](auto& value) { 81 ASSERT(value.is_object()); 82 const JsonObject& widget_object = value.as_object(); 83 auto name = widget_object.get("name").to_string(); 84 auto class_name = widget_object.get("class").to_string(); 85 out() << " RefPtr<" << class_name << "> " << name << ";"; 86 }); 87 88 out() << " UI_" << name << "();"; 89 90 out() << "};"; 91 92 out() << "UI_" << name << "::UI_" << name << "()"; 93 out() << "{"; 94 95 out() << " main_widget = GUI::Widget::construct();"; 96 out() << " main_widget->set_fill_with_background_color(true);"; 97 98 widgets.as_array().for_each([&](auto& value) { 99 ASSERT(value.is_object()); 100 const JsonObject& widget_object = value.as_object(); 101 auto name = widget_object.get("name").to_string(); 102 auto class_name = widget_object.get("class").to_string(); 103 out() << " " << name << " = main_widget->add<" << class_name << ">();"; 104 105 widget_object.for_each_member([&](auto& property_name, const JsonValue& property_value) { 106 if (property_name == "class") 107 return; 108 109 String value; 110 111 if (property_value.is_null()) 112 value = "{}"; 113 else 114 value = property_value.serialized<StringBuilder>(); 115 116 out() << " " << name << "->set_" << property_name << "(" << value << ");"; 117 }); 118 119 out() << ""; 120 }); 121 out() << "}"; 122 123 return 0; 124}