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: Nov 23, 2012
32 * Author: Daniel Felipe Gonzalez Obando
35 #include "modelCDMPackage.h"
43 #include "CDMUtilities.h"
45 modelCDMPackage::modelCDMPackage()
50 modelCDMPackage::modelCDMPackage(modelCDMIProjectTreeNode* parent, const std::string& path, const std::string& name, const int& level)
52 std::cout << "creating package: " + path + "\n";
53 this->parent = parent;
54 this->type = wxDIR_DIRS;
58 std::string pathMakeLists = path + CDMUtilities::SLASH + "CMakeLists.txt";
60 std::ifstream confFile;
61 confFile.open((pathMakeLists).c_str());
64 while(confFile.is_open() && !confFile.eof())
67 std::getline(confFile,word,'(');
68 std::vector<std::string> wordBits;
69 CDMUtilities::splitter::split(wordBits,word," (\n",CDMUtilities::splitter::no_empties);
71 if(wordBits[wordBits.size()-1] == "SET")
74 std::getline(confFile,word,')');
75 CDMUtilities::splitter::split(wordBits, word, " ", CDMUtilities::splitter::no_empties);
76 if(wordBits[0] == "BBTK_PACKAGE_NAME")
79 for (int i = 2; i < (int)(wordBits.size()); i++)
81 word += " " + wordBits[i];
84 CDMUtilities::splitter::split(wordBits, word, "\"", CDMUtilities::splitter::no_empties);
86 this->namePackage = wordBits[0];
88 else if(wordBits[0] == "${BBTK_PACKAGE_NAME}_AUTHOR")
91 for (int i = 2; i < (int)(wordBits.size()); i++)
93 word += " " + wordBits[i];
96 CDMUtilities::splitter::split(wordBits, word, "\"", CDMUtilities::splitter::no_empties);
98 this->authors = wordBits[0];
100 else if(wordBits[0] == "${BBTK_PACKAGE_NAME}_DESCRIPTION")
103 for (int i = 2; i < (int)(wordBits.size()); i++)
105 word += " " + wordBits[i];
108 CDMUtilities::splitter::split(wordBits, word, "\"", CDMUtilities::splitter::no_empties);
110 this->description = wordBits[0];
112 else if(wordBits[0] == "${BBTK_PACKAGE_NAME}_MAJOR_VERSION")
114 this->version = wordBits[1];
116 else if(wordBits[0] == "${BBTK_PACKAGE_NAME}_MINOR_VERSION")
118 this->version += "." + wordBits[1];
120 else if(wordBits[0] == "${BBTK_PACKAGE_NAME}_BUILD_VERSION")
122 this->version += "." + wordBits[1];
130 //check all folders and files
131 wxDir dir(crea::std2wx((path).c_str()));
137 bool cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_DIRS);
140 std::string stdfileName = crea::wx2std(fileName);
141 //if src, check for black boxes
142 if(stdfileName == "src")
144 this->src = new modelCDMPackageSrc(this, path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
145 this->children.push_back(this->src);
149 this->children.push_back(new modelCDMFolder(this, path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1));
152 cont = dir.GetNext(&fileName);
156 cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_FILES);
159 std::string stdfileName = crea::wx2std(fileName);
161 //if CMakeLists, create CMakeLists
162 if(stdfileName == "CMakeLists.txt")
164 this->CMakeLists = new modelCDMCMakeListsFile(this, path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
165 this->children.push_back(this->CMakeLists);
169 this->children.push_back(new modelCDMFile(this, path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1));
171 //if is an unknown file, create file
172 cont = dir.GetNext(&fileName);
175 this->SortChildren();
178 modelCDMPackage::~modelCDMPackage()
182 const std::string& modelCDMPackage::GetNamePackage() const
184 return this->namePackage;
187 const std::string& modelCDMPackage::GetAuthors() const
189 return this->authors;
192 const std::string& modelCDMPackage::GetAuthorsEmail() const
194 return this->authorsEmail;
197 const std::string& modelCDMPackage::GetVersion() const
199 return this->version;
202 const std::string& modelCDMPackage::GetDescription() const
204 return this->description;
207 modelCDMPackageSrc* modelCDMPackage::GetSrc() const
212 bool modelCDMPackage::SetAuthors(const std::string& authors, std::string*& result)
214 std::vector<std::string> words;
215 CDMUtilities::splitter::split(words, authors, ",\n", CDMUtilities::splitter::no_empties);
216 std::string authorsReal = words[0];
217 for (int i = 1; i < (int)(words.size()); i++)
219 authorsReal += "/" + words[i];
223 //opening original cmakelists
224 std::ifstream in((this->path + CDMUtilities::SLASH + "CMakeLists.txt").c_str());
227 result = new std::string("CMakeLists.txt file failed to open.");
230 //opening temporal cmakelists
231 std::ofstream out((this->path + CDMUtilities::SLASH + "CMakeLists.txt.tmp").c_str());
234 result = new std::string("CMakeLists.txt.tmp file failed to open.");
237 //copying contents from original to temporal and making changes
238 while (getline(in, line))
240 if(line.find("SET(${BBTK_PACKAGE_NAME}_AUTHOR") != std::string::npos)
241 line = "SET(${BBTK_PACKAGE_NAME}_AUTHOR \"" + authorsReal + "\")";
242 out << line << std::endl;
246 //delete old file and rename new file
248 std::string renameCommand = "move /Y \"" + this->path + CDMUtilities::SLASH + "CMakeLists.txt.tmp\" \"" + this->path + CDMUtilities::SLASH + "CMakeLists.txt\"";
250 std::string renameCommand = "mv \"" + this->path + CDMUtilities::SLASH + "CMakeLists.txt.tmp\" \"" + this->path + CDMUtilities::SLASH + "CMakeLists.txt\"";
253 if(system(renameCommand.c_str()))
255 result = new std::string("An error occurred while running '" + renameCommand + "'.");
259 this->authors = authorsReal;
263 bool modelCDMPackage::SetAuthorsEmail(const std::string& email, std::string*& result)
265 //TODO: implement method
269 bool modelCDMPackage::SetVersion(const std::string& version, std::string*& result)
271 std::vector<std::string> vers;
272 CDMUtilities::splitter::split(vers, version, " .", CDMUtilities::splitter::no_empties);
276 //opening original cmakelists
277 std::ifstream in((this->path + CDMUtilities::SLASH + "CMakeLists.txt").c_str());
280 result = new std::string("CMakeLists.txt file failed to open.");
283 //opening temporal cmakelists
284 std::ofstream out((this->path + CDMUtilities::SLASH + "CMakeLists.txt.tmp").c_str());
287 result = new std::string("CMakeLists.txt.tmp file failed to open.");
290 //copying contents from original to temporal and making changes
291 while (getline(in, line))
293 if(line.find("SET(${BBTK_PACKAGE_NAME}_MAJOR_VERSION") != std::string::npos)
294 line = "SET(${BBTK_PACKAGE_NAME}_MAJOR_VERSION " + vers[0] + ")";
295 else if(line.find("SET(${BBTK_PACKAGE_NAME}_VERSION") != std::string::npos)
296 line = "SET(${BBTK_PACKAGE_NAME}_MINOR_VERSION " + vers[1] + ")";
297 else if(line.find("SET(${BBTK_PACKAGE_NAME}_BUILD_VERSION") != std::string::npos)
298 line = "SET(${BBTK_PACKAGE_NAME}_BUILD_VERSION " + vers[2] + ")";
299 out << line << std::endl;
303 //delete old file and rename new file
305 std::string renameCommand = "move /Y \"" + this->path + CDMUtilities::SLASH + "CMakeLists.txt.tmp\" \"" + this->path + CDMUtilities::SLASH + "CMakeLists.txt\"";
307 std::string renameCommand = "mv \"" + this->path + CDMUtilities::SLASH + "CMakeLists.txt.tmp\" \"" + this->path + CDMUtilities::SLASH + "CMakeLists.txt\"";
310 if(system(renameCommand.c_str()))
312 result = new std::string("An error occurred while running '" + renameCommand + "'.");
316 this->version = vers[0] + "." + vers[1] + "." + vers[2];
320 bool modelCDMPackage::SetDescription(const std::string& description, std::string*& result)
322 std::vector<std::string> words;
323 CDMUtilities::splitter::split(words, description, " \n", CDMUtilities::splitter::no_empties);
324 std::string descriptionReal = words[0];
325 for (int i = 1; i < (int)(words.size()); i++)
327 descriptionReal += " " + words[i];
331 //opening original cmakelists
332 std::ifstream in((this->path + CDMUtilities::SLASH + "CMakeLists.txt").c_str());
335 result = new std::string("CMakeLists.txt file failed to open.");
338 //opening temporal cmakelists
339 std::ofstream out((this->path + CDMUtilities::SLASH + "CMakeLists.txt.tmp").c_str());
342 result = new std::string("CMakeLists.txt.tmp file failed to open.");
345 //copying contents from original to temporal and making changes
346 while (getline(in, line))
348 if(line.find("SET(${BBTK_PACKAGE_NAME}_DESCRIPTION") != std::string::npos)
349 line = "SET(${BBTK_PACKAGE_NAME}_DESCRIPTION \"" + descriptionReal + "\")";
350 out << line << std::endl;
354 //delete old file and rename new file
356 std::string renameCommand = "move /Y \"" + this->path + CDMUtilities::SLASH + "CMakeLists.txt.tmp\" \"" + this->path + CDMUtilities::SLASH + "CMakeLists.txt\"";
358 std::string renameCommand = "mv \"" + this->path + CDMUtilities::SLASH + "CMakeLists.txt.tmp\" \"" + this->path + CDMUtilities::SLASH + "CMakeLists.txt\"";
361 if(system(renameCommand.c_str()))
363 result = new std::string("An error occurred while running '" + renameCommand + "'.");
367 this->description = descriptionReal;
371 modelCDMBlackBox* modelCDMPackage::CreateBlackBox(
372 std::string*& result,
373 const std::string& name,
374 const std::string& type,
375 const std::string& format,
376 const std::string& categories,
377 const std::string& authors,
378 const std::string& authorsEmail,
379 const std::string& description
382 return this->src->CreateBlackBox(result,name, this->namePackage, type,format,categories,authors,authorsEmail,description);
385 const bool modelCDMPackage::Refresh(std::string*& result)
387 std::cout << "refreshing package " << this->namePackage << std::endl;
388 this->type = wxDIR_DIRS;
392 std::string pathMakeLists = path + CDMUtilities::SLASH + "CMakeLists.txt";
394 std::ifstream confFile;
395 confFile.open((pathMakeLists).c_str());
398 while(confFile.is_open() && !confFile.eof())
401 std::getline(confFile,word,'(');
402 std::vector<std::string> wordBits;
403 CDMUtilities::splitter::split(wordBits,word," (\n",CDMUtilities::splitter::no_empties);
405 if(wordBits[wordBits.size()-1] == "SET")
408 std::getline(confFile,word,')');
409 CDMUtilities::splitter::split(wordBits, word, " ", CDMUtilities::splitter::no_empties);
410 if(wordBits[0] == "BBTK_PACKAGE_NAME")
413 for (int i = 2; i < (int)(wordBits.size()); i++)
415 word += " " + wordBits[i];
418 CDMUtilities::splitter::split(wordBits, word, "\"", CDMUtilities::splitter::no_empties);
420 this->namePackage = wordBits[0];
422 else if(wordBits[0] == "${BBTK_PACKAGE_NAME}_AUTHOR")
425 for (int i = 2; i < (int)(wordBits.size()); i++)
427 word += " " + wordBits[i];
430 CDMUtilities::splitter::split(wordBits, word, "\"", CDMUtilities::splitter::no_empties);
432 this->authors = wordBits[0];
434 else if(wordBits[0] == "${BBTK_PACKAGE_NAME}_DESCRIPTION")
437 for (int i = 2; i < (int)(wordBits.size()); i++)
439 word += " " + wordBits[i];
442 CDMUtilities::splitter::split(wordBits, word, "\"", CDMUtilities::splitter::no_empties);
444 this->description = wordBits[0];
446 else if(wordBits[0] == "${BBTK_PACKAGE_NAME}_MAJOR_VERSION")
448 this->version = wordBits[1];
450 else if(wordBits[0] == "${BBTK_PACKAGE_NAME}_MINOR_VERSION")
452 this->version += "." + wordBits[1];
454 else if(wordBits[0] == "${BBTK_PACKAGE_NAME}_BUILD_VERSION")
456 this->version += "." + wordBits[1];
463 std::vector<bool> checked(this->children.size(), false);
464 bool checkedSrc = false;
467 wxDir dir(crea::std2wx((this->path).c_str()));
471 bool cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_DIRS);
475 std::string stdfileName = crea::wx2std(fileName);
477 //detect black boxes in src
478 if(stdfileName == "src")
480 //check if box already exist
482 if (this->src != NULL)
485 int pos = std::find(this->children.begin(), this->children.end(), this->src) - this->children.begin();
488 if(!this->src->Refresh(result))
493 this->src = new modelCDMPackageSrc(this, path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level +1);
494 this->children.push_back(this->src);
500 //check if folder already exist
502 for (int i = 0; !found && i < (int)(this->children.size()); i++)
504 if (this->children[i]->GetName() == stdfileName)
508 if(!this->children[i]->Refresh(result))
514 modelCDMFolder* folder = new modelCDMFolder(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
515 this->children.push_back(folder);
518 cont = dir.GetNext(&fileName);
522 cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_FILES);
525 std::string stdfileName = crea::wx2std(fileName);
527 //if CMakeLists, create CMakeLists
528 if(stdfileName == "CMakeLists.txt")
530 if (this->CMakeLists == NULL)
532 this->CMakeLists = new modelCDMCMakeListsFile(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
533 this->children.push_back(this->CMakeLists);
537 int pos = std::find(this->children.begin(), this->children.end(), this->CMakeLists) - this->children.begin();
539 if(!this->CMakeLists->Refresh(result))
543 //if is an unknown file, create file
547 for (int i = 0; !found && i < (int)(this->children.size()); i++)
549 if (this->children[i]->GetName() == stdfileName)
553 if(!this->children[i]->Refresh(result))
560 modelCDMFile* file = new modelCDMFile(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
561 this->children.push_back(file);
565 cont = dir.GetNext(&fileName);
574 for (int i = 0; i < (int)(checked.size()); i++)
578 delete this->children[i];
579 this->children.erase(this->children.begin()+i);
580 checked.erase(checked.begin()+i);
584 this->SortChildren();
588 void modelCDMPackage::CheckStructure(std::map<std::string, bool>& properties)
591 if(this->CMakeLists != NULL)
594 std::ifstream confFile;
595 confFile.open((this->CMakeLists->GetPath()).c_str());
597 //take everything that is not commented
598 std::string fileContent;
601 std::vector<std::string> words;
602 while(confFile.is_open() && !confFile.eof())
604 std::getline(confFile,word, '\n');
607 CDMUtilities::splitter::split(words, word, "#", CDMUtilities::splitter::empties_ok);
608 if (words.size() > 0)
611 CDMUtilities::splitter::split(words, word, " ", CDMUtilities::splitter::empties_ok);
612 for (int i = 0; i < (int)(words.size()); i++)
614 if(words[i].substr(0,2) == "//")
616 fileContent += words[i] + " ";
622 //check every instruction
623 std::stringstream ss(fileContent);
626 std::getline(ss,word, '(');
628 //check instruction name
629 CDMUtilities::splitter::split(words, word, " ", CDMUtilities::splitter::no_empties);
632 if (words.size() > 0 && words[words.size()-1] == "SET")
634 std::getline(ss,word, ')');
636 CDMUtilities::splitter::split(words, word, " \t", CDMUtilities::splitter::no_empties);
637 if (words.size() > 1)
639 if (words[0] == "${BBTK_PACKAGE_NAME}_USE_VTK" && words[1] == "ON")
641 properties["package " + this->name + " set USE_VTK"] = true;
643 else if (words[0] == "${BBTK_PACKAGE_NAME}_USE_ITK" && words[1] == "ON")
645 properties["package " + this->name + " set USE_ITK"] = true;
647 else if (words[0] == "${BBTK_PACKAGE_NAME}_USE_GDCM" && words[1] == "ON")
649 properties["package " + this->name + " set USE_GDCM"] = true;
651 else if (words[0] == "${BBTK_PACKAGE_NAME}_USE_GDCM_VTK" && words[1] == "ON")
653 properties["package " + this->name + " set USE_GDCM_VTK"] = true;
655 else if (words[0] == "${BBTK_PACKAGE_NAME}_USE_GSMIS" && words[1] == "ON")
657 properties["package " + this->name + " set USE_GSMIS"] = true;
659 else if (words[0] == "${BBTK_PACKAGE_NAME}_USE_WXWIDGETS" && words[1] == "ON")
661 properties["package " + this->name + " set USE_WXWIDGETS"] = true;
663 else if (words[0] == "${BBTK_PACKAGE_NAME}_USE_KWWIDGETS" && words[1] == "ON")
665 properties["package " + this->name + " set USE_KWWIDGETS"] = true;
667 else if (words[0] == "USE_BOOST" && words[1] == "ON")
669 properties["package " + this->name + " set USE_BOOST"] = true;
671 else if (words[0] == "${BBTK_PACKAGE_NAME}_INCLUDE_DIRS")
673 for (int i = 1; i < (int)(words.size()); i++)
675 if(words[i].substr(0,2) == "${" || words[i].substr(0,2) == "..")
676 properties["package " + this->name + " dir " + words[i]] = true;
679 else if (words[0] == "${BBTK_PACKAGE_NAME}_LIBS")
681 for (int i = 1; i < (int)(words.size()); i++)
683 properties["package " + this->name + " lib " + words[i]] = true;