Skip to content

Commit

Permalink
Update the cache from the root dir in project settings
Browse files Browse the repository at this point in the history
  • Loading branch information
mls-m5 committed Jul 27, 2024
1 parent df2e0ef commit dc0efb9
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 33 deletions.
2 changes: 1 addition & 1 deletion src/core/coreenvironment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ CoreEnvironment::CoreEnvironment(ThreadContext &context,
std::ofstream{_consoleTtyPath.path()}
<< std::endl; // Prevent the listen file from waiting

_project->updateCache(std::filesystem::current_path());
_project->updateCache();

subscribeToLog([this](LogType type, std::string_view str) {
printToAllLogs(type, std::string{str});
Expand Down
10 changes: 4 additions & 6 deletions src/files/project.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,10 @@ Project::Project(std::filesystem::path projectPath,
_settings.load(projectFile);
}

void Project::updateCache(const std::filesystem::path &pathInProject,
size_t max) {
void Project::updateCache(size_t max) {
_tv();
_fileCache = findProjectFiles(pathInProject, max);
loadProjectFile();
_fileCache = findProjectFiles(_settings.root, max);
if (_settings.buildCommand.empty()) {
_settings.buildCommand = guessBuildCommand();
}
Expand Down Expand Up @@ -141,9 +140,6 @@ std::vector<std::filesystem::path> Project::findProjectFiles(
#endif

auto &root = _settings.root;
// if (root.empty()) {
// root = this->findRoot(pathInProject);
// }

if (root.empty()) {
return {};
Expand Down Expand Up @@ -190,6 +186,8 @@ std::vector<std::filesystem::path> Project::findProjectFiles(
}
}

paths.push_back(_settings.settingsPath);

_extensions.clear();
_extensions.assign(extensions.begin(), extensions.end());
std::sort(_extensions.begin(), _extensions.end(), [](auto &a, auto &b) {
Expand Down
3 changes: 1 addition & 2 deletions src/files/project.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ class Project {
DirectoryNotifications &files,
IJobQueue &guiQueue);

void updateCache(const std::filesystem::path &pathInProject,
size_t max = 100000);
void updateCache(size_t max = 100000);

const ProjectSettings &settings() const {
return _settings;
Expand Down
62 changes: 41 additions & 21 deletions src/files/projectsettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <fstream>
#include <iostream>
#include <json/json.h>
#include <stdexcept>
#include <string>
#include <vector>

Expand Down Expand Up @@ -44,7 +45,12 @@ bool ProjectSettings::load(std::filesystem::path projectFile) {

auto json = Json{};
try {
std::fstream{projectFile} >> json;
auto file = std::ifstream{projectFile};
if (!file.is_open()) {
throw std::runtime_error{"could not open file " +
projectFile.string()};
}
file >> json;
}
catch (Json::ParsingError &error) {
logStatusMessage(error.what());
Expand All @@ -65,31 +71,31 @@ bool ProjectSettings::load(std::filesystem::path projectFile) {
}
}

// TODO: Add more multiple build configuration
// TODO: Add multiple build configuration
if (auto it = json.find("build"); it != json.end()) {
buildCommand = it->value;
}

// TODO: Add more multiple build configuration
// TODO: Add multiple build configuration
if (auto it = json.find("run"); it != json.end()) {
runCommand = it->value;
}

if (auto it = json.find("flags"); it != json.end()) {
flags.clear();
// if (auto it = json.find("flags"); it != json.end()) {
// flags.clear();

if (it->type == Json::String) {
flags.push_back(it->value);
}
else if (it->type == Json::Array) {
flags.clear();
for (auto &value : *it) {
if (value.type == Json::String) {
flags.push_back(translateInclude(value.value, root));
}
}
}
}
// if (it->type == Json::String) {
// flags.push_back(it->value);
// }
// else if (it->type == Json::Array) {
// flags.clear();
// for (auto &value : *it) {
// if (value.type == Json::String) {
// flags.push_back(translateInclude(value.value, root));
// }
// }
// }
// }

if (auto it = json.find("debug"); it != json.end()) {
if (auto command = it->find("command"); command != it->end()) {
Expand All @@ -104,16 +110,26 @@ bool ProjectSettings::load(std::filesystem::path projectFile) {
}

void ProjectSettings::save() {
std::cerr << "saving project is not implemented yet" << std::endl;
// std::cerr << "saving project is not implemented yet" << std::endl;

auto json = Json{};

auto &flags = json["flags"] = Json{Json::Array};
// auto &flags = json["flags"];
// flags.type = Json::Array;

for (auto &flag : flags) {
flags.push_back(flag);
// for (auto &flag : flags) {
// flags.push_back(flag);
// }

if (settingsPath.empty()) {
// Cannot save project
return;
}

json["root"] = root;

std::ofstream{settingsPath} << json;

// TODO: Continue implementing this
}

Expand Down Expand Up @@ -196,6 +212,9 @@ void ProjectSettings::makeProjectAvailable(std::filesystem::path path) {
}

std::filesystem::path ProjectSettings::findRoot(std::filesystem::path arg) {
if (arg.empty()) {
arg = std::filesystem::current_path();
}
auto path = std::filesystem::absolute(arg);

do {
Expand Down Expand Up @@ -223,6 +242,7 @@ std::filesystem::path ProjectSettings::createAnonymousProject(
auto settings = ProjectSettings{};
settings.settingsPath = (projectDirectory() / root.stem().string())
.replace_extension(projectExtension);
settings.root = root;
settings.save();
settings.makeProjectAvailable(settings.settingsPath);
return settings.settingsPath;
Expand Down
4 changes: 2 additions & 2 deletions src/views/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ MainWindow::MainWindow(CoreEnvironment &core,
_inputFocus = editor;
});

updateLocatorBuffer();
// updateLocatorBuffer();
}

MainWindow::~MainWindow() {
Expand Down Expand Up @@ -262,7 +262,7 @@ bool MainWindow::keyPress(std::shared_ptr<IEnvironment> env) {

void MainWindow::updateLocatorBuffer() {
auto &project = _env->core().project();
project.updateCache(std::filesystem::current_path());
project.updateCache();
_env->core().files().directoryNotifications().path(
project.settings()
.root); // TODO: This should probably be handled somewhere else
Expand Down
2 changes: 1 addition & 1 deletion test/project_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ TEST_CASE("load") {

auto project = Project{path, dirNotifications, jobQueue};

project.updateCache(path);
project.updateCache();

ASSERT_EQ(project.settings().root, std::filesystem::absolute(root));

Expand Down

0 comments on commit dc0efb9

Please sign in to comment.