浏览代码

HackStudio: Add a "Project Configuration" button in the Edit menu

Marco Cutecchia 3 年之前
父节点
当前提交
a7ba8677cd
共有 2 个文件被更改,包括 34 次插入0 次删除
  1. 32 0
      Userland/DevTools/HackStudio/HackStudioWidget.cpp
  2. 2 0
      Userland/DevTools/HackStudio/HackStudioWidget.h

+ 32 - 0
Userland/DevTools/HackStudio/HackStudioWidget.cpp

@@ -137,6 +137,8 @@ HackStudioWidget::HackStudioWidget(String path_to_project)
         m_stop_action->set_enabled(false);
     };
 
+    m_open_project_configuration_action = create_open_project_configuration_action();
+
     m_build_action = create_build_action();
     m_run_action = create_run_action();
     m_stop_action = create_stop_action();
@@ -1366,6 +1368,9 @@ void HackStudioWidget::create_edit_menu(GUI::Window& window)
     });
     vim_emulation_setting_action->set_checked(false);
     edit_menu.add_action(vim_emulation_setting_action);
+
+    edit_menu.add_separator();
+    edit_menu.add_action(*m_open_project_configuration_action);
 }
 
 void HackStudioWidget::create_build_menu(GUI::Window& window)
@@ -1648,6 +1653,33 @@ void HackStudioWidget::create_location_history_actions()
     m_locations_history_forward_action->set_enabled(false);
 }
 
+NonnullRefPtr<GUI::Action> HackStudioWidget::create_open_project_configuration_action()
+{
+    return GUI::Action::create("Project Configuration", Gfx::Bitmap::try_load_from_file("/res/icons/16x16/settings.png").release_value(), [&](auto&) {
+        auto parent_directory = LexicalPath::dirname(Project::config_file_path);
+        auto absolute_config_file_path = LexicalPath::absolute_path(m_project->root_path(), Project::config_file_path);
+
+        if (!Core::File::exists(absolute_config_file_path)) {
+            if (Core::File::exists(parent_directory) && !Core::File::is_directory(parent_directory)) {
+                GUI::MessageBox::show_error(window(), String::formatted("Cannot create the '{}' directory because there is already a file with that name", parent_directory));
+                return;
+            }
+
+            mkdir(LexicalPath::absolute_path(m_project->root_path(), parent_directory).characters(), 0755);
+
+            auto file = Core::File::open(absolute_config_file_path, Core::OpenMode::WriteOnly);
+            file.value()->write(
+                "{\n"
+                "    \"build_command\": \"your build command here\",\n"
+                "    \"run_command\": \"your run command here\"\n"
+                "}\n");
+            file.value()->close();
+        }
+
+        open_file(Project::config_file_path);
+    });
+}
+
 HackStudioWidget::ProjectLocation HackStudioWidget::current_project_location() const
 {
     return ProjectLocation { current_editor_wrapper().filename(), current_editor().cursor().line(), current_editor().cursor().column() };

+ 2 - 0
Userland/DevTools/HackStudio/HackStudioWidget.h

@@ -120,6 +120,7 @@ private:
     NonnullRefPtr<GUI::Action> create_run_action();
     NonnullRefPtr<GUI::Action> create_stop_action();
     NonnullRefPtr<GUI::Action> create_toggle_syntax_highlighting_mode_action();
+    NonnullRefPtr<GUI::Action> create_open_project_configuration_action();
     void create_location_history_actions();
 
     void add_new_editor_tab_widget(GUI::Widget& parent);
@@ -238,6 +239,7 @@ private:
     RefPtr<GUI::Action> m_locations_history_back_action;
     RefPtr<GUI::Action> m_locations_history_forward_action;
     RefPtr<GUI::Action> m_toggle_semantic_highlighting_action;
+    RefPtr<GUI::Action> m_open_project_configuration_action;
 
     RefPtr<Gfx::Font> read_editor_font_from_config();
     void change_editor_font(RefPtr<Gfx::Font>);