]> 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 #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 model
391   modelCDMPackage* package = new modelCDMPackage(this, this->path + CDMUtilities::SLASH + "bbtk_" + nameFixed + "_PKG", "bbtk_" + nameFixed + "_PKG", this->level + 1);
392   this->packages.push_back(package);
393   this->children.push_back(package);
394
395   //TODO: set package version
396
397   this->SortChildren();
398
399   result = new std::string(this->path + CDMUtilities::SLASH + name);
400   return package;
401 }
402
403 modelCDMIProjectTreeNode* modelCDMProject::CreateLibrary(
404     const std::string& name,
405     std::string*& result,
406     const std::string& path
407 )
408 {
409   if(this->lib != NULL)
410     {
411       return this->lib->CreateLibrary(name, result);
412     }
413   result = new std::string("there is no lib folder in this project.");
414   return NULL;
415 }
416
417 modelCDMIProjectTreeNode* modelCDMProject::CreateApplication(
418     const std::string& name,
419     const int& type,
420     std::string*& result,
421     const std::string& path
422 )
423 {
424   if(this->appli != NULL)
425     {
426       return this->appli->CreateApplication(name, type, result);
427     }
428   result = new std::string("there is no appli folder in this project.");
429   return NULL;
430 }
431
432 modelCDMIProjectTreeNode* modelCDMProject::CreateBlackBox(
433     const std::string& name,
434     const std::string& package,
435     const std::string& authors,
436     const std::string& authorsEmail,
437     const std::string& categories,
438     const std::string& description
439 )
440 {
441   //TODO: implement method
442   return NULL;
443 }
444
445 bool modelCDMProject::OpenCMakeListsFile(std::string*& result)
446 {
447   if (!CDMUtilities::openTextEditor(this->CMakeLists->GetPath()))
448     return true;
449   else
450     {
451       result = new std::string("Couldn't open CMakeLists file.");
452       return false;
453     }
454 }
455
456 const bool modelCDMProject::Refresh(std::string*& result)
457 {
458   std::cout << "refreshing project" << std::endl;
459   //open makelists file
460   std::string pathMakeLists = this->path + CDMUtilities::SLASH + "CMakeLists.txt";
461
462   std::ifstream confFile;
463   confFile.open((pathMakeLists).c_str());
464
465   std::string word;
466   while(confFile.is_open() && !confFile.eof())
467     {
468       //std::cout << "leyendo " << word << std::endl;
469       //get project name
470       std::getline(confFile,word,'(');
471       std::vector<std::string> wordBits;
472       CDMUtilities::splitter::split(wordBits,word," (\n",CDMUtilities::splitter::no_empties);
473
474       if(wordBits[wordBits.size()-1] == "PROJECT")
475         {
476           std::getline(confFile,word,')');
477           std::vector<std::string> nameBits;
478           CDMUtilities::splitter::split(nameBits, word, " ", CDMUtilities::splitter::no_empties);
479
480           this->name = this->nameProject = "";
481           for (int i = 0; i < (int)(nameBits.size()); i++)
482             {
483               if(i != 0)
484                 this->name += " ";
485               this->name += nameBits[i];
486             }
487           this->nameProject = this->name;
488
489         }
490
491
492       if(wordBits[wordBits.size()-1] == "SET")
493         {
494           //get project version
495           std::getline(confFile,word,')');
496           CDMUtilities::splitter::split(wordBits, word, " ", CDMUtilities::splitter::no_empties);
497           if(wordBits[0] == "PROJECT_MAJOR_VERSION")
498             {
499               version = wordBits[1];
500             }
501           if(wordBits[0] == "PROJECT_MINOR_VERSION")
502             {
503               version += "." + wordBits[1];
504             }
505           if(wordBits[0] == "PROJECT_BUILD_VERSION")
506             {
507               version += "." + wordBits[1];
508             }
509
510           //get project versionDate
511           if(wordBits[0] == "PROJECT_VERSION_DATE")
512             {
513               std::vector<std::string> versionBits;
514               CDMUtilities::splitter::split(versionBits, wordBits[1], "\"", CDMUtilities::splitter::no_empties);
515               versionDate = versionBits[0];
516             }
517         }
518     }
519   confFile.close();
520
521   this->type = wxDIR_DIRS;
522   this->level = 0;
523
524   std::vector<bool> checked(this->children.size(), false);
525   std::vector<bool> checkedPackages(this->packages.size(), false);
526
527   //check all folders
528   wxDir dir(crea::std2wx((this->path).c_str()));
529   if (dir.IsOpened())
530     {
531       wxString fileName;
532       bool cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_DIRS);
533       while (cont)
534         {
535           std::string stdfileName = crea::wx2std(fileName);
536
537           //if appli, create appli
538           if(stdfileName == "appli")
539             {
540               if (this->appli == NULL)
541                 {
542                   this->appli = new modelCDMAppli(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
543                   this->children.push_back(this->appli);
544                 }
545               else
546                 {
547                   int pos = std::find(this->children.begin(), this->children.end(), this->appli) - this->children.begin();
548                   checked[pos] = true;
549                   if(!this->appli->Refresh(result))
550                     return false;
551                 }
552             }
553           //if lib, create lib
554           else if(stdfileName == "lib")
555             {
556               if (this->lib == NULL)
557                 {
558                   this->lib = new modelCDMLib(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
559                   this->children.push_back(this->lib);
560                 }
561               else
562                 {
563                   int pos = std::find(this->children.begin(), this->children.end(), this->lib) - this->children.begin();
564                   checked[pos] = true;
565                   if(!this->lib->Refresh(result))
566                     return false;
567                 }
568
569             }
570           //if package , create package
571           else if(stdfileName.size() > 9 && stdfileName.substr(0,5) == "bbtk_" && stdfileName.substr(stdfileName.size()-4,4) == "_PKG")
572             {
573               bool found = false;
574               for (int i = 0; !found && i < (int)(this->packages.size()); i++)
575                 {
576                   if (this->packages[i]->GetName() == stdfileName)
577                     {
578                       found = true;
579                       int pos = std::find(this->children.begin(), this->children.end(), this->packages[i]) - this->children.begin();
580                       checked[pos] = true;
581                       checkedPackages[i] = true;
582                       if(!this->packages[i]->Refresh(result))
583                         return false;
584                     }
585                 }
586               if(!found)
587                 {
588                   modelCDMPackage* package = new modelCDMPackage(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
589                   this->packages.push_back(package);
590                   this->children.push_back(package);
591                 }
592
593             }
594           //if is an unknown folder, create folder
595           else
596             {
597               bool found = false;
598               for (int i = 0; !found && i < (int)(this->children.size()); i++)
599                 {
600                   if (this->children[i]->GetName() == stdfileName)
601                     {
602                       found = true;
603                       checked[i] = true;
604                       if(!this->children[i]->Refresh(result))
605                         return false;
606                     }
607                 }
608
609               if(!found)
610                 {
611                   modelCDMFolder* folder = new modelCDMFolder(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
612                   this->children.push_back(folder);
613                 }
614             }
615
616           cont = dir.GetNext(&fileName);
617         }
618
619       cont = dir.GetFirst(&fileName, wxEmptyString, wxDIR_FILES);
620       while (cont)
621         {
622           std::string stdfileName = crea::wx2std(fileName);
623
624           //if CMakeLists, create CMakeLists
625           if(stdfileName == "CMakeLists.txt")
626             {
627               if (this->CMakeLists == NULL)
628                 {
629                   this->CMakeLists = new modelCDMCMakeListsFile(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
630                   this->children.push_back(this->CMakeLists);
631                 }
632               else
633                 {
634                   int pos = std::find(this->children.begin(), this->children.end(), this->CMakeLists) - this->children.begin();
635                   checked[pos] = true;
636                   if(!this->CMakeLists->Refresh(result))
637                     return false;
638                 }
639             }
640           //if is an unknown file, create file
641           else
642             {
643               bool found = false;
644               for (int i = 0; !found && i < (int)(this->children.size()); i++)
645                 {
646                   if (this->children[i]->GetName() == stdfileName)
647                     {
648                       found = true;
649                       checked[i] = true;
650                       if(!this->children[i]->Refresh(result))
651                         return false;
652                     }
653                 }
654
655               if(!found)
656                 {
657                   modelCDMFile* file = new modelCDMFile(this, this->path + CDMUtilities::SLASH + stdfileName, stdfileName, this->level + 1);
658                   this->children.push_back(file);
659                 }
660             }
661
662           cont = dir.GetNext(&fileName);
663         }
664     }
665
666   for (int i = 0; i < (int)(checkedPackages.size()); i++)
667     {
668       if(!checkedPackages[i])
669         {
670           this->packages.erase(this->packages.begin()+i);
671           checkedPackages.erase(checkedPackages.begin()+i);
672           i--;
673         }
674     }
675   for (int i = 0; i < (int)(checked.size()); i++)
676     {
677       if(!checked[i])
678         {
679           delete this->children[i];
680           this->children.erase(this->children.begin()+i);
681           checked.erase(checked.begin()+i);
682           i--;
683         }
684     }
685
686   this->SortChildren();
687   std::sort(this->packages.begin(), this->packages.end(), CompareNodeItem);
688   return true;
689 }
690
691 bool modelCDMProject::ConfigureBuild(std::string*& result)
692 {
693   //TODO: adjust for windows and mac
694 #ifdef _WIN32
695   // ------ Windows
696   if(0 == system("cmake-gui"))
697     return true;
698   else
699     {
700           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.");
701       return false;
702     }
703 #elif __APPLE__
704   // ------ Apple
705 #else
706   // ------ Linux
707   //open binary folder
708   wxDir dir(crea::std2wx((this->buildPath).c_str()));
709
710   //if folder doesn't exist then create it
711   if (!dir.IsOpened())
712     {
713       //create command line to create folder
714       std::string createComm = "mkdir \"" + this->buildPath + "\"";
715       //execute creation command
716       if (system(createComm.c_str()))
717         {
718           //if there was an error then report it
719           result = new std::string("There was an error creating the build path: \"" + this->buildPath + "\"");
720           return false;
721         }
722     }
723   //create command line to execute ccmake
724   //TODO:: adjust for different Linux distributions
725   std::string confComm = "gnome-terminal --tab --working-directory=\"" + this->buildPath + "\" -e \"ccmake '" + this->path + "'\"";
726   //execute command
727   if(CDMUtilities::openTerminal(confComm))
728     {
729       //if there was an error then report it
730       result = new std::string("There was an error opening the configuration tool in the desired place.");
731       return false;
732     }
733 #endif
734   return true;
735 }
736
737 bool modelCDMProject::Build(std::string*& result, const std::string& line)
738 {
739   //TODO: adjust for windows and mac
740 #ifdef _WIN32
741   // ------ Windows
742         //\\..\\IDE\\VCExpress.exe \"" + this->buildPath + CDMUtilities::SLASH + this->nameProject + ".sln\"
743 //      std::string command = "\"" + std::string(getenv("VS90COMNTOOLS")) + "..\\IDE\\VCExpress.exe\" \"" + this->buildPath + CDMUtilities::SLASH + this->nameProject + ".sln\" &";
744         std::string command = "\"\"%VS90COMNTOOLS%..\\IDE\\VCExpress.exe\" \"" + this->buildPath + CDMUtilities::SLASH + this->nameProject + ".sln\" &\"";
745         command = "start cmd.exe /k " + command + " &";
746   if(0 == system(command.c_str()))
747     return true;
748   else
749     {
750       result = new std::string("An error has happened running: \"" + command + "\". Please make sure to have visual c++ express installed and to have the VS90COMNTOOLS environment variable set.");
751           return false;
752     }
753 #elif __APPLE__
754   // ------ Apple
755 #else
756   // ------ Linux
757   //open binary folder
758   wxDir dir(crea::std2wx((this->buildPath).c_str()));
759
760   //if binary folder can't be opened then return false
761   if (!dir.IsOpened())
762     {
763       result = new std::string("The build path could not be opened. Make sure to configure the project before compiling it");
764       return false;
765     }
766   //create make command
767   std::string makeComm = "gnome-terminal -e \"bash -c \\\"";
768   for (int i = 0; i < line.size(); i++)
769     {
770       if(line[i] == '"')
771         {
772           makeComm+="\\\\\\\"";
773         }
774       else if(line[i] == '\\')
775         {
776           makeComm+="\\\\\\\\";
777         }
778       else
779         {
780           makeComm.push_back(line[i]);
781         }
782     }
783   makeComm += "; echo -e '\\a'; bash";
784   makeComm += "\\\"\"";
785
786   std::cout << "executing '" << makeComm << "'" << std::endl;
787   //execute make command
788   if(system(makeComm.c_str()))
789     {
790       //if there was an error then report it
791       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.");
792       return false;
793     }
794 #endif
795   return true;
796 }
797
798 bool modelCDMProject::Connect(std::string*& result, const std::string& folder)
799 {
800   //TODO: adjust for mac
801 #ifdef _WIN32
802   // ------ Windows
803   //open binary folder
804   wxDir dir(crea::std2wx(folder));
805
806   //if binary folder can't be opened then return false
807   if (!dir.IsOpened())
808     {
809       result = new std::string("The path could not be opened. Make sure the folder exists and contains a bbtkPackage file.");
810       return false;
811     }
812   //create plug command
813   std::string plugComm = "bbPlugPackage \"" + folder + "\"";
814   std::cout << "executing '" << plugComm << "'" << std::endl;
815   //execute plug command
816   if(system(std::string("start cmd.exe /k \"" + plugComm + "\"").c_str()))
817     {
818       //if there was an error then report it
819       result = new std::string("There was an error plugging the packages of the project, please check the console to read more about the problem.");
820       return false;
821     }
822 #elif __APPLE__
823   // ------ Apple
824 #else
825   // ------ Linux
826   //open binary folder
827   wxDir dir(crea::std2wx(folder));
828
829   //if binary folder can't be opened then return false
830   if (!dir.IsOpened())
831     {
832       result = new std::string("The path could not be opened. Make sure the folder exists and contains a bbtkPackage file.");
833       return false;
834     }
835   //create plug command
836   std::string plugComm = "bbPlugPackage \"" + this->buildPath + "\" > \"" + this->buildPath + CDMUtilities::SLASH + "plugging.log\" 2>&1";
837   std::cout << "executing '" << plugComm << "'" << std::endl;
838   //execute plug command
839   if(system(plugComm.c_str()))
840     {
841       //if there was an error then report it
842       result = new std::string("There was an error plugging the packages of the project, please check the plugging.log in the build folder file to read more about the problem.");
843       return false;
844     }
845 #endif
846   return true;
847 }
848
849 void modelCDMProject::CheckStructure(std::map<std::string, bool>& properties)
850 {
851   //check cmake exist
852   if(this->CMakeLists != NULL)
853     {
854       //set properties parameters based on model
855       properties["project add appli"] = this->appli != NULL;
856       properties["project add lib"] = this->lib != NULL;
857       for (int i = 0; i < (int)(this->packages.size()); i++)
858         properties["project add " + packages[i]->GetName()] = false;
859
860       //open cmakelists
861       std::ifstream confFile;
862       confFile.open((this->CMakeLists->GetPath()).c_str());
863
864       //take everything that is not commented
865       std::string fileContent;
866
867       std::string word;
868       std::vector<std::string> words;
869       while(confFile.is_open() && !confFile.eof())
870         {
871           std::getline(confFile,word, '\n');
872           if(word[0] != '#')
873             {
874               CDMUtilities::splitter::split(words, word, "#", CDMUtilities::splitter::empties_ok);
875               if (words.size() > 0)
876                 {
877                   word = words[0];
878                   CDMUtilities::splitter::split(words, word, " ", CDMUtilities::splitter::empties_ok);
879                   for (int i = 0; i < (int)(words.size()); i++)
880                     {
881                       if(words[i].substr(0,2) == "//")
882                         break;
883                       fileContent += words[i] + " ";
884                     }
885                 }
886
887             }
888         }
889
890       //check every instruction
891       std::stringstream ss(fileContent);
892       while(!ss.eof())
893         {
894           std::getline(ss,word, '(');
895
896           //check instruction name
897           CDMUtilities::splitter::split(words, word, " ", CDMUtilities::splitter::no_empties);
898
899           //set instructions
900           if (words.size() > 0 && words[words.size()-1] == "SET")
901             {
902               std::getline(ss,word, ')');
903
904               CDMUtilities::splitter::split(words, word, " ", CDMUtilities::splitter::no_empties);
905               if (words.size() > 1)
906                 {
907                   if (words[0] == "USE_CREA" && words[1] == "ON")
908                     {
909                       properties["project set USE_CREA"] = true;
910                     }
911                   else if (words[0] == "USE_GDCM" && words[1] == "ON")
912                     {
913                       properties["project set USE_GDCM"] = true;
914                     }
915                   else if (words[0] == "USE_GDCM_VTK" && words[1] == "ON")
916                     {
917                       properties["project set USE_GDCM_VTK"] = true;
918                     }
919                   else if (words[0] == "USE_GDCM2" && words[1] == "ON")
920                     {
921                       properties["project set USE_GDCM2"] = true;
922                     }
923                   else if (words[0] == "USE_WXWIDGETS" && words[1] == "ON")
924                     {
925                       properties["project set USE_WXWIDGETS"] = true;
926                     }
927                   else if (words[0] == "USE_KWWIDGETS" && words[1] == "ON")
928                     {
929                       properties["project set USE_KWWIDGETS"] = true;
930                     }
931                   else if (words[0] == "USE_VTK" && words[1] == "ON")
932                     {
933                       properties["project set USE_VTK"] = true;
934                     }
935                   else if (words[0] == "USE_ITK" && words[1] == "ON")
936                     {
937                       properties["project set USE_ITK"] = true;
938                     }
939                   else if (words[0] == "USE_BOOST" && words[1] == "ON")
940                     {
941                       properties["project set USE_BOOST"] = true;
942                     }
943                 }
944             }
945           //add instructions
946           else if (words.size() > 0 && words[words.size()-1] == "ADD_SUBDIRECTORY")
947             {
948               std::getline(ss,word, ')');
949               //std::cout << word << std::endl;
950               CDMUtilities::splitter::split(words, word, " ", CDMUtilities::splitter::no_empties);
951
952               if (words.size() > 0)
953                 {
954                   if(words[0] == "appli")
955                     {
956                       properties["project add "+words[0]] = this->appli != NULL;
957                     }
958                   else if(words[0] == "lib")
959                     {
960                       properties["project add "+words[0]] = this->lib != NULL;
961                     }
962                   else
963                     {
964                       properties["project add "+words[0]] = true;
965                     }
966                 }
967             }
968         }
969
970     }
971
972   //check appli's structure
973   this->appli->CheckStructure(properties);
974   //check lib's structure
975   this->lib->CheckStructure(properties);
976   //check packages' structure
977   for (int i = 0; i < (int)(this->packages.size()); i++)
978     {
979       properties["package " + this->packages[i]->GetName()] = true;
980       this->packages[i]->CheckStructure(properties);
981     }
982 }