2 # ---------------------------------------------------------------------
4 # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image
6 # Authors : Eduardo Davila, Frederic Cervenansky, Claire Mouton
7 # Previous Authors : Laurent Guigues, Jean-Pierre Roux
8 # CreaTools website : www.creatis.insa-lyon.fr/site/fr/creatools_accueil
10 # This software is governed by the CeCILL-B license under French law and
11 # abiding by the rules of distribution of free software. You can use,
12 # modify and/ or redistribute the software under the terms of the CeCILL-B
13 # license as circulated by CEA, CNRS and INRIA at the following URL
14 # http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
15 # or in the file LICENSE.txt.
17 # As a counterpart to the access to the source code and rights to copy,
18 # modify and redistribute granted by the license, users are provided only
19 # with a limited warranty and the software's author, the holder of the
20 # economic rights, and the successive licensors have only limited
23 # The fact that you are presently reading this means that you have had
24 # knowledge of the CeCILL-B license and that you accept its terms.
25 # ------------------------------------------------------------------------
31 * Created on: 13/11/2012
32 * Author: Daniel Felipe Gonzalez Obando
35 #include "modelCDMProject.h"
44 #include "CDMUtilities.h"
48 modelCDMProject::modelCDMProject()
50 std::cout << "in constructor1" << std::endl;
53 this->CMakeLists = NULL;
56 modelCDMProject::modelCDMProject(
57 modelCDMIProjectTreeNode* parent,
58 const std::string& path,
59 const std::string& name,
60 const std::string& buildPath
63 std::cout << "creating project: " + name + " in " + path + "\n";
64 this->parent = parent;
65 this->path = CDMUtilities::fixPath(path);
67 std::string pathFixed(CDMUtilities::fixPath(path));
69 std::string pathMakeLists = pathFixed + CDMUtilities::SLASH + "CMakeLists.txt";
71 std::ifstream confFile;
72 confFile.open((pathMakeLists).c_str());
75 while(confFile.is_open() && !confFile.eof())
77 //std::cout << "leyendo " << word << std::endl;
79 std::getline(confFile,word,'(');
80 std::vector<std::string> wordBits;
81 CDMUtilities::splitter::split(wordBits,word," (\n",CDMUtilities::splitter::no_empties);
83 if(wordBits[wordBits.size()-1] == "PROJECT")
85 std::getline(confFile,word,')');
86 std::vector<std::string> nameBits;
87 CDMUtilities::splitter::split(nameBits, word, " ", CDMUtilities::splitter::no_empties);
89 this->name = this->nameProject = "";
90 for (int i = 0; i < (int)(nameBits.size()); i++)
94 this->name += nameBits[i];
96 this->nameProject = this->name;
101 if(wordBits[wordBits.size()-1] == "SET")
103 //get project version
104 std::getline(confFile,word,')');
105 CDMUtilities::splitter::split(wordBits, word, " ", CDMUtilities::splitter::no_empties);
106 if(wordBits[0] == "PROJECT_MAJOR_VERSION")
108 version = wordBits[1];
110 if(wordBits[0] == "PROJECT_MINOR_VERSION")
112 version += "." + wordBits[1];
114 if(wordBits[0] == "PROJECT_BUILD_VERSION")
116 version += "." + wordBits[1];
119 //get project versionDate
120 if(wordBits[0] == "PROJECT_VERSION_DATE")
122 std::vector<std::string> versionBits;
123 CDMUtilities::splitter::split(versionBits, wordBits[1], "\"", CDMUtilities::splitter::no_empties);
124 versionDate = versionBits[0];
126 //get project buildPath
130 this->buildPath = buildPath;
134 this->buildPath = this->path + "Bin";
140 this->type = wxDIR_DIRS;
143 this->children.clear();
146 this->packages.clear();
150 wxDir dir(crea::std2wx((pathFixed).c_str()));
154 bool cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_DIRS);
157 std::string stdfileName = crea::wx2std(fileName);
159 //if appli, create appli
160 if(stdfileName == "appli")
162 this->appli = new modelCDMAppli(this, pathFixed + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
163 this->children.push_back(this->appli);
166 else if(stdfileName == "lib")
168 this->lib = new modelCDMLib(this, pathFixed + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
169 this->children.push_back(this->lib);
171 //if package , create package
172 else if(stdfileName.size() > 9 && stdfileName.substr(0,5) == "bbtk_" && stdfileName.substr(stdfileName.size()-4,4) == "_PKG")
174 modelCDMPackage* package = new modelCDMPackage(this, pathFixed + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
175 this->packages.push_back(package);
176 this->children.push_back(package);
178 //if is an unknown folder, create folder
181 this->children.push_back(new modelCDMFolder(this, pathFixed + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1));
184 cont = dir.GetNext(&fileName);
187 cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_FILES);
190 std::string stdfileName = crea::wx2std(fileName);
192 //if CMakeLists, create CMakeLists
193 if(stdfileName == "CMakeLists.txt")
195 this->CMakeLists = new modelCDMCMakeListsFile(this, pathFixed + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
196 this->children.push_back(this->CMakeLists);
200 this->children.push_back(new modelCDMFile(this, pathFixed + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1));
202 //if is an unknown file, create file
203 cont = dir.GetNext(&fileName);
207 this->SortChildren();
208 std::sort(this->packages.begin(), this->packages.end(), CompareNodeItem);
212 modelCDMProject::~modelCDMProject()
216 const std::string& modelCDMProject::GetNameProject() const
218 return this->nameProject;
221 const std::string& modelCDMProject::GetVersion() const
223 return this->version;
226 const std::string& modelCDMProject::GetVersionDate() const
228 return this->versionDate;
231 const std::string& modelCDMProject::GetBuildPath() const
233 return this->buildPath;
236 const std::vector<modelCDMPackage*>& modelCDMProject::GetPackages() const
238 return this->packages;
241 modelCDMAppli* modelCDMProject::GetAppli() const
246 modelCDMLib* modelCDMProject::GetLib() const
251 std::string modelCDMProject::GetBuildInstruction() const
253 std::string makeComm = "make -C \"" + this->buildPath + "\""; /*> \"" + this->buildPath + CDMUtilities::SLASH + "building.log\" 2>&1";*/
257 bool modelCDMProject::SetVersion(const std::string& version, std::string*& result)
260 std::vector<std::string> vers;
261 CDMUtilities::splitter::split(vers, version, " .", CDMUtilities::splitter::no_empties);
263 time_t now = time(0);
266 localtime_s(<m, &now);
268 std::stringstream date;
269 date << ltm.tm_mday << "/" << 1 + ltm.tm_mon << "/" << 1900 + ltm.tm_year;
271 //set name of library in CMakeLists inside copied folder
273 std::ifstream in((this->path + CDMUtilities::SLASH + "CMakeLists.txt").c_str());
276 result = new std::string("CMakeLists.txt file failed to open.");
279 std::ofstream out((this->path + CDMUtilities::SLASH + "CMakeLists.txt.tmp").c_str());
282 result = new std::string("CMakeLists.txt.tmp file failed to open.");
285 while (getline(in, line))
287 if(line.find("SET(PROJECT_MAJOR_VERSION") != std::string::npos)
288 line = "SET(PROJECT_MAJOR_VERSION " + vers[0] + ")";
289 else if(line.find("SET(PROJECT_MINOR_VERSION") != std::string::npos)
290 line = "SET(PROJECT_MINOR_VERSION " + vers[1] + ")";
291 else if(line.find("SET(PROJECT_BUILD_VERSION") != std::string::npos)
292 line = "SET(PROJECT_BUILD_VERSION " + vers[2] + ")";
293 else if(line.find("SET(PROJECT_VERSION_DATE") != std::string::npos)
294 line = "SET(PROJECT_VERSION_DATE \"" + date.str() + "\")";
295 out << line << std::endl;
299 //delete old file and rename new file
300 std::string renameCommand = "mv \"" + this->path + CDMUtilities::SLASH + "CMakeLists.txt.tmp\" \"" + this->path + CDMUtilities::SLASH + "CMakeLists.txt\"";
301 if(system(renameCommand.c_str()))
303 result = new std::string("An error occurred while running '" + renameCommand + "'.");
307 this->version = vers[0] + "." + vers[1] + "." + vers[2];
308 this->versionDate = date.str();
312 bool modelCDMProject::SetBuildPath(const std::string& path, std::string*& result)
316 result = new std::string("The path cannot be empty");
319 if(path == this->path)
321 result = new std::string("The path cannot be same as the project sources");
324 this->buildPath = path;
328 modelCDMIProjectTreeNode* modelCDMProject::CreatePackage(
329 const std::string& name,
330 std::string*& result,
331 const std::string& authors,
332 const std::string& authorsEmail,
333 const std::string& description,
334 const std::string& version
337 //fixing input parameters
338 std::vector<std::string> words;
340 CDMUtilities::splitter::split(words,name," ",CDMUtilities::splitter::no_empties);
341 std::string nameFixed = "";
342 for (int i = 0; i < (int)(words.size()); i++)
344 nameFixed += words[i];
348 CDMUtilities::splitter::split(words,authors," ",CDMUtilities::splitter::no_empties);
349 std::string authorFixed;
350 for (int i = 0; i < (int)(words.size()); i++)
352 authorFixed += words[i];
356 std::string descriptionFixed;
357 CDMUtilities::splitter::split(words,authorsEmail," ",CDMUtilities::splitter::no_empties);
358 for (int i = 0; i < (int)(words.size()); i++)
360 descriptionFixed += words[i];
363 CDMUtilities::splitter::split(words,description," ",CDMUtilities::splitter::no_empties);
364 for (int i = 0; i < (int)(words.size()); i++)
366 descriptionFixed += "_" + words[i];
369 //call project to create package : use bbCreatePackage <path> <name> [author] [description]
370 std::string creationCommand = "bbCreatePackage \"" + this->path + "\" \"" + nameFixed + "\" \"" + authorFixed + "\" \"" + descriptionFixed + "\"";
371 //TODO: bbCreatePackage script always returning 0. It should return 1 or greater if any error
372 if(system(creationCommand.c_str()))
374 result = new std::string("An error occurred while running '" + creationCommand + "'.");
378 //add library to model
379 modelCDMPackage* package = new modelCDMPackage(this, this->path + CDMUtilities::SLASH + "bbtk_" + nameFixed + "_PKG", "bbtk_" + nameFixed + "_PKG", this->level + 1);
380 this->packages.push_back(package);
381 this->children.push_back(package);
383 //TODO: set package version
385 this->SortChildren();
387 result = new std::string(this->path + CDMUtilities::SLASH + name);
391 modelCDMIProjectTreeNode* modelCDMProject::CreateLibrary(
392 const std::string& name,
393 std::string*& result,
394 const std::string& path
397 if(this->lib != NULL)
399 return this->lib->CreateLibrary(name, result);
401 result = new std::string("there is no lib folder in this project.");
405 modelCDMIProjectTreeNode* modelCDMProject::CreateApplication(
406 const std::string& name,
407 std::string*& result,
408 const std::string& path
411 if(this->appli != NULL)
413 return this->appli->CreateApplication(name, result);
415 result = new std::string("there is no appli folder in this project.");
419 modelCDMIProjectTreeNode* modelCDMProject::CreateBlackBox(
420 const std::string& name,
421 const std::string& package,
422 const std::string& authors,
423 const std::string& authorsEmail,
424 const std::string& categories,
425 const std::string& description
428 //TODO: implement method
432 bool modelCDMProject::OpenCMakeListsFile(std::string*& result)
434 if (!CDMUtilities::openTextEditor(this->CMakeLists->GetPath()))
438 result = new std::string("Couldn't open CMakeLists file.");
443 const bool modelCDMProject::Refresh(std::string*& result)
445 std::cout << "refreshing project" << std::endl;
446 //open makelists file
447 std::string pathMakeLists = this->path + CDMUtilities::SLASH + "CMakeLists.txt";
449 std::ifstream confFile;
450 confFile.open((pathMakeLists).c_str());
453 while(confFile.is_open() && !confFile.eof())
455 //std::cout << "leyendo " << word << std::endl;
457 std::getline(confFile,word,'(');
458 std::vector<std::string> wordBits;
459 CDMUtilities::splitter::split(wordBits,word," (\n",CDMUtilities::splitter::no_empties);
461 if(wordBits[wordBits.size()-1] == "PROJECT")
463 std::getline(confFile,word,')');
464 std::vector<std::string> nameBits;
465 CDMUtilities::splitter::split(nameBits, word, " ", CDMUtilities::splitter::no_empties);
467 this->name = this->nameProject = "";
468 for (int i = 0; i < (int)(nameBits.size()); i++)
472 this->name += nameBits[i];
474 this->nameProject = this->name;
479 if(wordBits[wordBits.size()-1] == "SET")
481 //get project version
482 std::getline(confFile,word,')');
483 CDMUtilities::splitter::split(wordBits, word, " ", CDMUtilities::splitter::no_empties);
484 if(wordBits[0] == "PROJECT_MAJOR_VERSION")
486 version = wordBits[1];
488 if(wordBits[0] == "PROJECT_MINOR_VERSION")
490 version += "." + wordBits[1];
492 if(wordBits[0] == "PROJECT_BUILD_VERSION")
494 version += "." + wordBits[1];
497 //get project versionDate
498 if(wordBits[0] == "PROJECT_VERSION_DATE")
500 std::vector<std::string> versionBits;
501 CDMUtilities::splitter::split(versionBits, wordBits[1], "\"", CDMUtilities::splitter::no_empties);
502 versionDate = versionBits[0];
508 this->type = wxDIR_DIRS;
511 std::vector<bool> checked(this->children.size(), false);
512 std::vector<bool> checkedPackages(this->packages.size(), false);
515 wxDir dir(crea::std2wx((this->path).c_str()));
519 bool cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_DIRS);
522 std::string stdfileName = crea::wx2std(fileName);
524 //if appli, create appli
525 if(stdfileName == "appli")
527 if (this->appli == NULL)
529 this->appli = new modelCDMAppli(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
530 this->children.push_back(this->appli);
534 int pos = std::find(this->children.begin(), this->children.end(), this->appli) - this->children.begin();
536 if(!this->appli->Refresh(result))
541 else if(stdfileName == "lib")
543 if (this->lib == NULL)
545 this->lib = new modelCDMLib(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
546 this->children.push_back(this->lib);
550 int pos = std::find(this->children.begin(), this->children.end(), this->lib) - this->children.begin();
552 if(!this->lib->Refresh(result))
557 //if package , create package
558 else if(stdfileName.size() > 9 && stdfileName.substr(0,5) == "bbtk_" && stdfileName.substr(stdfileName.size()-4,4) == "_PKG")
561 for (int i = 0; !found && i < (int)(this->packages.size()); i++)
563 if (this->packages[i]->GetName() == stdfileName)
566 int pos = std::find(this->children.begin(), this->children.end(), this->packages[i]) - this->children.begin();
568 checkedPackages[i] = true;
569 if(!this->packages[i]->Refresh(result))
575 modelCDMPackage* package = new modelCDMPackage(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
576 this->packages.push_back(package);
577 this->children.push_back(package);
581 //if is an unknown folder, create folder
585 for (int i = 0; !found && i < (int)(this->children.size()); i++)
587 if (this->children[i]->GetName() == stdfileName)
591 if(!this->children[i]->Refresh(result))
598 modelCDMFolder* folder = new modelCDMFolder(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
599 this->children.push_back(folder);
603 cont = dir.GetNext(&fileName);
606 cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_FILES);
609 std::string stdfileName = crea::wx2std(fileName);
611 //if CMakeLists, create CMakeLists
612 if(stdfileName == "CMakeLists.txt")
614 if (this->CMakeLists == NULL)
616 this->CMakeLists = new modelCDMCMakeListsFile(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
617 this->children.push_back(this->CMakeLists);
621 int pos = std::find(this->children.begin(), this->children.end(), this->CMakeLists) - this->children.begin();
623 if(!this->CMakeLists->Refresh(result))
627 //if is an unknown file, create file
631 for (int i = 0; !found && i < (int)(this->children.size()); i++)
633 if (this->children[i]->GetName() == stdfileName)
637 if(!this->children[i]->Refresh(result))
644 modelCDMFile* file = new modelCDMFile(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
645 this->children.push_back(file);
649 cont = dir.GetNext(&fileName);
653 for (int i = 0; i < (int)(checkedPackages.size()); i++)
655 if(!checkedPackages[i])
657 this->packages.erase(this->packages.begin()+i);
658 checkedPackages.erase(checkedPackages.begin()+i);
662 for (int i = 0; i < (int)(checked.size()); i++)
666 delete this->children[i];
667 this->children.erase(this->children.begin()+i);
668 checked.erase(checked.begin()+i);
673 this->SortChildren();
674 std::sort(this->packages.begin(), this->packages.end(), CompareNodeItem);
678 bool modelCDMProject::ConfigureBuild(std::string*& result)
680 //TODO: adjust for windows and mac
688 wxDir dir(crea::std2wx((this->buildPath).c_str()));
690 //if folder doesn't exist then create it
693 //create command line to create folder
694 std::string createComm = "mkdir \"" + this->buildPath + "\"";
695 //execute creation command
696 if (system(createComm.c_str()))
698 //if there was an error then report it
699 result = new std::string("There was an error creating the build path: \"" + this->buildPath + "\"");
703 //create command line to execute ccmake
704 //TODO:: adjust for different Linux distributions
705 std::string confComm = "gnome-terminal --tab --working-directory=\"" + this->buildPath + "\" -e \"ccmake '" + this->path + "'\"";
707 if(CDMUtilities::openTerminal(confComm))
709 //if there was an error then report it
710 result = new std::string("There was an error opening the configuration tool in the desired place.");
717 bool modelCDMProject::Build(std::string*& result, const std::string& line)
719 //TODO: adjust for windows and mac
727 wxDir dir(crea::std2wx((this->buildPath).c_str()));
729 //if binary folder can't be opened then return false
732 result = new std::string("The build path could not be opened. Make sure to configure the project before compiling it");
735 //create make command
736 std::string makeComm = "gnome-terminal -e \"bash -c \\\"";
737 for (int i = 0; i < line.size(); i++)
741 makeComm+="\\\\\\\"";
743 else if(line[i] == '\\')
745 makeComm+="\\\\\\\\";
749 makeComm.push_back(line[i]);
752 makeComm += "; echo -e '\\a'; bash";
753 makeComm += "\\\"\"";
755 std::cout << "executing '" << makeComm << "'" << std::endl;
756 //execute make command
757 if(system(makeComm.c_str()))
759 //if there was an error then report it
760 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.");
767 bool modelCDMProject::Connect(std::string*& result)
769 //TODO: adjust for windows and mac
777 wxDir dir(crea::std2wx((this->buildPath).c_str()));
779 //if binary folder can't be opened then return false
782 result = new std::string("The build path could not be opened. Make sure to configure the project before compiling it");
785 //create plug command
786 std::string plugComm = "bbPlugPackage \"" + this->buildPath + "\" > \"" + this->buildPath + CDMUtilities::SLASH + "plugging.log\" 2>&1";
787 std::cout << "executing '" << plugComm << "'" << std::endl;
788 //execute plug command
789 if(system(plugComm.c_str()))
791 //if there was an error then report it
792 result = new std::string("There was an error plugging the packages of the project, please check the console to read more about the problem.");
799 void modelCDMProject::CheckStructure(std::map<std::string, bool>& properties)
802 if(this->CMakeLists != NULL)
804 //set properties parameters based on model
805 properties["project add appli"] = this->appli != NULL;
806 properties["project add lib"] = this->lib != NULL;
807 for (int i = 0; i < (int)(this->packages.size()); i++)
808 properties["project add " + packages[i]->GetName()] = false;
811 std::ifstream confFile;
812 confFile.open((this->CMakeLists->GetPath()).c_str());
814 //take everything that is not commented
815 std::string fileContent;
818 std::vector<std::string> words;
819 while(confFile.is_open() && !confFile.eof())
821 std::getline(confFile,word, '\n');
824 CDMUtilities::splitter::split(words, word, "#", CDMUtilities::splitter::empties_ok);
825 if (words.size() > 0)
828 CDMUtilities::splitter::split(words, word, " ", CDMUtilities::splitter::empties_ok);
829 for (int i = 0; i < (int)(words.size()); i++)
831 if(words[i].substr(0,2) == "//")
833 fileContent += words[i] + " ";
840 //check every instruction
841 std::stringstream ss(fileContent);
844 std::getline(ss,word, '(');
846 //check instruction name
847 CDMUtilities::splitter::split(words, word, " ", CDMUtilities::splitter::no_empties);
850 if (words.size() > 0 && words[words.size()-1] == "SET")
852 std::getline(ss,word, ')');
854 CDMUtilities::splitter::split(words, word, " ", CDMUtilities::splitter::no_empties);
855 if (words.size() > 1)
857 if (words[0] == "USE_CREA" && words[1] == "ON")
859 properties["project set USE_CREA"] = true;
861 else if (words[0] == "USE_GDCM" && words[1] == "ON")
863 properties["project set USE_GDCM"] = true;
865 else if (words[0] == "USE_GDCM_VTK" && words[1] == "ON")
867 properties["project set USE_GDCM_VTK"] = true;
869 else if (words[0] == "USE_GDCM2" && words[1] == "ON")
871 properties["project set USE_GDCM2"] = true;
873 else if (words[0] == "USE_WXWIDGETS" && words[1] == "ON")
875 properties["project set USE_WXWIDGETS"] = true;
877 else if (words[0] == "USE_KWWIDGETS" && words[1] == "ON")
879 properties["project set USE_KWWIDGETS"] = true;
881 else if (words[0] == "USE_VTK" && words[1] == "ON")
883 properties["project set USE_VTK"] = true;
885 else if (words[0] == "USE_ITK" && words[1] == "ON")
887 properties["project set USE_ITK"] = true;
889 else if (words[0] == "USE_BOOST" && words[1] == "ON")
891 properties["project set USE_BOOST"] = true;
896 else if (words.size() > 0 && words[words.size()-1] == "ADD_SUBDIRECTORY")
898 std::getline(ss,word, ')');
899 //std::cout << word << std::endl;
900 CDMUtilities::splitter::split(words, word, " ", CDMUtilities::splitter::no_empties);
902 if (words.size() > 0)
904 if(words[0] == "appli")
906 properties["project add "+words[0]] = this->appli != NULL;
908 else if(words[0] == "lib")
910 properties["project add "+words[0]] = this->lib != NULL;
914 properties["project add "+words[0]] = true;
922 //check appli's structure
923 this->appli->CheckStructure(properties);
924 //check lib's structure
925 this->lib->CheckStructure(properties);
926 //check packages' structure
927 for (int i = 0; i < (int)(this->packages.size()); i++)
929 properties["package " + this->packages[i]->GetName()] = true;
930 this->packages[i]->CheckStructure(properties);