1
0
Fork 0
mirror of https://github.com/LadybirdBrowser/ladybird.git synced 2025-06-09 17:44:56 +09:00

HackStudio: Allow customizing the actions of the Build & Run buttons

This commit introduces per-project settings that can be customized
through a JSON file placed in '.hackstudio/config.json' in the
project's root
This commit is contained in:
Marco Cutecchia 2022-03-18 23:17:22 +01:00 committed by Andreas Kling
parent 3e6c083754
commit 9096da19f1
Notes: sideshowbarker 2024-07-17 11:48:06 +09:00
7 changed files with 106 additions and 0 deletions

View file

@ -44,6 +44,7 @@ set(SOURCES
Locator.cpp
Project.cpp
ProjectBuilder.cpp
ProjectConfig.cpp
ProjectDeclarations.cpp
ProjectFile.cpp
ProjectTemplate.cpp

View file

@ -66,4 +66,13 @@ bool Project::project_is_serenity() const
return m_root_path.ends_with("Source/serenity");
}
NonnullOwnPtr<ProjectConfig> Project::config() const
{
auto config_or_error = ProjectConfig::try_load_project_config(LexicalPath::absolute_path(m_root_path, config_file_path));
if (config_or_error.is_error())
return ProjectConfig::create_empty();
return config_or_error.release_value();
}
}

View file

@ -6,6 +6,7 @@
#pragma once
#include "ProjectConfig.h"
#include "ProjectFile.h"
#include <AK/LexicalPath.h>
#include <AK/Noncopyable.h>
@ -32,6 +33,9 @@ public:
String to_absolute_path(String const&) const;
bool project_is_serenity() const;
static constexpr StringView config_file_path = ".hackstudio/config.json";
NonnullOwnPtr<ProjectConfig> config() const;
private:
explicit Project(String const& root_path);

View file

@ -16,6 +16,7 @@ namespace HackStudio {
ProjectBuilder::ProjectBuilder(NonnullRefPtr<TerminalWrapper> terminal, Project const& project)
: m_project_root(project.root_path())
, m_project(project)
, m_terminal(move(terminal))
, m_is_serenity(project.project_is_serenity() ? IsSerenityRepo::Yes : IsSerenityRepo::No)
{
@ -24,6 +25,12 @@ ProjectBuilder::ProjectBuilder(NonnullRefPtr<TerminalWrapper> terminal, Project
ErrorOr<void> ProjectBuilder::build(StringView active_file)
{
m_terminal->clear_including_history();
if (auto command = m_project.config()->build_command(); command.has_value()) {
TRY(m_terminal->run_command(command.value()));
return {};
}
if (active_file.is_null())
return Error::from_string_literal("no active file"sv);
@ -44,6 +51,11 @@ ErrorOr<void> ProjectBuilder::build(StringView active_file)
ErrorOr<void> ProjectBuilder::run(StringView active_file)
{
if (auto command = m_project.config()->run_command(); command.has_value()) {
TRY(m_terminal->run_command(command.value()));
return {};
}
if (active_file.is_null())
return Error::from_string_literal("no active file"sv);

View file

@ -49,6 +49,7 @@ private:
static ErrorOr<void> verify_cmake_is_installed();
String m_project_root;
Project const& m_project;
NonnullRefPtr<TerminalWrapper> m_terminal;
IsSerenityRepo m_is_serenity { IsSerenityRepo::No };
String m_serenity_component_cmake_file;

View file

@ -0,0 +1,46 @@
/*
* Copyright (c) 2022, the SerenityOS developers.
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include "ProjectConfig.h"
#include <AK/NonnullOwnPtr.h>
#include <LibCore/File.h>
namespace HackStudio {
ProjectConfig::ProjectConfig(JsonObject config)
: m_config(move(config))
{
}
ErrorOr<NonnullOwnPtr<ProjectConfig>> ProjectConfig::try_load_project_config(String path)
{
auto file = TRY(Core::File::open(path, Core::OpenMode::ReadOnly));
auto file_contents = file->read_all();
file->close();
auto json = TRY(JsonValue::from_string(StringView { file_contents }));
if (!json.is_object())
return Error::from_string_literal("The topmost JSON element is not an object");
return adopt_own(*new ProjectConfig(json.as_object()));
}
NonnullOwnPtr<ProjectConfig> ProjectConfig::create_empty()
{
JsonObject empty {};
return adopt_own(*new ProjectConfig(empty));
}
Optional<String> ProjectConfig::read_key(String key_name) const
{
auto const& value = m_config.get(key_name);
if (!value.is_string())
return {};
return { value.as_string() };
}
}

View file

@ -0,0 +1,33 @@
/*
* Copyright (c) 2022, the SerenityOS developers.
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/Error.h>
#include <AK/JsonObject.h>
#include <AK/Optional.h>
#include <AK/OwnPtr.h>
#include <LibCore/Object.h>
namespace HackStudio {
class ProjectConfig {
public:
static ErrorOr<NonnullOwnPtr<ProjectConfig>> try_load_project_config(String path);
static NonnullOwnPtr<ProjectConfig> create_empty();
ProjectConfig(JsonObject);
Optional<String> build_command() const { return read_key("build_command"); }
Optional<String> run_command() const { return read_key("run_command"); }
private:
Optional<String> read_key(String key_name) const;
JsonObject m_config;
};
}