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"
43 #include <boost/regex.hpp>
45 #include "CDMUtilities.h"
48 #include <wx/config.h>
50 modelCDMProject::modelCDMProject()
52 std::cout << "in constructor1" << std::endl;
55 this->CMakeLists = NULL;
58 modelCDMProject::modelCDMProject(
59 modelCDMIProjectTreeNode* parent,
60 const std::string& path,
61 const std::string& name,
62 const std::string& buildPath
65 std::cout << "creating project: " + name + " in " + path + "\n";
66 this->parent = parent;
67 this->path = CDMUtilities::fixPath(path);
69 std::string pathFixed(CDMUtilities::fixPath(path));
71 std::string pathMakeLists = pathFixed + CDMUtilities::SLASH + "CMakeLists.txt";
73 std::ifstream confFile;
74 confFile.open((pathMakeLists).c_str());
77 while(confFile.is_open() && !confFile.eof())
79 //std::cout << "leyendo " << word << std::endl;
81 std::getline(confFile,word,'(');
82 std::vector<std::string> wordBits;
83 CDMUtilities::splitter::split(wordBits,word," (\n",CDMUtilities::splitter::no_empties);
85 if(wordBits[wordBits.size()-1] == "PROJECT")
87 std::getline(confFile,word,')');
88 std::vector<std::string> nameBits;
89 CDMUtilities::splitter::split(nameBits, word, " ", CDMUtilities::splitter::no_empties);
91 this->name = this->nameProject = "";
92 for (int i = 0; i < (int)(nameBits.size()); i++)
96 this->name += nameBits[i];
98 this->nameProject = this->name;
103 if(wordBits[wordBits.size()-1] == "SET")
105 //get project version
106 std::getline(confFile,word,')');
107 CDMUtilities::splitter::split(wordBits, word, " ", CDMUtilities::splitter::no_empties);
108 if(wordBits[0] == "PROJECT_MAJOR_VERSION")
110 version = wordBits[1];
112 if(wordBits[0] == "PROJECT_MINOR_VERSION")
114 version += "." + wordBits[1];
116 if(wordBits[0] == "PROJECT_BUILD_VERSION")
118 version += "." + wordBits[1];
121 //get project versionDate
122 if(wordBits[0] == "PROJECT_VERSION_DATE")
124 std::vector<std::string> versionBits;
125 CDMUtilities::splitter::split(versionBits, wordBits[1], "\"", CDMUtilities::splitter::no_empties);
126 versionDate = versionBits[0];
128 //get project buildPath
132 this->buildPath = buildPath;
136 this->buildPath = this->path + "Bin";
142 this->type = wxDIR_DIRS;
145 this->children.clear();
148 this->packages.clear();
152 wxDir dir(crea::std2wx((pathFixed).c_str()));
156 bool cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_DIRS);
159 std::string stdfileName = crea::wx2std(fileName);
161 //if appli, create appli
162 if(stdfileName == "appli")
164 this->appli = new modelCDMAppli(this, pathFixed + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
165 this->children.push_back(this->appli);
168 else if(stdfileName == "lib")
170 this->lib = new modelCDMLib(this, pathFixed + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
171 this->children.push_back(this->lib);
175 bool isPackage = false;
177 // check if cmakelists file exist in folder.
178 std::string tmppath = this->GetPath() + CDMUtilities::SLASH + stdfileName + CDMUtilities::SLASH + "CMakeLists.txt";
179 CDMUtilities::CMLFile CMfile = CDMUtilities::readCMLFile(tmppath.c_str());
180 if (CMfile.size() > 0)
182 std::cout << "-->" << stdfileName << " ::has cmakelists" << std::endl;
183 // check if cmakelists file has the SET(BBTK_PACKAGE_NAME [pkgname]) instruction.
184 for (int i = 0; !isPackage && i < CMfile.size(); ++i) {
185 if (CMfile[i].first == "command" && CMfile[i].second.size() > 1)
187 std::string cm = CMfile[i].second[0];
190 while(pos < CMfile[i].second.size()-1 && !isalpha(CMfile[i].second[pos][0]))
193 std::string pm1 = CMfile[i].second[pos];
195 std::cout << cm << " " << pm1 << std::endl;
196 std::transform(cm.begin(), cm.end(), cm.begin(), ::toupper);
197 std::transform(pm1.begin(), pm1.end(), pm1.begin(), ::toupper);
198 if(cm == "SET" && pm1 == "BBTK_PACKAGE_NAME")
206 // if package, create package
209 std::cout << "is Package\n";
210 modelCDMPackage* package = new modelCDMPackage(this, pathFixed + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
211 std::cout << "Package created\n";
212 this->packages.push_back(package);
213 this->children.push_back(package);
215 // if is an unknown folder, create folder
218 this->children.push_back(new modelCDMFolder(this, pathFixed + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1));
223 cont = dir.GetNext(&fileName);
226 cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_FILES);
229 std::string stdfileName = crea::wx2std(fileName);
230 std::size_t fileTypePos = stdfileName.find_last_of(".");
231 std::string fileType;
232 if(fileTypePos != std::string::npos)
233 fileType = stdfileName.substr(fileTypePos);
237 //if CMakeLists, create CMakeLists
238 if(stdfileName == "CMakeLists.txt")
240 this->CMakeLists = new modelCDMCMakeListsFile(this, pathFixed + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
241 this->children.push_back(this->CMakeLists);
243 //if is a code file, create modelCDMCodeFile
246 fileType == ".cxx" ||
248 fileType == ".cpp" ||
249 fileType == ".txx" ||
250 fileType == ".cmake" )
252 this->children.push_back(new modelCDMCodeFile(this, pathFixed + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1));
254 //if is an unknown file, create file
257 this->children.push_back(new modelCDMFile(this, pathFixed + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1));
260 cont = dir.GetNext(&fileName);
264 this->SortChildren();
265 std::sort(this->packages.begin(), this->packages.end(), CompareNodeItem);
269 modelCDMProject::~modelCDMProject()
273 const std::string& modelCDMProject::GetNameProject() const
275 return this->nameProject;
278 const std::string& modelCDMProject::GetVersion() const
280 return this->version;
283 const std::string& modelCDMProject::GetVersionDate() const
285 return this->versionDate;
288 const std::string& modelCDMProject::GetBuildPath() const
290 return this->buildPath;
293 const std::vector<modelCDMPackage*>& modelCDMProject::GetPackages() const
295 return this->packages;
298 modelCDMAppli* modelCDMProject::GetAppli() const
303 modelCDMLib* modelCDMProject::GetLib() const
308 std::string modelCDMProject::GetBuildInstruction() const
310 wxConfigBase* pConfig = wxConfigBase::Get();
311 std::string commandline = crea::wx2std(pConfig->Read(wxT("BUILD_COMMAND"), crea::std2wx(CDMUtilities::BUILD_COMMAND)));
314 std::string makeComm = "cd \"" + this->buildPath + "\";" + commandline;// -C \"" + this->buildPath + "\""; /*> \"" + this->buildPath + CDMUtilities::SLASH + "building.log\" 2>&1";*/
318 bool modelCDMProject::SetVersion(const std::string& version, std::string*& result)
321 std::vector<std::string> vers;
322 CDMUtilities::splitter::split(vers, version, " .", CDMUtilities::splitter::no_empties);
324 time_t now = time(0);
328 localtime_s(<m, &now);
330 ltm = *(localtime(&now));
333 std::stringstream date;
334 date << ltm.tm_mday << "/" << 1 + ltm.tm_mon << "/" << 1900 + ltm.tm_year;
336 //set name of library in CMakeLists inside copied folder
338 std::ifstream in((this->path + CDMUtilities::SLASH + "CMakeLists.txt").c_str());
341 result = new std::string("CMakeLists.txt file failed to open.");
344 std::ofstream out((this->path + CDMUtilities::SLASH + "CMakeLists.txt.tmp").c_str());
347 result = new std::string("CMakeLists.txt.tmp file failed to open.");
350 while (getline(in, line))
352 if(line.find("SET(PROJECT_MAJOR_VERSION") != std::string::npos)
353 line = "SET(PROJECT_MAJOR_VERSION " + vers[0] + ")";
354 else if(line.find("SET(PROJECT_MINOR_VERSION") != std::string::npos)
355 line = "SET(PROJECT_MINOR_VERSION " + vers[1] + ")";
356 else if(line.find("SET(PROJECT_BUILD_VERSION") != std::string::npos)
357 line = "SET(PROJECT_BUILD_VERSION " + vers[2] + ")";
358 else if(line.find("SET(PROJECT_VERSION_DATE") != std::string::npos)
359 line = "SET(PROJECT_VERSION_DATE \"" + date.str() + "\")";
360 out << line << std::endl;
364 //delete old file and rename new file
366 std::string renameCommand = "move /Y \"" + this->path + CDMUtilities::SLASH + "CMakeLists.txt.tmp\" \"" + this->path + CDMUtilities::SLASH + "CMakeLists.txt\"";
368 std::string renameCommand = "mv \"" + this->path + CDMUtilities::SLASH + "CMakeLists.txt.tmp\" \"" + this->path + CDMUtilities::SLASH + "CMakeLists.txt\"";
370 if(system(renameCommand.c_str()))
372 result = new std::string("An error occurred while running '" + renameCommand + "'.");
376 this->version = vers[0] + "." + vers[1] + "." + vers[2];
377 this->versionDate = date.str();
381 bool modelCDMProject::SetBuildPath(const std::string& path, std::string*& result)
385 result = new std::string("The path cannot be empty");
388 if(path == this->path)
390 result = new std::string("The path cannot be same as the project sources");
393 this->buildPath = path;
397 modelCDMIProjectTreeNode* modelCDMProject::CreatePackage(
398 const std::string& name,
399 std::string*& result,
400 const std::string& authors,
401 const std::string& authorsEmail,
402 const std::string& description,
403 const std::string& version
406 //fixing input parameters
407 std::vector<std::string> words;
409 CDMUtilities::splitter::split(words,name," '/\"\\,.",CDMUtilities::splitter::no_empties);
410 std::string nameFixed = "";
411 for (int i = 0; i < (int)(words.size()); i++)
413 nameFixed += words[i];
417 CDMUtilities::splitter::split(words,authors," '/\"\\,.",CDMUtilities::splitter::no_empties);
418 std::string authorFixed;
419 for (int i = 0; i < (int)(words.size()); i++)
421 authorFixed += words[i];
425 std::string descriptionFixed;
426 CDMUtilities::splitter::split(words,authorsEmail," '/\"\\,",CDMUtilities::splitter::no_empties);
427 for (int i = 0; i < (int)(words.size()); i++)
429 descriptionFixed += words[i] + "/";
432 CDMUtilities::splitter::split(words,description," '\"",CDMUtilities::splitter::no_empties);
433 for (int i = 0; i < (int)(words.size()); i++)
435 descriptionFixed += "_" + words[i];
438 //call project to create package : use bbCreatePackage <path> <name> [author] [description]
439 std::string creationCommand = "bbCreatePackage \"" + this->path + "\" \"" + nameFixed + "\" \"" + authorFixed + "\" \"" + descriptionFixed + "\"";
440 //TODO: bbCreatePackage script always returning 0. It should return 1 or greater if any error
441 bool resultCommand = 0 != system(creationCommand.c_str());
443 resultCommand = false;
447 result = new std::string("An error occurred while running '" + creationCommand + "'.");
451 //add library to project CMakeLists
452 std::fstream out1((this->path + CDMUtilities::SLASH + "CMakeLists.txt").c_str(), std::fstream::in | std::fstream::out | std::fstream::app);
455 out1 << "ADD_SUBDIRECTORY(bbtk_" << nameFixed << "_PKG)" << std::endl;
459 //add library to model
460 modelCDMPackage* package = new modelCDMPackage(this, this->path + CDMUtilities::SLASH + "bbtk_" + nameFixed + "_PKG", "bbtk_" + nameFixed + "_PKG", this->level + 1);
461 this->packages.push_back(package);
462 this->children.push_back(package);
464 //TODO: set package version
466 this->SortChildren();
468 result = new std::string(this->path + CDMUtilities::SLASH + name);
472 modelCDMIProjectTreeNode* modelCDMProject::CreateLibrary(
473 const std::string& name,
474 std::string*& result,
475 const std::string& path
478 if(this->lib != NULL)
480 return this->lib->CreateLibrary(name, result);
482 result = new std::string("there is no lib folder in this project.");
486 modelCDMIProjectTreeNode* modelCDMProject::CreateApplication(
487 const std::string& name,
489 std::string*& result,
490 const std::string& path
493 if(this->appli != NULL)
495 return this->appli->CreateApplication(name, type, result);
497 result = new std::string("there is no appli folder in this project.");
501 modelCDMIProjectTreeNode* modelCDMProject::CreateBlackBox(
502 const std::string& name,
503 const std::string& package,
504 const std::string& authors,
505 const std::string& authorsEmail,
506 const std::string& categories,
507 const std::string& description
510 //TODO: implement method
514 bool modelCDMProject::OpenCMakeListsFile(std::string*& result)
516 if (!CDMUtilities::openTextEditor(this->CMakeLists->GetPath()))
520 result = new std::string("Couldn't open CMakeLists file.");
525 const bool modelCDMProject::Refresh(std::string*& result)
527 std::cout << "refreshing project" << std::endl;
528 //open makelists file
529 std::string pathMakeLists = this->path + CDMUtilities::SLASH + "CMakeLists.txt";
531 std::ifstream confFile;
532 confFile.open((pathMakeLists).c_str());
535 while(confFile.is_open() && !confFile.eof())
537 //std::cout << "leyendo " << word << std::endl;
539 std::getline(confFile,word,'(');
540 std::vector<std::string> wordBits;
541 CDMUtilities::splitter::split(wordBits,word," (\n",CDMUtilities::splitter::no_empties);
543 if(wordBits[wordBits.size()-1] == "PROJECT")
545 std::getline(confFile,word,')');
546 std::vector<std::string> nameBits;
547 CDMUtilities::splitter::split(nameBits, word, " ", CDMUtilities::splitter::no_empties);
549 this->name = this->nameProject = "";
550 for (int i = 0; i < (int)(nameBits.size()); i++)
554 this->name += nameBits[i];
556 this->nameProject = this->name;
561 if(wordBits[wordBits.size()-1] == "SET")
563 //get project version
564 std::getline(confFile,word,')');
565 CDMUtilities::splitter::split(wordBits, word, " ", CDMUtilities::splitter::no_empties);
566 if(wordBits[0] == "PROJECT_MAJOR_VERSION")
568 version = wordBits[1];
570 if(wordBits[0] == "PROJECT_MINOR_VERSION")
572 version += "." + wordBits[1];
574 if(wordBits[0] == "PROJECT_BUILD_VERSION")
576 version += "." + wordBits[1];
579 //get project versionDate
580 if(wordBits[0] == "PROJECT_VERSION_DATE")
582 std::vector<std::string> versionBits;
583 CDMUtilities::splitter::split(versionBits, wordBits[1], "\"", CDMUtilities::splitter::no_empties);
584 versionDate = versionBits[0];
590 this->type = wxDIR_DIRS;
593 std::vector<bool> checked(this->children.size(), false);
594 std::vector<bool> checkedPackages(this->packages.size(), false);
597 wxDir dir(crea::std2wx((this->path).c_str()));
601 bool cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_DIRS);
604 std::string stdfileName = crea::wx2std(fileName);
606 //if appli, create appli
607 if(stdfileName == "appli")
609 if (this->appli == NULL)
611 this->appli = new modelCDMAppli(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
612 this->children.push_back(this->appli);
616 int pos = std::find(this->children.begin(), this->children.end(), this->appli) - this->children.begin();
618 if(!this->appli->Refresh(result))
623 else if(stdfileName == "lib")
625 if (this->lib == NULL)
627 this->lib = new modelCDMLib(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
628 this->children.push_back(this->lib);
632 int pos = std::find(this->children.begin(), this->children.end(), this->lib) - this->children.begin();
634 if(!this->lib->Refresh(result))
639 //if package , create package
640 /*else if(stdfileName.size() > 9 && stdfileName.substr(0,5) == "bbtk_" && stdfileName.substr(stdfileName.size()-4,4) == "_PKG")
643 for (int i = 0; !found && i < (int)(this->packages.size()); i++)
645 if (this->packages[i]->GetName() == stdfileName)
648 int pos = std::find(this->children.begin(), this->children.end(), this->packages[i]) - this->children.begin();
650 checkedPackages[i] = true;
651 if(!this->packages[i]->Refresh(result))
657 modelCDMPackage* package = new modelCDMPackage(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
658 this->packages.push_back(package);
659 this->children.push_back(package);
666 bool isPackage = false;
668 // check if cmakelists file exist in folder.
669 std::string tmppath = this->GetPath() + CDMUtilities::SLASH + stdfileName + CDMUtilities::SLASH + "CMakeLists.txt";
670 CDMUtilities::CMLFile CMfile = CDMUtilities::readCMLFile(tmppath.c_str());
671 if (CMfile.size() > 0)
674 // check if cmakelists file has the SET(BBTK_PACKAGE_NAME [pkgname]) instruction.
675 for (int i = 0; !isPackage && i < CMfile.size(); ++i) {
676 if (CMfile[i].first == "command" && CMfile[i].second.size() > 1)
678 std::string cm = CMfile[i].second[0];
681 while(pos < CMfile[i].second.size()-1 && !isalpha(CMfile[i].second[pos][0]))
684 std::string pm1 = CMfile[i].second[pos];
686 std::cout << cm << " " << pm1 << std::endl;
687 std::transform(cm.begin(), cm.end(), cm.begin(), ::toupper);
688 std::transform(pm1.begin(), pm1.end(), pm1.begin(), ::toupper);
689 if(cm == "SET" && pm1 == "BBTK_PACKAGE_NAME")
697 // if package, create package
701 for (int i = 0; !found && i < (int)(this->packages.size()); i++)
703 if (this->packages[i]->GetName() == stdfileName)
706 int pos = std::find(this->children.begin(), this->children.end(), this->packages[i]) - this->children.begin();
708 checkedPackages[i] = true;
709 if(!this->packages[i]->Refresh(result))
716 modelCDMPackage* package = new modelCDMPackage(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
717 this->packages.push_back(package);
718 this->children.push_back(package);
721 //if is an unknown folder, create folder
725 for (int i = 0; !found && i < (int)(this->children.size()); i++)
727 if (this->children[i]->GetName() == stdfileName)
731 if(!this->children[i]->Refresh(result))
738 modelCDMFolder* folder = new modelCDMFolder(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
739 this->children.push_back(folder);
744 cont = dir.GetNext(&fileName);
747 cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_FILES);
750 std::string stdfileName = crea::wx2std(fileName);
751 std::size_t fileTypePos = stdfileName.find_last_of(".");
752 std::string fileType;
753 if(fileTypePos != std::string::npos)
754 fileType = stdfileName.substr(fileTypePos);
758 //if CMakeLists, create CMakeLists
759 if(stdfileName == "CMakeLists.txt")
761 if (this->CMakeLists == NULL)
763 this->CMakeLists = new modelCDMCMakeListsFile(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
764 this->children.push_back(this->CMakeLists);
768 int pos = std::find(this->children.begin(), this->children.end(), this->CMakeLists) - this->children.begin();
770 if(!this->CMakeLists->Refresh(result))
774 //if is a code file, create modelCDMCodeFile
775 //if is an unknown file, create file
779 for (int i = 0; !found && i < (int)(this->children.size()); i++)
781 if (this->children[i]->GetName() == stdfileName)
785 if(!this->children[i]->Refresh(result))
794 fileType == ".cxx" ||
796 fileType == ".cpp" ||
797 fileType == ".txx" ||
798 fileType == ".cmake" )
800 this->children.push_back(new modelCDMCodeFile(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1));
804 modelCDMFile* file = new modelCDMFile(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
805 this->children.push_back(file);
810 cont = dir.GetNext(&fileName);
814 for (int i = 0; i < (int)(checkedPackages.size()); i++)
816 if(!checkedPackages[i])
818 this->packages.erase(this->packages.begin()+i);
819 checkedPackages.erase(checkedPackages.begin()+i);
823 for (int i = 0; i < (int)(checked.size()); i++)
827 delete this->children[i];
828 this->children.erase(this->children.begin()+i);
829 checked.erase(checked.begin()+i);
834 this->SortChildren();
835 std::sort(this->packages.begin(), this->packages.end(), CompareNodeItem);
839 bool modelCDMProject::ConfigureBuild(std::string*& result)
841 //TODO: adjust for windows and mac
844 if(0 == system("cmake-gui"))
848 result = new std::string("There was an error opening cmake-gui. Please make sure it's installed and that cmake's bin folder is in the system path.");
856 wxDir dir(crea::std2wx((this->buildPath).c_str()));
858 //if folder doesn't exist then create it
861 //create command line to create folder
862 std::string createComm = "mkdir \"" + this->buildPath + "\"";
863 //execute creation command
864 if (system(createComm.c_str()))
866 //if there was an error then report it
867 result = new std::string("There was an error creating the build path: \"" + this->buildPath + "\"");
871 //create command line to execute ccmake
872 //TODO:: adjust for different Linux distributions
873 std::string confComm = "gnome-terminal --tab --working-directory=\"" + this->buildPath + "\" -e \"ccmake '" + this->path + "'\"";
875 if(CDMUtilities::openTerminal(confComm))
877 //if there was an error then report it
878 result = new std::string("There was an error opening the configuration tool in the desired place.");
885 bool modelCDMProject::Build(std::string*& result, const std::string& line)
887 //TODO: adjust for windows and mac
890 std::string command = "start \"\" \"" + this->buildPath + CDMUtilities::SLASH + this->nameProject + ".sln\"";
891 //wxMessageBox(crea::std2wx(command), wxT("Project Compilation - Check!"));
892 if(0 == system(command.c_str()))
896 result = new std::string("An error has happened running: \"" + command + "\". Please make sure to have visual c++ installed.");
904 wxDir dir(crea::std2wx((this->buildPath).c_str()));
906 //if binary folder can't be opened then return false
909 result = new std::string("The build path could not be opened. Make sure to configure the project before compiling it");
912 //create make command
913 std::string makeComm = "gnome-terminal -e \"bash -c \\\"";
914 for (int i = 0; i < line.size(); i++)
918 makeComm+="\\\\\\\"";
920 else if(line[i] == '\\')
922 makeComm+="\\\\\\\\";
926 makeComm.push_back(line[i]);
929 makeComm += "; echo -e '\\a'; bash";
930 makeComm += "\\\"\"";
932 std::cout << "executing '" << makeComm << "'" << std::endl;
933 //execute make command
934 if(system(makeComm.c_str()))
936 //if there was an error then report it
937 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.");
944 bool modelCDMProject::Connect(std::string*& result, const std::string& folder)
946 //TODO: adjust for mac
950 wxDir dir(crea::std2wx(folder));
952 //if binary folder can't be opened then return false
955 result = new std::string("The path could not be opened. Make sure the folder exists and contains a bbtkPackage file.");
958 //create plug command
959 std::string plugComm = "bbPlugPackage \"" + folder + "\"";
960 std::cout << "executing '" << plugComm << "'" << std::endl;
961 //execute plug command
962 if(system(std::string("start cmd.exe /k \"" + plugComm + "\"").c_str()))
964 //if there was an error then report it
965 result = new std::string("There was an error plugging the packages of the project, please check the console to read more about the problem.");
973 wxDir dir(crea::std2wx(folder));
975 //if binary folder can't be opened then return false
978 result = new std::string("The path could not be opened. Make sure the folder exists and contains a bbtkPackage file.");
981 //create plug command
982 std::string plugComm = "bbPlugPackage \"" + this->buildPath + "\"";
984 std::string Comm = "gnome-terminal -e \"bash -c \\\"";
985 for (int i = 0; i < plugComm.size(); i++)
987 if(plugComm[i] == '"')
991 else if(plugComm[i] == '\\')
997 Comm.push_back(plugComm[i]);
1000 Comm += "; echo -e '\\a'; bash";
1004 std::cout << "executing '" << Comm << "'" << std::endl;
1005 //execute plug command
1006 if(system(Comm.c_str()))
1008 //if there was an error then report it
1009 result = new std::string("There was an error plugging the packages of the project, please check the console to read more about the problem.");
1016 void modelCDMProject::CheckStructure(std::map<std::string, bool>& properties)
1019 if(this->CMakeLists != NULL)
1021 //set properties parameters based on model
1022 properties["project add appli"] = this->appli != NULL;
1023 properties["project add lib"] = this->lib != NULL;
1024 for (int i = 0; i < (int)(this->packages.size()); i++)
1025 properties["project add " + packages[i]->GetName()] = false;
1028 std::ifstream confFile;
1029 confFile.open((this->CMakeLists->GetPath()).c_str());
1031 //take everything that is not commented
1032 std::string fileContent;
1035 std::vector<std::string> words;
1036 while(confFile.is_open() && !confFile.eof())
1038 std::getline(confFile,word, '\n');
1041 CDMUtilities::splitter::split(words, word, "#", CDMUtilities::splitter::empties_ok);
1042 if (words.size() > 0)
1045 CDMUtilities::splitter::split(words, word, " ", CDMUtilities::splitter::empties_ok);
1046 for (int i = 0; i < (int)(words.size()); i++)
1048 if(words[i].substr(0,2) == "//")
1050 fileContent += words[i] + " ";
1057 //check every instruction
1058 std::stringstream ss(fileContent);
1061 std::getline(ss,word, '(');
1063 //check instruction name
1064 CDMUtilities::splitter::split(words, word, " ", CDMUtilities::splitter::no_empties);
1067 if (words.size() > 0 && words[words.size()-1] == "SET")
1069 std::getline(ss,word, ')');
1071 CDMUtilities::splitter::split(words, word, " ", CDMUtilities::splitter::no_empties);
1072 if (words.size() > 1)
1074 if (words[0] == "USE_CREA" && words[1] == "ON")
1076 properties["project set USE_CREA"] = true;
1078 else if (words[0] == "USE_GDCM" && words[1] == "ON")
1080 properties["project set USE_GDCM"] = true;
1082 else if (words[0] == "USE_GDCM_VTK" && words[1] == "ON")
1084 properties["project set USE_GDCM_VTK"] = true;
1086 else if (words[0] == "USE_GDCM2" && words[1] == "ON")
1088 properties["project set USE_GDCM2"] = true;
1090 else if (words[0] == "USE_WXWIDGETS" && words[1] == "ON")
1092 properties["project set USE_WXWIDGETS"] = true;
1094 else if (words[0] == "USE_KWWIDGETS" && words[1] == "ON")
1096 properties["project set USE_KWWIDGETS"] = true;
1098 else if (words[0] == "USE_VTK" && words[1] == "ON")
1100 properties["project set USE_VTK"] = true;
1102 else if (words[0] == "USE_ITK" && words[1] == "ON")
1104 properties["project set USE_ITK"] = true;
1106 else if (words[0] == "USE_BOOST" && words[1] == "ON")
1108 properties["project set USE_BOOST"] = true;
1113 else if (words.size() > 0 && words[words.size()-1] == "ADD_SUBDIRECTORY")
1115 std::getline(ss,word, ')');
1116 //std::cout << word << std::endl;
1117 CDMUtilities::splitter::split(words, word, " ", CDMUtilities::splitter::no_empties);
1119 if (words.size() > 0)
1121 if(words[0] == "appli")
1123 properties["project add "+words[0]] = this->appli != NULL;
1125 else if(words[0] == "lib")
1127 properties["project add "+words[0]] = this->lib != NULL;
1131 properties["project add "+words[0]] = true;
1139 //check appli's structure
1140 this->appli->CheckStructure(properties);
1141 //check lib's structure
1142 this->lib->CheckStructure(properties);
1143 //check packages' structure
1144 for (int i = 0; i < (int)(this->packages.size()); i++)
1146 properties["package " + this->packages[i]->GetName()] = true;
1147 this->packages[i]->CheckStructure(properties);
1151 bool modelCDMProject::IsPackageIncluded(const std::string& package_name)
1153 if (this->HasCMakeLists())
1155 CDMUtilities::CMLFile cmlFile = CDMUtilities::readCMLFile(this->CMakeLists->GetPath().c_str());
1156 for (int i = 0; i < cmlFile.size(); ++i)
1158 if (cmlFile[i].first=="command" && cmlFile[i].second[0] == "ADD_SUBDIRECTORY")
1161 while (pos < cmlFile[i].second.size())
1163 if (!isspace(cmlFile[i].second[pos][0]) && cmlFile[i].second[pos][0] != '(' && cmlFile[i].second[pos][0] != '#')
1165 if (package_name == cmlFile[i].second[pos])
1178 bool modelCDMProject::SetPackageInclude(const std::string& package_name, const bool& toInclude)
1180 if (this->HasCMakeLists())
1182 CDMUtilities::CMLFile cmlFile = CDMUtilities::readCMLFile(this->CMakeLists->GetPath().c_str());
1186 for (int i = 0; i < cmlFile.size(); ++i)
1188 if(toInclude && cmlFile[i].first == "comment")
1190 std::vector<std::string> segments;
1191 std::string line = cmlFile[i].second[0];
1192 while(line[0] == '#')
1195 CDMUtilities::splitter::split(segments, line, " ()", CDMUtilities::splitter::no_empties);
1196 if (segments.size() > 1 && segments[0] == "ADD_SUBDIRECTORY" && segments[1] == package_name)
1199 while(cmlFile[i].second[0][0] == '#')
1200 cmlFile[i].second[0].erase(0,1);
1203 else if(cmlFile[i].first == "command" && cmlFile[i].second[0] == "ADD_SUBDIRECTORY")
1206 while (pos < cmlFile[i].second.size())
1208 if (!isspace(cmlFile[i].second[pos][0]) && cmlFile[i].second[pos][0] != '(' && cmlFile[i].second[pos][0] != '#')
1210 if (package_name == cmlFile[i].second[pos])
1215 cmlFile[i].first = "comment";
1216 cmlFile[i].second[0] = "#" + cmlFile[i].second[0];
1217 while (cmlFile[i].second.size() > 1)
1219 cmlFile[i].second[0] += cmlFile[i].second[1];
1220 cmlFile[i].second.erase(cmlFile[i].second.begin()+1);
1231 if (!found && toInclude)
1233 CDMUtilities::syntaxElement element;
1234 element.first = "command";
1235 element.second.push_back("ADD_SUBDIRECTORY(" + package_name + ")");
1236 cmlFile.push_back(element);
1239 return CDMUtilities::writeCMLFile(this->CMakeLists->GetPath().c_str(),cmlFile);
1244 std::map<std::string, bool> modelCDMProject::Get3rdPartyLibraries()
1246 std::map<std::string, std::string> correspondence;
1247 correspondence["USE_CREA"] = "Crea";
1248 correspondence["USE_GDCM"] = "GDCM";
1249 correspondence["USE_GDCM_VTK"] = "GDCM_VTK";
1250 correspondence["USE_GDCM2"] = "GDCM2";
1251 correspondence["USE_WXWIDGETS"] = "WxWidgets";
1252 correspondence["USE_KWWIDGETS"] = "KWWidgets";
1253 correspondence["USE_VTK"] = "VTK";
1254 correspondence["USE_ITK"] = "ITK";
1255 correspondence["USE_BOOST"] = "Boost";
1257 std::map<std::string, bool> res;
1258 res["Crea"] = false;
1259 res["GDCM"] = false;
1260 res["GDCM_VTK"] = false;
1261 res["GDCM2"] = false;
1262 res["WxWidgets"] = false;
1263 res["KWWidgets"] = false;
1266 res["Boost"] = false;
1268 if (this->HasCMakeLists())
1270 std::string CMfile = CDMUtilities::readFile(this->CMakeLists->GetPath().c_str());
1272 boost::regex expression("^\\h*SET([\\s]|#[^\\n]*\\n)*\\(([\\s]|#[^\\n]*\\n)*USE_\\w+\\s+ON");
1273 std::string::const_iterator start, end;
1274 start = CMfile.begin();
1276 boost::match_results<std::string::const_iterator> what;
1277 boost::match_flag_type flags = boost::match_default;
1278 while(boost::regex_search(start, end, what, expression, flags))
1280 //std::cout << what[0].str() << std::endl;
1281 boost::regex expression1 = boost::regex("USE_\\w+");
1282 std::string::const_iterator start1, end1;
1283 start1 = what[0].first;
1284 end1 = what[0].second;
1285 boost::match_results<std::string::const_iterator> what1;
1286 if(boost::regex_search(start1, end1, what1, expression1, flags))
1288 std::string dete = what1.str();
1289 CDMUtilities::normalizeStr(dete);
1290 //std::cout << dete << std::endl;
1291 if(correspondence.find(dete) != correspondence.end())
1292 res[correspondence[dete]] = true;
1294 start = what[0].second;
1301 bool modelCDMProject::Set3rdPartyLibrary(const std::string& library_name,
1302 const bool& toInclude)
1304 std::map<std::string, std::string> correspondence;
1305 correspondence["Crea"] = "USE_CREA";
1306 correspondence["GDCM"] = "USE_GDCM";
1307 correspondence["GDCM_VTK"] = "USE_GDCM_VTK";
1308 correspondence["GDCM2"] = "USE_GDCM2";
1309 correspondence["WxWidgets"] = "USE_WXWIDGETS";
1310 correspondence["KWWidgets"] = "USE_KWWIDGETS";
1311 correspondence["VTK"] = "USE_VTK";
1312 correspondence["ITK"] = "USE_ITK";
1313 correspondence["Boost"] = "USE_BOOST";
1315 if (correspondence.find(library_name) != correspondence.end())
1317 std::string library_command = correspondence[library_name];
1319 if (this->HasCMakeLists())
1321 std::string CMfile = CDMUtilities::readFile(this->CMakeLists->GetPath().c_str());
1322 std::string resCMfile = "";
1324 std::string::const_iterator start, end;
1325 boost::match_results<std::string::const_iterator> what, tmp;
1326 boost::match_flag_type flags = boost::match_default;
1329 start = CMfile.begin();
1332 //search for existing inclusions
1333 boost::regex expression("^\\h*SET([\\s]|#[^\\n]*\\n)*\\(([\\s]|#[^\\n]*\\n)*" + library_command + "\\s+ON\\s*\\)");
1334 while(boost::regex_search(start, end, what, expression, flags))
1337 resCMfile += what.prefix().str();
1340 std::string dete = what.str();
1341 int pos = dete.find("ON",0);
1342 dete.replace(pos, 2, "OFF");
1346 resCMfile += what.str();
1348 start = what[0].second;
1352 resCMfile += tmp.suffix().str();
1355 start = CMfile.begin();
1358 //search for existing exclusions
1359 boost::regex expression("^\\h*SET([\\s]|#[^\\n]*\\n)*\\(([\\s]|#[^\\n]*\\n)*" + library_command + "\\s+OFF\\s*\\)");
1360 while(boost::regex_search(start, end, what, expression, flags))
1363 resCMfile += what.prefix().str();
1366 std::string dete = what.str();
1367 int pos = dete.find("OFF",0);
1368 dete.replace(pos, 3, "ON");
1372 resCMfile += what.str();
1374 start = what[0].second;
1378 resCMfile += tmp.suffix().str();
1381 start = CMfile.begin();
1384 //search for existing commented inclusions
1385 expression = boost::regex("^\\h*#+\\h*SET([\\s]|#[^\\n]*\\n)*\\(([\\s]|#[^\\n]*\\n)*" + library_command + "\\s+ON\\s*\\)");
1386 while(boost::regex_search(start, end, what, expression, flags))
1389 resCMfile += what.prefix().str();
1392 std::string dete = what.str();
1393 for (int i = 0; i < dete.size(); ++i) {
1403 resCMfile += what.str();
1406 start = what[0].second;
1410 resCMfile += tmp.suffix().str();
1415 start = CMfile.begin();
1418 //search for position to insert
1419 expression = boost::regex("^\\h*#\\h*Libraries\\/tools used\\h*$");
1420 if(boost::regex_search(start, end, what, expression, flags))
1423 resCMfile += what.prefix().str();
1424 resCMfile += what.str();
1425 resCMfile += "\nSET(" + library_command + " ON)";
1426 resCMfile += what.suffix().str();
1440 return CDMUtilities::writeFile(this->CMakeLists->GetPath().c_str(), resCMfile);