2020-01-18 11:38:21 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 11:24:48 +03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 11:38:21 +03:00
|
|
|
*/
|
|
|
|
|
2019-10-21 19:46:55 +03:00
|
|
|
#include "Project.h"
|
2020-08-10 23:28:38 +03:00
|
|
|
#include "HackStudio.h"
|
2023-03-21 18:35:30 +03:00
|
|
|
#include <LibFileSystem/FileSystem.h>
|
2019-12-23 02:14:24 +03:00
|
|
|
|
2020-08-17 16:22:30 +03:00
|
|
|
namespace HackStudio {
|
|
|
|
|
2022-12-04 21:02:33 +03:00
|
|
|
Project::Project(DeprecatedString const& root_path)
|
2020-12-10 20:59:03 +03:00
|
|
|
: m_root_path(root_path)
|
2019-10-21 19:46:55 +03:00
|
|
|
{
|
2020-12-10 20:59:03 +03:00
|
|
|
m_model = GUI::FileSystemModel::create(root_path, GUI::FileSystemModel::Mode::FilesAndDirectories);
|
2019-12-23 02:14:24 +03:00
|
|
|
}
|
|
|
|
|
2022-12-04 21:02:33 +03:00
|
|
|
OwnPtr<Project> Project::open_with_root_path(DeprecatedString const& root_path)
|
2019-10-21 19:46:55 +03:00
|
|
|
{
|
2023-03-21 18:35:30 +03:00
|
|
|
if (!FileSystem::is_directory(root_path))
|
2021-01-11 02:29:28 +03:00
|
|
|
return {};
|
2020-12-10 20:59:03 +03:00
|
|
|
return adopt_own(*new Project(root_path));
|
2019-10-26 22:27:57 +03:00
|
|
|
}
|
|
|
|
|
2020-12-10 20:59:03 +03:00
|
|
|
template<typename Callback>
|
|
|
|
static void traverse_model(const GUI::FileSystemModel& model, const GUI::ModelIndex& index, Callback callback)
|
2019-12-28 07:24:30 +03:00
|
|
|
{
|
2020-12-10 20:59:03 +03:00
|
|
|
if (index.is_valid())
|
|
|
|
callback(index);
|
|
|
|
auto row_count = model.row_count(index);
|
|
|
|
if (!row_count)
|
|
|
|
return;
|
|
|
|
for (int row = 0; row < row_count; ++row) {
|
|
|
|
auto child_index = model.index(row, GUI::FileSystemModel::Column::Name, index);
|
|
|
|
traverse_model(model, child_index, callback);
|
2019-10-26 22:27:57 +03:00
|
|
|
}
|
2019-10-21 19:46:55 +03:00
|
|
|
}
|
2019-10-27 21:39:15 +03:00
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
void Project::for_each_text_file(Function<void(ProjectFile const&)> callback) const
|
2019-10-27 21:39:15 +03:00
|
|
|
{
|
2020-12-10 20:59:03 +03:00
|
|
|
traverse_model(model(), {}, [&](auto& index) {
|
2021-09-08 21:36:34 +03:00
|
|
|
auto file = create_file(model().full_path(index));
|
2021-07-05 19:51:26 +03:00
|
|
|
callback(*file);
|
2020-12-10 20:59:03 +03:00
|
|
|
});
|
2020-03-13 16:38:21 +03:00
|
|
|
}
|
|
|
|
|
2022-12-04 21:02:33 +03:00
|
|
|
NonnullRefPtr<ProjectFile> Project::create_file(DeprecatedString const& path) const
|
2019-12-23 02:14:24 +03:00
|
|
|
{
|
2021-03-05 17:42:32 +03:00
|
|
|
auto full_path = to_absolute_path(path);
|
2021-08-12 23:22:44 +03:00
|
|
|
return ProjectFile::construct_with_name(full_path);
|
2019-12-23 02:14:24 +03:00
|
|
|
}
|
2020-08-17 16:22:30 +03:00
|
|
|
|
2022-12-04 21:02:33 +03:00
|
|
|
DeprecatedString Project::to_absolute_path(DeprecatedString const& path) const
|
2021-03-05 17:42:32 +03:00
|
|
|
{
|
|
|
|
if (LexicalPath { path }.is_absolute()) {
|
|
|
|
return path;
|
|
|
|
}
|
2022-12-04 21:02:33 +03:00
|
|
|
return LexicalPath { DeprecatedString::formatted("{}/{}", m_root_path, path) }.string();
|
2021-03-05 17:42:32 +03:00
|
|
|
}
|
|
|
|
|
2022-01-07 17:27:13 +03:00
|
|
|
bool Project::project_is_serenity() const
|
|
|
|
{
|
|
|
|
// FIXME: Improve this heuristic
|
|
|
|
// Running "Meta/serenity.sh copy-src" installs the serenity repository at this path in the home directory
|
2022-07-11 20:32:29 +03:00
|
|
|
return m_root_path.ends_with("Source/serenity"sv);
|
2022-01-07 17:27:13 +03:00
|
|
|
}
|
|
|
|
|
2022-03-19 01:17:22 +03:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2020-08-17 16:22:30 +03:00
|
|
|
}
|