]> Creatis software - crea.git/blob - lib/creaDevManagerLib/modelCDMProject.cpp
Feature #1711 CreaDevManager application implementation
[crea.git] / lib / creaDevManagerLib / modelCDMProject.cpp
1 /*
2 # ---------------------------------------------------------------------
3 #
4 # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image 
5 #                        pour la Sant�)
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
9 #
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.
16 #
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
21 #  liability. 
22 #
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 # ------------------------------------------------------------------------ 
26  */
27
28 /*
29  * modelCDMProject.cpp
30  *
31  *  Created on: 13/11/2012
32  *      Author: Daniel Felipe Gonzalez Obando
33  */
34
35 #include "modelCDMProject.h"
36
37 #include <iostream>
38 #include <sstream>
39 #include <vector>
40 #include <algorithm>
41 #include <fstream>
42 #include <ctime>
43
44 #include "CDMUtilities.h"
45 #include "creaWx.h"
46 #include "wx/dir.h"
47
48 modelCDMProject::modelCDMProject()
49 {
50   std::cout << "in constructor1" << std::endl;
51   this->appli = NULL;
52   this->lib = NULL;
53   this->CMakeLists = NULL;
54 }
55
56 modelCDMProject::modelCDMProject(
57     modelCDMIProjectTreeNode* parent,
58     const std::string& path,
59     const std::string& name,
60     const std::string& buildPath
61 )
62 {
63   std::cout << "creating project: " + name + " in " + path + "\n";
64   this->parent = parent;
65   this->path = CDMUtilities::fixPath(path);
66   //open makelists file
67   std::string pathFixed(CDMUtilities::fixPath(path));
68
69   std::string pathMakeLists = pathFixed + CDMUtilities::SLASH + "CMakeLists.txt";
70
71   std::ifstream confFile;
72   confFile.open((pathMakeLists).c_str());
73
74   std::string word;
75   while(confFile.is_open() && !confFile.eof())
76     {
77       //std::cout << "leyendo " << word << std::endl;
78       //get project name
79       std::getline(confFile,word,'(');
80       std::vector<std::string> wordBits;
81       CDMUtilities::splitter::split(wordBits,word," (\n",CDMUtilities::splitter::no_empties);
82
83       if(wordBits[wordBits.size()-1] == "PROJECT")
84         {
85           std::getline(confFile,word,')');
86           std::vector<std::string> nameBits;
87           CDMUtilities::splitter::split(nameBits, word, " ", CDMUtilities::splitter::no_empties);
88
89           this->name = this->nameProject = "";
90           for (int i = 0; i < (int)(nameBits.size()); i++)
91             {
92               if(i != 0)
93                 this->name += " ";
94               this->name += nameBits[i];
95             }
96           this->nameProject = this->name;
97
98         }
99
100
101       if(wordBits[wordBits.size()-1] == "SET")
102         {
103           //get project version
104           std::getline(confFile,word,')');
105           CDMUtilities::splitter::split(wordBits, word, " ", CDMUtilities::splitter::no_empties);
106           if(wordBits[0] == "PROJECT_MAJOR_VERSION")
107             {
108               version = wordBits[1];
109             }
110           if(wordBits[0] == "PROJECT_MINOR_VERSION")
111             {
112               version += "." + wordBits[1];
113             }
114           if(wordBits[0] == "PROJECT_BUILD_VERSION")
115             {
116               version += "." + wordBits[1];
117             }
118
119           //get project versionDate
120           if(wordBits[0] == "PROJECT_VERSION_DATE")
121             {
122               std::vector<std::string> versionBits;
123               CDMUtilities::splitter::split(versionBits, wordBits[1], "\"", CDMUtilities::splitter::no_empties);
124               versionDate = versionBits[0];
125             }
126           //get project buildPath
127
128           if (buildPath != "")
129             {
130               this->buildPath = buildPath;
131             }
132           else
133             {
134               this->buildPath = this->path + "Bin";
135             }
136         }
137     }
138   confFile.close();
139
140   this->type = wxDIR_DIRS;
141   this->level = 0;
142
143   this->children.clear();
144   this->appli = NULL;
145   this->lib = NULL;
146   this->packages.clear();
147
148
149   //check all folders
150   wxDir dir(crea::std2wx((pathFixed).c_str()));
151   if (dir.IsOpened())
152     {
153       wxString fileName;
154       bool cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_DIRS);
155       while (cont)
156         {
157           std::string stdfileName = crea::wx2std(fileName);
158
159           //if appli, create appli
160           if(stdfileName == "appli")
161             {
162               this->appli = new modelCDMAppli(this, pathFixed + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
163               this->children.push_back(this->appli);
164             }
165           //if lib, create lib
166           else if(stdfileName == "lib")
167             {
168               this->lib = new modelCDMLib(this, pathFixed + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
169               this->children.push_back(this->lib);
170             }
171           //if package , create package
172           else if(stdfileName.size() > 9 && stdfileName.substr(0,5) == "bbtk_" && stdfileName.substr(stdfileName.size()-4,4) == "_PKG")
173             {
174               modelCDMPackage* package = new modelCDMPackage(this, pathFixed + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
175               this->packages.push_back(package);
176               this->children.push_back(package);
177             }
178           //if is an unknown folder, create folder
179           else
180             {
181               this->children.push_back(new modelCDMFolder(this, pathFixed + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1));
182             }
183
184           cont = dir.GetNext(&fileName);
185         }
186
187       cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_FILES);
188       while (cont)
189         {
190           std::string stdfileName = crea::wx2std(fileName);
191
192           //if CMakeLists, create CMakeLists
193           if(stdfileName == "CMakeLists.txt")
194             {
195               this->CMakeLists = new modelCDMCMakeListsFile(this, pathFixed + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
196               this->children.push_back(this->CMakeLists);
197             }
198           else
199             {
200               this->children.push_back(new modelCDMFile(this, pathFixed + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1));
201             }
202           //if is an unknown file, create file
203           cont = dir.GetNext(&fileName);
204         }
205     }
206
207   this->SortChildren();
208   std::sort(this->packages.begin(), this->packages.end(), CompareNodeItem);
209
210 }
211
212 modelCDMProject::~modelCDMProject()
213 {
214 }
215
216 const std::string& modelCDMProject::GetNameProject() const
217 {
218   return this->nameProject;
219 }
220
221 const std::string& modelCDMProject::GetVersion() const
222 {
223   return this->version;
224 }
225
226 const std::string& modelCDMProject::GetVersionDate() const
227 {
228   return this->versionDate;
229 }
230
231 const std::string& modelCDMProject::GetBuildPath() const
232 {
233   return this->buildPath;
234 }
235
236 const std::vector<modelCDMPackage*>& modelCDMProject::GetPackages() const
237 {
238   return this->packages;
239 }
240
241 modelCDMAppli* modelCDMProject::GetAppli() const
242 {
243   return this->appli;
244 }
245
246 modelCDMLib* modelCDMProject::GetLib() const
247 {
248   return this->lib;
249 }
250
251 std::string modelCDMProject::GetBuildInstruction() const
252 {
253   std::string makeComm = "make -C \"" + this->buildPath + "\""; /*> \"" + this->buildPath + CDMUtilities::SLASH + "building.log\" 2>&1";*/
254   return makeComm;
255 }
256
257 bool modelCDMProject::SetVersion(const std::string& version, std::string*& result)
258 {
259
260   std::vector<std::string> vers;
261   CDMUtilities::splitter::split(vers, version, " .", CDMUtilities::splitter::no_empties);
262
263   time_t now = time(0);
264
265   tm ltm;
266 #ifdef _WIN32
267   localtime_s(&ltm, &now);
268 #else
269   ltm = *(localtime(&now));
270 #endif
271
272   std::stringstream date;
273   date << ltm.tm_mday << "/" << 1 + ltm.tm_mon << "/" << 1900 + ltm.tm_year;
274
275   //set name of library in CMakeLists inside copied folder
276   std::string line;
277   std::ifstream in((this->path + CDMUtilities::SLASH + "CMakeLists.txt").c_str());
278   if( !in.is_open())
279     {
280       result = new std::string("CMakeLists.txt file failed to open.");
281       return false;
282     }
283   std::ofstream out((this->path + CDMUtilities::SLASH + "CMakeLists.txt.tmp").c_str());
284   if( !out.is_open())
285     {
286       result = new std::string("CMakeLists.txt.tmp file failed to open.");
287       return false;
288     }
289   while (getline(in, line))
290     {
291       if(line.find("SET(PROJECT_MAJOR_VERSION") != std::string::npos)
292         line = "SET(PROJECT_MAJOR_VERSION " + vers[0] + ")";
293       else if(line.find("SET(PROJECT_MINOR_VERSION") != std::string::npos)
294         line = "SET(PROJECT_MINOR_VERSION " + vers[1] + ")";
295       else if(line.find("SET(PROJECT_BUILD_VERSION") != std::string::npos)
296         line = "SET(PROJECT_BUILD_VERSION " + vers[2] + ")";
297       else if(line.find("SET(PROJECT_VERSION_DATE") != std::string::npos)
298         line = "SET(PROJECT_VERSION_DATE \"" + date.str() + "\")";
299       out << line << std::endl;
300     }
301   in.close();
302   out.close();
303   //delete old file and rename new file
304 #ifdef _WIN32
305   std::string renameCommand = "move /Y \"" + this->path + CDMUtilities::SLASH + "CMakeLists.txt.tmp\" \"" + this->path + CDMUtilities::SLASH + "CMakeLists.txt\"";
306 #else
307   std::string renameCommand = "mv \"" + this->path + CDMUtilities::SLASH + "CMakeLists.txt.tmp\" \"" + this->path + CDMUtilities::SLASH + "CMakeLists.txt\"";
308 #endif
309   if(system(renameCommand.c_str()))
310     {
311       result = new std::string("An error occurred while running '" + renameCommand + "'.");
312       return false;
313     }
314
315   this->version = vers[0] + "." + vers[1] + "." + vers[2];
316   this->versionDate = date.str();
317   return true;
318 }
319
320 bool modelCDMProject::SetBuildPath(const std::string& path, std::string*& result)
321 {
322   if(path == "")
323     {
324       result = new std::string("The path cannot be empty");
325       return false;
326     }
327   if(path == this->path)
328     {
329       result = new std::string("The path cannot be same as the project sources");
330       return false;
331     }
332   this->buildPath = path;
333   return true;
334 }
335
336 modelCDMIProjectTreeNode* modelCDMProject::CreatePackage(
337     const std::string& name,
338     std::string*& result,
339     const std::string& authors,
340     const std::string& authorsEmail,
341     const std::string& description,
342     const std::string& version
343 )
344 {
345   //fixing input parameters
346   std::vector<std::string> words;
347
348   CDMUtilities::splitter::split(words,name," '/\"\\,.",CDMUtilities::splitter::no_empties);
349   std::string nameFixed = "";
350   for (int i = 0; i < (int)(words.size()); i++)
351     {
352       nameFixed += words[i];
353     }
354
355   words.clear();
356   CDMUtilities::splitter::split(words,authors," '/\"\\,.",CDMUtilities::splitter::no_empties);
357   std::string authorFixed;
358   for (int i = 0; i < (int)(words.size()); i++)
359     {
360       authorFixed += words[i];
361     }
362
363   words.clear();
364   std::string descriptionFixed;
365   CDMUtilities::splitter::split(words,authorsEmail," '/\"\\,",CDMUtilities::splitter::no_empties);
366   for (int i = 0; i < (int)(words.size()); i++)
367     {
368       descriptionFixed += words[i] + "/";
369     }
370   words.clear();
371   CDMUtilities::splitter::split(words,description," '\"",CDMUtilities::splitter::no_empties);
372   for (int i = 0; i < (int)(words.size()); i++)
373     {
374       descriptionFixed += "_" + words[i];
375     }
376
377   //call project to create package : use bbCreatePackage <path> <name> [author] [description]
378   std::string creationCommand = "bbCreatePackage \"" + this->path + "\" \"" + nameFixed + "\" \"" + authorFixed + "\" \"" + descriptionFixed + "\"";
379   //TODO: bbCreatePackage script always returning 0. It should return 1 or greater if any error
380   bool resultCommand = 0 != system(creationCommand.c_str());
381 #ifdef _WIN32
382   resultCommand = false;
383 #endif
384   if(resultCommand)
385     {
386       result = new std::string("An error occurred while running '" + creationCommand + "'.");
387       return NULL;
388     }
389
390   //add library to project CMakeLists
391   std::fstream out1((this->path + CDMUtilities::SLASH + "CMakeLists.txt").c_str(), std::fstream::in | std::fstream::out | std::fstream::app);
392   if (out1.is_open())
393     {
394       out1 << "ADD_SUBDIRECTORY(bbtk_" << nameFixed << "_PKG)" << std::endl;
395       out1.close();
396     }
397
398   //add library to model
399   modelCDMPackage* package = new modelCDMPackage(this, this->path + CDMUtilities::SLASH + "bbtk_" + nameFixed + "_PKG", "bbtk_" + nameFixed + "_PKG", this->level + 1);
400   this->packages.push_back(package);
401   this->children.push_back(package);
402
403   //TODO: set package version
404
405   this->SortChildren();
406
407   result = new std::string(this->path + CDMUtilities::SLASH + name);
408   return package;
409 }
410
411 modelCDMIProjectTreeNode* modelCDMProject::CreateLibrary(
412     const std::string& name,
413     std::string*& result,
414     const std::string& path
415 )
416 {
417   if(this->lib != NULL)
418     {
419       return this->lib->CreateLibrary(name, result);
420     }
421   result = new std::string("there is no lib folder in this project.");
422   return NULL;
423 }
424
425 modelCDMIProjectTreeNode* modelCDMProject::CreateApplication(
426     const std::string& name,
427     const int& type,
428     std::string*& result,
429     const std::string& path
430 )
431 {
432   if(this->appli != NULL)
433     {
434       return this->appli->CreateApplication(name, type, result);
435     }
436   result = new std::string("there is no appli folder in this project.");
437   return NULL;
438 }
439
440 modelCDMIProjectTreeNode* modelCDMProject::CreateBlackBox(
441     const std::string& name,
442     const std::string& package,
443     const std::string& authors,
444     const std::string& authorsEmail,
445     const std::string& categories,
446     const std::string& description
447 )
448 {
449   //TODO: implement method
450   return NULL;
451 }
452
453 bool modelCDMProject::OpenCMakeListsFile(std::string*& result)
454 {
455   if (!CDMUtilities::openTextEditor(this->CMakeLists->GetPath()))
456     return true;
457   else
458     {
459       result = new std::string("Couldn't open CMakeLists file.");
460       return false;
461     }
462 }
463
464 const bool modelCDMProject::Refresh(std::string*& result)
465 {
466   std::cout << "refreshing project" << std::endl;
467   //open makelists file
468   std::string pathMakeLists = this->path + CDMUtilities::SLASH + "CMakeLists.txt";
469
470   std::ifstream confFile;
471   confFile.open((pathMakeLists).c_str());
472
473   std::string word;
474   while(confFile.is_open() && !confFile.eof())
475     {
476       //std::cout << "leyendo " << word << std::endl;
477       //get project name
478       std::getline(confFile,word,'(');
479       std::vector<std::string> wordBits;
480       CDMUtilities::splitter::split(wordBits,word," (\n",CDMUtilities::splitter::no_empties);
481
482       if(wordBits[wordBits.size()-1] == "PROJECT")
483         {
484           std::getline(confFile,word,')');
485           std::vector<std::string> nameBits;
486           CDMUtilities::splitter::split(nameBits, word, " ", CDMUtilities::splitter::no_empties);
487
488           this->name = this->nameProject = "";
489           for (int i = 0; i < (int)(nameBits.size()); i++)
490             {
491               if(i != 0)
492                 this->name += " ";
493               this->name += nameBits[i];
494             }
495           this->nameProject = this->name;
496
497         }
498
499
500       if(wordBits[wordBits.size()-1] == "SET")
501         {
502           //get project version
503           std::getline(confFile,word,')');
504           CDMUtilities::splitter::split(wordBits, word, " ", CDMUtilities::splitter::no_empties);
505           if(wordBits[0] == "PROJECT_MAJOR_VERSION")
506             {
507               version = wordBits[1];
508             }
509           if(wordBits[0] == "PROJECT_MINOR_VERSION")
510             {
511               version += "." + wordBits[1];
512             }
513           if(wordBits[0] == "PROJECT_BUILD_VERSION")
514             {
515               version += "." + wordBits[1];
516             }
517
518           //get project versionDate
519           if(wordBits[0] == "PROJECT_VERSION_DATE")
520             {
521               std::vector<std::string> versionBits;
522               CDMUtilities::splitter::split(versionBits, wordBits[1], "\"", CDMUtilities::splitter::no_empties);
523               versionDate = versionBits[0];
524             }
525         }
526     }
527   confFile.close();
528
529   this->type = wxDIR_DIRS;
530   this->level = 0;
531
532   std::vector<bool> checked(this->children.size(), false);
533   std::vector<bool> checkedPackages(this->packages.size(), false);
534
535   //check all folders
536   wxDir dir(crea::std2wx((this->path).c_str()));
537   if (dir.IsOpened())
538     {
539       wxString fileName;
540       bool cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_DIRS);
541       while (cont)
542         {
543           std::string stdfileName = crea::wx2std(fileName);
544
545           //if appli, create appli
546           if(stdfileName == "appli")
547             {
548               if (this->appli == NULL)
549                 {
550                   this->appli = new modelCDMAppli(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
551                   this->children.push_back(this->appli);
552                 }
553               else
554                 {
555                   int pos = std::find(this->children.begin(), this->children.end(), this->appli) - this->children.begin();
556                   checked[pos] = true;
557                   if(!this->appli->Refresh(result))
558                     return false;
559                 }
560             }
561           //if lib, create lib
562           else if(stdfileName == "lib")
563             {
564               if (this->lib == NULL)
565                 {
566                   this->lib = new modelCDMLib(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
567                   this->children.push_back(this->lib);
568                 }
569               else
570                 {
571                   int pos = std::find(this->children.begin(), this->children.end(), this->lib) - this->children.begin();
572                   checked[pos] = true;
573                   if(!this->lib->Refresh(result))
574                     return false;
575                 }
576
577             }
578           //if package , create package
579           else if(stdfileName.size() > 9 && stdfileName.substr(0,5) == "bbtk_" && stdfileName.substr(stdfileName.size()-4,4) == "_PKG")
580             {
581               bool found = false;
582               for (int i = 0; !found && i < (int)(this->packages.size()); i++)
583                 {
584                   if (this->packages[i]->GetName() == stdfileName)
585                     {
586                       found = true;
587                       int pos = std::find(this->children.begin(), this->children.end(), this->packages[i]) - this->children.begin();
588                       checked[pos] = true;
589                       checkedPackages[i] = true;
590                       if(!this->packages[i]->Refresh(result))
591                         return false;
592                     }
593                 }
594               if(!found)
595                 {
596                   modelCDMPackage* package = new modelCDMPackage(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
597                   this->packages.push_back(package);
598                   this->children.push_back(package);
599                 }
600
601             }
602           //if is an unknown folder, create folder
603           else
604             {
605               bool found = false;
606               for (int i = 0; !found && i < (int)(this->children.size()); i++)
607                 {
608                   if (this->children[i]->GetName() == stdfileName)
609                     {
610                       found = true;
611                       checked[i] = true;
612                       if(!this->children[i]->Refresh(result))
613                         return false;
614                     }
615                 }
616
617               if(!found)
618                 {
619                   modelCDMFolder* folder = new modelCDMFolder(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
620                   this->children.push_back(folder);
621                 }
622             }
623
624           cont = dir.GetNext(&fileName);
625         }
626
627       cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_FILES);
628       while (cont)
629         {
630           std::string stdfileName = crea::wx2std(fileName);
631
632           //if CMakeLists, create CMakeLists
633           if(stdfileName == "CMakeLists.txt")
634             {
635               if (this->CMakeLists == NULL)
636                 {
637                   this->CMakeLists = new modelCDMCMakeListsFile(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
638                   this->children.push_back(this->CMakeLists);
639                 }
640               else
641                 {
642                   int pos = std::find(this->children.begin(), this->children.end(), this->CMakeLists) - this->children.begin();
643                   checked[pos] = true;
644                   if(!this->CMakeLists->Refresh(result))
645                     return false;
646                 }
647             }
648           //if is an unknown file, create file
649           else
650             {
651               bool found = false;
652               for (int i = 0; !found && i < (int)(this->children.size()); i++)
653                 {
654                   if (this->children[i]->GetName() == stdfileName)
655                     {
656                       found = true;
657                       checked[i] = true;
658                       if(!this->children[i]->Refresh(result))
659                         return false;
660                     }
661                 }
662
663               if(!found)
664                 {
665                   modelCDMFile* file = new modelCDMFile(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
666                   this->children.push_back(file);
667                 }
668             }
669
670           cont = dir.GetNext(&fileName);
671         }
672     }
673
674   for (int i = 0; i < (int)(checkedPackages.size()); i++)
675     {
676       if(!checkedPackages[i])
677         {
678           this->packages.erase(this->packages.begin()+i);
679           checkedPackages.erase(checkedPackages.begin()+i);
680           i--;
681         }
682     }
683   for (int i = 0; i < (int)(checked.size()); i++)
684     {
685       if(!checked[i])
686         {
687           delete this->children[i];
688           this->children.erase(this->children.begin()+i);
689           checked.erase(checked.begin()+i);
690           i--;
691         }
692     }
693
694   this->SortChildren();
695   std::sort(this->packages.begin(), this->packages.end(), CompareNodeItem);
696   return true;
697 }
698
699 bool modelCDMProject::ConfigureBuild(std::string*& result)
700 {
701   //TODO: adjust for windows and mac
702 #ifdef _WIN32
703   // ------ Windows
704   if(0 == system("cmake-gui"))
705     return true;
706   else
707     {
708           result = new std::string("There was an error opening cmake-gui. Please make sure it's installed and that cmake's bin folder is in the system path.");
709       return false;
710     }
711 #elif __APPLE__
712   // ------ Apple
713 #else
714   // ------ Linux
715   //open binary folder
716   wxDir dir(crea::std2wx((this->buildPath).c_str()));
717
718   //if folder doesn't exist then create it
719   if (!dir.IsOpened())
720     {
721       //create command line to create folder
722       std::string createComm = "mkdir \"" + this->buildPath + "\"";
723       //execute creation command
724       if (system(createComm.c_str()))
725         {
726           //if there was an error then report it
727           result = new std::string("There was an error creating the build path: \"" + this->buildPath + "\"");
728           return false;
729         }
730     }
731   //create command line to execute ccmake
732   //TODO:: adjust for different Linux distributions
733   std::string confComm = "gnome-terminal --tab --working-directory=\"" + this->buildPath + "\" -e \"ccmake '" + this->path + "'\"";
734   //execute command
735   if(CDMUtilities::openTerminal(confComm))
736     {
737       //if there was an error then report it
738       result = new std::string("There was an error opening the configuration tool in the desired place.");
739       return false;
740     }
741 #endif
742   return true;
743 }
744
745 bool modelCDMProject::Build(std::string*& result, const std::string& line)
746 {
747   //TODO: adjust for windows and mac
748 #ifdef _WIN32
749   // ------ Windows
750         std::string command = "start \"\" \"" + this->buildPath + CDMUtilities::SLASH + this->nameProject + ".sln\"";
751   //wxMessageBox(crea::std2wx(command), wxT("Project Compilation - Check!"));
752         if(0 == system(command.c_str()))
753     return true;
754   else
755     {
756       result = new std::string("An error has happened running: \"" + command + "\". Please make sure to have visual c++ installed.");
757           return false;
758     }
759 #elif __APPLE__
760   // ------ Apple
761 #else
762   // ------ Linux
763   //open binary folder
764   wxDir dir(crea::std2wx((this->buildPath).c_str()));
765
766   //if binary folder can't be opened then return false
767   if (!dir.IsOpened())
768     {
769       result = new std::string("The build path could not be opened. Make sure to configure the project before compiling it");
770       return false;
771     }
772   //create make command
773   std::string makeComm = "gnome-terminal -e \"bash -c \\\"";
774   for (int i = 0; i < line.size(); i++)
775     {
776       if(line[i] == '"')
777         {
778           makeComm+="\\\\\\\"";
779         }
780       else if(line[i] == '\\')
781         {
782           makeComm+="\\\\\\\\";
783         }
784       else
785         {
786           makeComm.push_back(line[i]);
787         }
788     }
789   makeComm += "; echo -e '\\a'; bash";
790   makeComm += "\\\"\"";
791
792   std::cout << "executing '" << makeComm << "'" << std::endl;
793   //execute make command
794   if(system(makeComm.c_str()))
795     {
796       //if there was an error then report it
797       result = new std::string("There was an error compiling the project, please check the \"building.log\" file located in the build folder to read more about the problem.");
798       return false;
799     }
800 #endif
801   return true;
802 }
803
804 bool modelCDMProject::Connect(std::string*& result, const std::string& folder)
805 {
806   //TODO: adjust for mac
807 #ifdef _WIN32
808   // ------ Windows
809   //open binary folder
810   wxDir dir(crea::std2wx(folder));
811
812   //if binary folder can't be opened then return false
813   if (!dir.IsOpened())
814     {
815       result = new std::string("The path could not be opened. Make sure the folder exists and contains a bbtkPackage file.");
816       return false;
817     }
818   //create plug command
819   std::string plugComm = "bbPlugPackage \"" + folder + "\"";
820   std::cout << "executing '" << plugComm << "'" << std::endl;
821   //execute plug command
822   if(system(std::string("start cmd.exe /k \"" + plugComm + "\"").c_str()))
823     {
824       //if there was an error then report it
825       result = new std::string("There was an error plugging the packages of the project, please check the console to read more about the problem.");
826       return false;
827     }
828 #elif __APPLE__
829   // ------ Apple
830 #else
831   // ------ Linux
832   //open binary folder
833   wxDir dir(crea::std2wx(folder));
834
835   //if binary folder can't be opened then return false
836   if (!dir.IsOpened())
837     {
838       result = new std::string("The path could not be opened. Make sure the folder exists and contains a bbtkPackage file.");
839       return false;
840     }
841   //create plug command
842   std::string plugComm = "bbPlugPackage \"" + this->buildPath + "\"";
843
844   std::string Comm = "gnome-terminal -e \"bash -c \\\"";
845     for (int i = 0; i < plugComm.size(); i++)
846       {
847         if(plugComm[i] == '"')
848           {
849             Comm+="\\\\\\\"";
850           }
851         else if(plugComm[i] == '\\')
852           {
853             Comm+="\\\\\\\\";
854           }
855         else
856           {
857             Comm.push_back(plugComm[i]);
858           }
859       }
860     Comm += "; echo -e '\\a'; bash";
861     Comm += "\\\"\"";
862
863
864   std::cout << "executing '" << Comm << "'" << std::endl;
865   //execute plug command
866   if(system(Comm.c_str()))
867     {
868       //if there was an error then report it
869       result = new std::string("There was an error plugging the packages of the project, please check the console to read more about the problem.");
870       return false;
871     }
872 #endif
873   return true;
874 }
875
876 void modelCDMProject::CheckStructure(std::map<std::string, bool>& properties)
877 {
878   //check cmake exist
879   if(this->CMakeLists != NULL)
880     {
881       //set properties parameters based on model
882       properties["project add appli"] = this->appli != NULL;
883       properties["project add lib"] = this->lib != NULL;
884       for (int i = 0; i < (int)(this->packages.size()); i++)
885         properties["project add " + packages[i]->GetName()] = false;
886
887       //open cmakelists
888       std::ifstream confFile;
889       confFile.open((this->CMakeLists->GetPath()).c_str());
890
891       //take everything that is not commented
892       std::string fileContent;
893
894       std::string word;
895       std::vector<std::string> words;
896       while(confFile.is_open() && !confFile.eof())
897         {
898           std::getline(confFile,word, '\n');
899           if(word[0] != '#')
900             {
901               CDMUtilities::splitter::split(words, word, "#", CDMUtilities::splitter::empties_ok);
902               if (words.size() > 0)
903                 {
904                   word = words[0];
905                   CDMUtilities::splitter::split(words, word, " ", CDMUtilities::splitter::empties_ok);
906                   for (int i = 0; i < (int)(words.size()); i++)
907                     {
908                       if(words[i].substr(0,2) == "//")
909                         break;
910                       fileContent += words[i] + " ";
911                     }
912                 }
913
914             }
915         }
916
917       //check every instruction
918       std::stringstream ss(fileContent);
919       while(!ss.eof())
920         {
921           std::getline(ss,word, '(');
922
923           //check instruction name
924           CDMUtilities::splitter::split(words, word, " ", CDMUtilities::splitter::no_empties);
925
926           //set instructions
927           if (words.size() > 0 && words[words.size()-1] == "SET")
928             {
929               std::getline(ss,word, ')');
930
931               CDMUtilities::splitter::split(words, word, " ", CDMUtilities::splitter::no_empties);
932               if (words.size() > 1)
933                 {
934                   if (words[0] == "USE_CREA" && words[1] == "ON")
935                     {
936                       properties["project set USE_CREA"] = true;
937                     }
938                   else if (words[0] == "USE_GDCM" && words[1] == "ON")
939                     {
940                       properties["project set USE_GDCM"] = true;
941                     }
942                   else if (words[0] == "USE_GDCM_VTK" && words[1] == "ON")
943                     {
944                       properties["project set USE_GDCM_VTK"] = true;
945                     }
946                   else if (words[0] == "USE_GDCM2" && words[1] == "ON")
947                     {
948                       properties["project set USE_GDCM2"] = true;
949                     }
950                   else if (words[0] == "USE_WXWIDGETS" && words[1] == "ON")
951                     {
952                       properties["project set USE_WXWIDGETS"] = true;
953                     }
954                   else if (words[0] == "USE_KWWIDGETS" && words[1] == "ON")
955                     {
956                       properties["project set USE_KWWIDGETS"] = true;
957                     }
958                   else if (words[0] == "USE_VTK" && words[1] == "ON")
959                     {
960                       properties["project set USE_VTK"] = true;
961                     }
962                   else if (words[0] == "USE_ITK" && words[1] == "ON")
963                     {
964                       properties["project set USE_ITK"] = true;
965                     }
966                   else if (words[0] == "USE_BOOST" && words[1] == "ON")
967                     {
968                       properties["project set USE_BOOST"] = true;
969                     }
970                 }
971             }
972           //add instructions
973           else if (words.size() > 0 && words[words.size()-1] == "ADD_SUBDIRECTORY")
974             {
975               std::getline(ss,word, ')');
976               //std::cout << word << std::endl;
977               CDMUtilities::splitter::split(words, word, " ", CDMUtilities::splitter::no_empties);
978
979               if (words.size() > 0)
980                 {
981                   if(words[0] == "appli")
982                     {
983                       properties["project add "+words[0]] = this->appli != NULL;
984                     }
985                   else if(words[0] == "lib")
986                     {
987                       properties["project add "+words[0]] = this->lib != NULL;
988                     }
989                   else
990                     {
991                       properties["project add "+words[0]] = true;
992                     }
993                 }
994             }
995         }
996
997     }
998
999   //check appli's structure
1000   this->appli->CheckStructure(properties);
1001   //check lib's structure
1002   this->lib->CheckStructure(properties);
1003   //check packages' structure
1004   for (int i = 0; i < (int)(this->packages.size()); i++)
1005     {
1006       properties["package " + this->packages[i]->GetName()] = true;
1007       this->packages[i]->CheckStructure(properties);
1008     }
1009 }
1010
1011 bool modelCDMProject::IsPackageIncluded(const std::string& package_name)
1012 {
1013   if (this->HasCMakeLists())
1014     {
1015       CDMUtilities::CMLFile cmlFile = CDMUtilities::readCMLFile(this->CMakeLists->GetPath().c_str());
1016       for (int i = 0; i < cmlFile.size(); ++i)
1017         {
1018           if (cmlFile[i].first=="command" && cmlFile[i].second[0] == "ADD_SUBDIRECTORY")
1019             {
1020               int pos = 1;
1021               while (pos < cmlFile[i].second.size())
1022                 {
1023                   if (!isspace(cmlFile[i].second[pos][0]) && cmlFile[i].second[pos][0] != '(' && cmlFile[i].second[pos][0] != '#')
1024                     {
1025                       if (package_name == cmlFile[i].second[pos])
1026                         return true;
1027                       break;
1028                     }
1029                   pos++;
1030                 }
1031             }
1032         }
1033     }
1034   return false;
1035
1036 }
1037
1038 bool modelCDMProject::SetPackageInclude(const std::string& package_name, const bool& toInclude)
1039 {
1040   if (this->HasCMakeLists())
1041     {
1042       CDMUtilities::CMLFile cmlFile = CDMUtilities::readCMLFile(this->CMakeLists->GetPath().c_str());
1043
1044       bool found = false;
1045
1046       for (int i = 0; i < cmlFile.size(); ++i)
1047         {
1048           if(toInclude && cmlFile[i].first == "comment")
1049             {
1050               std::vector<std::string> segments;
1051               std::string line = cmlFile[i].second[0];
1052               while(line[0] == '#')
1053                 line.erase(0,1);
1054
1055               CDMUtilities::splitter::split(segments, line, " ()", CDMUtilities::splitter::no_empties);
1056               if (segments.size() > 1 && segments[0] == "ADD_SUBDIRECTORY" && segments[1] == package_name)
1057                 {
1058                   found = true;
1059                   while(cmlFile[i].second[0][0] == '#')
1060                     cmlFile[i].second[0].erase(0,1);
1061                 }
1062             }
1063           else if(cmlFile[i].first == "command" && cmlFile[i].second[0] == "ADD_SUBDIRECTORY")
1064             {
1065               int pos = 1;
1066               while (pos < cmlFile[i].second.size())
1067                 {
1068                   if (!isspace(cmlFile[i].second[pos][0]) && cmlFile[i].second[pos][0] != '(' && cmlFile[i].second[pos][0] != '#')
1069                     {
1070                       if (package_name == cmlFile[i].second[pos])
1071                         {
1072                           found = true;
1073                           if (!toInclude)
1074                             {
1075                               cmlFile[i].first = "comment";
1076                               cmlFile[i].second[0] = "#" + cmlFile[i].second[0];
1077                               while (cmlFile[i].second.size() > 1)
1078                                 {
1079                                   cmlFile[i].second[0] += cmlFile[i].second[1];
1080                                   cmlFile[i].second.erase(cmlFile[i].second.begin()+1);
1081                                 }
1082
1083                             }
1084                         }
1085                       break;
1086                     }
1087                   pos++;
1088                 }
1089             }
1090         }
1091       if (!found && toInclude)
1092         {
1093           CDMUtilities::syntaxElement element;
1094           element.first = "command";
1095           element.second.push_back("ADD_SUBDIRECTORY(" + package_name + ")");
1096           cmlFile.push_back(element);
1097         }
1098
1099       return CDMUtilities::writeCMLFile(this->CMakeLists->GetPath().c_str(),cmlFile);
1100     }
1101   return false;
1102 }