Skip to content

Commit

Permalink
Set up project when the core environment is set up
Browse files Browse the repository at this point in the history
Restart will be required for changing project in the future
  • Loading branch information
mls-m5 committed Jul 27, 2024
1 parent f544c2e commit df2e0ef
Show file tree
Hide file tree
Showing 9 changed files with 82 additions and 45 deletions.
7 changes: 4 additions & 3 deletions src/core/coreenvironment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@
#include <ostream>
#include <string_view>

CoreEnvironment::CoreEnvironment(ThreadContext &context)
CoreEnvironment::CoreEnvironment(ThreadContext &context,
std::filesystem::path projectPath)
: _context{&context}
, _project{std::make_unique<Project>(files().directoryNotifications(),
context.guiQueue())}
, _project{std::make_unique<Project>(
projectPath, files().directoryNotifications(), context.guiQueue())}
, _consoleTtyPath{createFifo(standardConsoleTtyPipePath())}
, _errorTtyPath{createFifo(standardErrorTtyPipePath())}
, _consoleInFile{_consoleTtyPath,
Expand Down
3 changes: 2 additions & 1 deletion src/core/coreenvironment.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
/// Environment shared with all user of the program/server
class CoreEnvironment {
public:
CoreEnvironment(ThreadContext &context);
CoreEnvironment(ThreadContext &context,
std::filesystem::path projectToOpen);
~CoreEnvironment();

CoreEnvironment(const CoreEnvironment &) = delete;
Expand Down
35 changes: 7 additions & 28 deletions src/files/project.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
#include <string_view>
#include <unordered_map>

Project::Project(DirectoryNotifications &directoryNotifications,
Project::Project(std::filesystem::path projectPath,
DirectoryNotifications &directoryNotifications,
IJobQueue &guiQueue)
: _tv{"Project"}
, _guiQueue{&guiQueue} {
Expand All @@ -35,31 +36,9 @@ Project::Project(DirectoryNotifications &directoryNotifications,
});
},
this);
}

std::filesystem::path Project::findRoot(std::filesystem::path arg) const {
_tv();

auto path = std::filesystem::absolute(arg);

do {
if (std::filesystem::exists(path / ProjectSettings::projectFileName)) {
return path;
}

if (std::filesystem::exists(path / ".git")) {
return path;
}

path = path.parent_path();

} while (!path.empty() && path != "/");

if (path.empty() || path == "/") {
return {};
}

return arg.parent_path();
auto projectFile = ProjectSettings::findProject(projectPath, true);
_settings.load(projectFile);
}

void Project::updateCache(const std::filesystem::path &pathInProject,
Expand Down Expand Up @@ -162,9 +141,9 @@ std::vector<std::filesystem::path> Project::findProjectFiles(
#endif

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

if (root.empty()) {
return {};
Expand Down
6 changes: 4 additions & 2 deletions src/files/project.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ class Project {
Unknown,
};

Project(DirectoryNotifications &files, IJobQueue &guiQueue);
Project(std::filesystem::path projectFile,
DirectoryNotifications &files,
IJobQueue &guiQueue);

void updateCache(const std::filesystem::path &pathInProject,
size_t max = 100000);
Expand Down Expand Up @@ -64,7 +66,7 @@ class Project {
ProjectLanguage guessProjectLanguage() const;

// Set the current root
std::filesystem::path findRoot(std::filesystem::path) const;
// std::filesystem::path findRoot(std::filesystem::path) const;

/// When there is no build command provided. Try to guess what it could be
/// based on what files are in the project
Expand Down
54 changes: 51 additions & 3 deletions src/files/projectsettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,10 @@ bool ProjectSettings::load(std::filesystem::path projectFile) {
}
else {
if (projectFile.has_parent_path()) {
root = projectFile.parent_path();
root = std::filesystem::absolute(projectFile.parent_path());
}
else {
root = findRoot(projectFile);
}
}

Expand Down Expand Up @@ -115,7 +118,7 @@ void ProjectSettings::save() {
}

std::filesystem::path ProjectSettings::findProject(
std::filesystem::path fileInPath) {
std::filesystem::path fileInPath, bool createIfDoesNotExist) {
auto projects = fetchRecentProjects();
for (auto &project : projects) {
if (fileInPath.parent_path().string().starts_with(
Expand All @@ -124,7 +127,19 @@ std::filesystem::path ProjectSettings::findProject(
}
}

return {};
auto foundRoot = findRoot(fileInPath);
if (foundRoot.empty()) {
return {};
}

if (std::filesystem::exists(foundRoot / projectFileName)) {
return foundRoot / projectFileName;
}

if (!createIfDoesNotExist) {
return {};
}
return createAnonymousProject(foundRoot);
}

std::vector<ProjectSettings> ProjectSettings::fetchRecentProjects() {
Expand Down Expand Up @@ -179,3 +194,36 @@ void ProjectSettings::makeProjectAvailable(std::filesystem::path path) {
file << item.string() << "\n";
}
}

std::filesystem::path ProjectSettings::findRoot(std::filesystem::path arg) {
auto path = std::filesystem::absolute(arg);

do {
if (std::filesystem::exists(path / ProjectSettings::projectFileName)) {
return path;
}

if (std::filesystem::exists(path / ".git")) {
return path;
}

path = path.parent_path();

} while (!path.empty() && path != "/");

if (path.empty() || path == "/") {
return {};
}

return arg.parent_path();
}

std::filesystem::path ProjectSettings::createAnonymousProject(
std::filesystem::path root) {
auto settings = ProjectSettings{};
settings.settingsPath = (projectDirectory() / root.stem().string())
.replace_extension(projectExtension);
settings.save();
settings.makeProjectAvailable(settings.settingsPath);
return settings.settingsPath;
}
8 changes: 7 additions & 1 deletion src/files/projectsettings.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,18 @@ struct ProjectSettings {
bool load(std::filesystem::path path);
void save();

std::filesystem::path findProject(std::filesystem::path fileInPath);
static std::filesystem::path findProject(std::filesystem::path fileInPath,
bool createIfDoesNotExist);

static std::vector<ProjectSettings> fetchRecentProjects();
static void makeProjectAvailable(std::filesystem::path settingsPath);

static inline const std::string_view projectFileName = ".medit.json";
// When the project files are named
static inline const std::string_view projectExtension = ".medit";

static std::filesystem::path findRoot(std::filesystem::path);

static std::filesystem::path createAnonymousProject(
std::filesystem::path root);
};
6 changes: 5 additions & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "settings.h"
#include "thinmain.h"
#include "views/mainwindow.h"
#include <filesystem>
#include <iostream>
#include <string>
#include <thread>
Expand Down Expand Up @@ -120,6 +121,9 @@ struct User {
if (settings.file.empty()) {
mainWindow->updateLocatorBuffer();
}
else if (std::filesystem::is_directory(settings.file)) {
// Handled elsewhere
}
else {
mainWindow->open(settings.file);
mainWindow->currentEditor()->removeEmptyUnsavedBufferHistory();
Expand Down Expand Up @@ -203,7 +207,7 @@ void MainData::start(const Settings &settings) {

context = std::make_shared<ThreadContext>(*jobQueue, *guiQueue, *timer);

mainData.core = std::make_unique<CoreEnvironment>(*context);
mainData.core = std::make_unique<CoreEnvironment>(*context, settings.file);

registerDefaultPlugins(*core);
core->plugins().sort();
Expand Down
6 changes: 1 addition & 5 deletions src/views/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,7 @@ MainWindow::MainWindow(CoreEnvironment &core,
screen.palette(palette);
}

_locator->visible(false); // #include "views/commandpalette.h"
// #include "views/completeview.h"
// #include "views/editor.h"
// #include "views/locator.h"

_locator->visible(false);
_locator->mode(createInsertMode());
_locator->showLines(false);

Expand Down
2 changes: 1 addition & 1 deletion test/project_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ TEST_CASE("load") {

auto jobQueue = JobQueue{};

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

project.updateCache(path);

Expand Down

0 comments on commit df2e0ef

Please sign in to comment.