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 < 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 bool modelCDMProject::SetVersion(const std::string& version, std::string*& result)
254 std::vector<std::string> vers;
255 CDMUtilities::splitter::split(vers, version, " .", CDMUtilities::splitter::no_empties);
257 time_t now = time(0);
258 tm* ltm = localtime(&now);
260 std::stringstream date;
261 date << ltm->tm_mday << "/" << 1 + ltm->tm_mon << "/" << 1900 + ltm->tm_year;
263 //set name of library in CMakeLists inside copied folder
265 std::ifstream in((this->path + CDMUtilities::SLASH + "CMakeLists.txt").c_str());
268 result = new std::string("CMakeLists.txt file failed to open.");
271 std::ofstream out((this->path + CDMUtilities::SLASH + "CMakeLists.txt.tmp").c_str());
274 result = new std::string("CMakeLists.txt.tmp file failed to open.");
277 while (getline(in, line))
279 if(line.find("SET(PROJECT_MAJOR_VERSION") != std::string::npos)
280 line = "SET(PROJECT_MAJOR_VERSION " + vers[0] + ")";
281 else if(line.find("SET(PROJECT_MINOR_VERSION") != std::string::npos)
282 line = "SET(PROJECT_MINOR_VERSION " + vers[1] + ")";
283 else if(line.find("SET(PROJECT_BUILD_VERSION") != std::string::npos)
284 line = "SET(PROJECT_BUILD_VERSION " + vers[2] + ")";
285 else if(line.find("SET(PROJECT_VERSION_DATE") != std::string::npos)
286 line = "SET(PROJECT_VERSION_DATE \"" + date.str() + "\")";
287 out << line << std::endl;
291 //delete old file and rename new file
292 std::string renameCommand = "mv \"" + this->path + CDMUtilities::SLASH + "CMakeLists.txt.tmp\" \"" + this->path + CDMUtilities::SLASH + "CMakeLists.txt\"";
293 if(system(renameCommand.c_str()))
295 result = new std::string("An error occurred while running '" + renameCommand + "'.");
299 this->version = vers[0] + "." + vers[1] + "." + vers[2];
300 this->versionDate = date.str();
304 bool modelCDMProject::SetBuildPath(const std::string& path, std::string*& result)
308 result = new std::string("The path cannot be empty");
311 if(path == this->path)
313 result = new std::string("The path cannot be same as the project sources");
316 this->buildPath = path;
320 modelCDMIProjectTreeNode* modelCDMProject::CreatePackage(
321 const std::string& name,
322 std::string*& result,
323 const std::string& authors,
324 const std::string& authorsEmail,
325 const std::string& description,
326 const std::string& version
329 //fixing input parameters
330 std::vector<std::string> words;
332 CDMUtilities::splitter::split(words,name," ",CDMUtilities::splitter::no_empties);
333 std::string nameFixed = "";
334 for (int i = 0; i < words.size(); i++)
336 nameFixed += words[i];
340 CDMUtilities::splitter::split(words,authors," ",CDMUtilities::splitter::no_empties);
341 std::string authorFixed;
342 for (int i = 0; i < words.size(); i++)
344 authorFixed += words[i];
348 std::string descriptionFixed;
349 CDMUtilities::splitter::split(words,authorsEmail," ",CDMUtilities::splitter::no_empties);
350 for (int i = 0; i < words.size(); i++)
352 descriptionFixed += words[i];
355 CDMUtilities::splitter::split(words,description," ",CDMUtilities::splitter::no_empties);
356 for (int i = 0; i < words.size(); i++)
358 descriptionFixed += "_" + words[i];
361 //call project to create package : use bbCreatePackage <path> <name> [author] [description]
362 std::string creationCommand = "bbCreatePackage \"" + this->path + "\" \"" + nameFixed + "\" \"" + authorFixed + "\" \"" + descriptionFixed + "\"";
363 //TODO: bbCreatePackage script always returning 0. It should return 1 or greater if any error
364 if(system(creationCommand.c_str()))
366 result = new std::string("An error occurred while running '" + creationCommand + "'.");
370 //add library to model
371 modelCDMPackage* package = new modelCDMPackage(this, this->path + CDMUtilities::SLASH + "bbtk_" + nameFixed + "_PKG", "bbtk_" + nameFixed + "_PKG", this->level + 1);
372 this->packages.push_back(package);
373 this->children.push_back(package);
375 //TODO: set package version
377 this->SortChildren();
379 result = new std::string(this->path + CDMUtilities::SLASH + name);
383 modelCDMIProjectTreeNode* modelCDMProject::CreateLibrary(
384 const std::string& name,
385 std::string*& result,
386 const std::string& path
389 if(this->lib != NULL)
391 return this->lib->CreateLibrary(name, result);
393 result = new std::string("there is no lib folder in this project.");
397 modelCDMIProjectTreeNode* modelCDMProject::CreateApplication(
398 const std::string& name,
399 std::string*& result,
400 const std::string& path
403 if(this->appli != NULL)
405 return this->appli->CreateApplication(name, result);
407 result = new std::string("there is no appli folder in this project.");
411 modelCDMIProjectTreeNode* modelCDMProject::CreateBlackBox(
412 const std::string& name,
413 const std::string& package,
414 const std::string& authors,
415 const std::string& authorsEmail,
416 const std::string& categories,
417 const std::string& description
420 //TODO: implement method
424 bool modelCDMProject::OpenCMakeListsFile(std::string*& result)
426 if (!CDMUtilities::openTextEditor(this->CMakeLists->GetPath()))
430 result = new std::string("Couldn't open CMakeLists file.");
435 const bool modelCDMProject::Refresh(std::string*& result)
437 std::cout << "refreshing project" << std::endl;
438 //open makelists file
439 std::string pathMakeLists = this->path + CDMUtilities::SLASH + "CMakeLists.txt";
441 std::ifstream confFile;
442 confFile.open((pathMakeLists).c_str());
445 while(confFile.is_open() && !confFile.eof())
447 //std::cout << "leyendo " << word << std::endl;
449 std::getline(confFile,word,'(');
450 std::vector<std::string> wordBits;
451 CDMUtilities::splitter::split(wordBits,word," (\n",CDMUtilities::splitter::no_empties);
453 if(wordBits[wordBits.size()-1] == "PROJECT")
455 std::getline(confFile,word,')');
456 std::vector<std::string> nameBits;
457 CDMUtilities::splitter::split(nameBits, word, " ", CDMUtilities::splitter::no_empties);
459 this->name = this->nameProject = "";
460 for (int i = 0; i < nameBits.size(); i++)
464 this->name += nameBits[i];
466 this->nameProject = this->name;
471 if(wordBits[wordBits.size()-1] == "SET")
473 //get project version
474 std::getline(confFile,word,')');
475 CDMUtilities::splitter::split(wordBits, word, " ", CDMUtilities::splitter::no_empties);
476 if(wordBits[0] == "PROJECT_MAJOR_VERSION")
478 version = wordBits[1];
480 if(wordBits[0] == "PROJECT_MINOR_VERSION")
482 version += "." + wordBits[1];
484 if(wordBits[0] == "PROJECT_BUILD_VERSION")
486 version += "." + wordBits[1];
489 //get project versionDate
490 if(wordBits[0] == "PROJECT_VERSION_DATE")
492 std::vector<std::string> versionBits;
493 CDMUtilities::splitter::split(versionBits, wordBits[1], "\"", CDMUtilities::splitter::no_empties);
494 versionDate = versionBits[0];
500 this->type = wxDIR_DIRS;
503 std::vector<bool> checked(this->children.size(), false);
504 std::vector<bool> checkedPackages(this->packages.size(), false);
507 wxDir dir(crea::std2wx((this->path).c_str()));
511 bool cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_DIRS);
514 std::string stdfileName = crea::wx2std(fileName);
516 //if appli, create appli
517 if(stdfileName == "appli")
519 if (this->appli == NULL)
521 this->appli = new modelCDMAppli(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
522 this->children.push_back(this->appli);
526 int pos = std::find(this->children.begin(), this->children.end(), this->appli) - this->children.begin();
528 if(!this->appli->Refresh(result))
533 else if(stdfileName == "lib")
535 if (this->lib == NULL)
537 this->lib = new modelCDMLib(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
538 this->children.push_back(this->lib);
542 int pos = std::find(this->children.begin(), this->children.end(), this->lib) - this->children.begin();
544 if(!this->lib->Refresh(result))
549 //if package , create package
550 else if(stdfileName.size() > 9 && stdfileName.substr(0,5) == "bbtk_" && stdfileName.substr(stdfileName.size()-4,4) == "_PKG")
553 for (int i = 0; !found && i < this->packages.size(); i++)
555 if (this->packages[i]->GetName() == stdfileName)
558 int pos = std::find(this->children.begin(), this->children.end(), this->packages[i]) - this->children.begin();
560 checkedPackages[i] = true;
561 if(!this->packages[i]->Refresh(result))
567 modelCDMPackage* package = new modelCDMPackage(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
568 this->packages.push_back(package);
569 this->children.push_back(package);
573 //if is an unknown folder, create folder
577 for (int i = 0; !found && i < this->children.size(); i++)
579 if (this->children[i]->GetName() == stdfileName)
583 if(!this->children[i]->Refresh(result))
590 modelCDMFolder* folder = new modelCDMFolder(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
591 this->children.push_back(folder);
595 cont = dir.GetNext(&fileName);
598 cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_FILES);
601 std::string stdfileName = crea::wx2std(fileName);
603 //if CMakeLists, create CMakeLists
604 if(stdfileName == "CMakeLists.txt")
606 if (this->CMakeLists == NULL)
608 this->CMakeLists = new modelCDMCMakeListsFile(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
609 this->children.push_back(this->CMakeLists);
613 int pos = std::find(this->children.begin(), this->children.end(), this->CMakeLists) - this->children.begin();
615 if(!this->CMakeLists->Refresh(result))
619 //if is an unknown file, create file
623 for (int i = 0; !found && i < this->children.size(); i++)
625 if (this->children[i]->GetName() == stdfileName)
629 if(!this->children[i]->Refresh(result))
636 modelCDMFile* file = new modelCDMFile(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
637 this->children.push_back(file);
641 cont = dir.GetNext(&fileName);
645 for (int i = 0; i < checkedPackages.size(); i++)
647 if(!checkedPackages[i])
649 this->packages.erase(this->packages.begin()+i);
650 checkedPackages.erase(checkedPackages.begin()+i);
654 for (int i = 0; i < checked.size(); i++)
658 delete this->children[i];
659 this->children.erase(this->children.begin()+i);
660 checked.erase(checked.begin()+i);
665 this->SortChildren();
666 std::sort(this->packages.begin(), this->packages.end(), CompareNodeItem);
670 bool modelCDMProject::ConfigureBuild(std::string*& result)
672 //TODO: adjust for windows and mac
680 wxDir dir(crea::std2wx((this->buildPath).c_str()));
682 //if folder doesn't exist then create it
685 //create command line to create folder
686 std::string createComm = "mkdir \"" + this->buildPath + "\"";
687 //execute creation command
688 if (system(createComm.c_str()))
690 //if there was an error then report it
691 result = new std::string("There was an error creating the build path: \"" + this->buildPath + "\"");
695 //create command line to execute ccmake
696 //TODO:: adjust for different Linux distributions
697 std::string confComm = "gnome-terminal --tab --working-directory=\"" + this->buildPath + "\" -e \"ccmake '" + this->path + "'\"";
699 if(CDMUtilities::openTerminal(confComm))
701 //if there was an error then report it
702 result = new std::string("There was an error opening the configuration tool in the desired place.");
709 bool modelCDMProject::Build(std::string*& result)
711 //TODO: adjust for windows and mac
719 wxDir dir(crea::std2wx((this->buildPath).c_str()));
721 //if binary folder can't be opened then return false
724 result = new std::string("The build path could not be opened. Make sure to configure the project before compiling it");
727 //create make command
728 std::string makeComm = "make -C \"" + this->buildPath + "\" > \"" + this->buildPath + CDMUtilities::SLASH + "building.log\" 2>&1";
729 std::cout << "executing '" << makeComm << "'" << std::endl;
730 //execute make command
731 if(system(makeComm.c_str()))
733 //if there was an error then report it
734 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.");
741 bool modelCDMProject::Connect(std::string*& result)
743 //TODO: adjust for windows and mac
751 wxDir dir(crea::std2wx((this->buildPath).c_str()));
753 //if binary folder can't be opened then return false
756 result = new std::string("The build path could not be opened. Make sure to configure the project before compiling it");
759 //create plug command
760 std::string plugComm = "bbPlugPackage \"" + this->buildPath + "\" > \"" + this->buildPath + CDMUtilities::SLASH + "plugging.log\" 2>&1";
761 std::cout << "executing '" << plugComm << "'" << std::endl;
762 //execute plug command
763 if(system(plugComm.c_str()))
765 //if there was an error then report it
766 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.");