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 < 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 < 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 < 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 < 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
247 std::string renameCommand = "mv \"" + this->path + CDMUtilities::SLASH + "CMakeLists.txt.tmp\" \"" + this->path + CDMUtilities::SLASH + "CMakeLists.txt\"";
248 if(system(renameCommand.c_str()))
250 result = new std::string("An error occurred while running '" + renameCommand + "'.");
254 this->authors = authorsReal;
258 bool modelCDMPackage::SetAuthorsEmail(const std::string& email, std::string*& result)
260 //TODO: implement method
264 bool modelCDMPackage::SetVersion(const std::string& version, std::string*& result)
266 std::vector<std::string> vers;
267 CDMUtilities::splitter::split(vers, version, " .", CDMUtilities::splitter::no_empties);
271 //opening original cmakelists
272 std::ifstream in((this->path + CDMUtilities::SLASH + "CMakeLists.txt").c_str());
275 result = new std::string("CMakeLists.txt file failed to open.");
278 //opening temporal cmakelists
279 std::ofstream out((this->path + CDMUtilities::SLASH + "CMakeLists.txt.tmp").c_str());
282 result = new std::string("CMakeLists.txt.tmp file failed to open.");
285 //copying contents from original to temporal and making changes
286 while (getline(in, line))
288 if(line.find("SET(${BBTK_PACKAGE_NAME}_MAJOR_VERSION") != std::string::npos)
289 line = "SET(${BBTK_PACKAGE_NAME}_MAJOR_VERSION " + vers[0] + ")";
290 else if(line.find("SET(${BBTK_PACKAGE_NAME}_VERSION") != std::string::npos)
291 line = "SET(${BBTK_PACKAGE_NAME}_MINOR_VERSION " + vers[1] + ")";
292 else if(line.find("SET(${BBTK_PACKAGE_NAME}_BUILD_VERSION") != std::string::npos)
293 line = "SET(${BBTK_PACKAGE_NAME}_BUILD_VERSION " + vers[2] + ")";
294 out << line << std::endl;
298 //delete old file and rename new file
299 std::string renameCommand = "mv \"" + this->path + CDMUtilities::SLASH + "CMakeLists.txt.tmp\" \"" + this->path + CDMUtilities::SLASH + "CMakeLists.txt\"";
300 if(system(renameCommand.c_str()))
302 result = new std::string("An error occurred while running '" + renameCommand + "'.");
306 this->version = vers[0] + "." + vers[1] + "." + vers[2];
310 bool modelCDMPackage::SetDescription(const std::string& description, std::string*& result)
312 std::vector<std::string> words;
313 CDMUtilities::splitter::split(words, description, " \n", CDMUtilities::splitter::no_empties);
314 std::string descriptionReal = words[0];
315 for (int i = 1; i < words.size(); i++)
317 descriptionReal += " " + words[i];
321 //opening original cmakelists
322 std::ifstream in((this->path + CDMUtilities::SLASH + "CMakeLists.txt").c_str());
325 result = new std::string("CMakeLists.txt file failed to open.");
328 //opening temporal cmakelists
329 std::ofstream out((this->path + CDMUtilities::SLASH + "CMakeLists.txt.tmp").c_str());
332 result = new std::string("CMakeLists.txt.tmp file failed to open.");
335 //copying contents from original to temporal and making changes
336 while (getline(in, line))
338 if(line.find("SET(${BBTK_PACKAGE_NAME}_DESCRIPTION") != std::string::npos)
339 line = "SET(${BBTK_PACKAGE_NAME}_DESCRIPTION \"" + descriptionReal + "\")";
340 out << line << std::endl;
344 //delete old file and rename new file
345 std::string renameCommand = "mv \"" + this->path + CDMUtilities::SLASH + "CMakeLists.txt.tmp\" \"" + this->path + CDMUtilities::SLASH + "CMakeLists.txt\"";
346 if(system(renameCommand.c_str()))
348 result = new std::string("An error occurred while running '" + renameCommand + "'.");
352 this->description = descriptionReal;
356 modelCDMBlackBox* modelCDMPackage::CreateBlackBox(
357 std::string*& result,
358 const std::string& name,
359 const std::string& type,
360 const std::string& format,
361 const std::string& categories,
362 const std::string& authors,
363 const std::string& authorsEmail,
364 const std::string& description
367 return this->src->CreateBlackBox(result,name, this->namePackage, type,format,categories,authors,authorsEmail,description);
370 const bool modelCDMPackage::Refresh(std::string*& result)
372 std::cout << "refreshing package " << this->namePackage << std::endl;
373 this->type = wxDIR_DIRS;
377 std::string pathMakeLists = path + CDMUtilities::SLASH + "CMakeLists.txt";
379 std::ifstream confFile;
380 confFile.open((pathMakeLists).c_str());
383 while(confFile.is_open() && !confFile.eof())
386 std::getline(confFile,word,'(');
387 std::vector<std::string> wordBits;
388 CDMUtilities::splitter::split(wordBits,word," (\n",CDMUtilities::splitter::no_empties);
390 if(wordBits[wordBits.size()-1] == "SET")
393 std::getline(confFile,word,')');
394 CDMUtilities::splitter::split(wordBits, word, " ", CDMUtilities::splitter::no_empties);
395 if(wordBits[0] == "BBTK_PACKAGE_NAME")
398 for (int i = 2; i < wordBits.size(); i++)
400 word += " " + wordBits[i];
403 CDMUtilities::splitter::split(wordBits, word, "\"", CDMUtilities::splitter::no_empties);
405 this->namePackage = wordBits[0];
407 else if(wordBits[0] == "${BBTK_PACKAGE_NAME}_AUTHOR")
410 for (int i = 2; i < wordBits.size(); i++)
412 word += " " + wordBits[i];
415 CDMUtilities::splitter::split(wordBits, word, "\"", CDMUtilities::splitter::no_empties);
417 this->authors = wordBits[0];
419 else if(wordBits[0] == "${BBTK_PACKAGE_NAME}_DESCRIPTION")
422 for (int i = 2; i < wordBits.size(); i++)
424 word += " " + wordBits[i];
427 CDMUtilities::splitter::split(wordBits, word, "\"", CDMUtilities::splitter::no_empties);
429 this->description = wordBits[0];
431 else if(wordBits[0] == "${BBTK_PACKAGE_NAME}_MAJOR_VERSION")
433 this->version = wordBits[1];
435 else if(wordBits[0] == "${BBTK_PACKAGE_NAME}_MINOR_VERSION")
437 this->version += "." + wordBits[1];
439 else if(wordBits[0] == "${BBTK_PACKAGE_NAME}_BUILD_VERSION")
441 this->version += "." + wordBits[1];
448 std::vector<bool> checked(this->children.size(), false);
449 bool checkedSrc = false;
452 wxDir dir(crea::std2wx((this->path).c_str()));
456 bool cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_DIRS);
460 std::string stdfileName = crea::wx2std(fileName);
462 //detect black boxes in src
463 if(stdfileName == "src")
465 //check if box already exist
467 if (this->src != NULL)
470 int pos = std::find(this->children.begin(), this->children.end(), this->src) - this->children.begin();
473 if(!this->src->Refresh(result))
478 this->src = new modelCDMPackageSrc(this, path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level +1);
479 this->children.push_back(this->src);
485 //check if folder already exist
487 for (int i = 0; !found && i < this->children.size(); i++)
489 if (this->children[i]->GetName() == stdfileName)
493 if(!this->children[i]->Refresh(result))
499 modelCDMFolder* folder = new modelCDMFolder(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
500 this->children.push_back(folder);
503 cont = dir.GetNext(&fileName);
507 cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_FILES);
510 std::string stdfileName = crea::wx2std(fileName);
512 //if CMakeLists, create CMakeLists
513 if(stdfileName == "CMakeLists.txt")
515 if (this->CMakeLists == NULL)
517 this->CMakeLists = new modelCDMCMakeListsFile(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
518 this->children.push_back(this->CMakeLists);
522 int pos = std::find(this->children.begin(), this->children.end(), this->CMakeLists) - this->children.begin();
524 if(!this->CMakeLists->Refresh(result))
528 //if is an unknown file, create file
532 for (int i = 0; !found && i < this->children.size(); i++)
534 if (this->children[i]->GetName() == stdfileName)
538 if(!this->children[i]->Refresh(result))
545 modelCDMFile* file = new modelCDMFile(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
546 this->children.push_back(file);
550 cont = dir.GetNext(&fileName);
559 for (int i = 0; i < checked.size(); i++)
563 delete this->children[i];
564 this->children.erase(this->children.begin()+i);
565 checked.erase(checked.begin()+i);
569 this->SortChildren();
573 void modelCDMPackage::CheckStructure(std::map<std::string, bool>& properties)
576 if(this->CMakeLists != NULL)
579 std::ifstream confFile;
580 confFile.open((this->CMakeLists->GetPath()).c_str());
582 //take everything that is not commented
583 std::string fileContent;
586 std::vector<std::string> words;
587 while(confFile.is_open() && !confFile.eof())
589 std::getline(confFile,word, '\n');
592 CDMUtilities::splitter::split(words, word, "#", CDMUtilities::splitter::empties_ok);
593 if (words.size() > 0)
596 CDMUtilities::splitter::split(words, word, " ", CDMUtilities::splitter::empties_ok);
597 for (int i = 0; i < words.size(); i++)
599 if(words[i].substr(0,2) == "//")
601 fileContent += words[i] + " ";
607 //check every instruction
608 std::stringstream ss(fileContent);
611 std::getline(ss,word, '(');
613 //check instruction name
614 CDMUtilities::splitter::split(words, word, " ", CDMUtilities::splitter::no_empties);
617 if (words.size() > 0 && words[words.size()-1] == "SET")
619 std::getline(ss,word, ')');
621 CDMUtilities::splitter::split(words, word, " \t", CDMUtilities::splitter::no_empties);
622 if (words.size() > 1)
624 if (words[0] == "${BBTK_PACKAGE_NAME}_USE_VTK" && words[1] == "ON")
626 properties["package " + this->name + " set USE_VTK"] = true;
628 else if (words[0] == "${BBTK_PACKAGE_NAME}_USE_ITK" && words[1] == "ON")
630 properties["package " + this->name + " set USE_ITK"] = true;
632 else if (words[0] == "${BBTK_PACKAGE_NAME}_USE_GDCM" && words[1] == "ON")
634 properties["package " + this->name + " set USE_GDCM"] = true;
636 else if (words[0] == "${BBTK_PACKAGE_NAME}_USE_GDCM_VTK" && words[1] == "ON")
638 properties["package " + this->name + " set USE_GDCM_VTK"] = true;
640 else if (words[0] == "${BBTK_PACKAGE_NAME}_USE_GSMIS" && words[1] == "ON")
642 properties["package " + this->name + " set USE_GSMIS"] = true;
644 else if (words[0] == "${BBTK_PACKAGE_NAME}_USE_WXWIDGETS" && words[1] == "ON")
646 properties["package " + this->name + " set USE_WXWIDGETS"] = true;
648 else if (words[0] == "${BBTK_PACKAGE_NAME}_USE_KWWIDGETS" && words[1] == "ON")
650 properties["package " + this->name + " set USE_KWWIDGETS"] = true;
652 else if (words[0] == "USE_BOOST" && words[1] == "ON")
654 properties["package " + this->name + " set USE_BOOST"] = true;
656 else if (words[0] == "${BBTK_PACKAGE_NAME}_INCLUDE_DIRS")
658 for (int i = 1; i < words.size(); i++)
660 if(words[i].substr(0,2) == "${" || words[i].substr(0,2) == "..")
661 properties["package " + this->name + " dir " + words[i]] = true;
664 else if (words[0] == "${BBTK_PACKAGE_NAME}_LIBS")
666 for (int i = 1; i < words.size(); i++)
668 properties["package " + this->name + " lib " + words[i]] = true;