summaryrefslogtreecommitdiff
path: root/core/src/Model/Project.cpp
diff options
context:
space:
mode:
authorrtk0c <[email protected]>2021-05-30 23:00:41 -0700
committerrtk0c <[email protected]>2021-05-30 23:00:41 -0700
commitc90f78df080a9891930ee346b0ad87498ba5b697 (patch)
treec4f4c475086337e25cbb985625423591c47310e1 /core/src/Model/Project.cpp
parent088da97531935a61870ecada10f06b9b9a8255d1 (diff)
Initial work on templates UI
Diffstat (limited to 'core/src/Model/Project.cpp')
-rw-r--r--core/src/Model/Project.cpp134
1 files changed, 101 insertions, 33 deletions
diff --git a/core/src/Model/Project.cpp b/core/src/Model/Project.cpp
index 2d7c82a..3cb3cb4 100644
--- a/core/src/Model/Project.cpp
+++ b/core/src/Model/Project.cpp
@@ -25,19 +25,19 @@ void ReadItemList(ItemList<T>& list, const fs::path& filePath)
}
}
-Project::Project(const fs::path& rootPath)
- : mRootPath{ rootPath }
+Project::Project(fs::path rootPath)
+ : mRootPath{ std::move(rootPath) }
, mRootPathString{ mRootPath.string() }
, Database(*this)
{
// TODO better diagnostic
const char* kInvalidFormatErr = "Failed to load project: invalid format.";
- std::ifstream ifs(rootPath / "cplt_project.json");
+ std::ifstream ifs(mRootPath / "cplt_project.json");
if (!ifs) {
std::string message;
message += "Failed to load project file at '";
- message += rootPath.string();
+ message += mRootPath.string();
message += "'.";
throw std::runtime_error(message);
}
@@ -81,7 +81,7 @@ Project::Project(const fs::path& rootPath)
}
}
-Project::Project(std::filesystem::path rootPath, std::string name)
+Project::Project(fs::path rootPath, std::string name)
: mRootPath{ std::move(rootPath) }
, mRootPathString{ mRootPath.string() }
, mName{ std::move(name) }
@@ -99,6 +99,36 @@ const std::string& Project::GetPathString() const
return mRootPathString;
}
+fs::path Project::GetDatabasesDirectory() const
+{
+ return mRootPath / "databases";
+}
+
+fs::path Project::GetItemsDirectory() const
+{
+ return mRootPath / "items";
+}
+
+fs::path Project::GetWorkflowsDirectory() const
+{
+ return mRootPath / "workflows";
+}
+
+fs::path Project::GetWorkflowPath(std::string_view name) const
+{
+ return (mRootPath / "workflows" / name).concat(".cplt-workflow");
+}
+
+fs::path Project::GetTemplatesDirectory() const
+{
+ return mRootPath / "templates";
+}
+
+fs::path Project::GetTemplatePath(std::string_view name) const
+{
+ return (mRootPath / "templates" / name).concat(".cplt-template");
+}
+
const std::string& Project::GetName() const
{
return mName;
@@ -109,7 +139,7 @@ void Project::SetName(std::string name)
mName = std::move(name);
}
-const decltype(Project::mWorkflows)& Project::GetWorkflows() const
+const tsl::array_map<char, WorkflowInfo>& Project::GetWorkflows() const
{
return mWorkflows;
}
@@ -132,11 +162,12 @@ std::unique_ptr<Workflow> Project::CreateWorkflow(std::string_view name)
}
auto workflow = std::make_unique<Workflow>();
- auto [it, DISCARD] = mWorkflows.insert(name, WorkflowInfo{});
- auto& info = it.value();
-
- info.Name = name;
- info.Path = GetWorkflowPath(name);
+ auto [it, DISCARD] = mWorkflows.insert(
+ name,
+ WorkflowInfo{
+ .Path = GetWorkflowPath(name),
+ .Name = std::string(name),
+ });
return workflow;
}
@@ -155,9 +186,9 @@ bool Project::RemoveWorkflow(std::string_view name)
return true;
}
-bool Project::RenameWorkflow(std::string_view name, std::string_view newName)
+bool Project::RenameWorkflow(std::string_view oldName, std::string_view newName)
{
- auto iter = mWorkflows.find(name);
+ auto iter = mWorkflows.find(oldName);
if (iter == mWorkflows.end()) return false;
auto info = std::move(iter.value());
@@ -173,6 +204,63 @@ bool Project::RenameWorkflow(std::string_view name, std::string_view newName)
return true;
}
+const tsl::array_map<char, TemplateInfo>& Project::GetTemplates() const
+{
+ return mTemplates;
+}
+
+std::unique_ptr<Template> Project::LoadTemplate(std::string_view name)
+{
+ auto iter = mTemplates.find(name);
+ if (iter == mTemplates.end()) {
+ return iter.value().LoadFromDisk();
+ } else {
+ return nullptr;
+ }
+}
+
+TemplateInfo* Project::InsertTemplate(std::string_view name, TemplateInfo info)
+{
+ auto [it, inserted] = mTemplates.insert(name, info);
+ if (inserted) {
+ return &it.value();
+ } else {
+ return nullptr;
+ }
+}
+
+bool Project::RemoveTemplate(std::string_view name)
+{
+ auto iter = mTemplates.find(name);
+ if (iter == mTemplates.end()) {
+ return false;
+ }
+ auto& info = iter.value();
+
+ fs::remove(info.Path);
+ mTemplates.erase(iter);
+
+ return true;
+}
+
+bool Project::RenameTemplate(std::string_view oldName, std::string_view newName)
+{
+ auto iter = mTemplates.find(oldName);
+ if (iter == mTemplates.end()) return false;
+
+ auto info = std::move(iter.value());
+
+ auto& oldPath = info.Path;
+ auto newPath = GetTemplatePath(newName);
+ fs::rename(oldPath, newPath);
+ info.Path = std::move(newPath);
+
+ mTemplates.insert(newName, std::move(info));
+ mTemplates.erase(iter);
+
+ return true;
+}
+
Json::Value Project::Serialize()
{
Json::Value root(Json::objectValue);
@@ -201,23 +289,3 @@ void Project::WriteToDisk()
WriteItemList(Factories, itemsDir / "factories.json");
WriteItemList(Customers, itemsDir / "customers.json");
}
-
-std::filesystem::path Project::GetDatabasesDirectory() const
-{
- return mRootPath / "databases";
-}
-
-std::filesystem::path Project::GetItemsDirectory() const
-{
- return mRootPath / "items";
-}
-
-std::filesystem::path Project::GetWorkflowsDirectory() const
-{
- return mRootPath / "workflows";
-}
-
-std::filesystem::path Project::GetWorkflowPath(std::string_view name) const
-{
- return (mRootPath / "workflows" / name).concat(".cplt-workflow");
-}