mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-06-12 02:30:30 +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:
parent
3e6c083754
commit
9096da19f1
Notes:
sideshowbarker
2024-07-17 11:48:06 +09:00
Author: https://github.com/mrkct
Commit: 9096da19f1
Pull-request: https://github.com/SerenityOS/serenity/pull/13155
Reviewed-by: https://github.com/itamar8910 ✅
7 changed files with 106 additions and 0 deletions
|
@ -44,6 +44,7 @@ set(SOURCES
|
||||||
Locator.cpp
|
Locator.cpp
|
||||||
Project.cpp
|
Project.cpp
|
||||||
ProjectBuilder.cpp
|
ProjectBuilder.cpp
|
||||||
|
ProjectConfig.cpp
|
||||||
ProjectDeclarations.cpp
|
ProjectDeclarations.cpp
|
||||||
ProjectFile.cpp
|
ProjectFile.cpp
|
||||||
ProjectTemplate.cpp
|
ProjectTemplate.cpp
|
||||||
|
|
|
@ -66,4 +66,13 @@ bool Project::project_is_serenity() const
|
||||||
return m_root_path.ends_with("Source/serenity");
|
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();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,6 +6,7 @@
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include "ProjectConfig.h"
|
||||||
#include "ProjectFile.h"
|
#include "ProjectFile.h"
|
||||||
#include <AK/LexicalPath.h>
|
#include <AK/LexicalPath.h>
|
||||||
#include <AK/Noncopyable.h>
|
#include <AK/Noncopyable.h>
|
||||||
|
@ -32,6 +33,9 @@ public:
|
||||||
String to_absolute_path(String const&) const;
|
String to_absolute_path(String const&) const;
|
||||||
bool project_is_serenity() const;
|
bool project_is_serenity() const;
|
||||||
|
|
||||||
|
static constexpr StringView config_file_path = ".hackstudio/config.json";
|
||||||
|
NonnullOwnPtr<ProjectConfig> config() const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
explicit Project(String const& root_path);
|
explicit Project(String const& root_path);
|
||||||
|
|
||||||
|
|
|
@ -16,6 +16,7 @@ namespace HackStudio {
|
||||||
|
|
||||||
ProjectBuilder::ProjectBuilder(NonnullRefPtr<TerminalWrapper> terminal, Project const& project)
|
ProjectBuilder::ProjectBuilder(NonnullRefPtr<TerminalWrapper> terminal, Project const& project)
|
||||||
: m_project_root(project.root_path())
|
: m_project_root(project.root_path())
|
||||||
|
, m_project(project)
|
||||||
, m_terminal(move(terminal))
|
, m_terminal(move(terminal))
|
||||||
, m_is_serenity(project.project_is_serenity() ? IsSerenityRepo::Yes : IsSerenityRepo::No)
|
, 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)
|
ErrorOr<void> ProjectBuilder::build(StringView active_file)
|
||||||
{
|
{
|
||||||
m_terminal->clear_including_history();
|
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())
|
if (active_file.is_null())
|
||||||
return Error::from_string_literal("no active file"sv);
|
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)
|
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())
|
if (active_file.is_null())
|
||||||
return Error::from_string_literal("no active file"sv);
|
return Error::from_string_literal("no active file"sv);
|
||||||
|
|
||||||
|
|
|
@ -49,6 +49,7 @@ private:
|
||||||
static ErrorOr<void> verify_cmake_is_installed();
|
static ErrorOr<void> verify_cmake_is_installed();
|
||||||
|
|
||||||
String m_project_root;
|
String m_project_root;
|
||||||
|
Project const& m_project;
|
||||||
NonnullRefPtr<TerminalWrapper> m_terminal;
|
NonnullRefPtr<TerminalWrapper> m_terminal;
|
||||||
IsSerenityRepo m_is_serenity { IsSerenityRepo::No };
|
IsSerenityRepo m_is_serenity { IsSerenityRepo::No };
|
||||||
String m_serenity_component_cmake_file;
|
String m_serenity_component_cmake_file;
|
||||||
|
|
46
Userland/DevTools/HackStudio/ProjectConfig.cpp
Normal file
46
Userland/DevTools/HackStudio/ProjectConfig.cpp
Normal 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() };
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
33
Userland/DevTools/HackStudio/ProjectConfig.h
Normal file
33
Userland/DevTools/HackStudio/ProjectConfig.h
Normal 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;
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue