#include "modelCDMProject.h"
#include <iostream>
+#include <sstream>
#include <vector>
#include <algorithm>
#include <fstream>
+#include <ctime>
#include "CDMUtilities.h"
#include "creaWx.h"
modelCDMProject::modelCDMProject(
const std::string& path,
+ const std::string& name,
const std::string& buildPath
)
{
+ std::cout << "creating project: " + name + " in " + path + "\n";
this->path = CDMUtilities::fixPath(path);
//open makelists file
std::string pathFixed(CDMUtilities::fixPath(path));
- //TODO: set pathMakeLists for windows
- std::string pathMakeLists = pathFixed + "/CMakeLists.txt";
+ std::string pathMakeLists = pathFixed + CDMUtilities::SLASH + "CMakeLists.txt";
std::ifstream confFile;
confFile.open((pathMakeLists).c_str());
//if appli, create appli
if(stdfileName == "appli")
{
- this->appli = new modelCDMAppli(pathFixed + "/appli", this->level + 1);
+ this->appli = new modelCDMAppli(pathFixed + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
this->children.push_back(this->appli);
}
//if lib, create lib
else if(stdfileName == "lib")
{
- this->lib = new modelCDMLib(pathFixed + "/lib", this->level + 1);
+ this->lib = new modelCDMLib(pathFixed + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
this->children.push_back(this->lib);
}
//if package , create package
else if(stdfileName.size() > 9 && stdfileName.substr(0,5) == "bbtk_" && stdfileName.substr(stdfileName.size()-4,4) == "_PKG")
{
- modelCDMPackage* package = new modelCDMPackage(pathFixed + "/" + stdfileName, this->level + 1);
+ modelCDMPackage* package = new modelCDMPackage(pathFixed + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
this->packages.push_back(package);
this->children.push_back(package);
}
//if is an unknown folder, create folder
else
{
- this->children.push_back(new modelCDMFolder(pathFixed + "/" + stdfileName, this->level + 1));
+ this->children.push_back(new modelCDMFolder(pathFixed + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1));
}
cont = dir.GetNext(&fileName);
//if CMakeLists, create CMakeLists
if(stdfileName == "CMakeLists.txt")
{
- this->CMakeLists = new modelCDMCMakeListsFile(pathFixed + "/" + stdfileName, this->level + 1);
+ this->CMakeLists = new modelCDMCMakeListsFile(pathFixed + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
this->children.push_back(this->CMakeLists);
}
else
{
- this->children.push_back(new modelCDMFile(pathFixed + "/" + stdfileName, this->level + 1));
+ this->children.push_back(new modelCDMFile(pathFixed + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1));
}
//if is an unknown file, create file
cont = dir.GetNext(&fileName);
}
this->SortChildren();
+ std::sort(this->packages.begin(), this->packages.end(), CompareNodeItem);
}
return this->buildPath;
}
+const std::vector<modelCDMPackage*>& modelCDMProject::GetPackages() const
+{
+ return this->packages;
+}
+
+modelCDMAppli* modelCDMProject::GetAppli() const
+{
+ return this->appli;
+}
+
+modelCDMLib* modelCDMProject::GetLib() const
+{
+ return this->lib;
+}
+
bool modelCDMProject::SetVersion(const std::string& version, std::string*& result)
{
- //TODO: implement method
+
+ std::vector<std::string> vers;
+ CDMUtilities::splitter::split(vers, version, " .", CDMUtilities::splitter::no_empties);
+
+ time_t now = time(0);
+ tm* ltm = localtime(&now);
+
+ std::stringstream date;
+ date << ltm->tm_mday << "/" << 1 + ltm->tm_mon << "/" << 1900 + ltm->tm_year;
+
+ //set name of library in CMakeLists inside copied folder
+ std::string line;
+ std::ifstream in((this->path + CDMUtilities::SLASH + "CMakeLists.txt").c_str());
+ if( !in.is_open())
+ {
+ result = new std::string("CMakeLists.txt file failed to open.");
+ return false;
+ }
+ std::ofstream out((this->path + CDMUtilities::SLASH + "CMakeLists.txt.tmp").c_str());
+ if( !out.is_open())
+ {
+ result = new std::string("CMakeLists.txt.tmp file failed to open.");
+ return false;
+ }
+ while (getline(in, line))
+ {
+ if(line.find("SET(PROJECT_MAJOR_VERSION") != std::string::npos)
+ line = "SET(PROJECT_MAJOR_VERSION " + vers[0] + ")";
+ else if(line.find("SET(PROJECT_MINOR_VERSION") != std::string::npos)
+ line = "SET(PROJECT_MINOR_VERSION " + vers[1] + ")";
+ else if(line.find("SET(PROJECT_BUILD_VERSION") != std::string::npos)
+ line = "SET(PROJECT_BUILD_VERSION " + vers[2] + ")";
+ else if(line.find("SET(PROJECT_VERSION_DATE") != std::string::npos)
+ line = "SET(PROJECT_VERSION_DATE \"" + date.str() + "\")";
+ out << line << std::endl;
+ }
+ in.close();
+ out.close();
+ //delete old file and rename new file
+ std::string renameCommand = "mv \"" + this->path + CDMUtilities::SLASH + "CMakeLists.txt.tmp\" \"" + this->path + CDMUtilities::SLASH + "CMakeLists.txt\"";
+ if(system(renameCommand.c_str()))
+ {
+ result = new std::string("An error occurred while running '" + renameCommand + "'.");
+ return false;
+ }
+
+ this->version = vers[0] + "." + vers[1] + "." + vers[2];
+ this->versionDate = date.str();
return true;
}
bool modelCDMProject::SetBuildPath(const std::string& path, std::string*& result)
{
- //TODO: implement method
+ if(path == "")
+ {
+ result = new std::string("The path cannot be empty");
+ return false;
+ }
+ if(path == this->path)
+ {
+ result = new std::string("The path cannot be same as the project sources");
+ return false;
+ }
+ this->buildPath = path;
return true;
}
std::string*& result,
const std::string& authors,
const std::string& authorsEmail,
- const std::string& version,
- const std::string& description
+ const std::string& description,
+ const std::string& version
)
{
- //TODO: implement method
- return NULL;
+ //fixing input parameters
+ std::vector<std::string> words;
+
+ CDMUtilities::splitter::split(words,name," ",CDMUtilities::splitter::no_empties);
+ std::string nameFixed = "";
+ for (int i = 0; i < words.size(); i++)
+ {
+ nameFixed += words[i];
+ }
+
+ words.clear();
+ CDMUtilities::splitter::split(words,authors," ",CDMUtilities::splitter::no_empties);
+ std::string authorFixed;
+ for (int i = 0; i < words.size(); i++)
+ {
+ authorFixed += words[i];
+ }
+
+ words.clear();
+ std::string descriptionFixed;
+ CDMUtilities::splitter::split(words,authorsEmail," ",CDMUtilities::splitter::no_empties);
+ for (int i = 0; i < words.size(); i++)
+ {
+ descriptionFixed += words[i];
+ }
+ words.clear();
+ CDMUtilities::splitter::split(words,description," ",CDMUtilities::splitter::no_empties);
+ for (int i = 0; i < words.size(); i++)
+ {
+ descriptionFixed += "_" + words[i];
+ }
+
+ //call project to create package : use bbCreatePackage <path> <name> [author] [description]
+ std::string creationCommand = "bbCreatePackage \"" + this->path + "\" \"" + nameFixed + "\" \"" + authorFixed + "\" \"" + descriptionFixed + "\"";
+ //TODO: bbCreatePackage script always returning 0. It should return 1 or greater if any error
+ if(system(creationCommand.c_str()))
+ {
+ result = new std::string("An error occurred while running '" + creationCommand + "'.");
+ return NULL;
+ }
+
+ //add library to model
+ modelCDMPackage* package = new modelCDMPackage(this->path + CDMUtilities::SLASH + "bbtk_" + nameFixed + "_PKG", "bbtk_" + nameFixed + "_PKG", this->level + 1);
+ this->packages.push_back(package);
+ this->children.push_back(package);
+
+ //TODO: set package version
+
+ this->SortChildren();
+
+ result = new std::string(this->path + CDMUtilities::SLASH + name);
+ return package;
}
modelCDMIProjectTreeNode* modelCDMProject::CreateLibrary(
const std::string& path
)
{
- //TODO: implement method
+ if(this->appli != NULL)
+ {
+ return this->appli->CreateApplication(name, result);
+ }
+ result = new std::string("there is no appli folder in this project.");
return NULL;
}
bool modelCDMProject::OpenCMakeListsFile(std::string*& result)
{
- //TODO: implement method
- return true;
+ if (!CDMUtilities::openTextEditor(this->CMakeLists->GetPath()))
+ return true;
+ else
+ {
+ result = new std::string("Couldn't open CMakeLists file.");
+ return false;
+ }
}
const bool modelCDMProject::Refresh(std::string*& result)
{
std::cout << "refreshing project" << std::endl;
//open makelists file
- //TODO: set pathMakeLists for windows
- std::string pathMakeLists = this->path + "/CMakeLists.txt";
+ std::string pathMakeLists = this->path + CDMUtilities::SLASH + "CMakeLists.txt";
std::ifstream confFile;
confFile.open((pathMakeLists).c_str());
{
if (this->appli == NULL)
{
- this->appli = new modelCDMAppli(this->path + "/appli", this->level + 1);
+ this->appli = new modelCDMAppli(this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
this->children.push_back(this->appli);
}
else
{
if (this->lib == NULL)
{
- this->lib = new modelCDMLib(this->path + "/lib", this->level + 1);
+ this->lib = new modelCDMLib(this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
this->children.push_back(this->lib);
}
else
//if package , create package
else if(stdfileName.size() > 9 && stdfileName.substr(0,5) == "bbtk_" && stdfileName.substr(stdfileName.size()-4,4) == "_PKG")
{
- std::string packageName = stdfileName.substr(5, stdfileName.size()-9);
bool found = false;
- for (int i = 0;!found && i < this->packages.size(); i++)
+ for (int i = 0; !found && i < this->packages.size(); i++)
{
- if (this->packages[i]->GetName() == packageName)
+ if (this->packages[i]->GetName() == stdfileName)
{
found = true;
int pos = std::find(this->children.begin(), this->children.end(), this->packages[i]) - this->children.begin();
}
if(!found)
{
- modelCDMPackage* package = new modelCDMPackage(this->path + "/" + stdfileName, this->level + 1);
+ modelCDMPackage* package = new modelCDMPackage(this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
this->packages.push_back(package);
this->children.push_back(package);
}
if(!found)
{
- modelCDMFolder* folder = new modelCDMFolder(this->path + "/" + stdfileName, this->level + 1);
+ modelCDMFolder* folder = new modelCDMFolder(this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
this->children.push_back(folder);
}
}
{
if (this->CMakeLists == NULL)
{
- this->CMakeLists = new modelCDMCMakeListsFile(this->path + "/" + stdfileName, this->level + 1);
+ this->CMakeLists = new modelCDMCMakeListsFile(this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
this->children.push_back(this->CMakeLists);
}
else
else
{
bool found = false;
- for (int i = 0; i <!found && this->children.size(); i++)
+ for (int i = 0; !found && i < this->children.size(); i++)
{
if (this->children[i]->GetName() == stdfileName)
{
if(!found)
{
- modelCDMFile* file = new modelCDMFile(this->path + "/" + stdfileName, this->level + 1);
+ modelCDMFile* file = new modelCDMFile(this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
this->children.push_back(file);
}
}
if(!checkedPackages[i])
{
this->packages.erase(this->packages.begin()+i);
- checkedPackages.erase(checkedPackages.begin()+i);
- i--;
+ checkedPackages.erase(checkedPackages.begin()+i);
+ i--;
}
}
for (int i = 0; i < checked.size(); i++)
i--;
}
}
+
this->SortChildren();
+ std::sort(this->packages.begin(), this->packages.end(), CompareNodeItem);
return true;
}
bool modelCDMProject::ConfigureBuild(std::string*& result)
{
- //TODO: implement method
+ //TODO: adjust for windows and mac
+#ifdef _WIN32
+ // ------ Windows
+#elif __APPLE__
+ // ------ Apple
+#else
+ // ------ Linux
+ //open binary folder
+ wxDir dir(crea::std2wx((this->buildPath).c_str()));
+
+ //if folder doesn't exist then create it
+ if (!dir.IsOpened())
+ {
+ //create command line to create folder
+ std::string createComm = "mkdir \"" + this->buildPath + "\"";
+ //execute creation command
+ if (system(createComm.c_str()))
+ {
+ //if there was an error then report it
+ result = new std::string("There was an error creating the build path: \"" + this->buildPath + "\"");
+ return false;
+ }
+ }
+ //create command line to execute ccmake
+ //TODO:: adjust for different Linux distributions
+ std::string confComm = "gnome-terminal --tab --working-directory=\"" + this->buildPath + "\" -e \"ccmake '" + this->path + "'\"";
+ //execute command
+ if(CDMUtilities::openTerminal(confComm))
+ {
+ //if there was an error then report it
+ result = new std::string("There was an error opening the configuration tool in the desired place.");
+ return false;
+ }
+#endif
return true;
}
bool modelCDMProject::Build(std::string*& result)
{
- //TODO: implement method
+ //TODO: adjust for windows and mac
+#ifdef _WIN32
+ // ------ Windows
+#elif __APPLE__
+ // ------ Apple
+#else
+ // ------ Linux
+ //open binary folder
+ wxDir dir(crea::std2wx((this->buildPath).c_str()));
+
+ //if binary folder can't be opened then return false
+ if (!dir.IsOpened())
+ {
+ result = new std::string("The build path could not be opened. Make sure to configure the project before compiling it");
+ return false;
+ }
+ //create make command
+ std::string makeComm = "make -C \"" + this->buildPath + "\" > \"" + this->buildPath + CDMUtilities::SLASH + "building.log\" 2>&1";
+ std::cout << "executing '" << makeComm << "'" << std::endl;
+ //execute make command
+ if(system(makeComm.c_str()))
+ {
+ //if there was an error then report it
+ result = new std::string("There was an error compiling the project, please check the \"building.log\" file located in the build folder to read more about the problem.");
+ return false;
+ }
+#endif
return true;
}
bool modelCDMProject::Connect(std::string*& result)
{
- //TODO: implement method
- return true;
+ //TODO: adjust for windows and mac
+ #ifdef _WIN32
+ // ------ Windows
+ #elif __APPLE__
+ // ------ Apple
+ #else
+ // ------ Linux
+ //open binary folder
+ wxDir dir(crea::std2wx((this->buildPath).c_str()));
+
+ //if binary folder can't be opened then return false
+ if (!dir.IsOpened())
+ {
+ result = new std::string("The build path could not be opened. Make sure to configure the project before compiling it");
+ return false;
+ }
+ //create plug command
+ std::string plugComm = "bbPlugPackage \"" + this->buildPath + "\" > \"" + this->buildPath + CDMUtilities::SLASH + "plugging.log\" 2>&1";
+ std::cout << "executing '" << plugComm << "'" << std::endl;
+ //execute plug command
+ if(system(plugComm.c_str()))
+ {
+ //if there was an error then report it
+ result = new std::string("There was an error plugging the packages of the project, please check the \"plugging.log\" file located in the build folder to read more about the problem.");
+ return false;
+ }
+ #endif
+ return true;
}