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 std::cout << "creating project: " + name + " in " + path + "\n";
63 this->path = CDMUtilities::fixPath(path);
65 std::string pathFixed(CDMUtilities::fixPath(path));
67 std::string pathMakeLists = pathFixed + CDMUtilities::SLASH + "CMakeLists.txt";
69 std::ifstream confFile;
70 confFile.open((pathMakeLists).c_str());
73 while(confFile.is_open() && !confFile.eof())
75 //std::cout << "leyendo " << word << std::endl;
77 std::getline(confFile,word,'(');
78 std::vector<std::string> wordBits;
79 CDMUtilities::splitter::split(wordBits,word," (\n",CDMUtilities::splitter::no_empties);
81 if(wordBits[wordBits.size()-1] == "PROJECT")
83 std::getline(confFile,word,')');
84 std::vector<std::string> nameBits;
85 CDMUtilities::splitter::split(nameBits, word, " ", CDMUtilities::splitter::no_empties);
87 this->name = this->nameProject = "";
88 for (int i = 0; i < nameBits.size(); i++)
92 this->name += nameBits[i];
94 this->nameProject = this->name;
99 if(wordBits[wordBits.size()-1] == "SET")
101 //get project version
102 std::getline(confFile,word,')');
103 CDMUtilities::splitter::split(wordBits, word, " ", CDMUtilities::splitter::no_empties);
104 if(wordBits[0] == "PROJECT_MAJOR_VERSION")
106 version = wordBits[1];
108 if(wordBits[0] == "PROJECT_MINOR_VERSION")
110 version += "." + wordBits[1];
112 if(wordBits[0] == "PROJECT_BUILD_VERSION")
114 version += "." + wordBits[1];
117 //get project versionDate
118 if(wordBits[0] == "PROJECT_VERSION_DATE")
120 std::vector<std::string> versionBits;
121 CDMUtilities::splitter::split(versionBits, wordBits[1], "\"", CDMUtilities::splitter::no_empties);
122 versionDate = versionBits[0];
124 //get project buildPath
128 this->buildPath = buildPath;
132 this->buildPath = this->path + "Bin";
138 this->type = wxDIR_DIRS;
141 this->children.clear();
144 this->packages.clear();
148 wxDir dir(crea::std2wx((pathFixed).c_str()));
152 bool cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_DIRS);
155 std::string stdfileName = crea::wx2std(fileName);
157 //if appli, create appli
158 if(stdfileName == "appli")
160 this->appli = new modelCDMAppli(pathFixed + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
161 this->children.push_back(this->appli);
164 else if(stdfileName == "lib")
166 this->lib = new modelCDMLib(pathFixed + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
167 this->children.push_back(this->lib);
169 //if package , create package
170 else if(stdfileName.size() > 9 && stdfileName.substr(0,5) == "bbtk_" && stdfileName.substr(stdfileName.size()-4,4) == "_PKG")
172 modelCDMPackage* package = new modelCDMPackage(pathFixed + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
173 this->packages.push_back(package);
174 this->children.push_back(package);
176 //if is an unknown folder, create folder
179 this->children.push_back(new modelCDMFolder(pathFixed + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1));
182 cont = dir.GetNext(&fileName);
185 cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_FILES);
188 std::string stdfileName = crea::wx2std(fileName);
190 //if CMakeLists, create CMakeLists
191 if(stdfileName == "CMakeLists.txt")
193 this->CMakeLists = new modelCDMCMakeListsFile(pathFixed + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
194 this->children.push_back(this->CMakeLists);
198 this->children.push_back(new modelCDMFile(pathFixed + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1));
200 //if is an unknown file, create file
201 cont = dir.GetNext(&fileName);
205 this->SortChildren();
206 std::sort(this->packages.begin(), this->packages.end(), CompareNodeItem);
210 modelCDMProject::~modelCDMProject()
214 const std::string& modelCDMProject::GetNameProject() const
216 return this->nameProject;
219 const std::string& modelCDMProject::GetVersion() const
221 return this->version;
224 const std::string& modelCDMProject::GetVersionDate() const
226 return this->versionDate;
229 const std::string& modelCDMProject::GetBuildPath() const
231 return this->buildPath;
234 const std::vector<modelCDMPackage*>& modelCDMProject::GetPackages() const
236 return this->packages;
239 modelCDMAppli* modelCDMProject::GetAppli() const
244 modelCDMLib* modelCDMProject::GetLib() const
249 bool modelCDMProject::SetVersion(const std::string& version, std::string*& result)
252 std::vector<std::string> vers;
253 CDMUtilities::splitter::split(vers, version, " .", CDMUtilities::splitter::no_empties);
255 time_t now = time(0);
256 tm* ltm = localtime(&now);
258 std::stringstream date;
259 date << ltm->tm_mday << "/" << 1 + ltm->tm_mon << "/" << 1900 + ltm->tm_year;
261 //set name of library in CMakeLists inside copied folder
263 std::ifstream in((this->path + CDMUtilities::SLASH + "CMakeLists.txt").c_str());
266 result = new std::string("CMakeLists.txt file failed to open.");
269 std::ofstream out((this->path + CDMUtilities::SLASH + "CMakeLists.txt.tmp").c_str());
272 result = new std::string("CMakeLists.txt.tmp file failed to open.");
275 while (getline(in, line))
277 if(line.find("SET(PROJECT_MAJOR_VERSION") != std::string::npos)
278 line = "SET(PROJECT_MAJOR_VERSION " + vers[0] + ")";
279 else if(line.find("SET(PROJECT_MINOR_VERSION") != std::string::npos)
280 line = "SET(PROJECT_MINOR_VERSION " + vers[1] + ")";
281 else if(line.find("SET(PROJECT_BUILD_VERSION") != std::string::npos)
282 line = "SET(PROJECT_BUILD_VERSION " + vers[2] + ")";
283 else if(line.find("SET(PROJECT_VERSION_DATE") != std::string::npos)
284 line = "SET(PROJECT_VERSION_DATE \"" + date.str() + "\")";
285 out << line << std::endl;
289 //delete old file and rename new file
290 std::string renameCommand = "mv \"" + this->path + CDMUtilities::SLASH + "CMakeLists.txt.tmp\" \"" + this->path + CDMUtilities::SLASH + "CMakeLists.txt\"";
291 if(system(renameCommand.c_str()))
293 result = new std::string("An error occurred while running '" + renameCommand + "'.");
297 this->version = vers[0] + "." + vers[1] + "." + vers[2];
298 this->versionDate = date.str();
302 bool modelCDMProject::SetBuildPath(const std::string& path, std::string*& result)
306 result = new std::string("The path cannot be empty");
309 if(path == this->path)
311 result = new std::string("The path cannot be same as the project sources");
314 this->buildPath = path;
318 modelCDMIProjectTreeNode* modelCDMProject::CreatePackage(
319 const std::string& name,
320 std::string*& result,
321 const std::string& authors,
322 const std::string& authorsEmail,
323 const std::string& description,
324 const std::string& version
327 //fixing input parameters
328 std::vector<std::string> words;
330 CDMUtilities::splitter::split(words,name," ",CDMUtilities::splitter::no_empties);
331 std::string nameFixed = "";
332 for (int i = 0; i < words.size(); i++)
334 nameFixed += words[i];
338 CDMUtilities::splitter::split(words,authors," ",CDMUtilities::splitter::no_empties);
339 std::string authorFixed;
340 for (int i = 0; i < words.size(); i++)
342 authorFixed += words[i];
346 std::string descriptionFixed;
347 CDMUtilities::splitter::split(words,authorsEmail," ",CDMUtilities::splitter::no_empties);
348 for (int i = 0; i < words.size(); i++)
350 descriptionFixed += words[i];
353 CDMUtilities::splitter::split(words,description," ",CDMUtilities::splitter::no_empties);
354 for (int i = 0; i < words.size(); i++)
356 descriptionFixed += "_" + words[i];
359 //call project to create package : use bbCreatePackage <path> <name> [author] [description]
360 std::string creationCommand = "bbCreatePackage \"" + this->path + "\" \"" + nameFixed + "\" \"" + authorFixed + "\" \"" + descriptionFixed + "\"";
361 //TODO: bbCreatePackage script always returning 0. It should return 1 or greater if any error
362 if(system(creationCommand.c_str()))
364 result = new std::string("An error occurred while running '" + creationCommand + "'.");
368 //add library to model
369 modelCDMPackage* package = new modelCDMPackage(this->path + CDMUtilities::SLASH + "bbtk_" + nameFixed + "_PKG", "bbtk_" + nameFixed + "_PKG", this->level + 1);
370 this->packages.push_back(package);
371 this->children.push_back(package);
373 //TODO: set package version
375 this->SortChildren();
377 result = new std::string(this->path + CDMUtilities::SLASH + name);
381 modelCDMIProjectTreeNode* modelCDMProject::CreateLibrary(
382 const std::string& name,
383 std::string*& result,
384 const std::string& path
387 if(this->lib != NULL)
389 return this->lib->CreateLibrary(name, result);
391 result = new std::string("there is no lib folder in this project.");
395 modelCDMIProjectTreeNode* modelCDMProject::CreateApplication(
396 const std::string& name,
397 std::string*& result,
398 const std::string& path
401 if(this->appli != NULL)
403 return this->appli->CreateApplication(name, result);
405 result = new std::string("there is no appli folder in this project.");
409 modelCDMIProjectTreeNode* modelCDMProject::CreateBlackBox(
410 const std::string& name,
411 const std::string& package,
412 const std::string& authors,
413 const std::string& authorsEmail,
414 const std::string& categories,
415 const std::string& description
418 //TODO: implement method
422 bool modelCDMProject::OpenCMakeListsFile(std::string*& result)
424 if (!CDMUtilities::openTextEditor(this->CMakeLists->GetPath()))
428 result = new std::string("Couldn't open CMakeLists file.");
433 const bool modelCDMProject::Refresh(std::string*& result)
435 std::cout << "refreshing project" << std::endl;
436 //open makelists file
437 std::string pathMakeLists = this->path + CDMUtilities::SLASH + "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 + CDMUtilities::SLASH + stdfileName, stdfileName, 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 + CDMUtilities::SLASH + stdfileName, stdfileName, 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")
551 for (int i = 0; !found && i < this->packages.size(); i++)
553 if (this->packages[i]->GetName() == stdfileName)
556 int pos = std::find(this->children.begin(), this->children.end(), this->packages[i]) - this->children.begin();
558 checkedPackages[i] = true;
559 if(!this->packages[i]->Refresh(result))
565 modelCDMPackage* package = new modelCDMPackage(this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
566 this->packages.push_back(package);
567 this->children.push_back(package);
571 //if is an unknown folder, create folder
575 for (int i = 0; !found && i < this->children.size(); i++)
577 if (this->children[i]->GetName() == stdfileName)
581 if(!this->children[i]->Refresh(result))
588 modelCDMFolder* folder = new modelCDMFolder(this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
589 this->children.push_back(folder);
593 cont = dir.GetNext(&fileName);
596 cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_FILES);
599 std::string stdfileName = crea::wx2std(fileName);
601 //if CMakeLists, create CMakeLists
602 if(stdfileName == "CMakeLists.txt")
604 if (this->CMakeLists == NULL)
606 this->CMakeLists = new modelCDMCMakeListsFile(this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
607 this->children.push_back(this->CMakeLists);
611 int pos = std::find(this->children.begin(), this->children.end(), this->CMakeLists) - this->children.begin();
613 if(!this->CMakeLists->Refresh(result))
617 //if is an unknown file, create file
621 for (int i = 0; !found && i < this->children.size(); i++)
623 if (this->children[i]->GetName() == stdfileName)
627 if(!this->children[i]->Refresh(result))
634 modelCDMFile* file = new modelCDMFile(this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
635 this->children.push_back(file);
639 cont = dir.GetNext(&fileName);
643 for (int i = 0; i < checkedPackages.size(); i++)
645 if(!checkedPackages[i])
647 this->packages.erase(this->packages.begin()+i);
648 checkedPackages.erase(checkedPackages.begin()+i);
652 for (int i = 0; i < checked.size(); i++)
656 delete this->children[i];
657 this->children.erase(this->children.begin()+i);
658 checked.erase(checked.begin()+i);
663 this->SortChildren();
664 std::sort(this->packages.begin(), this->packages.end(), CompareNodeItem);
668 bool modelCDMProject::ConfigureBuild(std::string*& result)
670 //TODO: implement method
674 bool modelCDMProject::Build(std::string*& result)
676 //TODO: implement method
680 bool modelCDMProject::Connect(std::string*& result)
682 //TODO: implement method