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& buildPath
61 this->path = CDMUtilities::fixPath(path);
63 std::string pathFixed(CDMUtilities::fixPath(path));
65 //TODO: set pathMakeLists for windows
66 std::string pathMakeLists = pathFixed + "/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 + "/appli", this->level + 1);
160 this->children.push_back(this->appli);
163 else if(stdfileName == "lib")
165 this->lib = new modelCDMLib(pathFixed + "/lib", 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 + "/" + 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 + "/" + 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 + "/" + stdfileName, this->level + 1);
193 this->children.push_back(this->CMakeLists);
197 this->children.push_back(new modelCDMFile(pathFixed + "/" + 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 << "/" << ltm->tm_mon << "/" << 1900 + ltm->tm_year;
259 //set name of library in CMakeLists inside copied folder
261 std::ifstream in((this->path + "/CMakeLists.txt").c_str());
264 result = new std::string("CMakeLists.txt file failed to open.");
267 std::ofstream out((this->path + "/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 + "/CMakeLists.txt.tmp " + this->path + "/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 //TODO: fix for windows
368 modelCDMPackage* package = new modelCDMPackage(this->path + "/bbtk_" + nameFixed + "_PKG", this->level + 1);
369 this->packages.push_back(package);
370 this->children.push_back(package);
372 //TODO: set package version
374 this->SortChildren();
376 result = new std::string(this->path + "/" + name);
380 modelCDMIProjectTreeNode* modelCDMProject::CreateLibrary(
381 const std::string& name,
382 std::string*& result,
383 const std::string& path
386 if(this->lib != NULL)
388 return this->lib->CreateLibrary(name, result);
390 result = new std::string("there is no lib folder in this project.");
394 modelCDMIProjectTreeNode* modelCDMProject::CreateApplication(
395 const std::string& name,
396 std::string*& result,
397 const std::string& path
400 if(this->appli != NULL)
402 return this->appli->CreateApplication(name, result);
404 result = new std::string("there is no appli folder in this project.");
408 modelCDMIProjectTreeNode* modelCDMProject::CreateBlackBox(
409 const std::string& name,
410 const std::string& package,
411 const std::string& authors,
412 const std::string& authorsEmail,
413 const std::string& categories,
414 const std::string& description
417 //TODO: implement method
421 bool modelCDMProject::OpenCMakeListsFile(std::string*& result)
423 if (!CDMUtilities::openTextEditor(this->CMakeLists->GetPath()))
427 result = new std::string("Couldn't open CMakeLists file.");
432 const bool modelCDMProject::Refresh(std::string*& result)
434 std::cout << "refreshing project" << std::endl;
435 //open makelists file
436 //TODO: set pathMakeLists for windows
437 std::string pathMakeLists = this->path + "/CMakeLists.txt";
439 std::ifstream confFile;
440 confFile.open((pathMakeLists).c_str());
443 while(confFile.is_open() && !confFile.eof())
445 //std::cout << "leyendo " << word << std::endl;
447 std::getline(confFile,word,'(');
448 std::vector<std::string> wordBits;
449 CDMUtilities::splitter::split(wordBits,word," (\n",CDMUtilities::splitter::no_empties);
451 if(wordBits[wordBits.size()-1] == "PROJECT")
453 std::getline(confFile,word,')');
454 std::vector<std::string> nameBits;
455 CDMUtilities::splitter::split(nameBits, word, " ", CDMUtilities::splitter::no_empties);
457 this->name = this->nameProject = "";
458 for (int i = 0; i < nameBits.size(); i++)
462 this->name += nameBits[i];
464 this->nameProject = this->name;
469 if(wordBits[wordBits.size()-1] == "SET")
471 //get project version
472 std::getline(confFile,word,')');
473 CDMUtilities::splitter::split(wordBits, word, " ", CDMUtilities::splitter::no_empties);
474 if(wordBits[0] == "PROJECT_MAJOR_VERSION")
476 version = wordBits[1];
478 if(wordBits[0] == "PROJECT_MINOR_VERSION")
480 version += "." + wordBits[1];
482 if(wordBits[0] == "PROJECT_BUILD_VERSION")
484 version += "." + wordBits[1];
487 //get project versionDate
488 if(wordBits[0] == "PROJECT_VERSION_DATE")
490 std::vector<std::string> versionBits;
491 CDMUtilities::splitter::split(versionBits, wordBits[1], "\"", CDMUtilities::splitter::no_empties);
492 versionDate = versionBits[0];
498 this->type = wxDIR_DIRS;
501 std::vector<bool> checked(this->children.size(), false);
502 std::vector<bool> checkedPackages(this->packages.size(), false);
505 wxDir dir(crea::std2wx((this->path).c_str()));
509 bool cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_DIRS);
512 std::string stdfileName = crea::wx2std(fileName);
514 //if appli, create appli
515 if(stdfileName == "appli")
517 if (this->appli == NULL)
519 this->appli = new modelCDMAppli(this->path + "/appli", this->level + 1);
520 this->children.push_back(this->appli);
524 int pos = std::find(this->children.begin(), this->children.end(), this->appli) - this->children.begin();
526 if(!this->appli->Refresh(result))
531 else if(stdfileName == "lib")
533 if (this->lib == NULL)
535 this->lib = new modelCDMLib(this->path + "/lib", this->level + 1);
536 this->children.push_back(this->lib);
540 int pos = std::find(this->children.begin(), this->children.end(), this->lib) - this->children.begin();
542 if(!this->lib->Refresh(result))
547 //if package , create package
548 else if(stdfileName.size() > 9 && stdfileName.substr(0,5) == "bbtk_" && stdfileName.substr(stdfileName.size()-4,4) == "_PKG")
550 std::string packageName = stdfileName.substr(5, stdfileName.size()-9);
552 for (int i = 0;!found && i < this->packages.size(); i++)
554 if (this->packages[i]->GetName() == packageName)
557 int pos = std::find(this->children.begin(), this->children.end(), this->packages[i]) - this->children.begin();
559 checkedPackages[i] = true;
560 if(!this->packages[i]->Refresh(result))
566 modelCDMPackage* package = new modelCDMPackage(this->path + "/" + stdfileName, this->level + 1);
567 this->packages.push_back(package);
568 this->children.push_back(package);
572 //if is an unknown folder, create folder
576 for (int i = 0; !found && i < this->children.size(); i++)
578 if (this->children[i]->GetName() == stdfileName)
582 if(!this->children[i]->Refresh(result))
589 modelCDMFolder* folder = new modelCDMFolder(this->path + "/" + stdfileName, this->level + 1);
590 this->children.push_back(folder);
594 cont = dir.GetNext(&fileName);
597 cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_FILES);
600 std::string stdfileName = crea::wx2std(fileName);
602 //if CMakeLists, create CMakeLists
603 if(stdfileName == "CMakeLists.txt")
605 if (this->CMakeLists == NULL)
607 this->CMakeLists = new modelCDMCMakeListsFile(this->path + "/" + stdfileName, this->level + 1);
608 this->children.push_back(this->CMakeLists);
612 int pos = std::find(this->children.begin(), this->children.end(), this->CMakeLists) - this->children.begin();
614 if(!this->CMakeLists->Refresh(result))
618 //if is an unknown file, create file
622 for (int i = 0; i <!found && this->children.size(); i++)
624 if (this->children[i]->GetName() == stdfileName)
628 if(!this->children[i]->Refresh(result))
635 modelCDMFile* file = new modelCDMFile(this->path + "/" + stdfileName, this->level + 1);
636 this->children.push_back(file);
640 cont = dir.GetNext(&fileName);
644 for (int i = 0; i < checkedPackages.size(); i++)
646 if(!checkedPackages[i])
648 this->packages.erase(this->packages.begin()+i);
649 checkedPackages.erase(checkedPackages.begin()+i);
653 for (int i = 0; i < checked.size(); i++)
657 delete this->children[i];
658 this->children.erase(this->children.begin()+i);
659 checked.erase(checked.begin()+i);
663 this->SortChildren();
667 bool modelCDMProject::ConfigureBuild(std::string*& result)
669 //TODO: implement method
673 bool modelCDMProject::Build(std::string*& result)
675 //TODO: implement method
679 bool modelCDMProject::Connect(std::string*& result)
681 //TODO: implement method