]> Creatis software - crea.git/blob - lib/creaDevManagerLib/modelCDMProject.cpp
Feature #1711
[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   localtime_s(&ltm, &now);
267
268   std::stringstream date;
269   date << ltm.tm_mday << "/" << 1 + ltm.tm_mon << "/" << 1900 + ltm.tm_year;
270
271   //set name of library in CMakeLists inside copied folder
272   std::string line;
273   std::ifstream in((this->path + CDMUtilities::SLASH + "CMakeLists.txt").c_str());
274   if( !in.is_open())
275     {
276       result = new std::string("CMakeLists.txt file failed to open.");
277       return false;
278     }
279   std::ofstream out((this->path + CDMUtilities::SLASH + "CMakeLists.txt.tmp").c_str());
280   if( !out.is_open())
281     {
282       result = new std::string("CMakeLists.txt.tmp file failed to open.");
283       return false;
284     }
285   while (getline(in, line))
286     {
287       if(line.find("SET(PROJECT_MAJOR_VERSION") != std::string::npos)
288         line = "SET(PROJECT_MAJOR_VERSION " + vers[0] + ")";
289       else if(line.find("SET(PROJECT_MINOR_VERSION") != std::string::npos)
290         line = "SET(PROJECT_MINOR_VERSION " + vers[1] + ")";
291       else if(line.find("SET(PROJECT_BUILD_VERSION") != std::string::npos)
292         line = "SET(PROJECT_BUILD_VERSION " + vers[2] + ")";
293       else if(line.find("SET(PROJECT_VERSION_DATE") != std::string::npos)
294         line = "SET(PROJECT_VERSION_DATE \"" + date.str() + "\")";
295       out << line << std::endl;
296     }
297   in.close();
298   out.close();
299   //delete old file and rename new file
300   std::string renameCommand = "mv \"" + this->path + CDMUtilities::SLASH + "CMakeLists.txt.tmp\" \"" + this->path + CDMUtilities::SLASH + "CMakeLists.txt\"";
301   if(system(renameCommand.c_str()))
302     {
303       result = new std::string("An error occurred while running '" + renameCommand + "'.");
304       return false;
305     }
306
307   this->version = vers[0] + "." + vers[1] + "." + vers[2];
308   this->versionDate = date.str();
309   return true;
310 }
311
312 bool modelCDMProject::SetBuildPath(const std::string& path, std::string*& result)
313 {
314   if(path == "")
315     {
316       result = new std::string("The path cannot be empty");
317       return false;
318     }
319   if(path == this->path)
320     {
321       result = new std::string("The path cannot be same as the project sources");
322       return false;
323     }
324   this->buildPath = path;
325   return true;
326 }
327
328 modelCDMIProjectTreeNode* modelCDMProject::CreatePackage(
329     const std::string& name,
330     std::string*& result,
331     const std::string& authors,
332     const std::string& authorsEmail,
333     const std::string& description,
334     const std::string& version
335 )
336 {
337   //fixing input parameters
338   std::vector<std::string> words;
339
340   CDMUtilities::splitter::split(words,name," ",CDMUtilities::splitter::no_empties);
341   std::string nameFixed = "";
342   for (int i = 0; i < (int)(words.size()); i++)
343     {
344       nameFixed += words[i];
345     }
346
347   words.clear();
348   CDMUtilities::splitter::split(words,authors," ",CDMUtilities::splitter::no_empties);
349   std::string authorFixed;
350   for (int i = 0; i < (int)(words.size()); i++)
351     {
352       authorFixed += words[i];
353     }
354
355   words.clear();
356   std::string descriptionFixed;
357   CDMUtilities::splitter::split(words,authorsEmail," ",CDMUtilities::splitter::no_empties);
358   for (int i = 0; i < (int)(words.size()); i++)
359     {
360       descriptionFixed += words[i];
361     }
362   words.clear();
363   CDMUtilities::splitter::split(words,description," ",CDMUtilities::splitter::no_empties);
364   for (int i = 0; i < (int)(words.size()); i++)
365     {
366       descriptionFixed += "_" + words[i];
367     }
368
369   //call project to create package : use bbCreatePackage <path> <name> [author] [description]
370   std::string creationCommand = "bbCreatePackage \"" + this->path + "\" \"" + nameFixed + "\" \"" + authorFixed + "\" \"" + descriptionFixed + "\"";
371   //TODO: bbCreatePackage script always returning 0. It should return 1 or greater if any error
372   if(system(creationCommand.c_str()))
373     {
374       result = new std::string("An error occurred while running '" + creationCommand + "'.");
375       return NULL;
376     }
377
378   //add library to model
379   modelCDMPackage* package = new modelCDMPackage(this, this->path + CDMUtilities::SLASH + "bbtk_" + nameFixed + "_PKG", "bbtk_" + nameFixed + "_PKG", this->level + 1);
380   this->packages.push_back(package);
381   this->children.push_back(package);
382
383   //TODO: set package version
384
385   this->SortChildren();
386
387   result = new std::string(this->path + CDMUtilities::SLASH + name);
388   return package;
389 }
390
391 modelCDMIProjectTreeNode* modelCDMProject::CreateLibrary(
392     const std::string& name,
393     std::string*& result,
394     const std::string& path
395 )
396 {
397   if(this->lib != NULL)
398     {
399       return this->lib->CreateLibrary(name, result);
400     }
401   result = new std::string("there is no lib folder in this project.");
402   return NULL;
403 }
404
405 modelCDMIProjectTreeNode* modelCDMProject::CreateApplication(
406     const std::string& name,
407     std::string*& result,
408     const std::string& path
409 )
410 {
411   if(this->appli != NULL)
412     {
413       return this->appli->CreateApplication(name, result);
414     }
415   result = new std::string("there is no appli folder in this project.");
416   return NULL;
417 }
418
419 modelCDMIProjectTreeNode* modelCDMProject::CreateBlackBox(
420     const std::string& name,
421     const std::string& package,
422     const std::string& authors,
423     const std::string& authorsEmail,
424     const std::string& categories,
425     const std::string& description
426 )
427 {
428   //TODO: implement method
429   return NULL;
430 }
431
432 bool modelCDMProject::OpenCMakeListsFile(std::string*& result)
433 {
434   if (!CDMUtilities::openTextEditor(this->CMakeLists->GetPath()))
435     return true;
436   else
437     {
438       result = new std::string("Couldn't open CMakeLists file.");
439       return false;
440     }
441 }
442
443 const bool modelCDMProject::Refresh(std::string*& result)
444 {
445   std::cout << "refreshing project" << std::endl;
446   //open makelists file
447   std::string pathMakeLists = this->path + CDMUtilities::SLASH + "CMakeLists.txt";
448
449   std::ifstream confFile;
450   confFile.open((pathMakeLists).c_str());
451
452   std::string word;
453   while(confFile.is_open() && !confFile.eof())
454     {
455       //std::cout << "leyendo " << word << std::endl;
456       //get project name
457       std::getline(confFile,word,'(');
458       std::vector<std::string> wordBits;
459       CDMUtilities::splitter::split(wordBits,word," (\n",CDMUtilities::splitter::no_empties);
460
461       if(wordBits[wordBits.size()-1] == "PROJECT")
462         {
463           std::getline(confFile,word,')');
464           std::vector<std::string> nameBits;
465           CDMUtilities::splitter::split(nameBits, word, " ", CDMUtilities::splitter::no_empties);
466
467           this->name = this->nameProject = "";
468           for (int i = 0; i < (int)(nameBits.size()); i++)
469             {
470               if(i != 0)
471                 this->name += " ";
472               this->name += nameBits[i];
473             }
474           this->nameProject = this->name;
475
476         }
477
478
479       if(wordBits[wordBits.size()-1] == "SET")
480         {
481           //get project version
482           std::getline(confFile,word,')');
483           CDMUtilities::splitter::split(wordBits, word, " ", CDMUtilities::splitter::no_empties);
484           if(wordBits[0] == "PROJECT_MAJOR_VERSION")
485             {
486               version = wordBits[1];
487             }
488           if(wordBits[0] == "PROJECT_MINOR_VERSION")
489             {
490               version += "." + wordBits[1];
491             }
492           if(wordBits[0] == "PROJECT_BUILD_VERSION")
493             {
494               version += "." + wordBits[1];
495             }
496
497           //get project versionDate
498           if(wordBits[0] == "PROJECT_VERSION_DATE")
499             {
500               std::vector<std::string> versionBits;
501               CDMUtilities::splitter::split(versionBits, wordBits[1], "\"", CDMUtilities::splitter::no_empties);
502               versionDate = versionBits[0];
503             }
504         }
505     }
506   confFile.close();
507
508   this->type = wxDIR_DIRS;
509   this->level = 0;
510
511   std::vector<bool> checked(this->children.size(), false);
512   std::vector<bool> checkedPackages(this->packages.size(), false);
513
514   //check all folders
515   wxDir dir(crea::std2wx((this->path).c_str()));
516   if (dir.IsOpened())
517     {
518       wxString fileName;
519       bool cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_DIRS);
520       while (cont)
521         {
522           std::string stdfileName = crea::wx2std(fileName);
523
524           //if appli, create appli
525           if(stdfileName == "appli")
526             {
527               if (this->appli == NULL)
528                 {
529                   this->appli = new modelCDMAppli(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
530                   this->children.push_back(this->appli);
531                 }
532               else
533                 {
534                   int pos = std::find(this->children.begin(), this->children.end(), this->appli) - this->children.begin();
535                   checked[pos] = true;
536                   if(!this->appli->Refresh(result))
537                     return false;
538                 }
539             }
540           //if lib, create lib
541           else if(stdfileName == "lib")
542             {
543               if (this->lib == NULL)
544                 {
545                   this->lib = new modelCDMLib(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
546                   this->children.push_back(this->lib);
547                 }
548               else
549                 {
550                   int pos = std::find(this->children.begin(), this->children.end(), this->lib) - this->children.begin();
551                   checked[pos] = true;
552                   if(!this->lib->Refresh(result))
553                     return false;
554                 }
555
556             }
557           //if package , create package
558           else if(stdfileName.size() > 9 && stdfileName.substr(0,5) == "bbtk_" && stdfileName.substr(stdfileName.size()-4,4) == "_PKG")
559             {
560               bool found = false;
561               for (int i = 0; !found && i < (int)(this->packages.size()); i++)
562                 {
563                   if (this->packages[i]->GetName() == stdfileName)
564                     {
565                       found = true;
566                       int pos = std::find(this->children.begin(), this->children.end(), this->packages[i]) - this->children.begin();
567                       checked[pos] = true;
568                       checkedPackages[i] = true;
569                       if(!this->packages[i]->Refresh(result))
570                         return false;
571                     }
572                 }
573               if(!found)
574                 {
575                   modelCDMPackage* package = new modelCDMPackage(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
576                   this->packages.push_back(package);
577                   this->children.push_back(package);
578                 }
579
580             }
581           //if is an unknown folder, create folder
582           else
583             {
584               bool found = false;
585               for (int i = 0; !found && i < (int)(this->children.size()); i++)
586                 {
587                   if (this->children[i]->GetName() == stdfileName)
588                     {
589                       found = true;
590                       checked[i] = true;
591                       if(!this->children[i]->Refresh(result))
592                         return false;
593                     }
594                 }
595
596               if(!found)
597                 {
598                   modelCDMFolder* folder = new modelCDMFolder(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
599                   this->children.push_back(folder);
600                 }
601             }
602
603           cont = dir.GetNext(&fileName);
604         }
605
606       cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_FILES);
607       while (cont)
608         {
609           std::string stdfileName = crea::wx2std(fileName);
610
611           //if CMakeLists, create CMakeLists
612           if(stdfileName == "CMakeLists.txt")
613             {
614               if (this->CMakeLists == NULL)
615                 {
616                   this->CMakeLists = new modelCDMCMakeListsFile(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
617                   this->children.push_back(this->CMakeLists);
618                 }
619               else
620                 {
621                   int pos = std::find(this->children.begin(), this->children.end(), this->CMakeLists) - this->children.begin();
622                   checked[pos] = true;
623                   if(!this->CMakeLists->Refresh(result))
624                     return false;
625                 }
626             }
627           //if is an unknown file, create file
628           else
629             {
630               bool found = false;
631               for (int i = 0; !found && i < (int)(this->children.size()); i++)
632                 {
633                   if (this->children[i]->GetName() == stdfileName)
634                     {
635                       found = true;
636                       checked[i] = true;
637                       if(!this->children[i]->Refresh(result))
638                         return false;
639                     }
640                 }
641
642               if(!found)
643                 {
644                   modelCDMFile* file = new modelCDMFile(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
645                   this->children.push_back(file);
646                 }
647             }
648
649           cont = dir.GetNext(&fileName);
650         }
651     }
652
653   for (int i = 0; i < (int)(checkedPackages.size()); i++)
654     {
655       if(!checkedPackages[i])
656         {
657           this->packages.erase(this->packages.begin()+i);
658           checkedPackages.erase(checkedPackages.begin()+i);
659           i--;
660         }
661     }
662   for (int i = 0; i < (int)(checked.size()); i++)
663     {
664       if(!checked[i])
665         {
666           delete this->children[i];
667           this->children.erase(this->children.begin()+i);
668           checked.erase(checked.begin()+i);
669           i--;
670         }
671     }
672
673   this->SortChildren();
674   std::sort(this->packages.begin(), this->packages.end(), CompareNodeItem);
675   return true;
676 }
677
678 bool modelCDMProject::ConfigureBuild(std::string*& result)
679 {
680   //TODO: adjust for windows and mac
681 #ifdef _WIN32
682   // ------ Windows
683 #elif __APPLE__
684   // ------ Apple
685 #else
686   // ------ Linux
687   //open binary folder
688   wxDir dir(crea::std2wx((this->buildPath).c_str()));
689
690   //if folder doesn't exist then create it
691   if (!dir.IsOpened())
692     {
693       //create command line to create folder
694       std::string createComm = "mkdir \"" + this->buildPath + "\"";
695       //execute creation command
696       if (system(createComm.c_str()))
697         {
698           //if there was an error then report it
699           result = new std::string("There was an error creating the build path: \"" + this->buildPath + "\"");
700           return false;
701         }
702     }
703   //create command line to execute ccmake
704   //TODO:: adjust for different Linux distributions
705   std::string confComm = "gnome-terminal --tab --working-directory=\"" + this->buildPath + "\" -e \"ccmake '" + this->path + "'\"";
706   //execute command
707   if(CDMUtilities::openTerminal(confComm))
708     {
709       //if there was an error then report it
710       result = new std::string("There was an error opening the configuration tool in the desired place.");
711       return false;
712     }
713 #endif
714   return true;
715 }
716
717 bool modelCDMProject::Build(std::string*& result, const std::string& line)
718 {
719   //TODO: adjust for windows and mac
720 #ifdef _WIN32
721   // ------ Windows
722 #elif __APPLE__
723   // ------ Apple
724 #else
725   // ------ Linux
726   //open binary folder
727   wxDir dir(crea::std2wx((this->buildPath).c_str()));
728
729   //if binary folder can't be opened then return false
730   if (!dir.IsOpened())
731     {
732       result = new std::string("The build path could not be opened. Make sure to configure the project before compiling it");
733       return false;
734     }
735   //create make command
736   std::string makeComm = "gnome-terminal -e \"bash -c \\\"";
737   for (int i = 0; i < line.size(); i++)
738     {
739       if(line[i] == '"')
740         {
741           makeComm+="\\\\\\\"";
742         }
743       else if(line[i] == '\\')
744         {
745           makeComm+="\\\\\\\\";
746         }
747       else
748         {
749           makeComm.push_back(line[i]);
750         }
751     }
752   makeComm += "; echo -e '\\a'; bash";
753   makeComm += "\\\"\"";
754
755   std::cout << "executing '" << makeComm << "'" << std::endl;
756   //execute make command
757   if(system(makeComm.c_str()))
758     {
759       //if there was an error then report it
760       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.");
761       return false;
762     }
763 #endif
764   return true;
765 }
766
767 bool modelCDMProject::Connect(std::string*& result)
768 {
769   //TODO: adjust for windows and mac
770 #ifdef _WIN32
771   // ------ Windows
772 #elif __APPLE__
773   // ------ Apple
774 #else
775   // ------ Linux
776   //open binary folder
777   wxDir dir(crea::std2wx((this->buildPath).c_str()));
778
779   //if binary folder can't be opened then return false
780   if (!dir.IsOpened())
781     {
782       result = new std::string("The build path could not be opened. Make sure to configure the project before compiling it");
783       return false;
784     }
785   //create plug command
786   std::string plugComm = "bbPlugPackage \"" + this->buildPath + "\" > \"" + this->buildPath + CDMUtilities::SLASH + "plugging.log\" 2>&1";
787   std::cout << "executing '" << plugComm << "'" << std::endl;
788   //execute plug command
789   if(system(plugComm.c_str()))
790     {
791       //if there was an error then report it
792       result = new std::string("There was an error plugging the packages of the project, please check the console to read more about the problem.");
793       return false;
794     }
795 #endif
796   return true;
797 }
798
799 void modelCDMProject::CheckStructure(std::map<std::string, bool>& properties)
800 {
801   //check cmake exist
802   if(this->CMakeLists != NULL)
803     {
804       //set properties parameters based on model
805       properties["project add appli"] = this->appli != NULL;
806       properties["project add lib"] = this->lib != NULL;
807       for (int i = 0; i < (int)(this->packages.size()); i++)
808         properties["project add " + packages[i]->GetName()] = false;
809
810       //open cmakelists
811       std::ifstream confFile;
812       confFile.open((this->CMakeLists->GetPath()).c_str());
813
814       //take everything that is not commented
815       std::string fileContent;
816
817       std::string word;
818       std::vector<std::string> words;
819       while(confFile.is_open() && !confFile.eof())
820         {
821           std::getline(confFile,word, '\n');
822           if(word[0] != '#')
823             {
824               CDMUtilities::splitter::split(words, word, "#", CDMUtilities::splitter::empties_ok);
825               if (words.size() > 0)
826                 {
827                   word = words[0];
828                   CDMUtilities::splitter::split(words, word, " ", CDMUtilities::splitter::empties_ok);
829                   for (int i = 0; i < (int)(words.size()); i++)
830                     {
831                       if(words[i].substr(0,2) == "//")
832                         break;
833                       fileContent += words[i] + " ";
834                     }
835                 }
836
837             }
838         }
839
840       //check every instruction
841       std::stringstream ss(fileContent);
842       while(!ss.eof())
843         {
844           std::getline(ss,word, '(');
845
846           //check instruction name
847           CDMUtilities::splitter::split(words, word, " ", CDMUtilities::splitter::no_empties);
848
849           //set instructions
850           if (words.size() > 0 && words[words.size()-1] == "SET")
851             {
852               std::getline(ss,word, ')');
853
854               CDMUtilities::splitter::split(words, word, " ", CDMUtilities::splitter::no_empties);
855               if (words.size() > 1)
856                 {
857                   if (words[0] == "USE_CREA" && words[1] == "ON")
858                     {
859                       properties["project set USE_CREA"] = true;
860                     }
861                   else if (words[0] == "USE_GDCM" && words[1] == "ON")
862                     {
863                       properties["project set USE_GDCM"] = true;
864                     }
865                   else if (words[0] == "USE_GDCM_VTK" && words[1] == "ON")
866                     {
867                       properties["project set USE_GDCM_VTK"] = true;
868                     }
869                   else if (words[0] == "USE_GDCM2" && words[1] == "ON")
870                     {
871                       properties["project set USE_GDCM2"] = true;
872                     }
873                   else if (words[0] == "USE_WXWIDGETS" && words[1] == "ON")
874                     {
875                       properties["project set USE_WXWIDGETS"] = true;
876                     }
877                   else if (words[0] == "USE_KWWIDGETS" && words[1] == "ON")
878                     {
879                       properties["project set USE_KWWIDGETS"] = true;
880                     }
881                   else if (words[0] == "USE_VTK" && words[1] == "ON")
882                     {
883                       properties["project set USE_VTK"] = true;
884                     }
885                   else if (words[0] == "USE_ITK" && words[1] == "ON")
886                     {
887                       properties["project set USE_ITK"] = true;
888                     }
889                   else if (words[0] == "USE_BOOST" && words[1] == "ON")
890                     {
891                       properties["project set USE_BOOST"] = true;
892                     }
893                 }
894             }
895           //add instructions
896           else if (words.size() > 0 && words[words.size()-1] == "ADD_SUBDIRECTORY")
897             {
898               std::getline(ss,word, ')');
899               //std::cout << word << std::endl;
900               CDMUtilities::splitter::split(words, word, " ", CDMUtilities::splitter::no_empties);
901
902               if (words.size() > 0)
903                 {
904                   if(words[0] == "appli")
905                     {
906                       properties["project add "+words[0]] = this->appli != NULL;
907                     }
908                   else if(words[0] == "lib")
909                     {
910                       properties["project add "+words[0]] = this->lib != NULL;
911                     }
912                   else
913                     {
914                       properties["project add "+words[0]] = true;
915                     }
916                 }
917             }
918         }
919
920     }
921
922   //check appli's structure
923   this->appli->CheckStructure(properties);
924   //check lib's structure
925   this->lib->CheckStructure(properties);
926   //check packages' structure
927   for (int i = 0; i < (int)(this->packages.size()); i++)
928     {
929       properties["package " + this->packages[i]->GetName()] = true;
930       this->packages[i]->CheckStructure(properties);
931     }
932 }