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 const std::string& path,
58 const std::string& name,
59 const std::string& buildPath
62 this->path = CDMUtilities::fixPath(path);
64 std::string pathFixed(CDMUtilities::fixPath(path));
66 std::string pathMakeLists = pathFixed + CDMUtilities::SLASH + "CMakeLists.txt";
68 std::ifstream confFile;
69 confFile.open((pathMakeLists).c_str());
72 while(confFile.is_open() && !confFile.eof())
74 //std::cout << "leyendo " << word << std::endl;
76 std::getline(confFile,word,'(');
77 std::vector<std::string> wordBits;
78 CDMUtilities::splitter::split(wordBits,word," (\n",CDMUtilities::splitter::no_empties);
80 if(wordBits[wordBits.size()-1] == "PROJECT")
82 std::getline(confFile,word,')');
83 std::vector<std::string> nameBits;
84 CDMUtilities::splitter::split(nameBits, word, " ", CDMUtilities::splitter::no_empties);
86 this->name = this->nameProject = "";
87 for (int i = 0; i < nameBits.size(); i++)
91 this->name += nameBits[i];
93 this->nameProject = this->name;
98 if(wordBits[wordBits.size()-1] == "SET")
100 //get project version
101 std::getline(confFile,word,')');
102 CDMUtilities::splitter::split(wordBits, word, " ", CDMUtilities::splitter::no_empties);
103 if(wordBits[0] == "PROJECT_MAJOR_VERSION")
105 version = wordBits[1];
107 if(wordBits[0] == "PROJECT_MINOR_VERSION")
109 version += "." + wordBits[1];
111 if(wordBits[0] == "PROJECT_BUILD_VERSION")
113 version += "." + wordBits[1];
116 //get project versionDate
117 if(wordBits[0] == "PROJECT_VERSION_DATE")
119 std::vector<std::string> versionBits;
120 CDMUtilities::splitter::split(versionBits, wordBits[1], "\"", CDMUtilities::splitter::no_empties);
121 versionDate = versionBits[0];
123 //get project buildPath
127 this->buildPath = buildPath;
131 this->buildPath = this->path + "Bin";
137 this->type = wxDIR_DIRS;
140 this->children.clear();
143 this->packages.clear();
147 wxDir dir(crea::std2wx((pathFixed).c_str()));
151 bool cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_DIRS);
154 std::string stdfileName = crea::wx2std(fileName);
156 //if appli, create appli
157 if(stdfileName == "appli")
159 this->appli = new modelCDMAppli(pathFixed + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
160 this->children.push_back(this->appli);
163 else if(stdfileName == "lib")
165 this->lib = new modelCDMLib(pathFixed + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
166 this->children.push_back(this->lib);
168 //if package , create package
169 else if(stdfileName.size() > 9 && stdfileName.substr(0,5) == "bbtk_" && stdfileName.substr(stdfileName.size()-4,4) == "_PKG")
171 modelCDMPackage* package = new modelCDMPackage(pathFixed + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
172 this->packages.push_back(package);
173 this->children.push_back(package);
175 //if is an unknown folder, create folder
178 this->children.push_back(new modelCDMFolder(pathFixed + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1));
181 cont = dir.GetNext(&fileName);
184 cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_FILES);
187 std::string stdfileName = crea::wx2std(fileName);
189 //if CMakeLists, create CMakeLists
190 if(stdfileName == "CMakeLists.txt")
192 this->CMakeLists = new modelCDMCMakeListsFile(pathFixed + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
193 this->children.push_back(this->CMakeLists);
197 this->children.push_back(new modelCDMFile(pathFixed + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1));
199 //if is an unknown file, create file
200 cont = dir.GetNext(&fileName);
204 this->SortChildren();
208 modelCDMProject::~modelCDMProject()
212 const std::string& modelCDMProject::GetNameProject() const
214 return this->nameProject;
217 const std::string& modelCDMProject::GetVersion() const
219 return this->version;
222 const std::string& modelCDMProject::GetVersionDate() const
224 return this->versionDate;
227 const std::string& modelCDMProject::GetBuildPath() const
229 return this->buildPath;
232 const std::vector<modelCDMPackage*>& modelCDMProject::GetPackages() const
234 return this->packages;
237 modelCDMAppli* modelCDMProject::GetAppli() const
242 modelCDMLib* modelCDMProject::GetLib() const
247 bool modelCDMProject::SetVersion(const std::string& version, std::string*& result)
250 std::vector<std::string> vers;
251 CDMUtilities::splitter::split(vers, version, " .", CDMUtilities::splitter::no_empties);
253 time_t now = time(0);
254 tm* ltm = localtime(&now);
256 std::stringstream date;
257 date << ltm->tm_mday << "/" << 1 + ltm->tm_mon << "/" << 1900 + ltm->tm_year;
259 //set name of library in CMakeLists inside copied folder
261 std::ifstream in((this->path + CDMUtilities::SLASH + "CMakeLists.txt").c_str());
264 result = new std::string("CMakeLists.txt file failed to open.");
267 std::ofstream out((this->path + CDMUtilities::SLASH + "CMakeLists.txt.tmp").c_str());
270 result = new std::string("CMakeLists.txt.tmp file failed to open.");
273 while (getline(in, line))
275 if(line.find("SET(PROJECT_MAJOR_VERSION") != std::string::npos)
276 line = "SET(PROJECT_MAJOR_VERSION " + vers[0] + ")";
277 else if(line.find("SET(PROJECT_MINOR_VERSION") != std::string::npos)
278 line = "SET(PROJECT_MINOR_VERSION " + vers[1] + ")";
279 else if(line.find("SET(PROJECT_BUILD_VERSION") != std::string::npos)
280 line = "SET(PROJECT_BUILD_VERSION " + vers[2] + ")";
281 else if(line.find("SET(PROJECT_VERSION_DATE") != std::string::npos)
282 line = "SET(PROJECT_VERSION_DATE \"" + date.str() + "\")";
283 out << line << std::endl;
287 //delete old file and rename new file
288 std::string renameCommand = "mv \"" + this->path + CDMUtilities::SLASH + "CMakeLists.txt.tmp\" \"" + this->path + CDMUtilities::SLASH + "CMakeLists.txt\"";
289 if(system(renameCommand.c_str()))
291 result = new std::string("An error occurred while running '" + renameCommand + "'.");
295 this->version = vers[0] + "." + vers[1] + "." + vers[2];
296 this->versionDate = date.str();
300 bool modelCDMProject::SetBuildPath(const std::string& path, std::string*& result)
304 result = new std::string("The path cannot be empty");
307 if(path == this->path)
309 result = new std::string("The path cannot be same as the project sources");
312 this->buildPath = path;
316 modelCDMIProjectTreeNode* modelCDMProject::CreatePackage(
317 const std::string& name,
318 std::string*& result,
319 const std::string& authors,
320 const std::string& authorsEmail,
321 const std::string& description,
322 const std::string& version
325 //fixing input parameters
326 std::vector<std::string> words;
328 CDMUtilities::splitter::split(words,name," ",CDMUtilities::splitter::no_empties);
329 std::string nameFixed = "";
330 for (int i = 0; i < words.size(); i++)
332 nameFixed += words[i];
336 CDMUtilities::splitter::split(words,authors," ",CDMUtilities::splitter::no_empties);
337 std::string authorFixed;
338 for (int i = 0; i < words.size(); i++)
340 authorFixed += words[i];
344 std::string descriptionFixed;
345 CDMUtilities::splitter::split(words,authorsEmail," ",CDMUtilities::splitter::no_empties);
346 for (int i = 0; i < words.size(); i++)
348 descriptionFixed += words[i];
351 CDMUtilities::splitter::split(words,description," ",CDMUtilities::splitter::no_empties);
352 for (int i = 0; i < words.size(); i++)
354 descriptionFixed += "_" + words[i];
357 //call project to create package : use bbCreatePackage <path> <name> [author] [description]
358 std::string creationCommand = "bbCreatePackage \"" + this->path + "\" \"" + nameFixed + "\" \"" + authorFixed + "\" \"" + descriptionFixed + "\"";
359 //TODO: bbCreatePackage script always returning 0. It should return 1 or greater if any error
360 if(system(creationCommand.c_str()))
362 result = new std::string("An error occurred while running '" + creationCommand + "'.");
366 //add library to model
367 modelCDMPackage* package = new modelCDMPackage(this->path + CDMUtilities::SLASH + "bbtk_" + nameFixed + "_PKG", "bbtk_" + nameFixed + "_PKG", this->level + 1);
368 this->packages.push_back(package);
369 this->children.push_back(package);
371 //TODO: set package version
373 this->SortChildren();
375 result = new std::string(this->path + CDMUtilities::SLASH + name);
379 modelCDMIProjectTreeNode* modelCDMProject::CreateLibrary(
380 const std::string& name,
381 std::string*& result,
382 const std::string& path
385 if(this->lib != NULL)
387 return this->lib->CreateLibrary(name, result);
389 result = new std::string("there is no lib folder in this project.");
393 modelCDMIProjectTreeNode* modelCDMProject::CreateApplication(
394 const std::string& name,
395 std::string*& result,
396 const std::string& path
399 if(this->appli != NULL)
401 return this->appli->CreateApplication(name, result);
403 result = new std::string("there is no appli folder in this project.");
407 modelCDMIProjectTreeNode* modelCDMProject::CreateBlackBox(
408 const std::string& name,
409 const std::string& package,
410 const std::string& authors,
411 const std::string& authorsEmail,
412 const std::string& categories,
413 const std::string& description
416 //TODO: implement method
420 bool modelCDMProject::OpenCMakeListsFile(std::string*& result)
422 if (!CDMUtilities::openTextEditor(this->CMakeLists->GetPath()))
426 result = new std::string("Couldn't open CMakeLists file.");
431 const bool modelCDMProject::Refresh(std::string*& result)
433 std::cout << "refreshing project" << std::endl;
434 //open makelists file
435 std::string pathMakeLists = this->path + CDMUtilities::SLASH + "CMakeLists.txt";
437 std::ifstream confFile;
438 confFile.open((pathMakeLists).c_str());
441 while(confFile.is_open() && !confFile.eof())
443 //std::cout << "leyendo " << word << std::endl;
445 std::getline(confFile,word,'(');
446 std::vector<std::string> wordBits;
447 CDMUtilities::splitter::split(wordBits,word," (\n",CDMUtilities::splitter::no_empties);
449 if(wordBits[wordBits.size()-1] == "PROJECT")
451 std::getline(confFile,word,')');
452 std::vector<std::string> nameBits;
453 CDMUtilities::splitter::split(nameBits, word, " ", CDMUtilities::splitter::no_empties);
455 this->name = this->nameProject = "";
456 for (int i = 0; i < nameBits.size(); i++)
460 this->name += nameBits[i];
462 this->nameProject = this->name;
467 if(wordBits[wordBits.size()-1] == "SET")
469 //get project version
470 std::getline(confFile,word,')');
471 CDMUtilities::splitter::split(wordBits, word, " ", CDMUtilities::splitter::no_empties);
472 if(wordBits[0] == "PROJECT_MAJOR_VERSION")
474 version = wordBits[1];
476 if(wordBits[0] == "PROJECT_MINOR_VERSION")
478 version += "." + wordBits[1];
480 if(wordBits[0] == "PROJECT_BUILD_VERSION")
482 version += "." + wordBits[1];
485 //get project versionDate
486 if(wordBits[0] == "PROJECT_VERSION_DATE")
488 std::vector<std::string> versionBits;
489 CDMUtilities::splitter::split(versionBits, wordBits[1], "\"", CDMUtilities::splitter::no_empties);
490 versionDate = versionBits[0];
496 this->type = wxDIR_DIRS;
499 std::vector<bool> checked(this->children.size(), false);
500 std::vector<bool> checkedPackages(this->packages.size(), false);
503 wxDir dir(crea::std2wx((this->path).c_str()));
507 bool cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_DIRS);
510 std::string stdfileName = crea::wx2std(fileName);
512 //if appli, create appli
513 if(stdfileName == "appli")
515 if (this->appli == NULL)
517 this->appli = new modelCDMAppli(this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
518 this->children.push_back(this->appli);
522 int pos = std::find(this->children.begin(), this->children.end(), this->appli) - this->children.begin();
524 if(!this->appli->Refresh(result))
529 else if(stdfileName == "lib")
531 if (this->lib == NULL)
533 this->lib = new modelCDMLib(this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
534 this->children.push_back(this->lib);
538 int pos = std::find(this->children.begin(), this->children.end(), this->lib) - this->children.begin();
540 if(!this->lib->Refresh(result))
545 //if package , create package
546 else if(stdfileName.size() > 9 && stdfileName.substr(0,5) == "bbtk_" && stdfileName.substr(stdfileName.size()-4,4) == "_PKG")
548 std::string packageName = stdfileName.substr(5, stdfileName.size()-9);
550 for (int i = 0;!found && i < this->packages.size(); i++)
552 if (this->packages[i]->GetName() == packageName)
555 int pos = std::find(this->children.begin(), this->children.end(), this->packages[i]) - this->children.begin();
557 checkedPackages[i] = true;
558 if(!this->packages[i]->Refresh(result))
564 modelCDMPackage* package = new modelCDMPackage(this->path + CDMUtilities::SLASH + stdfileName, stdfileName,this->level + 1);
565 this->packages.push_back(package);
566 this->children.push_back(package);
570 //if is an unknown folder, create folder
574 for (int i = 0; !found && i < this->children.size(); i++)
576 if (this->children[i]->GetName() == stdfileName)
580 if(!this->children[i]->Refresh(result))
587 modelCDMFolder* folder = new modelCDMFolder(this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
588 this->children.push_back(folder);
592 cont = dir.GetNext(&fileName);
595 cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_FILES);
598 std::string stdfileName = crea::wx2std(fileName);
600 //if CMakeLists, create CMakeLists
601 if(stdfileName == "CMakeLists.txt")
603 if (this->CMakeLists == NULL)
605 this->CMakeLists = new modelCDMCMakeListsFile(this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
606 this->children.push_back(this->CMakeLists);
610 int pos = std::find(this->children.begin(), this->children.end(), this->CMakeLists) - this->children.begin();
612 if(!this->CMakeLists->Refresh(result))
616 //if is an unknown file, create file
620 for (int i = 0; i <!found && this->children.size(); i++)
622 if (this->children[i]->GetName() == stdfileName)
626 if(!this->children[i]->Refresh(result))
633 modelCDMFile* file = new modelCDMFile(this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
634 this->children.push_back(file);
638 cont = dir.GetNext(&fileName);
642 for (int i = 0; i < checkedPackages.size(); i++)
644 if(!checkedPackages[i])
646 this->packages.erase(this->packages.begin()+i);
647 checkedPackages.erase(checkedPackages.begin()+i);
651 for (int i = 0; i < checked.size(); i++)
655 delete this->children[i];
656 this->children.erase(this->children.begin()+i);
657 checked.erase(checked.begin()+i);
661 this->SortChildren();
665 bool modelCDMProject::ConfigureBuild(std::string*& result)
667 //TODO: implement method
671 bool modelCDMProject::Build(std::string*& result)
673 //TODO: implement method
677 bool modelCDMProject::Connect(std::string*& result)
679 //TODO: implement method