X-Git-Url: https://git.creatis.insa-lyon.fr/pubgit/?a=blobdiff_plain;f=lib%2FcreaDevManagerLib%2FmodelCDMMain.cpp;h=8d69741da2b3d46229e93248e4ae5943355ad256;hb=a26a54f4555934ed0f3720bea6bb95913d7f02d6;hp=e9df13ed38242c21fced54e9a490bdf032ea6ea5;hpb=d63809f12ef02087c94159a16a83ec0f31ae0d74;p=crea.git diff --git a/lib/creaDevManagerLib/modelCDMMain.cpp b/lib/creaDevManagerLib/modelCDMMain.cpp index e9df13e..8d69741 100644 --- a/lib/creaDevManagerLib/modelCDMMain.cpp +++ b/lib/creaDevManagerLib/modelCDMMain.cpp @@ -38,24 +38,29 @@ #include #include #include +#include #include #include #include "CDMUtilities.h" +#include "modelCDMProject.h" modelCDMMain::modelCDMMain() { - //TODO: implement method + this->project = NULL; } modelCDMMain::~modelCDMMain() { - //TODO: implement method + if(project != NULL) + { + delete this->project; + this->project = NULL; + } } -const modelCDMProject* modelCDMMain::GetProject() const +modelCDMProject* modelCDMMain::GetProject() const { - //TODO: implement method return this->project; } @@ -71,8 +76,7 @@ bool modelCDMMain::CreateProject( std::string locationFixed = CDMUtilities::fixPath(location); std::cout << "Opening path: "<< locationFixed << std::endl; - //TODO: create Project given the source folder - + //creates project in disk #if(_WIN32) std::string command("creaNewProject.bat "); @@ -80,60 +84,79 @@ bool modelCDMMain::CreateProject( std::string command2("del "); command += "\"" + locationFixed + "\" \"" + name + "\""; - command1 += "\"" + locationFixed +"\\"+name+"\\CMakeLists.txt.in\" " + "NameOfTheProject " + name + "> \"" + locationFixed + "\\" + name + "\\CMakeLists.txt\""; - command2 += "\"" + locationFixed +"\\"+name+"\\CMakeLists.txt.in\""; - if ( ! system ( command.c_str() ) ) - { - system ( command1.c_str() ); - system ( command2.c_str() ); - - // Create a Package at the same time. JPR - char *author = author.c_str(); - std::string nomDirectory = locationFixed + "\\" + name; - std::string nomPackageDirectory = nomDirectory + "\\" + "bbtk_" + name + "_PKG"; - std::string bbCreatePackage("bbCreatePackage "); - bbCreatePackage += nomDirectory + " " + name + " " + author + " " + description; - system (bbCreatePackage.c_str()); - std::string add; - add = "echo ADD_SUBDIRECTORY(bbtk_" + name + "_PKG) >> " + nomDirectory + "/CMakeLists.txt"; - system(add.c_str()); + command1 += "\"" + locationFixed + CDMUtilities::SLASH + name + CDMUtilities::SLASH + "CMakeLists.txt.in\" " + "PROJECT_NAME " + name + "> \"" + locationFixed + CDMUtilities::SLASH + name + CDMUtilities::SLASH + "CMakeLists.txt\""; + command2 += "\"" + locationFixed + CDMUtilities::SLASH + name + CDMUtilities::SLASH + "CMakeLists.txt.in\""; + + if (system (command.c_str())) + { + result = new std::string("An error occurred while running '" + command + "'."); + return false; } - else + + if (system (command1.c_str())) { - result = new std::string("An error occured while running '" + command + "'."); + result = new std::string("An error occurred while running '" + command1 + "'."); + return false; + } + if (system (command2.c_str())) + { + result = new std::string("An error occurred while running '" + command2 + "'."); return false; } -#else - // ------ LINUX / MacOS - std::string command("creaNewProject.sh "); - command += "\"" + locationFixed + "\"" +" " + name; - std::cout << "executing " << command << std::endl; - if ( ! system ( command.c_str() ) ) + std::string nomDirectory = locationFixed + CDMUtilities::SLASH + name; + std::string nomPackageDirectory = nomDirectory + CDMUtilities::SLASH + "bbtk_" + name + "_PKG"; + std::string bbCreatePackage("bbCreatePackage "); + bbCreatePackage += "\"" + nomDirectory + "\" \"" + name + "\" \"" + author + "\" \"" + description + "\""; + if (!system (bbCreatePackage.c_str())) + { + result = new std::string("An error occurred while running '" + bbCreatePackage + "'."); + return false; + } + std::string add; + add = "echo ADD_SUBDIRECTORY(bbtk_" + name + "_PKG) >> \"" + nomDirectory + CDMUtilities::SLASH + "CMakeLists.txt\""; + if (system (add.c_str())) { - std::string nomDirectory = locationFixed + "/" + name; - std::string nomPackageDirectory = nomDirectory + "/" + "bbtk_" + name + "_PKG"; + result = new std::string("An error occurred while running '" + add + "'."); + return false; + } - std::string bbCreatePackage("bbCreatePackage "); - bbCreatePackage += nomDirectory + " " + name + " " + author + " " + description; - std::cout << "executing " << bbCreatePackage << std::endl; - system (bbCreatePackage.c_str()); + this->project = new modelCDMProject(NULL, nomDirectory, name); - std::string add; - add = "echo 'ADD_SUBDIRECTORY(bbtk_" + name + "_PKG)' >> " + nomDirectory + "/CMakeLists.txt"; - //std::cout << add << std::endl; - std::cout << "executing " << add << std::endl; - system(add.c_str()); - } - else +#else + // ------ LINUX / MacOS + std::string command("creaNewProject.sh "); + command += "\"" + locationFixed + "\"" +" \"" + name + "\""; + //std::cout << "executing " << command << std::endl; + if (system ( command.c_str() ) ) { result = new std::string("An error occured while running '" + command + "'."); return false; } -#endif + std::string nomDirectory = locationFixed + CDMUtilities::SLASH + name; + std::string nomPackageDirectory = nomDirectory + CDMUtilities::SLASH + "bbtk_" + name + "_PKG"; + + std::string bbCreatePackage("bbCreatePackage "); + bbCreatePackage += "\"" + nomDirectory + "\" \"" + name + "\" \"" + author + "\" \"" + description + "\""; + //std::cout << "executing " << bbCreatePackage << std::endl; + system (bbCreatePackage.c_str()); + std::string add; + add = "echo 'ADD_SUBDIRECTORY(bbtk_" + name + "_PKG)' >> \"" + nomDirectory + CDMUtilities::SLASH + "CMakeLists.txt\""; + //std::cout << "executing " << add << std::endl; + system(add.c_str()); + + if(this->project != NULL) + { + if (!CloseProject(result)) + return false; + } + + this->project = new modelCDMProject(NULL, nomDirectory, name); + +#endif return true; } @@ -143,7 +166,7 @@ bool modelCDMMain::OpenProject( std::string*& result ) { - std::cout << "Open selection path: "<< path << std::endl; + //std::cout << "Open selection path: "<< path << std::endl; //get fixed path std::string pathFixed = CDMUtilities::fixPath(path); std::cout << "Opening path: "<< pathFixed << std::endl; @@ -153,29 +176,57 @@ bool modelCDMMain::OpenProject( std::string pathBuild = ""; //check if Makefile file exists - std::string pathMakefile = pathFixed + "/Makefile"; - FILE* pFile = fopen(pathMakefile.c_str(), "r"); - +#ifdef _WIN32 + std::string pathMakefile = pathFixed + CDMUtilities::SLASH + "CMakeCache.txt"; + FILE* pFile; + errno_t errorOpen = fopen_s(&pFile, pathMakefile.c_str(), "r"); +#else + std::string pathMakefile = pathFixed + CDMUtilities::SLASH + "Makefile"; + FILE* pFile; + pFile = fopen(pathMakefile.c_str(), "r"); + bool errorOpen = (pFile == NULL); +#endif //is the binary folder - if (pFile != NULL) + if (!errorOpen && pFile) { fclose(pFile); std::ifstream readFile; readFile.open(pathMakefile.c_str()); std::string word; + +#ifdef _WIN32 + while(!isBinary && !readFile.eof()) + { + getline(readFile, word, '\n'); + std::vector words; + CDMUtilities::splitter::split(words,word,"=",CDMUtilities::splitter::no_empties); + if(words.size() && words[0] == "CMAKE_HOME_DIRECTORY:INTERNAL") + { + pathBuild = pathFixed; + pathFixed = CDMUtilities::fixPath(words[1]); + for (int i = 0; i < (int)(pathFixed.size()); i++) + { + if (pathFixed[i]=='/') + pathFixed[i]='\\'; + } + isBinary = true; + } + } +#else while(!isBinary && readFile >> word) { - if(word == "CMAKE_SOURCE_DIR") + if(word == "CMAKE_SOURCE_DIR") { readFile >> word; readFile.ignore(); getline(readFile, word, '\n'); pathBuild = pathFixed; - pathFixed = word; + pathFixed = CDMUtilities::fixPath(word); isBinary = true; } - } + } +#endif readFile.close(); } @@ -183,11 +234,16 @@ bool modelCDMMain::OpenProject( bool isSource = false; std::string pathSource = ""; //check if CMakeLists file exists - std::string pathCMakeLists = pathFixed + "/CMakeLists.txt"; + std::string pathCMakeLists = pathFixed + CDMUtilities::SLASH + "CMakeLists.txt"; +#ifdef _WIN32 + errorOpen = fopen_s(&pFile, pathCMakeLists.c_str(), "r"); +#else pFile = fopen(pathCMakeLists.c_str(), "r"); + errorOpen = (pFile == NULL); +#endif //is the source folder - if (pFile != NULL) + if (!errorOpen && pFile) { fclose(pFile); std::ifstream readFile; @@ -197,12 +253,14 @@ bool modelCDMMain::OpenProject( while(!isSource && !readFile.eof()) { - std::getline(readFile,word,'\n'); - int pos = word.find("PROJECT"); - if(pos != std::string::npos) + std::getline(readFile,word,'('); + std::vector wordBits; + CDMUtilities::splitter::split(wordBits,word," (\n",CDMUtilities::splitter::no_empties); + + if(wordBits[wordBits.size()-1] == "PROJECT") { - pathSource = pathFixed; isSource = true; + pathSource = pathFixed; } } readFile.close(); @@ -211,20 +269,32 @@ bool modelCDMMain::OpenProject( //if is source folder if(isSource) { - std::cout << "Project sources at: " << pathSource; + if(this->project != NULL) + { + if (!CloseProject(result)) + return false; + } + std::vector words; + CDMUtilities::splitter::split(words, pathSource, CDMUtilities::SLASH, CDMUtilities::splitter::no_empties); + + std::cout << "Project sources at: " << pathSource << std::endl; if(isBinary) { - std::cout << ", and built in: " << pathBuild; + std::cout << ", and built in: " << pathBuild << std::endl; + + this->project = new modelCDMProject(NULL, pathSource, words[words.size()-1], pathBuild); + } + else + { + this->project = new modelCDMProject(NULL, pathSource, words[words.size()-1]); } - std::cout << std::endl; } else { - result = new std::string("No source folder found. Please make sure to select either the project's build or source folder."); + result = new std::string("No source folder found. Please make sure to select either the project's build or source folder. " + pathBuild + pathFixed); return false; } - //TODO: create Project given the source folder return true; } @@ -232,14 +302,37 @@ bool modelCDMMain::RefreshProject( std::string*& result ) { - //TODO: recreate the project using the project's path - return true; + //recreate the project using the project's path + if (this->project != NULL) + { + return this->project->Refresh(result); + } + else + { + result = new std::string("There is no open project."); + return false; + } +} + +std::map& modelCDMMain::GetModelElements() +{ + return this->modelElements; } bool modelCDMMain::CloseProject( std::string*& result ) { - //TODO: delete the project tree and leave it as NULL - return true; + //delete the project tree and leave it as NULL + if (this->project != NULL) + { + delete this->project; + this->project = NULL; + return true; + } + else + { + result = new std::string("There is no open project."); + return false; + } }